Skip to content

Commit f96c30d

Browse files
committed
cargo-miri: avoid set_env
1 parent dc51e86 commit f96c30d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

cargo-miri/src/phases.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
9494
let target = target.as_ref().unwrap_or(host);
9595

9696
// We always setup.
97-
setup(&subcommand, target, &rustc_version, verbose);
97+
let miri_sysroot = setup(&subcommand, target, &rustc_version, verbose);
9898

9999
// Invoke actual cargo for the job, but with different flags.
100100
// We re-use `cargo test` and `cargo run`, which makes target and binary handling very easy but
@@ -159,6 +159,8 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
159159
// Forward all further arguments (not consumed by `ArgSplitFlagValue`) to cargo.
160160
cmd.args(args);
161161

162+
// Let it know where the Miri sysroot lives.
163+
cmd.env("MIRI_SYSROOT", miri_sysroot);
162164
// Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation,
163165
// i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish
164166
// the two codepaths. (That extra argument is why we prefer this over setting `RUSTC`.)

cargo-miri/src/setup.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ use crate::util::*;
1313
/// Performs the setup required to make `cargo miri` work: Getting a custom-built libstd. Then sets
1414
/// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
1515
/// done all this already.
16-
pub fn setup(subcommand: &MiriCommand, target: &str, rustc_version: &VersionMeta, verbose: usize) {
16+
pub fn setup(
17+
subcommand: &MiriCommand,
18+
target: &str,
19+
rustc_version: &VersionMeta,
20+
verbose: usize,
21+
) -> PathBuf {
1722
let only_setup = matches!(subcommand, MiriCommand::Setup);
1823
let ask_user = !only_setup;
1924
let print_sysroot = only_setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
20-
if !only_setup && std::env::var_os("MIRI_SYSROOT").is_some() {
21-
// Skip setup step if MIRI_SYSROOT is explicitly set, *unless* we are `cargo miri setup`.
22-
return;
25+
if !only_setup {
26+
if let Some(sysroot) = std::env::var_os("MIRI_SYSROOT") {
27+
// Skip setup step if MIRI_SYSROOT is explicitly set, *unless* we are `cargo miri setup`.
28+
return sysroot.into();
29+
}
2330
}
2431

2532
// Determine where the rust sources are located. The env var trumps auto-detection.
@@ -92,6 +99,8 @@ pub fn setup(subcommand: &MiriCommand, target: &str, rustc_version: &VersionMeta
9299
command.env("RUSTC", &cargo_miri_path);
93100
}
94101
command.env("MIRI_CALLED_FROM_SETUP", "1");
102+
// Miri expects `MIRI_SYSROOT` to be set when invoked in target mode. Even if that directory is empty.
103+
command.env("MIRI_SYSROOT", &sysroot_dir);
95104
// Make sure there are no other wrappers getting in our way (Cc
96105
// https://github.com/rust-lang/miri/issues/1421,
97106
// https://github.com/rust-lang/miri/issues/2429). Looks like setting
@@ -117,8 +126,6 @@ pub fn setup(subcommand: &MiriCommand, target: &str, rustc_version: &VersionMeta
117126
// the user might have set, which is consistent with normal `cargo build` that does
118127
// not apply `RUSTFLAGS` to the sysroot either.
119128
let rustflags = &["-Cdebug-assertions=off", "-Coverflow-checks=on"];
120-
// Make sure all target-level Miri invocations know their sysroot.
121-
std::env::set_var("MIRI_SYSROOT", &sysroot_dir);
122129

123130
// Do the build.
124131
if print_sysroot {
@@ -159,4 +166,6 @@ pub fn setup(subcommand: &MiriCommand, target: &str, rustc_version: &VersionMeta
159166
// Print just the sysroot and nothing else to stdout; this way we do not need any escaping.
160167
println!("{}", sysroot_dir.display());
161168
}
169+
170+
sysroot_dir
162171
}

0 commit comments

Comments
 (0)