Skip to content

Commit 1af09e7

Browse files
authored
Merge pull request #2644 from hermit-os/nix
fix: prepare build system for Nix
2 parents fd9cf30 + 4cee2bc commit 1af09e7

7 files changed

Lines changed: 57 additions & 46 deletions

File tree

build.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ fn assemble_x86_64_smp_boot() -> Result<()> {
2727
let boot_bc = out_dir.join("boot.bc");
2828
let boot_bin = out_dir.join("boot.bin");
2929

30-
let llvm_as = binutil("llvm-as")?;
31-
let rust_lld = binutil("rust-lld")?;
30+
let llvm_as = binutil("llvm-as");
31+
let lld = lld();
3232

3333
let assembly = fs::read_to_string(boot_s)?;
3434

@@ -55,7 +55,7 @@ module asm "
5555
.with_context(|| format!("Failed to run llvm-as from {}", llvm_as.display()))?;
5656
assert!(status.success());
5757

58-
let status = Command::new(&rust_lld)
58+
let status = Command::new(&lld)
5959
.arg("-flavor")
6060
.arg("gnu")
6161
.arg("--image-base=0x8000")
@@ -65,27 +65,32 @@ module asm "
6565
.arg(&boot_bin)
6666
.arg(&boot_bc)
6767
.status()
68-
.with_context(|| format!("Failed to run rust-lld from {}", rust_lld.display()))?;
68+
.with_context(|| format!("Failed to run lld from {}", lld.display()))?;
6969
assert!(status.success());
7070

7171
println!("cargo:rerun-if-changed={}", boot_s.display());
7272
Ok(())
7373
}
7474

