Skip to content

Commit 7d7b932

Browse files
committed
Auto merge of #14432 - weihanglo:target-applies-to-host, r=epage
fix: -Cmetadata includes whether extra rustflags is same as host ### What does this PR try to resolve? Fixes #14253 The root cause is described in <#14253 (comment)>: > `-Ztarget-applies-to-host --config target-applies-to-host=false` make it possible to build two units for the same output directory with different rustflags (one with artifact/target rustflags, one with none/host rust flags). Those flags aren't included in the `-Cmetadata` hash which is used to disambiguate different versions of crates, resulting in a conflict. The actual error being produced appears to be the result of two invocations of `rustc` racing and clobbering each other… While we don't hash RUSTFLAGS because it may contain absolute paths that hurts reproducibility, we track whether a unit's RUSTFLAGS is from host config, so that we can generate a different metadata hash for runtime and compile-time units. ### How should we test and review this PR? This hack is only enabled when `target-applies-to-host=false` and when compile kind is the host platform, so the shouldn't affect any stable behavior.
2 parents 9e152bb + d12c716 commit 7d7b932

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/cargo/core/compiler/build_runner/compilation_files.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,26 @@ fn compute_metadata(
649649
// with user dependencies.
650650
unit.is_std.hash(&mut hasher);
651651

652+
// While we don't hash RUSTFLAGS because it may contain absolute paths that
653+
// hurts reproducibility, we track whether a unit's RUSTFLAGS is from host
654+
// config, so that we can generate a different metadata hash for runtime
655+
// and compile-time units.
656+
//
657+
// HACK: This is a temporary hack for fixing rust-lang/cargo#14253
658+
// Need to find a long-term solution to replace this fragile workaround.
659+
// See https://github.com/rust-lang/cargo/pull/14432#discussion_r1725065350
660+
if unit.kind.is_host() && !bcx.gctx.target_applies_to_host().unwrap_or_default() {
661+
let host_info = bcx.target_data.info(CompileKind::Host);
662+
let target_configs_are_different = unit.rustflags != host_info.rustflags
663+
|| unit.rustdocflags != host_info.rustdocflags
664+
|| bcx
665+
.target_data
666+
.target_config(CompileKind::Host)
667+
.links_overrides
668+
!= unit.links_overrides;
669+
target_configs_are_different.hash(&mut hasher);
670+
}
671+
652672
MetaInfo {
653673
meta_hash: Metadata(hasher.finish()),
654674
use_extra_filename: should_use_metadata(bcx, unit),

tests/testsuite/rustflags.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,3 +1642,66 @@ fn target_applies_to_host_rustdocflags_works() {
16421642
)
16431643
.run();
16441644
}
1645+
1646+
#[cargo_test]
1647+
fn host_config_shared_build_dep() {
1648+
// rust-lang/cargo#14253
1649+
Package::new("cc", "1.0.0").publish();
1650+
let p = project()
1651+
.file(
1652+
"Cargo.toml",
1653+
r#"
1654+
[package]
1655+
name = "bootstrap"
1656+
edition = "2021"
1657+
1658+
[dependencies]
1659+
cc = "1.0.0"
1660+
1661+
[build-dependencies]
1662+
cc = "1.0.0"
1663+
1664+
[profile.dev]
1665+
debug = 0
1666+
"#,
1667+
)
1668+
.file("src/lib.rs", "")
1669+
.file("build.rs", "fn main() {}")
1670+
.file(
1671+
".cargo/config.toml",
1672+
"
1673+
target-applies-to-host=false
1674+
1675+
[host]
1676+
rustflags = ['--cfg', 'from_host']
1677+
1678+
[build]
1679+
rustflags = ['--cfg', 'from_target']
1680+
",
1681+
)
1682+
.build();
1683+
1684+
p.cargo("build -v")
1685+
.masquerade_as_nightly_cargo(&["target-applies-to-host"])
1686+
.arg("-Ztarget-applies-to-host")
1687+
.arg("-Zhost-config")
1688+
.with_stderr_data(
1689+
str![[r#"
1690+
[UPDATING] `dummy-registry` index
1691+
[LOCKING] 2 packages to latest compatible versions
1692+
[DOWNLOADING] crates ...
1693+
[DOWNLOADED] cc v1.0.0 (registry `dummy-registry`)
1694+
[COMPILING] cc v1.0.0
1695+
[RUNNING] `rustc --crate-name cc [..]--cfg from_host[..]`
1696+
[RUNNING] `rustc --crate-name cc [..]--cfg from_target[..]`
1697+
[COMPILING] bootstrap v0.0.0 ([ROOT]/foo)
1698+
[RUNNING] `rustc --crate-name build_script_build [..]--cfg from_host[..]`
1699+
[RUNNING] `[ROOT]/foo/target/debug/build/bootstrap-[HASH]/build-script-build`
1700+
[RUNNING] `rustc --crate-name bootstrap[..]--cfg from_target[..]`
1701+
[FINISHED] `dev` profile [unoptimized] target(s) in [ELAPSED]s
1702+
1703+
"#]]
1704+
.unordered(),
1705+
)
1706+
.run();
1707+
}

0 commit comments

Comments
 (0)