Skip to content

Commit 2af535c

Browse files
committed
set --sysroot outside the driver rather than messing with the arguments passed to the driver
1 parent 969f4cc commit 2af535c

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,9 @@ Moreover, Miri recognizes some environment variables:
464464
standard library that it will build and use for interpretation. This directory
465465
must point to the `library` subdirectory of a `rust-lang/rust` repository
466466
checkout.
467-
* `MIRI_SYSROOT` (recognized by `cargo miri` and the Miri driver) indicates the sysroot to use. When
467+
* `MIRI_SYSROOT` (recognized by `cargo miri` and the test suite) indicates the sysroot to use. When
468468
using `cargo miri`, this skips the automatic setup -- only set this if you do not want to use the
469-
automatically created sysroot. For directly invoking the Miri driver, this variable (or a
470-
`--sysroot` flag) is mandatory. When invoking `cargo miri setup`, this indicates where the sysroot
469+
automatically created sysroot. When invoking `cargo miri setup`, this indicates where the sysroot
471470
will be put.
472471
* `MIRI_TEST_TARGET` (recognized by the test suite and the `./miri` script) indicates which target
473472
architecture to test against. `miri` and `cargo miri` accept the `--target` flag for the same

cargo-miri/src/phases.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
168168
// Forward all further arguments (not consumed by `ArgSplitFlagValue`) to cargo.
169169
cmd.args(args);
170170

171-
// Let it know where the Miri sysroot lives.
172-
cmd.env("MIRI_SYSROOT", miri_sysroot);
173171
// Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation,
174172
// i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish
175173
// the two codepaths. (That extra argument is why we prefer this over setting `RUSTC`.)
@@ -196,10 +194,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
196194
// always applied. However, buggy build scripts (https://github.com/eyre-rs/eyre/issues/84) and
197195
// also cargo (https://github.com/rust-lang/cargo/issues/10885) will invoke `rustc` even when
198196
// `RUSTC_WRAPPER` is set, bypassing the wrapper. To make sure everything is coherent, we want
199-
// that to be the Miri driver, but acting as rustc, on the target level. (Target, rather than
200-
// host, is needed for cross-interpretation situations.) This is not a perfect emulation of real
201-
// rustc (it might be unable to produce binaries since the sysroot is check-only), but it's as
202-
// close as we can get, and it's good enough for autocfg.
197+
// that to be the Miri driver, but acting as rustc, on the target level.
203198
//
204199
// In `main`, we need the value of `RUSTC` to distinguish RUSTC_WRAPPER invocations from rustdoc
205200
// or TARGET_RUNNER invocations, so we canonicalize it here to make it exceedingly unlikely that
@@ -208,11 +203,18 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
208203
// bootstrap `rustc` thing in our way! Instead, we have MIRI_HOST_SYSROOT to use for host
209204
// builds.
210205
cmd.env("RUSTC", fs::canonicalize(find_miri()).unwrap());
206+
// In case we get invoked as RUSTC without the wrapper, let's be a target rustc. (Target,
207+
// rather than host, is needed for cross-interpretation situations.) This is not a perfect
208+
// emulation of real rustc (it might be unable to produce binaries since the sysroot is
209+
// check-only), but it's as close as we can get, and it's good enough for eyre.
210+
// (But we won't actually have the Miri sysroot so this will not necessarily work great.)
211211
cmd.env("MIRI_BE_RUSTC", "target"); // we better remember to *unset* this in the other phases!
212212

213213
// Set rustdoc to us as well, so we can run doctests.
214214
cmd.env("RUSTDOC", &cargo_miri_path);
215215

