Skip to content

Commit 3d237be

Browse files
committed
Auto merge of #2436 - RalfJung:lib-crates, r=oli-obk
fix build.rs invoking RUSTC to do check builds This makes the Miri driver, when invokved via the RUSTC env var from inside a build script, behave almost entirely like rustc. I had to redo how we propagate sysroot information for this (which is actually back to how we used to do sysroot propagation many years ago). Fixes #2431
2 parents 30d1c68 + 9406b6d commit 3d237be

File tree

8 files changed

+136
-81
lines changed

8 files changed

+136
-81
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,9 @@ Moreover, Miri recognizes some environment variables:
409409
checkout. Note that changing files in that directory does not automatically
410410
trigger a re-build of the standard library; you have to clear the Miri build
411411
cache manually (on Linux, `rm -rf ~/.cache/miri`).
412-
* `MIRI_SYSROOT` (recognized by `cargo miri` and the test suite) indicates the
413-
sysroot to use. Only set this if you do not want to use the automatically
414-
created sysroot. (The `miri` driver sysroot is controlled via the `--sysroot`
415-
flag instead.)
412+
* `MIRI_SYSROOT` (recognized by `cargo miri` and the Miri driver) indicates the sysroot to use. When
413+
using `cargo miri`, only set this if you do not want to use the automatically created sysroot. For
414+
directly invoking the Miri driver, this variable (or a `--sysroot` flag) is mandatory.
416415
* `MIRI_TEST_TARGET` (recognized by the test suite and the `./miri` script) indicates which target
417416
architecture to test against. `miri` and `cargo miri` accept the `--target` flag for the same
418417
purpose.

cargo-miri/bin.rs

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,6 @@ fn forward_patched_extern_arg(args: &mut impl Iterator<Item = String>, cmd: &mut
205205
}
206206
}
207207

208-
fn forward_miri_sysroot(cmd: &mut Command) {
209-
let sysroot = env::var_os("MIRI_SYSROOT").expect("the wrapper should have set MIRI_SYSROOT");
210-
cmd.arg("--sysroot");
211-
cmd.arg(sysroot);
212-
}
213-
214208
/// Escapes `s` in a way that is suitable for using it as a string literal in TOML syntax.
215209
fn escape_for_toml(s: &str) -> String {
216210
// We want to surround this string in quotes `"`. So we first escape all quotes,
@@ -237,8 +231,15 @@ fn miri() -> Command {
237231
Command::new(find_miri())
238232
}
239233

234+
fn miri_for_host() -> Command {
235+
let mut cmd = miri();
236+
cmd.env("MIRI_BE_RUSTC", "host");
237+
cmd
238+
}
239+
240240
fn version_info() -> VersionMeta {
241-
VersionMeta::for_command(miri()).expect("failed to determine underlying rustc version of Miri")
241+
VersionMeta::for_command(miri_for_host())
242+
.expect("failed to determine underlying rustc version of Miri")
242243
}
243244

244245
fn cargo() -> Command {
@@ -336,7 +337,7 @@ fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
336337
a => show_error(format!("invalid answer `{}`", a)),
337338
};
338339
} else {
339-
println!("Running `{:?}` to {}.", cmd, text);
340+
eprintln!("Running `{:?}` to {}.", cmd, text);
340341
}
341342

