Skip to content

Commit 1f4eeb4

Browse files
committed
refactor(fingerprint): Track the intent for each use of UnitHash
We have - `-C metadata` - `-C extra-filename` - Uniquifying build scripts - Uniquifying scraped documentation The last two I'm tracking as a `unit_id`. As we evolve the first two hashes, we can decide which would be a better fit for backing this. For scraping, we could treat this as `-C extra-filename` but then we'd have a lot of `.expect()`s.
1 parent 3f79db7 commit 1f4eeb4

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,19 @@ pub struct Metadata {
9191
}
9292

9393
impl Metadata {
94-
/// The symbol hash to use.
95-
pub fn meta_hash(&self) -> UnitHash {
94+
/// A hash to identify a given [`Unit`] in the build graph
95+
pub fn unit_id(&self) -> UnitHash {
9696
self.meta_hash
9797
}
9898

99-
/// Whether or not the `-C extra-filename` flag is used to generate unique
100-
/// output filenames for this `Unit`.
101-
///
102-
/// If this is `true`, the `meta_hash` is used for the filename.
103-
pub fn use_extra_filename(&self) -> bool {
104-
self.use_extra_filename
99+
/// A hash to add to symbol naming through `-C metadata`
100+
pub fn c_metadata(&self) -> UnitHash {
101+
self.meta_hash
102+
}
103+
104+
/// A hash to add to file names through `-C extra-filename`
105+
pub fn c_extra_filename(&self) -> Option<UnitHash> {
106+
self.use_extra_filename.then_some(self.meta_hash)
105107
}
106108
}
107109

@@ -230,8 +232,8 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
230232
fn pkg_dir(&self, unit: &Unit) -> String {
231233
let name = unit.pkg.package_id().name();
232234
let meta = self.metas[unit];
233-
if meta.use_extra_filename() {
234-
format!("{}-{}", name, meta.meta_hash())
235+
if let Some(c_extra_filename) = meta.c_extra_filename() {
236+
format!("{}-{}", name, c_extra_filename)
235237
} else {
236238
format!("{}-{}", name, self.target_short_hash(unit))
237239
}
@@ -475,7 +477,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
475477
let file_name = format!(
476478
"{}-{}.examples",
477479
unit.pkg.name(),
478-
self.metadata(unit).meta_hash()
480+
self.metadata(unit).unit_id()
479481
);
480482
let path = self.deps_dir(unit).join(file_name);
481483
vec![OutputFile {
@@ -533,9 +535,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
533535
let mut outputs = Vec::new();
534536
for file_type in file_types {
535537
let meta = self.metas[unit];
536-
let meta_opt = meta
537-
.use_extra_filename()
538-
.then(|| meta.meta_hash().to_string());
538+
let meta_opt = meta.c_extra_filename().map(|h| h.to_string());
539539
let path = out_dir.join(file_type.output_filename(&unit.target, meta_opt.as_deref()));
540540

541541
// If, the `different_binary_name` feature is enabled, the name of the hardlink will

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
443443
/// Returns the metadata hash for a `RunCustomBuild` unit.
444444
pub fn get_run_build_script_metadata(&self, unit: &Unit) -> UnitHash {
445445
assert!(unit.mode.is_run_custom_build());
446-
self.files().metadata(unit).meta_hash()
446+
self.files().metadata(unit).unit_id()
447447
}
448448

449449
pub fn is_primary_package(&self, unit: &Unit) -> bool {

src/cargo/core/compiler/job_queue/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ impl<'gctx> DrainState<'gctx> {
685685
.failed_scrape_units
686686
.lock()
687687
.unwrap()
688-
.insert(build_runner.files().metadata(&unit).meta_hash());
688+
.insert(build_runner.files().metadata(&unit).unit_id());
689689
self.queue.finish(&unit, &artifact);
690690
}
691691
Err(error) => {

src/cargo/core/compiler/mod.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,12 @@ fn rustc(
279279
// don't pass the `-l` flags.
280280
let pass_l_flag = unit.target.is_lib() || !unit.pkg.targets().iter().any(|t| t.is_lib());
281281

282-
let dep_info_name = if build_runner.files().metadata(unit).use_extra_filename() {
283-
format!(
284-
"{}-{}.d",
285-
unit.target.crate_name(),
286-
build_runner.files().metadata(unit).meta_hash()
287-
)
288-
} else {
289-
format!("{}.d", unit.target.crate_name())
290-
};
282+
let dep_info_name =
283+
if let Some(c_extra_filename) = build_runner.files().metadata(unit).c_extra_filename() {
284+
format!("{}-{}.d", unit.target.crate_name(), c_extra_filename)
285+
} else {
286+
format!("{}.d", unit.target.crate_name())
287+
};
291288
let rustc_dep_info_loc = root.join(dep_info_name);
292289
let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
293290

@@ -766,7 +763,7 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
766763
let metadata = build_runner.metadata_for_doc_units[unit];
767764
rustdoc
768765
.arg("-C")
769-
.arg(format!("metadata={}", metadata.meta_hash()));
766+
.arg(format!("metadata={}", metadata.c_metadata()));
770767

771768
if unit.mode.is_doc_scrape() {
772769
debug_assert!(build_runner.bcx.scrape_units.contains(unit));
@@ -844,7 +841,7 @@ fn rustdoc(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<W
844841
.iter()
845842
.map(|unit| {
846843
Ok((
847-
build_runner.files().metadata(unit).meta_hash(),
844+
build_runner.files().metadata(unit).unit_id(),
848845
scrape_output_path(build_runner, unit)?,
849846
))
850847
})
@@ -1158,10 +1155,11 @@ fn build_base_args(
11581155
cmd.args(&check_cfg_args(unit));
11591156

11601157
let meta = build_runner.files().metadata(unit);
1161-
cmd.arg("-C").arg(&format!("metadata={}", meta.meta_hash()));
1162-
if meta.use_extra_filename() {
1158+
cmd.arg("-C")
1159+
.arg(&format!("metadata={}", meta.c_metadata()));
1160+
if let Some(c_extra_filename) = meta.c_extra_filename() {
11631161
cmd.arg("-C")
1164-
.arg(&format!("extra-filename=-{}", meta.meta_hash()));
1162+
.arg(&format!("extra-filename=-{c_extra_filename}"));
11651163
}
11661164

11671165
if rpath {

0 commit comments

Comments
 (0)