Skip to content

Commit b5b5df5

Browse files
committed
Auto merge of #1710 - hyd-dev:crate-types, r=RalfJung
Patch `--extern` arguments in `phase_cargo_rustc` as well Fixes #1705.
2 parents c3f7069 + 1923044 commit b5b5df5

File tree

8 files changed

+74
-23
lines changed

8 files changed

+74
-23
lines changed

cargo-miri/bin.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ fn get_arg_flag_value(name: &str) -> Option<String> {
143143
ArgFlagValueIter::new(name).next()
144144
}
145145

146+
fn forward_patched_extern_arg(args: &mut impl Iterator<Item = String>, cmd: &mut Command) {
147+
cmd.arg("--extern"); // always forward flag, but adjust filename:
148+
let path = args.next().expect("`--extern` should be followed by a filename");
149+
if let Some(lib) = path.strip_suffix(".rlib") {
150+
// If this is an rlib, make it an rmeta.
151+
cmd.arg(format!("{}.rmeta", lib));
152+
} else {
153+
// Some other extern file (e.g. a `.so`). Forward unchanged.
154+
cmd.arg(path);
155+
}
156+
}
157+
146158
/// Returns the path to the `miri` binary
147159
fn find_miri() -> PathBuf {
148160
if let Some(path) = env::var_os("MIRI") {
@@ -553,7 +565,7 @@ fn phase_cargo_miri(mut args: env::Args) {
553565
exec(cmd)
554566
}
555567

556-
fn phase_cargo_rustc(args: env::Args) {
568+
fn phase_cargo_rustc(mut args: env::Args) {
557569
/// Determines if we are being invoked (as rustc) to build a crate for
558570
/// the "target" architecture, in contrast to the "host" architecture.
559571
/// Host crates are for build scripts and proc macros and still need to
@@ -596,15 +608,6 @@ fn phase_cargo_rustc(args: env::Args) {
596608
let target_crate = is_target_crate();
597609
let print = get_arg_flag_value("--print").is_some(); // whether this is cargo passing `--print` to get some infos
598610

599-
// cdylib is just skipped, we cannot interpret it and do not need it
600-
// for the rest of the build either.
601-
if get_arg_flag_value("--crate-type").as_deref() == Some("cdylib") {
602-
if verbose {
603-
eprint!("[cargo-miri rustc] (cdylib skipped)");
604-
}
605-
return;
606-
}
607-
608611
let store_json = |info: CrateRunInfo| {
609612
let filename = out_filename("", "");
610613
if verbose {
@@ -643,7 +646,7 @@ fn phase_cargo_rustc(args: env::Args) {
643646
if !print && target_crate {
644647
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
645648
let emit_flag = "--emit";
646-
for arg in args {
649+
while let Some(arg) = args.next() {
647650
if arg.starts_with(emit_flag) {
648651
// Patch this argument. First, extract its value.
649652
let val = &arg[emit_flag.len()..];
@@ -659,6 +662,10 @@ fn phase_cargo_rustc(args: env::Args) {
659662
}
660663
}
661664
cmd.arg(format!("{}={}", emit_flag, val.join(",")));
665+
} else if arg == "--extern" {
666+
// Patch `--extern` filenames, since Cargo sometimes passes stub `.rlib` files:
667+
// https://github.com/rust-lang/miri/issues/1705
668+
forward_patched_extern_arg(&mut args, &mut cmd);
662669
} else {
663670
cmd.arg(arg);
664671
}
@@ -734,21 +741,11 @@ fn phase_cargo_runner(binary: &Path, binary_args: env::Args) {
734741
// but when we run here, cargo does not interpret the JSON any more. `--json`
735742
// then also nees to be dropped.
736743
let mut args = info.args.into_iter();
737-
let extern_flag = "--extern";
738744
let error_format_flag = "--error-format";
739745
let json_flag = "--json";
740746
while let Some(arg) = args.next() {
741-
if arg == extern_flag {
742-
cmd.arg(extern_flag); // always forward flag, but adjust filename
743-
// `--extern` is always passed as a separate argument by cargo.
744-
let next_arg = args.next().expect("`--extern` should be followed by a filename");
745-
if let Some(next_lib) = next_arg.strip_suffix(".rlib") {
746-
// If this is an rlib, make it an rmeta.
747-
cmd.arg(format!("{}.rmeta", next_lib));
748-
} else {
749-
// Some other extern file (e.g., a `.so`). Forward unchanged.
750-
cmd.arg(next_arg);
751-
}
747+
if arg == "--extern" {
748+
forward_patched_extern_arg(&mut args, &mut cmd);
752749
} else if arg.starts_with(error_format_flag) {
753750
let suffix = &arg[error_format_flag.len()..];
754751
assert!(suffix.starts_with('='));

test-cargo-miri/Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-cargo-miri/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ edition = "2018"
99

1010
[dependencies]
1111
byteorder = "1.0"
12+
cdylib = { path = "cdylib" }
1213
issue_1567 = { path = "issue-1567" }
1314
issue_1691 = { path = "issue-1691" }
15+
issue_1705 = { path = "issue-1705" }
1416

1517
[dev-dependencies]
1618
rand = { version = "0.7", features = ["small_rng"] }

test-cargo-miri/cdylib/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "cdylib"
3+
version = "0.1.0"
4+
authors = ["Miri Team"]
5+
edition = "2018"
6+
7+
[lib]
8+
# cargo-miri used to handle `cdylib` crate-type specially (https://github.com/rust-lang/miri/pull/1577).
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
byteorder = "1.0"

test-cargo-miri/cdylib/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use byteorder::{BigEndian, ByteOrder};
2+
3+
#[no_mangle]
4+
extern "C" fn use_the_dependency() {
5+
let _n = <BigEndian as ByteOrder>::read_u64(&[1,2,3,4,5,6,7,8]);
6+
}

test-cargo-miri/issue-1705/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "issue_1705"
3+
version = "0.1.0"
4+
authors = ["Miri Team"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["lib", "staticlib", "cdylib"]
9+
10+
[dependencies]
11+
byteorder = "1.0"

test-cargo-miri/issue-1705/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use byteorder::{LittleEndian, ByteOrder};
2+
3+
pub fn use_the_dependency() {
4+
let _n = <LittleEndian as ByteOrder>::read_u32(&[1,2,3,4]);
5+
}

test-cargo-miri/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
/// assert!(cargo_miri_test::make_true());
44
/// ```
55
pub fn make_true() -> bool {
6+
issue_1567::use_the_dependency();
7+
issue_1705::use_the_dependency();
68
issue_1691::use_me()
79
}

0 commit comments

Comments
 (0)