342343
if cmd.status().unwrap_or_else(|_| panic!("failed to execute {:?}", cmd)).success().not() {
@@ -364,7 +365,7 @@ fn write_to_file(filename: &Path, content: &str) {
364365
/// Performs the setup required to make `cargo miri` work: Getting a custom-built libstd. Then sets
365366
/// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
366367
/// done all this already.
367-
fn setup(subcommand: &MiriCommand) {
368+
fn setup(subcommand: &MiriCommand, host: &str, target: &str) {
368369
let only_setup = matches!(subcommand, MiriCommand::Setup);
369370
let ask_user = !only_setup;
370371
let print_sysroot = only_setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
@@ -398,8 +399,10 @@ fn setup(subcommand: &MiriCommand) {
398399
}
399400
None => {
400401
// Check for `rust-src` rustup component.
401-
let output =
402-
miri().args(&["--print", "sysroot"]).output().expect("failed to determine sysroot");
402+
let output = miri_for_host()
403+
.args(&["--print", "sysroot"])
404+
.output()
405+
.expect("failed to determine sysroot");
403406
if !output.status.success() {
404407
show_error(format!(
405408
"Failed to determine sysroot; Miri said:\n{}",
@@ -472,18 +475,21 @@ path = "lib.rs"
472475
);
473476
write_to_file(&dir.join("lib.rs"), "#![no_std]");
474477

475-
// Determine architectures.
476-
// We always need to set a target so rustc bootstrap can tell apart host from target crates.
477-
let host = version_info().host;
478-
let target = get_arg_flag_value("--target");
479-
let target = target.as_ref().unwrap_or(&host);
478+
// Figure out where xargo will build its stuff.
479+
// Unfortunately, it puts things into a different directory when the
480+
// architecture matches the host.
481+
let sysroot = if target == host { dir.join("HOST") } else { PathBuf::from(dir) };
482+
// Make sure all target-level Miri invocations know their sysroot.
483+
std::env::set_var("MIRI_SYSROOT", &sysroot);
484+
480485
// Now invoke xargo.
481486
let mut command = xargo_check();
482487
command.arg("check").arg("-q");
483-
command.arg("--target").arg(target);
484488
command.current_dir(&dir);
485489
command.env("XARGO_HOME", &dir);
486490
command.env("XARGO_RUST_SRC", &rust_src);
491+
// We always need to set a target so rustc bootstrap can tell apart host from target crates.
492+
command.arg("--target").arg(target);
487493
// Use Miri as rustc to build a libstd compatible with us (and use the right flags).
488494
// However, when we are running in bootstrap, we cannot just overwrite `RUSTC`,
489495
// because we still need bootstrap to distinguish between host and target crates.
@@ -523,6 +529,7 @@ path = "lib.rs"
523529
command.stdout(process::Stdio::null());
524530
command.stderr(process::Stdio::null());
525531
}
532+
526533
// Finally run it!
527534
if command.status().expect("failed to run xargo").success().not() {
528535
if only_setup {
@@ -534,11 +541,6 @@ path = "lib.rs"
534541
}
535542
}
536543

537-
// That should be it! But we need to figure out where xargo built stuff.
538-
// Unfortunately, it puts things into a different directory when the
539-
// architecture matches the host.
540-
let sysroot = if target == &host { dir.join("HOST") } else { PathBuf::from(dir) };
541-
std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags
542544
// Figure out what to print.
543545
if only_setup {
544546
eprintln!("A sysroot for Miri is now available in `{}`.", sysroot.display());
@@ -677,8 +679,13 @@ fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
677679
};
678680
let verbose = num_arg_flag("-v");
679681

682+
// Determine the involved architectures.
683+
let host = version_info().host;
684+
let target = get_arg_flag_value("--target");
685+
let target = target.as_ref().unwrap_or(&host);
686+
680687
// We always setup.
681-
setup(&subcommand);
688+
setup(&subcommand, &host, target);
682689

683690
// Invoke actual cargo for the job, but with different flags.
684691
// We re-use `cargo test` and `cargo run`, which makes target and binary handling very easy but
@@ -727,7 +734,7 @@ fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
727734
if get_arg_flag_value("--target").is_none() {
728735
// No target given. Explicitly pick the host.
729736
cmd.arg("--target");
730-
cmd.arg(version_info().host);
737+
cmd.arg(&host);
731738
}
732739

733740
// Set ourselves as runner for al binaries invoked by cargo.
@@ -754,16 +761,21 @@ fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
754761
"WARNING: Ignoring `RUSTC` environment variable; set `MIRI` if you want to control the binary used as the driver."
755762
);
756763
}
757-
// We'd prefer to just clear this env var, but cargo does not always honor `RUSTC_WRAPPER`
758-
// (https://github.com/rust-lang/cargo/issues/10885). There is no good way to single out these invocations;
759-
// some build scripts use the RUSTC env var as well. So we set it directly to the `miri` driver and
760-
// hope that all they do is ask for the version number -- things could quickly go downhill from here.
764+
// Build scripts (and also cargo: https://github.com/rust-lang/cargo/issues/10885) will invoke
765+
// `rustc` even when `RUSTC_WRAPPER` is set. To make sure everything is coherent, we want that
766+
// to be the Miri driver, but acting as rustc, on the target level. (Target, rather than host,
767+
// is needed for cross-interpretation situations.) This is not a perfect emulation of real rustc
768+
// (it might be unable to produce binaries since the sysroot is check-only), but it's as close
769+
// as we can get, and it's good enough for autocfg.
770+
//
761771
// In `main`, we need the value of `RUSTC` to distinguish RUSTC_WRAPPER invocations from rustdoc
762772
// or TARGET_RUNNER invocations, so we canonicalize it here to make it exceedingly unlikely that
763-
// there would be a collision with other invocations of cargo-miri (as rustdoc or as runner).
764-
// We explicitly do this even if RUSTC_STAGE is set, since for these builds we do *not* want the
765-
// bootstrap `rustc` thing in our way! Instead, we have MIRI_HOST_SYSROOT to use for host builds.
773+
// there would be a collision with other invocations of cargo-miri (as rustdoc or as runner). We
774+
// explicitly do this even if RUSTC_STAGE is set, since for these builds we do *not* want the
775+
// bootstrap `rustc` thing in our way! Instead, we have MIRI_HOST_SYSROOT to use for host
776+
// builds.
766777
cmd.env("RUSTC", &fs::canonicalize(find_miri()).unwrap());
778+
cmd.env("MIRI_BE_RUSTC", "target"); // we better remember to *unset* this in the other phases!
767779

768780
// Set rustdoc to us as well, so we can run doctests.
769781
cmd.env("RUSTDOC", &cargo_miri_path);
@@ -832,10 +844,16 @@ fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
832844
}
833845
}
834846

847+
// phase_cargo_miri set `MIRI_BE_RUSTC` for when build scripts directly invoke the driver;
848+
// however, if we get called back by cargo here, we'll carefully compute the right flags
849+
// ourselves, so we first un-do what the earlier phase did.
850+
env::remove_var("MIRI_BE_RUSTC");
851+
835852
let verbose = std::env::var("MIRI_VERBOSE")
836853
.map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer"));
837854
let target_crate = is_target_crate();
838-
let print = get_arg_flag_value("--print").is_some() || has_arg_flag("-vV"); // whether this is cargo/xargo invoking rustc to get some infos
855+
// Determine whether this is cargo/xargo invoking rustc to get some infos.
856+
let info_query = get_arg_flag_value("--print").is_some() || has_arg_flag("-vV");
839857

840858
let store_json = |info: CrateRunInfo| {
841859
// Create a stub .d file to stop Cargo from "rebuilding" the crate:
@@ -857,7 +875,7 @@ fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
857875
info.store(&out_filename("", ".exe"));
858876
};
859877

860-
let runnable_crate = !print && is_runnable_crate();
878+
let runnable_crate = !info_query && is_runnable_crate();
861879

862880
if runnable_crate && target_crate {
863881
assert!(
@@ -919,7 +937,7 @@ fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
919937
let mut emit_link_hack = false;
920938
// Arguments are treated very differently depending on whether this crate is
921939
// for interpretation by Miri, or for use by a build script / proc macro.
922-
if !print && target_crate {
940+
if !info_query && target_crate {
923941
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
924942
let emit_flag = "--emit";
925943
while let Some(arg) = args.next() {
@@ -946,20 +964,16 @@ fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
946964
}
947965
}
948966

949-
// Use our custom sysroot (but not if that is what we are currently building).
950-
if phase != RustcPhase::Setup {
951-
forward_miri_sysroot(&mut cmd);
952-
}
953-
954967
// During setup, patch the panic runtime for `libpanic_abort` (mirroring what bootstrap usually does).
955968
if phase == RustcPhase::Setup
956969
&& get_arg_flag_value("--crate-name").as_deref() == Some("panic_abort")
957970
{
958971
cmd.arg("-C").arg("panic=abort");
959972
}
960973
} else {
961-
// For host crates (but not when we are printing), we might still have to set the sysroot.
962-
if !print {
974+
// For host crates (but not when we are just printing some info),
975+
// we might still have to set the sysroot.
976+
if !info_query {
963977
// When we're running `cargo-miri` from `x.py` we need to pass the sysroot explicitly
964978
// due to bootstrap complications.
965979
if let Some(sysroot) = std::env::var_os("MIRI_HOST_SYSROOT") {
@@ -980,7 +994,7 @@ fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
980994
// Run it.
981995
if verbose > 0 {
982996
eprintln!(
983-
"[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} print={print}"
997+
"[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} info_query={info_query}"
984998
);
985999
}
9861000
debug_cmd("[cargo-miri rustc]", verbose, &cmd);
@@ -1010,12 +1024,19 @@ enum RunnerPhase {
10101024
}
10111025

10121026
fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: RunnerPhase) {
1027+
// phase_cargo_miri set `MIRI_BE_RUSTC` for when build scripts directly invoke the driver;
1028+
// however, if we get called back by cargo here, we'll carefully compute the right flags
1029+
// ourselves, so we first un-do what the earlier phase did.
1030+
env::remove_var("MIRI_BE_RUSTC");
1031+
10131032
let verbose = std::env::var("MIRI_VERBOSE")
10141033
.map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer"));
10151034

10161035
let binary = binary_args.next().unwrap();
10171036
let file = File::open(&binary)
1018-
.unwrap_or_else(|_| show_error(format!("file {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`", binary)));
1037+
.unwrap_or_else(|_| show_error(format!(
1038+
"file {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`", binary
1039+
)));
10191040
let file = BufReader::new(file);
10201041

10211042
let info = serde_json::from_reader(file).unwrap_or_else(|_| {
@@ -1077,10 +1098,6 @@ fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: RunnerPhas
10771098
cmd.arg(arg);
10781099
}
10791100
}
1080-
// Set sysroot (if we are inside rustdoc, we already did that in `phase_cargo_rustdoc`).
1081-
if phase != RunnerPhase::Rustdoc {
1082-
forward_miri_sysroot(&mut cmd);
1083-
}
10841101
// Respect `MIRIFLAGS`.
10851102
if let Ok(a) = env::var("MIRIFLAGS") {
10861103
// This code is taken from `RUSTFLAGS` handling in cargo.
@@ -1151,7 +1168,7 @@ fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
11511168
cmd.arg("-Z").arg("unstable-options");
11521169

11531170
// rustdoc needs to know the right sysroot.
1154-
forward_miri_sysroot(&mut cmd);
1171+
cmd.arg("--sysroot").arg(env::var_os("MIRI_SYSROOT").unwrap());
11551172
// make sure the 'miri' flag is set for rustdoc
11561173
cmd.arg("--cfg").arg("miri");
11571174

miri

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ export RUSTFLAGS="-C link-args=-Wl,-rpath,$LIBDIR $RUSTFLAGS"
131131

132132
# Build a sysroot and set MIRI_SYSROOT to use it. Arguments are passed to `cargo miri setup`.
133133
build_sysroot() {
134-
export MIRI_SYSROOT="$($CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"
134+
if ! MIRI_SYSROOT="$($CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/cargo-miri/Cargo.toml -q -- miri setup --print-sysroot "$@")"; then
135+
echo "'cargo miri setup' failed"
136+
exit 1
137+
fi
138+
export MIRI_SYSROOT
135139
}
136140

137141
# Prepare and set MIRI_SYSROOT. Respects `MIRI_TEST_TARGET` and takes into account
@@ -201,7 +205,7 @@ run)
201205
$CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
202206
find_sysroot
203207
# Then run the actual command.
204-
exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- --sysroot "$MIRI_SYSROOT" $MIRIFLAGS "$@"
208+
exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"
205209
;;
206210
fmt)
207211
find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \

0 commit comments

Comments
 (0)