Skip to content

Commit

Permalink
Fixes executable linker
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Oct 15, 2023
1 parent d39ffd1 commit 44031a0
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 79 deletions.
32 changes: 31 additions & 1 deletion build-dist.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
.\dist\bin\nitro.exe build --pkg dist\share\nitro\nitro.npk std
$root = "$PSScriptRoot\dist\share\nitro"

# linux stubs
New-Item -ItemType Directory -Path "$root\stub\linux-gnu-x86_64" -Force

.\lib\llvm\bin\llvm-ifs.exe `
--output-elf="$root\stub\linux-gnu-x86_64\libc.so" `
--arch=x86_64 --bitwidth=64 --endianness=little `
"$PSScriptRoot\stub\linux-gnu\libc.ifs"

if ($LASTEXITCODE -ne 0) {
exit 1
}

# darwin stubs
New-Item -ItemType Directory -Path "$root\stub\darwin" -Force
Copy-Item "$PSScriptRoot\stub\darwin\libSystem.tbd" -Destination "$root\stub\darwin"

# win32 stubs
New-Item -ItemType Directory -Path "$root\stub\win32-x86_64" -Force
./lib/llvm/bin/llvm-lib.exe `
/def:"$PSScriptRoot\stub\win32\msvcrt.def" `
/machine:x64 `
/out:"$root\stub\win32-x86_64\msvcrt.lib"

if ($LASTEXITCODE -ne 0) {
exit 1
}

# std
.\dist\bin\nitro.exe build --pkg dist\share\nitro\nitro.npk

if ($LASTEXITCODE -ne 0) {
exit 1
Expand Down
24 changes: 23 additions & 1 deletion build-dist.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
#!/bin/sh -e
./dist/bin/nitro build --pkg dist/share/nitro/nitro.npk std
dir="dist/share/nitro"

# linux stubs
mkdir -p "$dir/stub/linux-gnu-x86_64"

./lib/llvm/bin/llvm-ifs \
--output-elf="$dir/stub/linux-gnu-x86_64/libc.so" \
--arch=x86_64 --bitwidth=64 --endianness=little \
stub/linux-gnu/libc.ifs

# darwin stubs
mkdir -p "$dir/stub/darwin"
cp stub/darwin/libSystem.tbd "$dir/stub/darwin"

# win32 stubs
mkdir -p "$dir/stub/win32-x86_64"
./lib/llvm/bin/llvm-lib \
/def:stub/win32/msvcrt.def \
/machine:x64 \
/out:"$dir/stub/win32-x86_64/msvcrt.lib"

# std
./dist/bin/nitro build --pkg "$dir/nitro.npk"
2 changes: 1 addition & 1 deletion stage0/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'a> Codegen<'a> {
};

// Create a function.
let name = CStr::from_bytes_with_nul(b"_main\0").unwrap();
let name = CStr::from_bytes_with_nul(b"main\0").unwrap();
let ret = LlvmType::Void(LlvmVoid::new(self));
let mut func = LlvmFunc::new(self, name, &[], ret);

Expand Down
51 changes: 47 additions & 4 deletions stage0/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::{command, value_parser, Arg, ArgMatches, Command};
use std::borrow::Cow;
use std::error::Error;
use std::fmt::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

mod ast;
Expand Down Expand Up @@ -48,14 +48,51 @@ fn main() -> ExitCode {
)
.get_matches();

// Get path to stubs.
let exe = match std::env::current_exe() {
Ok(v) => v,
Err(e) => {
eprintln!("Cannot get path of the executable: {}.", join_nested(&e));
return ExitCode::FAILURE;
}
};

let meta = match exe.symlink_metadata() {
Ok(v) => v,
Err(e) => {
eprintln!(
"Cannot get metadata of {}: {}.",
exe.display(),
join_nested(&e)
);
return ExitCode::FAILURE;
}
};

let exe = if !meta.is_symlink() {
exe
} else {
match exe.read_link() {
Ok(v) => v,
Err(e) => {
eprintln!(
"Cannot read the target of {}: {}.",
exe.display(),
join_nested(&e)
);
return ExitCode::FAILURE;
}
}
};

// Execute the command.
match args.subcommand().unwrap() {
("build", args) => build(args),
("build", args) => build(args, &exe),
_ => todo!(),
}
}

fn build(args: &ArgMatches) -> ExitCode {
fn build(args: &ArgMatches, exe: &Path) -> ExitCode {
// Initialize LLVM.
unsafe { llvm_init() };

Expand All @@ -68,11 +105,17 @@ fn build(args: &ArgMatches) -> ExitCode {
// Setup target resolver.
let targets = TargetResolver::new();

// Get path to stubs.
let mut stubs = exe.parent().unwrap().parent().unwrap().join("share");

stubs.push("nitro");
stubs.push("stub");

// Setup dependency resolver.
let deps = DependencyResolver::new();

// Open the project.
let mut project = match Project::open(path.as_ref(), &targets, &deps) {
let mut project = match Project::open(path.as_ref(), &targets, &stubs, &deps) {
Ok(v) => v,
Err(e) => {
eprintln!("Cannot open {}: {}.", path.display(), join_nested(&e));
Expand Down
4 changes: 4 additions & 0 deletions stage0/src/pkg/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ impl PrimitiveTarget {
pub fn os(&self) -> TargetOs {
self.os
}

pub fn env(&self) -> Option<TargetEnv> {
self.env
}
}

impl Display for PrimitiveTarget {
Expand Down
Loading

0 comments on commit 44031a0

Please sign in to comment.