Skip to content

Commit 361a488

Browse files
committed
Auto merge of #14107 - alexcrichton:no-hash-rustc-host, r=ehuss
Make `-Cmetadata` consistent across platforms This commit updates how `-Cmetadata` is calculated for each unit to optionally exclude the `host: ...` line that rustc prints for cross-compiled builds. Previously the full `verbose_version` was hashed for stable builds and the `host` was explicitly hashed for non-stable builds. For a build using `--target`, however, that means that the `-Cmetadata` will be different when producing the same target on different hosts (e.g. producing the binary once on Linux and once on macOS). This can hinder reproduction of a binary across different platforms even when the `--target` flag is used. For example in rust-lang/rust#117597 it was seen that a WebAssembly binary produced on different platforms was slightly different and this appears due to the differing `-Cmetadata` flags. After this commit the `-Cmetadata` flag is the same for two different platforms meaning that different platforms produce the same binary. I've tested locally and a simple project produces a different binary before this change but produces the same binary on two platforms after this change. Unfortunately automated testing of this change will be difficult since it requires two different host compilers, though.
2 parents 93ba1e6 + 2db0bab commit 361a488

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ fn compute_metadata(
620620
unit.target.name().hash(&mut hasher);
621621
unit.target.kind().hash(&mut hasher);
622622

623-
hash_rustc_version(bcx, &mut hasher);
623+
hash_rustc_version(bcx, &mut hasher, unit);
624624

625625
if build_runner.bcx.ws.is_member(&unit.pkg) {
626626
// This is primarily here for clippy. This ensures that the clippy
@@ -656,12 +656,19 @@ fn compute_metadata(
656656
}
657657

658658
/// Hash the version of rustc being used during the build process.
659-
fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) {
659+
fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher, unit: &Unit) {
660660
let vers = &bcx.rustc().version;
661661
if vers.pre.is_empty() || bcx.gctx.cli_unstable().separate_nightlies {
662662
// For stable, keep the artifacts separate. This helps if someone is
663-
// testing multiple versions, to avoid recompiles.
664-
bcx.rustc().verbose_version.hash(hasher);
663+
// testing multiple versions, to avoid recompiles. Note though that for
664+
// cross-compiled builds the `host:` line of `verbose_version` is
665+
// omitted since rustc should produce the same output for each target
666+
// regardless of the host.
667+
for line in bcx.rustc().verbose_version.lines() {
668+
if unit.kind.is_host() || !line.starts_with("host: ") {
669+
line.hash(hasher);
670+
}
671+
}
665672
return;
666673
}
667674
// On "nightly"/"beta"/"dev"/etc, keep each "channel" separate. Don't hash
@@ -674,7 +681,9 @@ fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) {
674681
// Keep "host" since some people switch hosts to implicitly change
675682
// targets, (like gnu vs musl or gnu vs msvc). In the future, we may want
676683
// to consider hashing `unit.kind.short_name()` instead.
677-
bcx.rustc().host.hash(hasher);
684+
if unit.kind.is_host() {
685+
bcx.rustc().host.hash(hasher);
686+
}
678687
// None of the other lines are important. Currently they are:
679688
// binary: rustc <-- or "rustdoc"
680689
// commit-hash: 38114ff16e7856f98b2b4be7ab4cd29b38bed59a

0 commit comments

Comments
 (0)