216+
// Forward some crucial information to our own re-invocations.
217+
cmd.env("MIRI_SYSROOT", miri_sysroot);
216218
cmd.env("MIRI_LOCAL_CRATES", local_crates(&metadata));
217219
if verbose > 0 {
218220
cmd.env("MIRI_VERBOSE", verbose.to_string()); // This makes the other phases verbose.
@@ -410,6 +412,8 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
410412
// Arguments are treated very differently depending on whether this crate is
411413
// for interpretation by Miri, or for use by a build script / proc macro.
412414
if target_crate {
415+
// Set the sysroot.
416+
cmd.arg("--sysroot").arg(env::var_os("MIRI_SYSROOT").unwrap());
413417
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
414418
let emit_flag = "--emit";
415419
while let Some(arg) = args.next() {
@@ -543,6 +547,12 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
543547
cmd.env(name, val);
544548
}
545549

550+
if phase != RunnerPhase::Rustdoc {
551+
// Set the sysroot. Not necessary in rustdoc, where we already set the sysroot when invoking
552+
// rustdoc itself, which will forward that flag when invoking rustc (i.e., us), so the flag
553+
// is present in `info.args`.
554+
cmd.arg("--sysroot").arg(env::var_os("MIRI_SYSROOT").unwrap());
555+
}
546556
// Forward rustc arguments.
547557
// We need to patch "--extern" filenames because we forced a check-only
548558
// build without cargo knowing about that: replace `.rlib` suffix by

miri-script/src/commands.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22
use std::ffi::OsString;
33
use std::io::Write;
44
use std::ops::Not;
5+
use std::path::PathBuf;
56
use std::process;
67
use std::thread;
78
use std::time;
@@ -20,10 +21,11 @@ const JOSH_FILTER: &str =
2021
const JOSH_PORT: &str = "42042";
2122

2223
impl MiriEnv {
23-
fn build_miri_sysroot(&mut self, quiet: bool) -> Result<()> {
24-
if self.sh.var("MIRI_SYSROOT").is_ok() {
24+
/// Returns the location of the sysroot.
25+
fn build_miri_sysroot(&mut self, quiet: bool) -> Result<PathBuf> {
26+
if let Some(miri_sysroot) = self.sh.var_os("MIRI_SYSROOT") {
2527
// Sysroot already set, use that.
26-
return Ok(());
28+
return Ok(miri_sysroot.into());
2729
}
2830
let manifest_path = path!(self.miri_dir / "cargo-miri" / "Cargo.toml");
2931
let Self { toolchain, cargo_extra_flags, .. } = &self;
@@ -57,8 +59,8 @@ impl MiriEnv {
5759
.with_context(|| "`cargo miri setup` failed")?;
5860
panic!("`cargo miri setup` didn't fail again the 2nd time?");
5961
};
60-
self.sh.set_var("MIRI_SYSROOT", output);
61-
Ok(())
62+
self.sh.set_var("MIRI_SYSROOT", &output);
63+
Ok(output.into())
6264
}
6365
}
6466

@@ -505,8 +507,10 @@ impl Command {
505507
flags.push("--edition=2021".into()); // keep in sync with `tests/ui.rs`.`
506508
}
507509

508-
// Prepare a sysroot.
509-
e.build_miri_sysroot(/* quiet */ true)?;
510+
// Prepare a sysroot, and add it to the flags.
511+
let miri_sysroot = e.build_miri_sysroot(/* quiet */ true)?;
512+
flags.push("--sysroot".into());
513+
flags.push(miri_sysroot.into());
510514

511515
// Then run the actual command. Also add MIRIFLAGS.
512516
let miri_manifest = path!(e.miri_dir / "Cargo.toml");

src/bin/miri.rs

-19
Original file line numberDiff line numberDiff line change
@@ -271,25 +271,6 @@ fn run_compiler(
271271
callbacks: &mut (dyn rustc_driver::Callbacks + Send),
272272
using_internal_features: std::sync::Arc<std::sync::atomic::AtomicBool>,
273273
) -> ! {
274-
if target_crate {
275-
// Miri needs a custom sysroot for target crates.
276-
// If no `--sysroot` is given, the `MIRI_SYSROOT` env var is consulted to find where
277-
// that sysroot lives, and that is passed to rustc.
278-
let sysroot_flag = "--sysroot";
279-
if !args.iter().any(|e| e.starts_with(sysroot_flag)) {
280-
// Using the built-in default here would be plain wrong, so we *require*
281-
// the env var to make sure things make sense.
282-
let miri_sysroot = env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
283-
show_error!(
284-
"Miri was invoked in 'target' mode without `MIRI_SYSROOT` or `--sysroot` being set"
285-
)
286-
});
287-
288-
args.push(sysroot_flag.to_owned());
289-
args.push(miri_sysroot);
290-
}
291-
}
292-
293274
// Don't insert `MIRI_DEFAULT_ARGS`, in particular, `--cfg=miri`, if we are building
294275
// a "host" crate. That may cause procedural macros (and probably build scripts) to
295276
// depend on Miri-only symbols, such as `miri_resolve_frame`:

tests/ui.rs

+7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ fn run_tests(
108108
config.program.envs.push(("RUST_BACKTRACE".into(), Some("1".into())));
109109

110110
// Add some flags we always want.
111+
config.program.args.push(
112+
format!(
113+
"--sysroot={}",
114+
env::var("MIRI_SYSROOT").expect("MIRI_SYSROOT must be set to run the ui test suite")
115+
)
116+
.into(),
117+
);
111118
config.program.args.push("-Dwarnings".into());
112119
config.program.args.push("-Dunused".into());
113120
config.program.args.push("-Ainternal_features".into());

0 commit comments

Comments
 (0)