@@ -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`.)
@@ -204,6 +202,8 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
204202 // Set rustdoc to us as well, so we can run doctests.
205203 cmd. env ( "RUSTDOC" , & cargo_miri_path) ;
206204
205+ // Forward some crucial information to our own re-invocations.
206+ cmd. env ( "MIRI_SYSROOT" , miri_sysroot) ;
207207 cmd. env ( "MIRI_LOCAL_CRATES" , local_crates ( & metadata) ) ;
208208 if verbose > 0 {
209209 cmd. env ( "MIRI_VERBOSE" , verbose. to_string ( ) ) ; // This makes the other phases verbose.
@@ -244,6 +244,16 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
244244 /// Cargo does not give us this information directly, so we need to check
245245 /// various command-line flags.
246246 fn is_runnable_crate ( ) -> bool {
247+ // Determine whether this is cargo invoking rustc to get some infos. Ideally we'd check "is
248+ // there a filename passed to rustc", but that's very hard as we would have to know whether
249+ // e.g. `--print foo` is a booolean flag `--print` followed by filename `foo` or equivalent
250+ // to `--print=foo`. So instead we use this more fragile approach of detecting the presence
251+ // of a "query" flag rather than the absence of a filename.
252+ let info_query = get_arg_flag_value ( "--print" ) . is_some ( ) || has_arg_flag ( "-vV" ) ;
253+ if info_query {
254+ // Nothing to run.
255+ return false ;
256+ }
247257 let is_bin = get_arg_flag_value ( "--crate-type" ) . as_deref ( ) . unwrap_or ( "bin" ) == "bin" ;
248258 let is_test = has_arg_flag ( "--test" ) ;
249259 is_bin || is_test
@@ -290,8 +300,6 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
290300 let verbose = std:: env:: var ( "MIRI_VERBOSE" )
291301 . map_or ( 0 , |verbose| verbose. parse ( ) . expect ( "verbosity flag must be an integer" ) ) ;
292302 let target_crate = is_target_crate ( ) ;
293- // Determine whether this is cargo invoking rustc to get some infos.
294- let info_query = get_arg_flag_value ( "--print" ) . is_some ( ) || has_arg_flag ( "-vV" ) ;
295303
296304 let store_json = |info : CrateRunInfo | {
297305 if get_arg_flag_value ( "--emit" ) . unwrap_or_default ( ) . split ( ',' ) . any ( |e| e == "dep-info" ) {
@@ -318,7 +326,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
318326 }
319327 } ;
320328
321- let runnable_crate = !info_query && is_runnable_crate ( ) ;
329+ let runnable_crate = is_runnable_crate ( ) ;
322330
323331 if runnable_crate && target_crate {
324332 assert ! (
@@ -392,7 +400,9 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
392400 let mut emit_link_hack = false ;
393401 // Arguments are treated very differently depending on whether this crate is
394402 // for interpretation by Miri, or for use by a build script / proc macro.
395- if !info_query && target_crate {
403+ if target_crate {
404+ // Set the sysroot.
405+ cmd. arg ( "--sysroot" ) . arg ( env:: var_os ( "MIRI_SYSROOT" ) . unwrap ( ) ) ;
396406 // Forward arguments, but remove "link" from "--emit" to make this a check-only build.
397407 let emit_flag = "--emit" ;
398408 while let Some ( arg) = args. next ( ) {
@@ -426,17 +436,14 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
426436 cmd. arg ( "-C" ) . arg ( "panic=abort" ) ;
427437 }
428438 } else {
429- // For host crates (but not when we are just printing some info),
430- // we might still have to set the sysroot.
431- if !info_query {
432- // When we're running `cargo-miri` from `x.py` we need to pass the sysroot explicitly
433- // due to bootstrap complications.
434- if let Some ( sysroot) = std:: env:: var_os ( "MIRI_HOST_SYSROOT" ) {
435- cmd. arg ( "--sysroot" ) . arg ( sysroot) ;
436- }
439+ // This is a host crate.
440+ // When we're running `cargo-miri` from `x.py` we need to pass the sysroot explicitly
441+ // due to bootstrap complications.
442+ if let Some ( sysroot) = std:: env:: var_os ( "MIRI_HOST_SYSROOT" ) {
443+ cmd. arg ( "--sysroot" ) . arg ( sysroot) ;
437444 }
438445
439- // For host crates or when we are printing, just forward everything.
446+ // Forward everything.
440447 cmd. args ( args) ;
441448 }
442449
@@ -448,9 +455,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
448455
449456 // Run it.
450457 if verbose > 0 {
451- eprintln ! (
452- "[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} info_query={info_query}"
453- ) ;
458+ eprintln ! ( "[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate}" ) ;
454459 }
455460
456461 // Create a stub .rlib file if "link" was requested by cargo.
@@ -531,6 +536,12 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
531536 cmd. env ( name, val) ;
532537 }
533538
539+ if phase != RunnerPhase :: Rustdoc {
540+ // Set the sysroot. Not necessary in rustdoc, where we already set the sysroot when invoking
541+ // rustdoc itself, which will forward that flag when invoking rustc (i.e., us), so the flag
542+ // is present in `info.args`.
543+ cmd. arg ( "--sysroot" ) . arg ( env:: var_os ( "MIRI_SYSROOT" ) . unwrap ( ) ) ;
544+ }
534545 // Forward rustc arguments.
535546 // We need to patch "--extern" filenames because we forced a check-only
536547 // build without cargo knowing about that: replace `.rlib` suffix by
@@ -539,15 +550,13 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
539550 // but when we run here, cargo does not interpret the JSON any more. `--json`
540551 // then also needs to be dropped.
541552 let mut args = info. args . into_iter ( ) ;
542- let error_format_flag = "--error-format" ;
543- let json_flag = "--json" ;
544553 while let Some ( arg) = args. next ( ) {
545554 if arg == "--extern" {
546555 forward_patched_extern_arg ( & mut args, & mut cmd) ;
547- } else if let Some ( suffix) = arg. strip_prefix ( error_format_flag ) {
556+ } else if let Some ( suffix) = arg. strip_prefix ( "--error-format" ) {
548557 assert ! ( suffix. starts_with( '=' ) ) ;
549558 // Drop this argument.
550- } else if let Some ( suffix) = arg. strip_prefix ( json_flag ) {
559+ } else if let Some ( suffix) = arg. strip_prefix ( "--json" ) {
551560 assert ! ( suffix. starts_with( '=' ) ) ;
552561 // Drop this argument.
553562 } else {
@@ -585,13 +594,11 @@ pub fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
585594 // just default to a straight-forward invocation for now:
586595 let mut cmd = Command :: new ( "rustdoc" ) ;
587596
588- let extern_flag = "--extern" ;
589- let runtool_flag = "--runtool" ;
590597 while let Some ( arg) = args. next ( ) {
591- if arg == extern_flag {
598+ if arg == "--extern" {
592599 // Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
593600 forward_patched_extern_arg ( & mut args, & mut cmd) ;
594- } else if arg == runtool_flag {
601+ } else if arg == "--runtool" {
595602 // An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
596603 // Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
597604 // otherwise, we won't be called as rustdoc at all.
0 commit comments