Skip to content

Commit ed76d20

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 + bb52965 commit ed76d20

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

cargo-miri/bin.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ fn find_miri() -> PathBuf {
212212
return path.into();
213213
}
214214
let mut path = std::env::current_exe().expect("current executable path invalid");
215-
path.set_file_name("miri");
215+
if cfg!(windows) {
216+
path.set_file_name("miri.exe");
217+
} else {
218+
path.set_file_name("miri");
219+
}
216220
path
217221
}
218222

@@ -661,14 +665,20 @@ fn phase_cargo_miri(mut args: env::Args) {
661665
);
662666
}
663667
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>.
668+
// We are going to invoke `MIRI` for everything, not `RUSTC`.
666669
if env::var_os("RUSTC").is_some() && env::var_os("MIRI").is_none() {
667670
println!(
668671
"WARNING: Ignoring `RUSTC` environment variable; set `MIRI` if you want to control the binary used as the driver."
669672
);
670673
}
671-
cmd.env_remove("RUSTC");
674+
// We'd prefer to just clear this env var, but cargo does not always honor `RUSTC_WRAPPER`
675+
// (https://github.com/rust-lang/cargo/issues/10885). There is no good way to single out these invocations;
676+
// some build scripts use the RUSTC env var as well. So we set it directly to the `miri` driver and
677+
// hope that all they do is ask for the version number -- things could quickly go downhill from here.
678+
// In `main`, we need the value of `RUSTC` to distinguish RUSTC_WRAPPER invocations from rustdoc
679+
// or TARGET_RUNNER invocations, so we canonicalize it here to make it exceedingly unlikely that
680+
// there would be a collision.
681+
cmd.env("RUSTC", &fs::canonicalize(find_miri()).unwrap());
672682

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

815825
if verbose > 0 {
816826
eprintln!(
817-
"[cargo-miri rustc] captured input:\n{}",
827+
"[cargo-miri rustc inside rustdoc] captured input:\n{}",
818828
std::str::from_utf8(&env.stdin).unwrap()
819829
);
820-
eprintln!("[cargo-miri rustc] {:?}", cmd);
830+
eprintln!("[cargo-miri rustc inside rustdoc] going to run:\n{:?}", cmd);
821831
}
822832

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

901911
// Run it.
902912
if verbose > 0 {
903-
eprint!("[cargo-miri rustc] ");
913+
eprintln!(
914+
"[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} print={print}"
915+
);
916+
eprintln!("[cargo-miri rustc] going to run:");
904917
if verbose > 1 {
905918
for (key, value) in env_vars_from_cmd(&cmd) {
906919
eprintln!("{key}={value:?} \\");
@@ -1173,8 +1186,14 @@ fn main() {
11731186

11741187
match args.next().as_deref() {
11751188
Some("miri") => phase_cargo_miri(args),
1176-
Some("rustc") => phase_rustc(args, RustcPhase::Build),
11771189
Some(arg) => {
1190+
// If the first arg is equal to the RUSTC variable (which should be set at this point),
1191+
// then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
1192+
// having both RUSTC and RUSTC_WRAPPER set (see
1193+
// https://github.com/rust-lang/cargo/issues/10886).
1194+
if arg == env::var_os("RUSTC").unwrap() {
1195+
return phase_rustc(args, RustcPhase::Build);
1196+
}
11781197
// We have to distinguish the "runner" and "rustdoc" cases.
11791198
// As runner, the first argument is the binary (a file that should exist, with an absolute path);
11801199
// 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)