Skip to content

Commit

Permalink
Auto merge of #1710 - hyd-dev:crate-types, r=RalfJung
Browse files Browse the repository at this point in the history
 Patch `--extern` arguments in `phase_cargo_rustc` as well

Fixes #1705.
  • Loading branch information
bors committed Feb 15, 2021
2 parents c3f7069 + 1923044 commit b5b5df5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 23 deletions.
43 changes: 20 additions & 23 deletions cargo-miri/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ fn get_arg_flag_value(name: &str) -> Option<String> {
ArgFlagValueIter::new(name).next()
}

fn forward_patched_extern_arg(args: &mut impl Iterator<Item = String>, cmd: &mut Command) {
cmd.arg("--extern"); // always forward flag, but adjust filename:
let path = args.next().expect("`--extern` should be followed by a filename");
if let Some(lib) = path.strip_suffix(".rlib") {
// If this is an rlib, make it an rmeta.
cmd.arg(format!("{}.rmeta", lib));
} else {
// Some other extern file (e.g. a `.so`). Forward unchanged.
cmd.arg(path);
}
}

/// Returns the path to the `miri` binary
fn find_miri() -> PathBuf {
if let Some(path) = env::var_os("MIRI") {
Expand Down Expand Up @@ -553,7 +565,7 @@ fn phase_cargo_miri(mut args: env::Args) {
exec(cmd)
}

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

// cdylib is just skipped, we cannot interpret it and do not need it
// for the rest of the build either.
if get_arg_flag_value("--crate-type").as_deref() == Some("cdylib") {
if verbose {
eprint!("[cargo-miri rustc] (cdylib skipped)");
}
return;
}

let store_json = |info: CrateRunInfo| {
let filename = out_filename("", "");
if verbose {
Expand Down Expand Up @@ -643,7 +646,7 @@ fn phase_cargo_rustc(args: env::Args) {
if !print && target_crate {
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
let emit_flag = "--emit";
for arg in args {
while let Some(arg) = args.next() {
if arg.starts_with(emit_flag) {
// Patch this argument. First, extract its value.
let val = &arg[emit_flag.len()..];
Expand All @@ -659,6 +662,10 @@ fn phase_cargo_rustc(args: env::Args) {
}
}
cmd.arg(format!("{}={}", emit_flag, val.join(",")));
} else if arg == "--extern" {
// Patch `--extern` filenames, since Cargo sometimes passes stub `.rlib` files:
// https://github.com/rust-lang/miri/issues/1705
forward_patched_extern_arg(&mut args, &mut cmd);
} else {
cmd.arg(arg);
}
Expand Down Expand Up @@ -734,21 +741,11 @@ fn phase_cargo_runner(binary: &Path, binary_args: env::Args) {
// but when we run here, cargo does not interpret the JSON any more. `--json`
// then also nees to be dropped.
let mut args = info.args.into_iter();
let extern_flag = "--extern";
let error_format_flag = "--error-format";
let json_flag = "--json";
while let Some(arg) = args.next() {
if arg == extern_flag {
cmd.arg(extern_flag); // always forward flag, but adjust filename
// `--extern` is always passed as a separate argument by cargo.
let next_arg = args.next().expect("`--extern` should be followed by a filename");
if let Some(next_lib) = next_arg.strip_suffix(".rlib") {
// If this is an rlib, make it an rmeta.
cmd.arg(format!("{}.rmeta", next_lib));
} else {
// Some other extern file (e.g., a `.so`). Forward unchanged.
cmd.arg(next_arg);
}
if arg == "--extern" {
forward_patched_extern_arg(&mut args, &mut cmd);
} else if arg.starts_with(error_format_flag) {
let suffix = &arg[error_format_flag.len()..];
assert!(suffix.starts_with('='));
Expand Down
16 changes: 16 additions & 0 deletions test-cargo-miri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test-cargo-miri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ edition = "2018"

[dependencies]
byteorder = "1.0"
cdylib = { path = "cdylib" }
issue_1567 = { path = "issue-1567" }
issue_1691 = { path = "issue-1691" }
issue_1705 = { path = "issue-1705" }

[dev-dependencies]
rand = { version = "0.7", features = ["small_rng"] }
Expand Down
12 changes: 12 additions & 0 deletions test-cargo-miri/cdylib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "cdylib"
version = "0.1.0"
authors = ["Miri Team"]
edition = "2018"

[lib]
# cargo-miri used to handle `cdylib` crate-type specially (https://github.com/rust-lang/miri/pull/1577).
crate-type = ["cdylib"]

[dependencies]
byteorder = "1.0"
6 changes: 6 additions & 0 deletions test-cargo-miri/cdylib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use byteorder::{BigEndian, ByteOrder};

#[no_mangle]
extern "C" fn use_the_dependency() {
let _n = <BigEndian as ByteOrder>::read_u64(&[1,2,3,4,5,6,7,8]);
}
11 changes: 11 additions & 0 deletions test-cargo-miri/issue-1705/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "issue_1705"
version = "0.1.0"
authors = ["Miri Team"]
edition = "2018"

[lib]
crate-type = ["lib", "staticlib", "cdylib"]

[dependencies]
byteorder = "1.0"
5 changes: 5 additions & 0 deletions test-cargo-miri/issue-1705/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use byteorder::{LittleEndian, ByteOrder};

pub fn use_the_dependency() {
let _n = <LittleEndian as ByteOrder>::read_u32(&[1,2,3,4]);
}
2 changes: 2 additions & 0 deletions test-cargo-miri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
/// assert!(cargo_miri_test::make_true());
/// ```
pub fn make_true() -> bool {
issue_1567::use_the_dependency();
issue_1705::use_the_dependency();
issue_1691::use_me()
}

0 comments on commit b5b5df5

Please sign in to comment.