Skip to content

Commit c0b22e0

Browse files
committed
Auto merge of #2409 - RalfJung:cargo-miri-rustc, r=RalfJung
cargo-miri: set RUSTC to us Works around rust-lang/cargo#10885.
2 parents 0f973bd + 0a9feb3 commit c0b22e0

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

cargo-miri/bin.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -661,14 +661,20 @@ fn phase_cargo_miri(mut args: env::Args) {
661661
);
662662
}
663663
cmd.env("RUSTC_WRAPPER", &cargo_miri_path);
664-
// Having both `RUSTC_WRAPPER` and `RUSTC` set does some odd things, so let's avoid that.
665-
// See <https://github.com/rust-lang/miri/issues/2238>.
664+
// We are going to invoke `MIRI` for everything, not `RUSTC`.
666665
if env::var_os("RUSTC").is_some() && env::var_os("MIRI").is_none() {
667666
println!(
668667
"WARNING: Ignoring `RUSTC` environment variable; set `MIRI` if you want to control the binary used as the driver."
669668
);
670669
}
671-
cmd.env_remove("RUSTC");
670+
// We'd prefer to just clear this env var, but cargo does not always honor `RUSTC_WRAPPER`
671+
// (https://github.com/rust-lang/cargo/issues/10885). There is no good way to single out these invocations;
672+
// some build scripts use the RUSTC env var as well. So we set it directly to the `miri` driver and
673+
// hope that all they do is ask for the version number -- things could quickly go downhill from here.
674+
// In `main`, we need the value of `RUSTC` to distinguish RUSTC_WRAPPER invocations from rustdoc
675+
// or TARGET_RUNNER invocations, so we canonicalize it here to make it exceedingly unlikely that
676+
// there would be a collision.
677+
cmd.env("RUSTC", &fs::canonicalize(find_miri()).unwrap());
672678

673679
let runner_env_name =
674680
|triple: &str| format!("CARGO_TARGET_{}_RUNNER", triple.to_uppercase().replace('-', "_"));
@@ -814,10 +820,10 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
814820

815821
if verbose > 0 {
816822
eprintln!(
817-
"[cargo-miri rustc] captured input:\n{}",
823+
"[cargo-miri rustc inside rustdoc] captured input:\n{}",
818824
std::str::from_utf8(&env.stdin).unwrap()
819825
);
820-
eprintln!("[cargo-miri rustc] {:?}", cmd);
826+
eprintln!("[cargo-miri rustc inside rustdoc] going to run:\n{:?}", cmd);
821827
}
822828

823829
exec_with_pipe(cmd, &env.stdin);
@@ -900,7 +906,10 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
900906

901907
// Run it.
902908
if verbose > 0 {
903-
eprint!("[cargo-miri rustc] ");
909+
eprintln!(
910+
"[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} print={print}"
911+
);
912+
eprintln!("[cargo-miri rustc] going to run:");
904913
if verbose > 1 {
905914
for (key, value) in env_vars_from_cmd(&cmd) {
906915
eprintln!("{key}={value:?} \\");
@@ -1173,8 +1182,14 @@ fn main() {
11731182

11741183
match args.next().as_deref() {
11751184
Some("miri") => phase_cargo_miri(args),
1176-
Some("rustc") => phase_rustc(args, RustcPhase::Build),
11771185
Some(arg) => {
1186+
// If the first arg is equal to the RUSTC variable (which should be set at this point),
1187+
// then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
1188+
// having both RUSTC and RUSTC_WRAPPER set (see
1189+
// https://github.com/rust-lang/cargo/issues/10886).
1190+
if arg == env::var_os("RUSTC").unwrap() {
1191+
return phase_rustc(args, RustcPhase::Build);
1192+
}
11781193
// We have to distinguish the "runner" and "rustdoc" cases.
11791194
// As runner, the first argument is the binary (a file that should exist, with an absolute path);
11801195
// as rustdoc, the first argument is a flag (`--something`).

src/bin/miri.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,17 @@ fn compile_time_sysroot() -> Option<String> {
220220
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
221221
Some(match (home, toolchain) {
222222
(Some(home), Some(toolchain)) => {
223-
// Check that at runtime, we are still in this toolchain.
224-
let toolchain_runtime =
225-
env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN"));
226-
if !matches!(toolchain_runtime, Some(r) if r == toolchain) {
227-
show_error(format!(
228-
"This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\
223+
// Check that at runtime, we are still in this toolchain (if there is any toolchain).
224+
if let Some(toolchain_runtime) =
225+
env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN"))
226+
{
227+
if toolchain_runtime != toolchain {
228+
show_error(format!(
229+
"This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\
229230
Make sure to run Miri in the toolchain it got built with, e.g. via `cargo +{toolchain} miri`."
230-
));
231+
));
232+
}
231233
}
232-
233234
format!("{}/toolchains/{}", home, toolchain)
234235
}
235236
_ => option_env!("RUST_SYSROOT")

0 commit comments

Comments
 (0)