Skip to content

Commit 5b877a3

Browse files
committed
cargo-miri: clean up info_query treatment a bit, and update comment about RUSTC
1 parent a9b4a94 commit 5b877a3

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/tools/miri/cargo-miri/src/phases.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,14 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
192192
"WARNING: Ignoring `RUSTC` environment variable; set `MIRI` if you want to control the binary used as the driver."
193193
);
194194
}
195-
// Build scripts (and also cargo: https://github.com/rust-lang/cargo/issues/10885) will invoke
196-
// `rustc` even when `RUSTC_WRAPPER` is set. To make sure everything is coherent, we want that
197-
// to be the Miri driver, but acting as rustc, on the target level. (Target, rather than host,
198-
// is needed for cross-interpretation situations.) This is not a perfect emulation of real rustc
199-
// (it might be unable to produce binaries since the sysroot is check-only), but it's as close
200-
// as we can get, and it's good enough for autocfg.
195+
// Ideally we would set RUSTC to some non-existent path, so we can be sure our wrapping is
196+
// always applied. However, buggy build scripts (https://github.com/eyre-rs/eyre/issues/84) and
197+
// also cargo (https://github.com/rust-lang/cargo/issues/10885) will invoke `rustc` even when
198+
// `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.
201203
//
202204
// In `main`, we need the value of `RUSTC` to distinguish RUSTC_WRAPPER invocations from rustdoc
203205
// or TARGET_RUNNER invocations, so we canonicalize it here to make it exceedingly unlikely that
@@ -251,6 +253,16 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
251253
/// Cargo does not give us this information directly, so we need to check
252254
/// various command-line flags.
253255
fn is_runnable_crate() -> bool {
256+
// Determine whether this is cargo invoking rustc to get some infos. Ideally we'd check "is
257+
// there a filename passed to rustc", but that's very hard as we would have to know whether
258+
// e.g. `--print foo` is a booolean flag `--print` followed by filename `foo` or equivalent
259+
// to `--print=foo`. So instead we use this more fragile approach of detecting the presence
260+
// of a "query" flag rather than the absence of a filename.
261+
let info_query = get_arg_flag_value("--print").is_some() || has_arg_flag("-vV");
262+
if info_query {
263+
// Nothing to run.
264+
return false;
265+
}
254266
let is_bin = get_arg_flag_value("--crate-type").as_deref().unwrap_or("bin") == "bin";
255267
let is_test = has_arg_flag("--test");
256268
is_bin || is_test
@@ -297,8 +309,6 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
297309
let verbose = std::env::var("MIRI_VERBOSE")
298310
.map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer"));
299311
let target_crate = is_target_crate();
300-
// Determine whether this is cargo invoking rustc to get some infos.
301-
let info_query = get_arg_flag_value("--print").is_some() || has_arg_flag("-vV");
302312

303313
let store_json = |info: CrateRunInfo| {
304314
if get_arg_flag_value("--emit").unwrap_or_default().split(',').any(|e| e == "dep-info") {
@@ -325,7 +335,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
325335
}
326336
};
327337

328-
let runnable_crate = !info_query && is_runnable_crate();
338+
let runnable_crate = is_runnable_crate();
329339

330340
if runnable_crate && target_crate {
331341
assert!(
@@ -399,7 +409,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
399409
let mut emit_link_hack = false;
400410
// Arguments are treated very differently depending on whether this crate is
401411
// for interpretation by Miri, or for use by a build script / proc macro.
402-
if !info_query && target_crate {
412+
if target_crate {
403413
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
404414
let emit_flag = "--emit";
405415
while let Some(arg) = args.next() {
@@ -433,17 +443,14 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
433443
cmd.arg("-C").arg("panic=abort");
434444
}
435445
} else {
436-
// For host crates (but not when we are just printing some info),
437-
// we might still have to set the sysroot.
438-
if !info_query {
439-
// When we're running `cargo-miri` from `x.py` we need to pass the sysroot explicitly
440-
// due to bootstrap complications.
441-
if let Some(sysroot) = std::env::var_os("MIRI_HOST_SYSROOT") {
442-
cmd.arg("--sysroot").arg(sysroot);
443-
}
446+
// This is a host crate.
447+
// When we're running `cargo-miri` from `x.py` we need to pass the sysroot explicitly
448+
// due to bootstrap complications.
449+
if let Some(sysroot) = std::env::var_os("MIRI_HOST_SYSROOT") {
450+
cmd.arg("--sysroot").arg(sysroot);
444451
}
445452

446-
// For host crates or when we are printing, just forward everything.
453+
// Forward everything.
447454
cmd.args(args);
448455
}
449456

@@ -455,9 +462,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
455462

456463
// Run it.
457464
if verbose > 0 {
458-
eprintln!(
459-
"[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} info_query={info_query}"
460-
);
465+
eprintln!("[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate}");
461466
}
462467

463468
// Create a stub .rlib file if "link" was requested by cargo.
@@ -546,15 +551,13 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
546551
// but when we run here, cargo does not interpret the JSON any more. `--json`
547552
// then also needs to be dropped.
548553
let mut args = info.args.into_iter();
549-
let error_format_flag = "--error-format";
550-
let json_flag = "--json";
551554
while let Some(arg) = args.next() {
552555
if arg == "--extern" {
553556
forward_patched_extern_arg(&mut args, &mut cmd);
554-
} else if let Some(suffix) = arg.strip_prefix(error_format_flag) {
557+
} else if let Some(suffix) = arg.strip_prefix("--error-format") {
555558
assert!(suffix.starts_with('='));
556559
// Drop this argument.
557-
} else if let Some(suffix) = arg.strip_prefix(json_flag) {
560+
} else if let Some(suffix) = arg.strip_prefix("--json") {
558561
assert!(suffix.starts_with('='));
559562
// Drop this argument.
560563
} else {
@@ -592,13 +595,11 @@ pub fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
592595
// just default to a straight-forward invocation for now:
593596
let mut cmd = Command::new("rustdoc");
594597

595-
let extern_flag = "--extern";
596-
let runtool_flag = "--runtool";
597598
while let Some(arg) = args.next() {
598-
if arg == extern_flag {
599+
if arg == "--extern" {
599600
// Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
600601
forward_patched_extern_arg(&mut args, &mut cmd);
601-
} else if arg == runtool_flag {
602+
} else if arg == "--runtool" {
602603
// An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
603604
// Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
604605
// otherwise, we won't be called as rustdoc at all.

0 commit comments

Comments
 (0)