Skip to content

Commit a4a3651

Browse files
authored
docs(fingerprint): cargo-rustc extra flags do not affect the metadata (#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
2 parents 9e2c367 + ce74bce commit a4a3651

File tree

2 files changed

+137
-43
lines changed

2 files changed

+137
-43
lines changed

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
//! -------------------------------------------|-------------|----------
6767
//! rustc | ✓ | ✓
6868
//! [`Profile`] | ✓ | ✓
69-
//! `cargo rustc` extra args | ✓ |
69+
//! `cargo rustc` extra args | ✓ |
7070
//! [`CompileMode`] | ✓ | ✓
7171
//! Target Name | ✓ | ✓
7272
//! `TargetKind` (bin/lib/etc.) | ✓ | ✓

tests/testsuite/rustflags.rs

Lines changed: 136 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,48 +1479,6 @@ fn env_rustflags_misspelled_build_script() {
14791479
.run();
14801480
}
14811481

1482-
#[cargo_test]
1483-
fn remap_path_prefix_ignored() {
1484-
let get_c_metadata_re =
1485-
regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?metadata=[^ ]+).*").unwrap();
1486-
let get_c_metadata = |output: RawOutput| {
1487-
let stderr = String::from_utf8(output.stderr).unwrap();
1488-
let mut c_metadata = get_c_metadata_re
1489-
.captures_iter(&stderr)
1490-
.map(|c| {
1491-
let (_, [name, c_metadata]) = c.extract();
1492-
format!("{name} {c_metadata}")
1493-
})
1494-
.collect::<Vec<_>>();
1495-
assert!(
1496-
!c_metadata.is_empty(),
1497-
"`{get_c_metadata_re:?}` did not match:\n```\n{stderr}\n```"
1498-
);
1499-
c_metadata.sort();
1500-
c_metadata.join("\n")
1501-
};
1502-
1503-
let p = project().file("src/lib.rs", "").build();
1504-
1505-
let build_output = p
1506-
.cargo("build -v")
1507-
.env(
1508-
"RUSTFLAGS",
1509-
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
1510-
)
1511-
.run();
1512-
let build_c_metadata = dbg!(get_c_metadata(build_output));
1513-
1514-
p.cargo("clean").run();
1515-
1516-
let rustc_output = p
1517-
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
1518-
.run();
1519-
let rustc_c_metadata = dbg!(get_c_metadata(rustc_output));
1520-
1521-
assert_data_eq!(rustc_c_metadata, build_c_metadata);
1522-
}
1523-
15241482
#[cargo_test]
15251483
fn remap_path_prefix_works() {
15261484
// Check that remap-path-prefix works.
@@ -1562,6 +1520,142 @@ fn remap_path_prefix_works() {
15621520
.run();
15631521
}
15641522

1523+
#[cargo_test]
1524+
fn rustflags_remap_path_prefix_ignored_for_c_metadata() {
1525+
let p = project().file("src/lib.rs", "").build();
1526+
1527+
let build_output = p
1528+
.cargo("build -v")
1529+
.env(
1530+
"RUSTFLAGS",
1531+
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
1532+
)
1533+
.run();
1534+
let first_c_metadata = dbg!(get_c_metadata(build_output));
1535+
1536+
p.cargo("clean").run();
1537+
1538+
let build_output = p
1539+
.cargo("build -v")
1540+
.env(
1541+
"RUSTFLAGS",
1542+
"--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo",
1543+
)
1544+
.run();
1545+
let second_c_metadata = dbg!(get_c_metadata(build_output));
1546+
1547+
assert_data_eq!(first_c_metadata, second_c_metadata);
1548+
}
1549+
1550+
#[cargo_test]
1551+
fn rustc_remap_path_prefix_ignored_for_c_metadata() {
1552+
let p = project().file("src/lib.rs", "").build();
1553+
1554+
let build_output = p
1555+
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
1556+
.run();
1557+
let first_c_metadata = dbg!(get_c_metadata(build_output));
1558+
1559+
p.cargo("clean").run();
1560+
1561+
let build_output = p
1562+
.cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo")
1563+
.run();
1564+
let second_c_metadata = dbg!(get_c_metadata(build_output));
1565+
1566+
assert_data_eq!(first_c_metadata, second_c_metadata);
1567+
}
1568+
1569+
// `--remap-path-prefix` is meant to take two different binaries and make them the same but the
1570+
// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change
1571+
#[cargo_test]
1572+
fn rustflags_remap_path_prefix_ignored_for_c_extra_filename() {
1573+
let p = project().file("src/lib.rs", "").build();
1574+
1575+
let build_output = p
1576+
.cargo("build -v")
1577+
.env(
1578+
"RUSTFLAGS",
1579+
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
1580+
)
1581+
.run();
1582+
let first_c_extra_filename = dbg!(get_c_extra_filename(build_output));
1583+
1584+
p.cargo("clean").run();
1585+
1586+
let build_output = p
1587+
.cargo("build -v")
1588+
.env(
1589+
"RUSTFLAGS",
1590+
"--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo",
1591+
)
1592+
.run();
1593+
let second_c_extra_filename = dbg!(get_c_extra_filename(build_output));
1594+
1595+
assert_data_eq!(first_c_extra_filename, second_c_extra_filename);
1596+
}
1597+
1598+
// `--remap-path-prefix` is meant to take two different binaries and make them the same but the
1599+
// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change
1600+
#[cargo_test]
1601+
fn rustc_remap_path_prefix_ignored_for_c_extra_filename() {
1602+
let p = project().file("src/lib.rs", "").build();
1603+
1604+
let build_output = p
1605+
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
1606+
.run();
1607+
let first_c_extra_filename = dbg!(get_c_extra_filename(build_output));
1608+
1609+
p.cargo("clean").run();
1610+
1611+
let build_output = p
1612+
.cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo")
1613+
.run();
1614+
let second_c_extra_filename = dbg!(get_c_extra_filename(build_output));
1615+
1616+
assert_data_eq!(first_c_extra_filename, second_c_extra_filename);
1617+
}
1618+
1619+
fn get_c_metadata(output: RawOutput) -> String {
1620+
let get_c_metadata_re =
1621+
regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?metadata=[^ ]+).*").unwrap();
1622+
1623+
let stderr = String::from_utf8(output.stderr).unwrap();
1624+
let mut c_metadata = get_c_metadata_re
1625+
.captures_iter(&stderr)
1626+
.map(|c| {
1627+
let (_, [name, c_metadata]) = c.extract();
1628+
format!("{name} {c_metadata}")
1629+
})
1630+
.collect::<Vec<_>>();
1631+
assert!(
1632+
!c_metadata.is_empty(),
1633+
"`{get_c_metadata_re:?}` did not match:\n```\n{stderr}\n```"
1634+
);
1635+
c_metadata.sort();
1636+
c_metadata.join("\n")
1637+
}
1638+
1639+
fn get_c_extra_filename(output: RawOutput) -> String {
1640+
let get_c_extra_filename_re =
1641+
regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?extra-filename=[^ ]+).*").unwrap();
1642+
1643+
let stderr = String::from_utf8(output.stderr).unwrap();
1644+
let mut c_extra_filename = get_c_extra_filename_re
1645+
.captures_iter(&stderr)
1646+
.map(|c| {
1647+
let (_, [name, c_extra_filename]) = c.extract();
1648+
format!("{name} {c_extra_filename}")
1649+
})
1650+
.collect::<Vec<_>>();
1651+
assert!(
1652+
!c_extra_filename.is_empty(),
1653+
"`{get_c_extra_filename_re:?}` did not match:\n```\n{stderr}\n```"
1654+
);
1655+
c_extra_filename.sort();
1656+
c_extra_filename.join("\n")
1657+
}
1658+
15651659
#[cargo_test]
15661660
fn host_config_rustflags_with_target() {
15671661
// regression test for https://github.com/rust-lang/cargo/issues/10206

0 commit comments

Comments
 (0)