Skip to content

Commit f5c945d

Browse files
committed
Switch to using crate type
1 parent fb7f803 commit f5c945d

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/bin/cargo-miri.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ fn get_arg_flag_value(name: &str) -> Option<String> {
8585
}
8686

8787
fn is_build_dep(mut args: impl Iterator<Item = String>) -> bool {
88+
//eprintln!("Out dir: {:?}", std::env::var("OUT_DIR"));
8889
args.any(|arg| arg.starts_with("--emit=") && arg.contains("link"))
8990
}
9091

@@ -96,20 +97,26 @@ fn is_build_dep(mut args: impl Iterator<Item = String>) -> bool {
9697
// not the wrapper is being invoked on the target crate.
9798
// For now, this is the best we can do
9899
fn is_target_crate(is_build_script: bool) -> bool {
100+
//eprintln!("Args: {:?}", std::env::args().collect::<Vec<_>>());
101+
let is_bin = get_arg_flag_value("--crate-type").as_deref() == Some("bin");
102+
let is_test = std::env::args().find(|arg| arg == "--test").is_some();
103+
(is_bin || is_test) && !is_build_script
99104
// Cargo sets this to the directory containing the manifest of the crate
100105
// the wrapper is being invoekd to compile. This should be unique
101106
// across the entire build (except for build scripts, which we handle below).
102107
// We cannot check the crate name, since this may not be unique
103108
// (e.g. if the build contains multiple versions of the same crate,
104109
// or the same crate from multiple sources)
105-
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").ok();
110+
//let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").ok();
106111

107112
// The manifest directory for our target crate. This is set by `cargo-miri`
108113
// (the original invoation by the user) by using `cargo_metadata` to locate
109114
// the manifest.
110-
let expected_dir = std::env::var("MIRI_MAGIC_DIR").expect("MIRI_MAGIC_DIR not set!");
115+
//let expected_dir = std::env::var("MIRI_MAGIC_DIR").expect("MIRI_MAGIC_DIR not set!");
111116

112-
manifest_dir == Some(expected_dir) && !is_build_script
117+
//manifest_dir == Some(expected_dir) && !is_build_script
118+
//
119+
//false
113120

114121
}
115122

@@ -427,7 +434,7 @@ path = "lib.rs"
427434
// architecture matches the host.
428435
let is_host = match target {
429436
None => true,
430-
Some(target) => target == rustc_version::version_meta().unwrap().host,
437+
Some(target) => target == host_triple(),
431438
};
432439
let sysroot = if is_host { dir.join("HOST") } else { PathBuf::from(dir) };
433440
std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags
@@ -521,6 +528,17 @@ fn in_cargo_miri() {
521528
// The remaining targets we do not even want to build.
522529
_ => continue,
523530
}
531+
532+
let target = match get_arg_flag_value("--target") {
533+
Some(target) => target,
534+
None => {
535+
let target = host_triple();
536+
cmd.arg("--target").arg(&target);
537+
target
538+
}
539+
};
540+
541+
524542
// Add user-defined args until first `--`.
525543
while let Some(arg) = args.next() {
526544
if arg == "--" {
@@ -530,7 +548,8 @@ fn in_cargo_miri() {
530548
}
531549

532550
let args_vec: Vec<String> = args.collect();
533-
cmd.env("MIRI_MAGIC_DIR", root_dir.clone());
551+
cmd.env("MIRI_MAGIC_TARGET", target.clone());
552+
//cmd.env("MIRI_MAGIC_DIR", root_dir.clone());
534553
cmd.env("MIRI_MAGIC_ARGS", serde_json::to_string(&args_vec).expect("failed to serialize args"));
535554

536555
// Add `--` (to end the `cargo` flags), and then the user flags. We add markers around the
@@ -552,6 +571,10 @@ fn in_cargo_miri() {
552571
}
553572
}
554573

574+
fn host_triple() -> String {
575+
rustc_version::version_meta().unwrap().host
576+
}
577+
555578
fn inside_cargo_rustc() {
556579
let sysroot = std::env::var("MIRI_SYSROOT").expect("The wrapper should have set MIRI_SYSROOT");
557580

0 commit comments

Comments
 (0)