@@ -168,8 +168,6 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
168
168
// Forward all further arguments (not consumed by `ArgSplitFlagValue`) to cargo.
169
169
cmd. args ( args) ;
170
170
171
- // Let it know where the Miri sysroot lives.
172
- cmd. env ( "MIRI_SYSROOT" , miri_sysroot) ;
173
171
// Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation,
174
172
// i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish
175
173
// 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>) {
204
202
// Set rustdoc to us as well, so we can run doctests.
205
203
cmd. env ( "RUSTDOC" , & cargo_miri_path) ;
206
204
205
+ // Forward some crucial information to our own re-invocations.
206
+ cmd. env ( "MIRI_SYSROOT" , miri_sysroot) ;
207
207
cmd. env ( "MIRI_LOCAL_CRATES" , local_crates ( & metadata) ) ;
208
208
if verbose > 0 {
209
209
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) {
244
244
/// Cargo does not give us this information directly, so we need to check
245
245
/// various command-line flags.
246
246
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
+ }
247
257
let is_bin = get_arg_flag_value ( "--crate-type" ) . as_deref ( ) . unwrap_or ( "bin" ) == "bin" ;
248
258
let is_test = has_arg_flag ( "--test" ) ;
249
259
is_bin || is_test
@@ -290,8 +300,6 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
290
300
let verbose = std:: env:: var ( "MIRI_VERBOSE" )
291
301
. map_or ( 0 , |verbose| verbose. parse ( ) . expect ( "verbosity flag must be an integer" ) ) ;
292
302
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" ) ;
295
303
296
304
let store_json = |info : CrateRunInfo | {
297
305
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) {
318
326
}
319
327
} ;
320
328
321
- let runnable_crate = !info_query && is_runnable_crate ( ) ;
329
+ let runnable_crate = is_runnable_crate ( ) ;
322
330
323
331
if runnable_crate && target_crate {
324
332
assert ! (
@@ -392,7 +400,9 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
392
400
let mut emit_link_hack = false ;
393
401
// Arguments are treated very differently depending on whether this crate is
394
402
// 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 ( ) ) ;
396
406
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
397
407
let emit_flag = "--emit" ;
398
408
while let Some ( arg) = args. next ( ) {
@@ -426,17 +436,14 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
426
436
cmd. arg ( "-C" ) . arg ( "panic=abort" ) ;
427
437
}
428
438
} 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) ;
437
444
}
438
445
439
- // For host crates or when we are printing, just forward everything.
446
+ // Forward everything.
440
447
cmd. args ( args) ;
441
448
}
442
449
@@ -448,9 +455,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
448
455
449
456
// Run it.
450
457
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}" ) ;
454
459
}
455
460
456
461
// 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
531
536
cmd. env ( name, val) ;
532
537
}
533
538
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
+ }
534
545
// Forward rustc arguments.
535
546
// We need to patch "--extern" filenames because we forced a check-only
536
547
// 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
539
550
// but when we run here, cargo does not interpret the JSON any more. `--json`
540
551
// then also needs to be dropped.
541
552
let mut args = info. args . into_iter ( ) ;
542
- let error_format_flag = "--error-format" ;
543
- let json_flag = "--json" ;
544
553
while let Some ( arg) = args. next ( ) {
545
554
if arg == "--extern" {
546
555
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" ) {
548
557
assert ! ( suffix. starts_with( '=' ) ) ;
549
558
// Drop this argument.
550
- } else if let Some ( suffix) = arg. strip_prefix ( json_flag ) {
559
+ } else if let Some ( suffix) = arg. strip_prefix ( "--json" ) {
551
560
assert ! ( suffix. starts_with( '=' ) ) ;
552
561
// Drop this argument.
553
562
} else {
@@ -585,13 +594,11 @@ pub fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
585
594
// just default to a straight-forward invocation for now:
586
595
let mut cmd = Command :: new ( "rustdoc" ) ;
587
596
588
- let extern_flag = "--extern" ;
589
- let runtool_flag = "--runtool" ;
590
597
while let Some ( arg) = args. next ( ) {
591
- if arg == extern_flag {
598
+ if arg == "--extern" {
592
599
// Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
593
600
forward_patched_extern_arg ( & mut args, & mut cmd) ;
594
- } else if arg == runtool_flag {
601
+ } else if arg == "--runtool" {
595
602
// An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
596
603
// Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
597
604
// otherwise, we won't be called as rustdoc at all.
0 commit comments