Skip to content

Commit

Permalink
refactor(fingerprint): Generate separate c_metadata / c_extra_filenam…
Browse files Browse the repository at this point in the history
…e hashes

For now, they should result in the same data.
  • Loading branch information
epage committed Nov 21, 2024
1 parent d2f1423 commit b4de315
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/cargo/core/compiler/build_runner/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,25 @@ impl fmt::Debug for UnitHash {
/// rebuild is needed.
#[derive(Copy, Clone, Debug)]
pub struct Metadata {
meta_hash: UnitHash,
use_extra_filename: bool,
unit_id: UnitHash,
c_metadata: UnitHash,
c_extra_filename: Option<UnitHash>,
}

impl Metadata {
/// A hash to identify a given [`Unit`] in the build graph
pub fn unit_id(&self) -> UnitHash {
self.meta_hash
self.unit_id
}

/// A hash to add to symbol naming through `-C metadata`
pub fn c_metadata(&self) -> UnitHash {
self.meta_hash
self.c_metadata
}

/// A hash to add to file names through `-C extra-filename`
pub fn c_extra_filename(&self) -> Option<UnitHash> {
self.use_extra_filename.then_some(self.meta_hash)
self.c_extra_filename
}
}

Expand Down Expand Up @@ -678,19 +679,34 @@ fn compute_metadata(
target_configs_are_different.hash(&mut shared_hasher);
}

let mut c_metadata_hasher = shared_hasher.clone();
// Mix in the target-metadata of all the dependencies of this target.
let mut dep_hashes = deps_metadata
let mut dep_c_metadata_hashes = deps_metadata
.iter()
.map(|m| m.meta_hash)
.map(|m| m.c_metadata)
.collect::<Vec<_>>();
dep_hashes.sort();
dep_hashes.hash(&mut shared_hasher);
dep_c_metadata_hashes.sort();
dep_c_metadata_hashes.hash(&mut c_metadata_hasher);

let meta_hash = UnitHash(shared_hasher.finish());
let mut c_extra_filename_hasher = shared_hasher.clone();
// Mix in the target-metadata of all the dependencies of this target.
let mut dep_c_extra_filename_hashes = deps_metadata
.iter()
.map(|m| m.c_extra_filename)
.collect::<Vec<_>>();
dep_c_extra_filename_hashes.sort();
dep_c_extra_filename_hashes.hash(&mut c_extra_filename_hasher);

let c_metadata = UnitHash(c_metadata_hasher.finish());
let c_extra_filename = UnitHash(c_extra_filename_hasher.finish());
let unit_id = c_extra_filename;

let c_extra_filename = use_extra_filename.then_some(c_extra_filename);

Metadata {
meta_hash,
use_extra_filename,
unit_id,
c_metadata,
c_extra_filename,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/cargo/util/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use std::hash::{Hasher, SipHasher};

#[derive(Clone)]
pub struct StableHasher(SipHasher);

impl StableHasher {
Expand Down

0 comments on commit b4de315

Please sign in to comment.