Skip to content

Commit 70f83a3

Browse files
committed
re-do cargo-miri host/target detection logic to match rustbuild
1 parent 17f740e commit 70f83a3

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/bin/cargo-miri.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,14 @@ fn in_cargo_miri() {
462462
}
463463
cmd.arg(arg);
464464
}
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+
}
465473

466474
// Serialize the remaining args into a special environemt variable.
467475
// This will be read by `inside_cargo_rustc` when we go to invoke
@@ -491,24 +499,21 @@ fn in_cargo_miri() {
491499
}
492500

493501
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.
498507
///
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".
502511
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()
508513
}
509514

510515
/// 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')
512517
/// Cargo does not give us this information directly, so we need to check
513518
/// various command-line flags.
514519
fn is_runnable_crate() -> bool {

0 commit comments

Comments
 (0)