Skip to content

Commit

Permalink
Auto merge of #2409 - RalfJung:cargo-miri-rustc, r=RalfJung
Browse files Browse the repository at this point in the history
cargo-miri: set RUSTC to us

Works around rust-lang/cargo#10885.
  • Loading branch information
bors committed Jul 21, 2022
2 parents 0f973bd + bb52965 commit ed76d20
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
35 changes: 27 additions & 8 deletions cargo-miri/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ fn find_miri() -> PathBuf {
return path.into();
}
let mut path = std::env::current_exe().expect("current executable path invalid");
path.set_file_name("miri");
if cfg!(windows) {
path.set_file_name("miri.exe");
} else {
path.set_file_name("miri");
}
path
}

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

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

if verbose > 0 {
eprintln!(
"[cargo-miri rustc] captured input:\n{}",
"[cargo-miri rustc inside rustdoc] captured input:\n{}",
std::str::from_utf8(&env.stdin).unwrap()
);
eprintln!("[cargo-miri rustc] {:?}", cmd);
eprintln!("[cargo-miri rustc inside rustdoc] going to run:\n{:?}", cmd);
}

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

// Run it.
if verbose > 0 {
eprint!("[cargo-miri rustc] ");
eprintln!(
"[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} print={print}"
);
eprintln!("[cargo-miri rustc] going to run:");
if verbose > 1 {
for (key, value) in env_vars_from_cmd(&cmd) {
eprintln!("{key}={value:?} \\");
Expand Down Expand Up @@ -1173,8 +1186,14 @@ fn main() {

match args.next().as_deref() {
Some("miri") => phase_cargo_miri(args),
Some("rustc") => phase_rustc(args, RustcPhase::Build),
Some(arg) => {
// If the first arg is equal to the RUSTC variable (which should be set at this point),
// then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
// having both RUSTC and RUSTC_WRAPPER set (see
// https://github.com/rust-lang/cargo/issues/10886).
if arg == env::var_os("RUSTC").unwrap() {
return phase_rustc(args, RustcPhase::Build);
}
// We have to distinguish the "runner" and "rustdoc" cases.
// As runner, the first argument is the binary (a file that should exist, with an absolute path);
// as rustdoc, the first argument is a flag (`--something`).
Expand Down
17 changes: 9 additions & 8 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,17 @@ fn compile_time_sysroot() -> Option<String> {
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
Some(match (home, toolchain) {
(Some(home), Some(toolchain)) => {
// Check that at runtime, we are still in this toolchain.
let toolchain_runtime =
env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN"));
if !matches!(toolchain_runtime, Some(r) if r == toolchain) {
show_error(format!(
"This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\
// Check that at runtime, we are still in this toolchain (if there is any toolchain).
if let Some(toolchain_runtime) =
env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN"))
{
if toolchain_runtime != toolchain {
show_error(format!(
"This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\
Make sure to run Miri in the toolchain it got built with, e.g. via `cargo +{toolchain} miri`."
));
));
}
}

format!("{}/toolchains/{}", home, toolchain)
}
_ => option_env!("RUST_SYSROOT")
Expand Down

0 comments on commit ed76d20

Please sign in to comment.