Skip to content

Commit 22691b9

Browse files
committed
Move Fingerprint::local into a Mutex
Removes the need for a wonky `with_local` method!
1 parent 49f6384 commit 22691b9

File tree

1 file changed

+23
-50
lines changed

1 file changed

+23
-50
lines changed

src/cargo/core/compiler/fingerprint.rs

+23-50
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ pub fn prepare_target<'a, 'cfg>(
275275
return Ok(Job::new(Work::noop(), Fresh));
276276
}
277277

278-
let pkg_root = unit.pkg.root().to_path_buf();
279-
let target_root = target_root(cx, unit);
280278
let write_fingerprint = if unit.mode.is_run_custom_build() {
281279
// For build scripts the `local` field of the fingerprint may change
282280
// while we're executing it. For example it could be in the legacy
@@ -295,30 +293,20 @@ pub fn prepare_target<'a, 'cfg>(
295293
let outputs = state.outputs.lock().unwrap();
296294
let outputs = &outputs[&key];
297295
let deps = BuildDeps::new(&output_path, Some(outputs));
296+
297+
// FIXME: it's basically buggy that we pass `None` to `call_box`
298+
// here. See documentation on `build_script_local_fingerprints`
299+
// below for more information. Despite this just try to proceed and
300+
// hobble along if it happens to return `Some`.
298301
if let Some(new_local) = gen_local.call_box(&deps, None)? {
299-
// Note that the fingerprint status here is also somewhat
300-
// tricky. We can't actually modify the `fingerprint`'s `local`
301-
// field, so we create a new fingerprint with the appropriate
302-
// `local`. To ensure the old version is used correctly we
303-
// force its memoized hash to be equal to our
304-
// `new_fingerprint`. This means usages of `fingerprint` in
305-
// various dependencies should work correctly because the hash
306-
// is still memoized to the correct value.
307-
let mut new_fingerprint = fingerprint.with_local(new_local);
308-
new_fingerprint.check_filesystem(&pkg_root, &target_root, false)?;
309-
*fingerprint.memoized_hash.lock().unwrap() = Some(new_fingerprint.hash());
310-
write_fingerprint(&loc, &new_fingerprint)
311-
} else {
312-
// FIXME: it's basically buggy that we pass `None` to
313-
// `call_box` above. See documentation on
314-
// `build_script_local_fingerprints` below for more
315-
// information. Despite this just try to proceed and hobble
316-
// along.
317-
write_fingerprint(&loc, &fingerprint)
302+
*fingerprint.local.lock().unwrap() = new_local;
303+
*fingerprint.memoized_hash.lock().unwrap() = None;
318304
}
305+
306+
write_fingerprint(&loc, &fingerprint)
319307
})
320308
} else {
321-
Work::new(move |_| write_fingerprint(&loc, &*fingerprint))
309+
Work::new(move |_| write_fingerprint(&loc, &fingerprint))
322310
};
323311

324312
Ok(Job::new(write_fingerprint, Dirty))
@@ -375,7 +363,7 @@ pub struct Fingerprint {
375363
deps: Vec<DepFingerprint>,
376364
/// Information about the inputs that affect this Unit (such as source
377365
/// file mtimes or build script environment variables).
378-
local: Vec<LocalFingerprint>,
366+
local: Mutex<Vec<LocalFingerprint>>,
379367
/// Cached hash of the `Fingerprint` struct. Used to improve performance
380368
/// for hashing.
381369
#[serde(skip)]
@@ -446,7 +434,6 @@ impl<'de> Deserialize<'de> for DepFingerprint {
446434
pkg_id,
447435
name,
448436
fingerprint: Arc::new(Fingerprint {
449-
local: vec![LocalFingerprint::Precalculated(String::new())],
450437
memoized_hash: Mutex::new(Some(hash)),
451438
..Fingerprint::new()
452439
}),
@@ -591,7 +578,7 @@ impl Fingerprint {
591578
path: 0,
592579
features: String::new(),
593580
deps: Vec::new(),
594-
local: Vec::new(),
581+
local: Mutex::new(Vec::new()),
595582
memoized_hash: Mutex::new(None),
596583
rustflags: Vec::new(),
597584
metadata: 0,
@@ -600,23 +587,6 @@ impl Fingerprint {
600587
}
601588
}
602589

603-
fn with_local(&self, local: Vec<LocalFingerprint>) -> Fingerprint {
604-
Fingerprint {
605-
fs_status: FsStatus::Stale,
606-
rustc: self.rustc,
607-
target: self.target,
608-
profile: self.profile,
609-
path: self.path,
610-
features: self.features.clone(),
611-
deps: self.deps.clone(),
612-
local,
613-
memoized_hash: Mutex::new(None),
614-
rustflags: self.rustflags.clone(),
615-
metadata: self.metadata,
616-
outputs: self.outputs.clone(),
617-
}
618-
}
619-
620590
fn hash(&self) -> u64 {
621591
if let Some(s) = *self.memoized_hash.lock().unwrap() {
622592
return s;
@@ -655,13 +625,15 @@ impl Fingerprint {
655625
if self.rustflags != old.rustflags {
656626
bail!("RUSTFLAGS has changed")
657627
}
658-
if self.local.len() != old.local.len() {
659-
bail!("local lens changed");
660-
}
661628
if self.metadata != old.metadata {
662629
bail!("metadata changed")
663630
}
664-
for (new, old) in self.local.iter().zip(&old.local) {
631+
let my_local = self.local.lock().unwrap();
632+
let old_local = old.local.lock().unwrap();
633+
if my_local.len() != old_local.len() {
634+
bail!("local lens changed");
635+
}
636+
for (new, old) in my_local.iter().zip(old_local.iter()) {
665637
match (new, old) {
666638
(LocalFingerprint::Precalculated(a), LocalFingerprint::Precalculated(b)) => {
667639
if a != b {
@@ -840,7 +812,7 @@ impl Fingerprint {
840812
// all our `LocalFingerprint` information to see if we have any stale
841813
// files for this package itself. If we do find something log a helpful
842814
// message and bail out so we stay stale.
843-
for local in self.local.iter() {
815+
for local in self.local.get_mut().unwrap().iter() {
844816
if let Some(file) = local.find_stale_file(pkg_root, target_root)? {
845817
file.log();
846818
return Ok(());
@@ -868,8 +840,9 @@ impl hash::Hash for Fingerprint {
868840
ref rustflags,
869841
..
870842
} = *self;
843+
let local = local.lock().unwrap();
871844
(
872-
rustc, features, target, path, profile, local, metadata, rustflags,
845+
rustc, features, target, path, profile, &*local, metadata, rustflags,
873846
)
874847
.hash(h);
875848

@@ -1089,7 +1062,7 @@ fn calculate_normal<'a, 'cfg>(
10891062
cx.bcx.resolve.features_sorted(unit.pkg.package_id())
10901063
),
10911064
deps,
1092-
local,
1065+
local: Mutex::new(local),
10931066
memoized_hash: Mutex::new(None),
10941067
metadata,
10951068
rustflags: extra_flags,
@@ -1141,7 +1114,7 @@ fn calculate_run_custom_build<'a, 'cfg>(
11411114
};
11421115

11431116
Ok(Fingerprint {
1144-
local,
1117+
local: Mutex::new(local),
11451118
rustc: util::hash_u64(&cx.bcx.rustc.verbose_version),
11461119
deps,
11471120
outputs: if overridden { Vec::new() } else { vec![output] },

0 commit comments

Comments
 (0)