Skip to content

fix: -Cmetadata includes whether extra rustflags is same as host #14432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/cargo/core/compiler/build_runner/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,26 @@ fn compute_metadata(
// with user dependencies.
unit.is_std.hash(&mut hasher);

// 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.
//
// HACK: This is a temporary hack for fixing rust-lang/cargo#14253
// Need to find a long-term solution to replace this fragile workaround.
// See https://github.com/rust-lang/cargo/pull/14432#discussion_r1725065350
if unit.kind.is_host() && !bcx.gctx.target_applies_to_host().unwrap_or_default() {
let host_info = bcx.target_data.info(CompileKind::Host);
let target_configs_are_different = unit.rustflags != host_info.rustflags
|| unit.rustdocflags != host_info.rustdocflags
|| bcx
.target_data
.target_config(CompileKind::Host)
.links_overrides
!= unit.links_overrides;
Comment on lines +662 to +668
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit brittle. If we don't remember to update this with anything else that is different but not in metadata already, we'll see this problem again.

The only thoughts I had are

  • Putting this question in the unit or host info to answer
  • Hashing unit.kind.is_host() && !bcx.gctx.target_applies_to_host().unwrap_or_default()

Note: answering this is more important for long term reliability and stabilization. This doesn't need to block fixing people who are using the unstable feature

target_configs_are_different.hash(&mut hasher);
}

MetaInfo {
meta_hash: Metadata(hasher.finish()),
use_extra_filename: should_use_metadata(bcx, unit),
Expand Down
63 changes: 63 additions & 0 deletions tests/testsuite/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,3 +1642,66 @@ fn target_applies_to_host_rustdocflags_works() {
)
.run();
}

#[cargo_test]
fn host_config_shared_build_dep() {
// rust-lang/cargo#14253
Package::new("cc", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "bootstrap"
edition = "2021"

[dependencies]
cc = "1.0.0"

[build-dependencies]
cc = "1.0.0"

[profile.dev]
debug = 0
"#,
)
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.file(
".cargo/config.toml",
"
target-applies-to-host=false

[host]
rustflags = ['--cfg', 'from_host']

[build]
rustflags = ['--cfg', 'from_target']
",
)
.build();

p.cargo("build -v")
.masquerade_as_nightly_cargo(&["target-applies-to-host"])
.arg("-Ztarget-applies-to-host")
.arg("-Zhost-config")
.with_stderr_data(
str![[r#"
[UPDATING] `dummy-registry` index
[LOCKING] 2 packages to latest compatible versions
[DOWNLOADING] crates ...
[DOWNLOADED] cc v1.0.0 (registry `dummy-registry`)
[COMPILING] cc v1.0.0
[RUNNING] `rustc --crate-name cc [..]--cfg from_host[..]`
[RUNNING] `rustc --crate-name cc [..]--cfg from_target[..]`
[COMPILING] bootstrap v0.0.0 ([ROOT]/foo)
[RUNNING] `rustc --crate-name build_script_build [..]--cfg from_host[..]`
[RUNNING] `[ROOT]/foo/target/debug/build/bootstrap-[HASH]/build-script-build`
[RUNNING] `rustc --crate-name bootstrap[..]--cfg from_target[..]`
[FINISHED] `dev` profile [unoptimized] target(s) in [ELAPSED]s

"#]]
.unordered(),
)
.run();
}