Skip to content

Commit 6c20a06

Browse files
author
Jon Gjengset
committed
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 dtolnay#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#9601, but does not break without. Fixes dtolnay#156.
1 parent 2778943 commit 6c20a06

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

build.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,37 @@ fn compile_probe() -> Option<ExitStatus> {
6767
let out_dir = env::var_os("OUT_DIR")?;
6868
let probefile = Path::new(&out_dir).join("probe.rs");
6969
fs::write(&probefile, PROBE).ok()?;
70-
Command::new(rustc)
71-
.stderr(Stdio::null())
70+
71+
// Make sure to pick up Cargo rustc configuration.
72+
let mut cmd = env::var_os("CARGO_RUSTC_WRAPPER").map_or_else(
73+
|| Command::new(&rustc),
74+
|wrapper| {
75+
let mut cmd = Command::new(wrapper);
76+
// The wrapper's first argument should always be the path to rustc.
77+
cmd.arg(&rustc);
78+
cmd
79+
},
80+
);
81+
82+
cmd.stderr(Stdio::null())
7283
.arg("--edition=2018")
7384
.arg("--crate-name=anyhow_build")
7485
.arg("--crate-type=lib")
7586
.arg("--emit=metadata")
7687
.arg("--out-dir")
7788
.arg(out_dir)
78-
.arg(probefile)
79-
.status()
80-
.ok()
89+
.arg(probefile);
90+
91+
// If Cargo wants to set RUSTFLAGS, use that.
92+
if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
93+
if !rustflags.is_empty() {
94+
for arg in rustflags.split('\x1f') {
95+
cmd.arg(arg);
96+
}
97+
}
98+
}
99+
100+
cmd.status().ok()
81101
}
82102

83103
fn rustc_minor_version() -> Option<u32> {

0 commit comments

Comments
 (0)