75-
fn binutil(name: &str) -> Result<PathBuf> {
75+
fn lld() -> PathBuf {
76+
let rust_lld = binutil("rust-lld");
77+
78+
if rust_lld.exists() {
79+
return rust_lld;
80+
}
81+
82+
binutil("lld")
83+
}
84+
85+
fn binutil(name: &str) -> PathBuf {
7686
let exe = format!("{name}{}", env::consts::EXE_SUFFIX);
7787

78-
let path = LlvmTools::new()
79-
.map_err(|err| match err {
80-
llvm_tools::Error::NotFound => anyhow!(
81-
"Could not find llvm-tools component\n\
82-
\n\
83-
Maybe the rustup component `llvm-tools` is missing? Install it through: `rustup component add llvm-tools`"
84-
),
85-
err => anyhow!("{err:?}"),
86-
})?
87-
.tool(&exe)
88-
.ok_or_else(|| anyhow!("could not find {exe}"))?;
89-
90-
Ok(path)
88+
if let Some(tool) = LlvmTools::new()
89+
.ok()
90+
.and_then(|llvm_tools| llvm_tools.tool(&exe))
91+
{
92+
return tool;
93+
}
94+
95+
PathBuf::from(exe)
9196
}

xtask/src/arch.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ impl Arch {
3030
rustup.args(["target", "add", self.triple()]);
3131

3232
eprintln!("$ {rustup:?}");
33-
let status = rustup.status()?;
33+
let status = match rustup.status() {
34+
Ok(status) => status,
35+
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
36+
Err(err) => return Err(err),
37+
};
3438
assert!(status.success());
3539

3640
Ok(())

xtask/src/archive.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ impl Archive {
4949
};
5050

5151
let all_symbols = {
52-
let nm = crate::binutil("nm").unwrap();
53-
let stdout = cmd!(sh, "{nm} --export-symbols {archive}").output()?.stdout;
52+
let llvm_nm = crate::binutil("llvm-nm");
53+
let stdout = cmd!(sh, "{llvm_nm} --export-symbols {archive}")
54+
.output()?
55+
.stdout;
5456
String::from_utf8(stdout)?
5557
};
5658

@@ -79,8 +81,8 @@ impl Archive {
7981
let rename_path = archive.with_extension("redefine-syms");
8082
sh.write_file(&rename_path, symbol_renames)?;
8183

82-
let objcopy = crate::binutil("objcopy").unwrap();
83-
cmd!(sh, "{objcopy} --redefine-syms={rename_path} {archive}").run()?;
84+
let llvm_objcopy = crate::binutil("llvm-objcopy");
85+
cmd!(sh, "{llvm_objcopy} --redefine-syms={rename_path} {archive}").run()?;
8486

8587
sh.remove_path(&rename_path)?;
8688

@@ -92,8 +94,8 @@ impl Archive {
9294
let archive = self.as_ref();
9395
let file = file.as_ref();
9496

95-
let ar = crate::binutil("ar").unwrap();
96-
cmd!(sh, "{ar} qL {archive} {file}").run()?;
97+
let llvm_ar = crate::binutil("llvm-ar");
98+
cmd!(sh, "{llvm_ar} qL {archive} {file}").run()?;
9799

98100
Ok(())
99101
}

xtask/src/binutil.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
use std::io;
21
use std::path::PathBuf;
32
use std::sync::LazyLock;
43

5-
pub fn binutil(name: &str) -> Option<PathBuf> {
6-
static LLVM_TOOLS: LazyLock<LlvmTools> = LazyLock::new(|| LlvmTools::new().unwrap());
4+
pub fn binutil(name: &str) -> PathBuf {
5+
static LLVM_TOOLS: LazyLock<Option<LlvmTools>> = LazyLock::new(LlvmTools::new);
76

8-
LLVM_TOOLS.tool(name)
7+
LLVM_TOOLS
8+
.as_ref()
9+
.and_then(|llvm_tools| llvm_tools.tool(name))
10+
.unwrap_or(PathBuf::from(name))
911
}
1012

1113
struct LlvmTools {
1214
bin: PathBuf,
1315
}
1416

1517
impl LlvmTools {
16-
pub fn new() -> io::Result<Self> {
18+
pub fn new() -> Option<Self> {
1719
let mut rustc = crate::rustc();
1820
rustc.args(["--print", "sysroot"]);
1921

2022
eprintln!("$ {rustc:?}");
21-
let output = rustc.output()?;
23+
let output = rustc.output().unwrap();
2224
assert!(output.status.success());
2325

2426
let sysroot = String::from_utf8(output.stdout).unwrap();
2527
let rustlib = [sysroot.trim_end(), "lib", "rustlib"]
2628
.iter()
2729
.collect::<PathBuf>();
2830

29-
let example_exe = exe("objdump");
30-
for entry in rustlib.read_dir()? {
31-
let bin = entry?.path().join("bin");
31+
let example_exe = exe("llvm-objdump");
32+
for entry in rustlib.read_dir().unwrap() {
33+
let bin = entry.unwrap().path().join("bin");
3234
if bin.join(&example_exe).exists() {
33-
return Ok(Self { bin });
35+
return Some(Self { bin });
3436
}
3537
}
36-
Err(io::Error::new(
37-
io::ErrorKind::NotFound,
38-
"Could not find llvm-tools component\n\
39-
\n\
40-
Maybe the rustup component `llvm-tools` is missing? Install it through: `rustup component add llvm-tools`",
41-
))
38+
39+
None
4240
}
4341

4442
pub fn tool(&self, name: &str) -> Option<PathBuf> {
@@ -49,5 +47,5 @@ impl LlvmTools {
4947

5048
fn exe(name: &str) -> String {
5149
let exe_suffix = std::env::consts::EXE_SUFFIX;
52-
format!("llvm-{name}{exe_suffix}")
50+
format!("{name}{exe_suffix}")
5351
}

xtask/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl Build {
7474
eprintln!("Building hermit-builtins");
7575
let mut cargo = crate::cargo();
7676
cargo
77+
.current_dir("hermit-builtins")
7778
.arg("build")
78-
.arg("--manifest-path=hermit-builtins/Cargo.toml")
7979
.arg("--profile")
8080
.arg(self.cargo_build.artifact.builtins_profile_path_component())
8181
.args(self.cargo_build.artifact.arch.builtins_cargo_args())

xtask/src/ci/qemu.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,8 @@ fn check_rftrace(image: &Path) -> Result<()> {
806806
let sh = crate::sh()?;
807807
let image_name = image.file_name().unwrap().to_str().unwrap();
808808

809-
let nm = crate::binutil("nm").unwrap();
810-
let symbols = cmd!(sh, "{nm} --demangle --numeric-sort {image}")
809+
let llvm_nm = crate::binutil("llvm-nm");
810+
let symbols = cmd!(sh, "{llvm_nm} --demangle --numeric-sort {image}")
811811
.output()?
812812
.stdout;
813813
sh.write_file(format!("shared/tracedir/{image_name}.sym"), symbols)?;

xtask/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ fn sanitize(cmd: &str) -> Command {
9999
env::vars()
100100
.filter(|(key, _value)| {
101101
key.starts_with("CARGO") && !key.starts_with("CARGO_HOME")
102-
|| key.starts_with("RUST") && !key.starts_with("RUSTUP_HOME")
102+
|| key.starts_with("RUST")
103+
&& !key.starts_with("RUSTUP_HOME")
104+
&& !key.starts_with("RUSTC_BOOTSTRAP")
103105
})
104106
.for_each(|(key, _value)| {
105107
cmd.env_remove(&key);

0 commit comments

Comments
 (0)