Skip to content

Commit

Permalink
Merge #159
Browse files Browse the repository at this point in the history
159: build.rs: Migrate to CARGO_ENCODED_RUSTFLAGS r=stlankes a=mkroening

Fixes #158.

Cargo introduced `CARGO_ENCODED_RUSTFLAGS` in rust-lang/cargo#9601 to make setting flags less error prone – it encodes arguments separated by `0x1f` (ASCII Unit Separator), instead of white spaces (old `RUSTFLAGS`).

`CARGO_ENCODED_RUSTFLAGS` are preferred over the old `RUSTFLAGS` in cargo. For build scripts, cargo converts its `RUSTFLAGS` to `CARGO_ENCODED_RUSTFLAGS`. For unset `RUSTFLAGS` it is set to an empty string. Thus our build script would call cargo for building libhermit-rs with a set – but empty – `CARGO_ENCODED_RUSTFLAGS`, which takes precedence over our prepared `RUSTFLAGS`. Specifically this caused our `-Zmutable-noalias=no` flag to be ignored, causing the same network issue as #128 again.

This PR adjusts our build script to make direct use of `CARGO_ENCODED_RUSTFLAGS`, the new and preferred way of handling flags in build scripts. This causes our flags to be correctly handled again.

Co-authored-by: Martin Kröning <mkroening@posteo.net>
  • Loading branch information
bors[bot] and mkroening committed Sep 16, 2021
2 parents fb1e44a + 5d332c9 commit a1a3157
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions hermit-sys/build.rs
Expand Up @@ -98,30 +98,29 @@ fn build_hermit(src_dir: &Path, target_dir_opt: Option<&Path>) {
}

let mut rustflags = vec!["-Zmutable-noalias=no".to_string()];
let outer_rustflags = env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();

#[cfg(feature = "instrument")]
{
rustflags.push("-Zinstrument-mcount".to_string());
// Add outer `RUSTFLAGS` to command
if let Ok(var) = env::var("RUSTFLAGS") {
rustflags.push(var);
}
// Add outer rustflags to command
rustflags.push(outer_rustflags);
}

#[cfg(not(feature = "instrument"))]
{
// If the `instrument` feature feature is not enabled,
// filter it from outer `RUSTFLAGS` before adding them to the command.
if let Ok(var) = env::var("RUSTFLAGS") {
let flags = var
.split(',')
// filter it from outer rustflags before adding them to the command.
if !outer_rustflags.is_empty() {
let flags = outer_rustflags
.split('\x1f')
.filter(|&flag| !flag.contains("instrument-mcount"))
.map(String::from);
rustflags.extend(flags);
}
}

cmd.env("RUSTFLAGS", rustflags.join(" "));
cmd.env("CARGO_ENCODED_RUSTFLAGS", rustflags.join("\x1f"));

let status = cmd.status().expect("failed to start kernel build");
assert!(status.success());
Expand Down

0 comments on commit a1a3157

Please sign in to comment.