@@ -462,6 +462,14 @@ fn in_cargo_miri() {
462
462
}
463
463
cmd. arg ( arg) ;
464
464
}
465
+ // We want to always run `cargo` with `--target`. This later helps us detect
466
+ // which crates are proc-macro/build-script (host crates) and which crates are
467
+ // needed for the program itself.
468
+ if get_arg_flag_value ( "--target" ) . is_none ( ) {
469
+ // When no `--target` is given, default to the host.
470
+ cmd. arg ( "--target" ) ;
471
+ cmd. arg ( rustc_version:: version_meta ( ) . unwrap ( ) . host ) ;
472
+ }
465
473
466
474
// Serialize the remaining args into a special environemt variable.
467
475
// This will be read by `inside_cargo_rustc` when we go to invoke
@@ -491,24 +499,21 @@ fn in_cargo_miri() {
491
499
}
492
500
493
501
fn inside_cargo_rustc ( ) {
494
- /// Determines if we are being invoked (as rustc) to build a runnable
495
- /// executable. We run "cargo check", so this should only happen when
496
- /// we are trying to compile a build script or build script dependency,
497
- /// which actually needs to be executed on the host platform.
502
+ /// Determines if we are being invoked (as rustc) to build a crate for
503
+ /// the "target" architecture, in contrast to the "host" architecture.
504
+ /// Host crates are for build scripts and proc macros and still need to
505
+ /// be built like normal; target crates need to be built for or interpreted
506
+ /// by Miri.
498
507
///
499
- /// Currently, we detect this by checking for "--emit=link",
500
- /// which indicates that Cargo instruced rustc to output
501
- /// a native object .
508
+ /// Currently, we detect this by checking for "--target=", which flag is
509
+ /// never set for host crates. This matches what rustc bootstrap does,
510
+ /// which hopefully makes it "reliable enough" .
502
511
fn is_target_crate ( ) -> bool {
503
- // `--emit` is sometimes missing, e.g. cargo calls rustc for "--print".
504
- // That is definitely not a target crate.
505
- // If `--emit` is present, then host crates are built ("--emit=link,...),
506
- // while the rest is only checked.
507
- get_arg_flag_value ( "--emit" ) . map_or ( false , |emit| !emit. contains ( "link" ) )
512
+ get_arg_flag_value ( "--target" ) . is_some ( )
508
513
}
509
514
510
515
/// Returns whether or not Cargo invoked the wrapper (this binary) to compile
511
- /// the final, target crate (either a test for 'cargo test', or a binary for 'cargo run')
516
+ /// the final, binary crate (either a test for 'cargo test', or a binary for 'cargo run')
512
517
/// Cargo does not give us this information directly, so we need to check
513
518
/// various command-line flags.
514
519
fn is_runnable_crate ( ) -> bool {
0 commit comments