Skip to content

Commit f90d21f

Browse files
committed
Make dep_targets consistent throughout compilation
Previously it depended on dynamic state that was calculated throughout a compilation which ended up causing different fingerprints showing up in a few locations, so this makes the invocation deterministic throughout `cargo_rustc`.
1 parent 67b3b44 commit f90d21f

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct Context<'a, 'cfg: 'a> {
4040
pub compilation: Compilation<'cfg>,
4141
pub packages: &'a PackageSet<'cfg>,
4242
pub build_state: Arc<BuildState>,
43+
pub build_script_overridden: HashSet<(PackageId, Kind)>,
4344
pub build_explicit_deps: HashMap<Unit<'a>, BuildDeps>,
4445
pub fingerprints: HashMap<Unit<'a>, Arc<Fingerprint>>,
4546
pub compiled: HashSet<Unit<'a>>,
@@ -156,6 +157,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
156157
used_in_plugin: HashSet::new(),
157158
incremental_enabled: incremental_enabled,
158159
jobserver: jobserver,
160+
build_script_overridden: HashSet::new(),
159161

160162
// TODO: Pre-Calculate these with a topo-sort, rather than lazy-calculating
161163
target_filenames: HashMap::new(),
@@ -499,7 +501,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
499501
self.resolve.features_sorted(unit.pkg.package_id()).hash(&mut hasher);
500502

501503
// Mix in the target-metadata of all the dependencies of this target
502-
if let Ok(deps) = self.used_deps(unit) {
504+
if let Ok(deps) = self.dep_targets(unit) {
503505
let mut deps_metadata = deps.into_iter().map(|dep_unit| {
504506
self.target_metadata(&dep_unit)
505507
}).collect::<Vec<_>>();
@@ -695,10 +697,18 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
695697
Ok(Arc::new(ret))
696698
}
697699

698-
fn used_deps(&self, unit: &Unit<'a>) -> CargoResult<Vec<Unit<'a>>> {
700+
/// For a package, return all targets which are registered as dependencies
701+
/// for that package.
702+
pub fn dep_targets(&self, unit: &Unit<'a>) -> CargoResult<Vec<Unit<'a>>> {
703+
if unit.profile.run_custom_build {
704+
return self.dep_run_custom_build(unit)
705+
} else if unit.profile.doc && !unit.profile.test {
706+
return self.doc_deps(unit);
707+
}
708+
699709
let id = unit.pkg.package_id();
700710
let deps = self.resolve.deps(id);
701-
deps.filter(|dep| {
711+
let mut ret = deps.filter(|dep| {
702712
unit.pkg.dependencies().iter().filter(|d| {
703713
d.name() == dep.name() && d.version_req().matches(dep.version())
704714
}).any(|d| {
@@ -747,20 +757,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
747757
}
748758
Err(e) => Some(Err(e))
749759
}
750-
}).collect::<CargoResult<Vec<_>>>()
751-
}
752-
753-
/// For a package, return all targets which are registered as dependencies
754-
/// for that package.
755-
pub fn dep_targets(&self, unit: &Unit<'a>) -> CargoResult<Vec<Unit<'a>>> {
756-
if unit.profile.run_custom_build {
757-
return self.dep_run_custom_build(unit)
758-
} else if unit.profile.doc && !unit.profile.test {
759-
return self.doc_deps(unit);
760-
}
761-
762-
let id = unit.pkg.package_id();
763-
let mut ret = self.used_deps(unit)?;
760+
}).collect::<CargoResult<Vec<_>>>()?;
764761

765762
// If this target is a build script, then what we've collected so far is
766763
// all we need. If this isn't a build script, then it depends on the
@@ -812,7 +809,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
812809
// actually depend on anything, we've reached the end of the dependency
813810
// chain as we've got all the info we're gonna get.
814811
let key = (unit.pkg.package_id().clone(), unit.kind);
815-
if self.build_state.outputs.lock().unwrap().contains_key(&key) {
812+
if self.build_script_overridden.contains(&key) {
816813
return Ok(Vec::new())
817814
}
818815

src/cargo/ops/cargo_rustc/custom_build.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ pub fn prepare<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>)
7777
-> CargoResult<(Work, Work, Freshness)> {
7878
let _p = profile::start(format!("build script prepare: {}/{}",
7979
unit.pkg, unit.target.name()));
80-
let overridden = cx.build_state.has_override(unit);
80+
81+
let key = (unit.pkg.package_id().clone(), unit.kind);
82+
let overridden = cx.build_script_overridden.contains(&key);
8183
let (work_dirty, work_fresh) = if overridden {
8284
(Work::noop(), Work::noop())
8385
} else {
@@ -314,18 +316,6 @@ impl BuildState {
314316
fn insert(&self, id: PackageId, kind: Kind, output: BuildOutput) {
315317
self.outputs.lock().unwrap().insert((id, kind), output);
316318
}
317-
318-
fn has_override(&self, unit: &Unit) -> bool {
319-
let key = unit.pkg.manifest().links().map(|l| (l.to_string(), unit.kind));
320-
match key.and_then(|k| self.overrides.get(&k)) {
321-
Some(output) => {
322-
self.insert(unit.pkg.package_id().clone(), unit.kind,
323-
output.clone());
324-
true
325-
}
326-
None => false,
327-
}
328-
}
329319
}
330320

331321
impl BuildOutput {
@@ -483,7 +473,7 @@ pub fn build_map<'b, 'cfg>(cx: &mut Context<'b, 'cfg>,
483473
// Recursive function to build up the map we're constructing. This function
484474
// memoizes all of its return values as it goes along.
485475
fn build<'a, 'b, 'cfg>(out: &'a mut HashMap<Unit<'b>, BuildScripts>,
486-
cx: &Context<'b, 'cfg>,
476+
cx: &mut Context<'b, 'cfg>,
487477
unit: &Unit<'b>)
488478
-> CargoResult<&'a BuildScripts> {
489479
// Do a quick pre-flight check to see if we've already calculated the
@@ -492,6 +482,20 @@ pub fn build_map<'b, 'cfg>(cx: &mut Context<'b, 'cfg>,
492482
return Ok(&out[unit])
493483
}
494484

485+
{
486+
let key = unit.pkg.manifest().links().map(|l| (l.to_string(), unit.kind));
487+
let build_state = &cx.build_state;
488+
if let Some(output) = key.and_then(|k| build_state.overrides.get(&k)) {
489+
let key = (unit.pkg.package_id().clone(), unit.kind);
490+
cx.build_script_overridden.insert(key.clone());
491+
build_state
492+
.outputs
493+
.lock()
494+
.unwrap()
495+
.insert(key, output.clone());
496+
}
497+
}
498+
495499
let mut ret = BuildScripts::default();
496500

497501
if !unit.target.is_custom_build() && unit.pkg.has_custom_build() {

0 commit comments

Comments
 (0)