Skip to content

Commit

Permalink
fix: -Cmetadata includes whether extra rustflags is same as host
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weihanglo committed Aug 21, 2024
1 parent 38addd7 commit c877f48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
17 changes: 17 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,23 @@ 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.
// This is essentially a hack for fixing rust-lang/cargo#14253.
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;
target_configs_are_different.hash(&mut hasher);
}

MetaInfo {
meta_hash: Metadata(hasher.finish()),
use_extra_filename: should_use_metadata(bcx, unit),
Expand Down
20 changes: 5 additions & 15 deletions tests/testsuite/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,24 +1691,14 @@ fn host_config_shared_build_dep() {
[LOCKING] 2 packages to latest compatible versions
[DOWNLOADING] crates ...
[DOWNLOADED] cc v1.0.0 (registry `dummy-registry`)
[WARNING] output filename collision.
The lib target `cc` in package `cc v1.0.0` has the same output filename as the lib target `cc` in package `cc v1.0.0`.
Colliding filename is: [ROOT]/foo/target/debug/deps/libcc-[HASH].rlib
The targets should have unique names.
Consider changing their names to be unique or compiling them separately.
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
[WARNING] output filename collision.
The lib target `cc` in package `cc v1.0.0` has the same output filename as the lib target `cc` in package `cc v1.0.0`.
Colliding filename is: [ROOT]/foo/target/debug/deps/libcc-[HASH].rmeta
The targets should have unique names.
Consider changing their names to be unique or compiling them separately.
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
[COMPILING] cc v1.0.0
[RUNNING] `rustc [..]--cfg from_host[..]`
[RUNNING] `rustc [..]--cfg from_target[..]`
[ERROR] failed to build archive: No such file or directory │
[ERROR] could not compile `cc` (lib) due to 1 previous error
[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
"#]])
.run();
Expand Down

0 comments on commit c877f48

Please sign in to comment.