From 6c31e5b74437a8918f2c1a0310546434a4d21e1b Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Fri, 18 Jun 2021 12:26:06 -0700 Subject: [PATCH] Include Cargo rustc configuration in probe Without this, environments that configure `RUSTFLAGS` or the `RUSTC_WRAPPER` in ways that break compilation with `backtrace` will fail to compile anyhow (see #156). With this, the compiler probe takes into account that Cargo configuration, and thus (more) accurately represents whether the `backtrace` feature can be safely enabled. Requires rust-lang/cargo#9600. Fixes #156. --- build.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index df2e73b..ac879ce 100644 --- a/build.rs +++ b/build.rs @@ -67,17 +67,39 @@ fn compile_probe() -> Option { let out_dir = env::var_os("OUT_DIR")?; let probefile = Path::new(&out_dir).join("probe.rs"); fs::write(&probefile, PROBE).ok()?; - Command::new(rustc) - .stderr(Stdio::null()) + + // Make sure to pick up Cargo rustc configuration. + let mut cmd = if let Some(wrapper) = env::var_os("CARGO_RUSTC_WRAPPER") { + let mut cmd = Command::new(wrapper); + // The wrapper's first argument should always be the path to rustc. + cmd.arg(rustc); + cmd + } else { + Command::new(rustc) + }; + + cmd.stderr(Stdio::null()) .arg("--edition=2018") .arg("--crate-name=anyhow_build") .arg("--crate-type=lib") .arg("--emit=metadata") .arg("--out-dir") .arg(out_dir) - .arg(probefile) - .status() - .ok() + .arg(probefile); + + // If Cargo wants to set RUSTFLAGS, use that. + if let Ok(rustflags) = env::var("CARGO_RUSTFLAGS") { + // NOTE: This is the same RUSTFLAGS splitting used in cargo. + for arg in rustflags + .split(' ') + .map(str::trim) + .filter(|s| !s.is_empty()) + { + cmd.arg(arg); + } + } + + cmd.status().ok() } fn rustc_minor_version() -> Option {