Skip to content

Commit

Permalink
fix(build.rs): keep CARGO_HOME and RUSTUP_HOME
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Oct 11, 2024
1 parent 977755f commit 354984d
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions rftrace/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::collections::HashSet;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::{env, fs};

fn build_backend() {
println!("Building Backend!");
Expand Down Expand Up @@ -85,15 +84,19 @@ fn main() {
if env::var_os("CARGO_FEATURE_STATICLIB").is_some() {
return;
}

build_backend();
}

/// Returns the Rustup proxy for Cargo.
// Adapted from Hermit.
fn cargo() -> Command {
let cargo = {
let exe = format!("cargo{}", env::consts::EXE_SUFFIX);
pub fn cargo() -> Command {
sanitize("cargo")
}

fn sanitize(cmd: &str) -> Command {
let cmd = {
let exe = format!("{cmd}{}", env::consts::EXE_SUFFIX);
// On windows, the userspace toolchain ends up in front of the rustup proxy in $PATH.
// To reach the rustup proxy nonetheless, we explicitly query $CARGO_HOME.
let mut cargo_home = PathBuf::from(env::var_os("CARGO_HOME").unwrap());
Expand All @@ -106,17 +109,22 @@ fn cargo() -> Command {
}
};

let mut cargo = Command::new(cargo);
let mut cmd = Command::new(cmd);

cmd.current_dir(env::var_os("CARGO_MANIFEST_DIR").unwrap());

// Remove rust-toolchain-specific environment variables from kernel cargo
cargo.env_remove("LD_LIBRARY_PATH");
cmd.env_remove("LD_LIBRARY_PATH");
env::vars()
.filter(|(key, _value)| key.starts_with("CARGO") || key.starts_with("RUST"))
.filter(|(key, _value)| {
key.starts_with("CARGO") && !key.starts_with("CARGO_HOME")
|| key.starts_with("RUST") && !key.starts_with("RUSTUP_HOME")
})
.for_each(|(key, _value)| {
cargo.env_remove(&key);
cmd.env_remove(&key);
});

cargo
cmd
}

/// Makes all internal symbols private to avoid duplicated symbols.
Expand Down

0 comments on commit 354984d

Please sign in to comment.