Skip to content

Commit

Permalink
docs(fingerprint): cargo-rustc extra flags do not affect the metadata (
Browse files Browse the repository at this point in the history
…#14898)

### What does this PR try to resolve?

As the test updates in this PR show, RUSTFLAGS and extra-flags have the
same
behavior: they don't affect `-Cextra-filename` or `-Cmetadata`.
I also verified this by code inspection.
I'm not sure why the table says this.

### How should we test and review this PR?

### Additional information
  • Loading branch information
weihanglo authored Dec 5, 2024
2 parents 9e2c367 + ce74bce commit a4a3651
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
//! -------------------------------------------|-------------|----------
//! rustc | ✓ | ✓
//! [`Profile`] | ✓ | ✓
//! `cargo rustc` extra args | ✓ |
//! `cargo rustc` extra args | ✓ |
//! [`CompileMode`] | ✓ | ✓
//! Target Name | ✓ | ✓
//! `TargetKind` (bin/lib/etc.) | ✓ | ✓
Expand Down
178 changes: 136 additions & 42 deletions tests/testsuite/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,48 +1479,6 @@ fn env_rustflags_misspelled_build_script() {
.run();
}

#[cargo_test]
fn remap_path_prefix_ignored() {
let get_c_metadata_re =
regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?metadata=[^ ]+).*").unwrap();
let get_c_metadata = |output: RawOutput| {
let stderr = String::from_utf8(output.stderr).unwrap();
let mut c_metadata = get_c_metadata_re
.captures_iter(&stderr)
.map(|c| {
let (_, [name, c_metadata]) = c.extract();
format!("{name} {c_metadata}")
})
.collect::<Vec<_>>();
assert!(
!c_metadata.is_empty(),
"`{get_c_metadata_re:?}` did not match:\n```\n{stderr}\n```"
);
c_metadata.sort();
c_metadata.join("\n")
};

let p = project().file("src/lib.rs", "").build();

let build_output = p
.cargo("build -v")
.env(
"RUSTFLAGS",
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
)
.run();
let build_c_metadata = dbg!(get_c_metadata(build_output));

p.cargo("clean").run();

let rustc_output = p
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
.run();
let rustc_c_metadata = dbg!(get_c_metadata(rustc_output));

assert_data_eq!(rustc_c_metadata, build_c_metadata);
}

#[cargo_test]
fn remap_path_prefix_works() {
// Check that remap-path-prefix works.
Expand Down Expand Up @@ -1562,6 +1520,142 @@ fn remap_path_prefix_works() {
.run();
}

#[cargo_test]
fn rustflags_remap_path_prefix_ignored_for_c_metadata() {
let p = project().file("src/lib.rs", "").build();

let build_output = p
.cargo("build -v")
.env(
"RUSTFLAGS",
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
)
.run();
let first_c_metadata = dbg!(get_c_metadata(build_output));

p.cargo("clean").run();

let build_output = p
.cargo("build -v")
.env(
"RUSTFLAGS",
"--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo",
)
.run();
let second_c_metadata = dbg!(get_c_metadata(build_output));

assert_data_eq!(first_c_metadata, second_c_metadata);
}

#[cargo_test]
fn rustc_remap_path_prefix_ignored_for_c_metadata() {
let p = project().file("src/lib.rs", "").build();

let build_output = p
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
.run();
let first_c_metadata = dbg!(get_c_metadata(build_output));

p.cargo("clean").run();

let build_output = p
.cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo")
.run();
let second_c_metadata = dbg!(get_c_metadata(build_output));

assert_data_eq!(first_c_metadata, second_c_metadata);
}

// `--remap-path-prefix` is meant to take two different binaries and make them the same but the
// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change
#[cargo_test]
fn rustflags_remap_path_prefix_ignored_for_c_extra_filename() {
let p = project().file("src/lib.rs", "").build();

let build_output = p
.cargo("build -v")
.env(
"RUSTFLAGS",
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
)
.run();
let first_c_extra_filename = dbg!(get_c_extra_filename(build_output));

p.cargo("clean").run();

let build_output = p
.cargo("build -v")
.env(
"RUSTFLAGS",
"--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo",
)
.run();
let second_c_extra_filename = dbg!(get_c_extra_filename(build_output));

assert_data_eq!(first_c_extra_filename, second_c_extra_filename);
}

// `--remap-path-prefix` is meant to take two different binaries and make them the same but the
// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change
#[cargo_test]
fn rustc_remap_path_prefix_ignored_for_c_extra_filename() {
let p = project().file("src/lib.rs", "").build();

let build_output = p
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
.run();
let first_c_extra_filename = dbg!(get_c_extra_filename(build_output));

p.cargo("clean").run();

let build_output = p
.cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo")
.run();
let second_c_extra_filename = dbg!(get_c_extra_filename(build_output));

assert_data_eq!(first_c_extra_filename, second_c_extra_filename);
}

fn get_c_metadata(output: RawOutput) -> String {
let get_c_metadata_re =
regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?metadata=[^ ]+).*").unwrap();

let stderr = String::from_utf8(output.stderr).unwrap();
let mut c_metadata = get_c_metadata_re
.captures_iter(&stderr)
.map(|c| {
let (_, [name, c_metadata]) = c.extract();
format!("{name} {c_metadata}")
})
.collect::<Vec<_>>();
assert!(
!c_metadata.is_empty(),
"`{get_c_metadata_re:?}` did not match:\n```\n{stderr}\n```"
);
c_metadata.sort();
c_metadata.join("\n")
}

fn get_c_extra_filename(output: RawOutput) -> String {
let get_c_extra_filename_re =
regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?extra-filename=[^ ]+).*").unwrap();

let stderr = String::from_utf8(output.stderr).unwrap();
let mut c_extra_filename = get_c_extra_filename_re
.captures_iter(&stderr)
.map(|c| {
let (_, [name, c_extra_filename]) = c.extract();
format!("{name} {c_extra_filename}")
})
.collect::<Vec<_>>();
assert!(
!c_extra_filename.is_empty(),
"`{get_c_extra_filename_re:?}` did not match:\n```\n{stderr}\n```"
);
c_extra_filename.sort();
c_extra_filename.join("\n")
}

#[cargo_test]
fn host_config_rustflags_with_target() {
// regression test for https://github.com/rust-lang/cargo/issues/10206
Expand Down

0 comments on commit a4a3651

Please sign in to comment.