Skip to content

Commit 7eb7422

Browse files
committed
condense fingerprint variants into one
1 parent 5b36d64 commit 7eb7422

File tree

2 files changed

+79
-94
lines changed

2 files changed

+79
-94
lines changed

src/cargo/core/compiler/fingerprint/dirty_reason.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pub enum DirtyReason {
3434
old: String,
3535
new: String,
3636
},
37+
ChecksumUseChanged {
38+
old: bool,
39+
new: bool,
40+
},
3741
DepInfoOutputChanged {
3842
old: PathBuf,
3943
new: PathBuf,
@@ -183,6 +187,16 @@ impl DirtyReason {
183187
DirtyReason::PrecalculatedComponentsChanged { .. } => {
184188
s.dirty_because(unit, "the precalculated components changed")
185189
}
190+
DirtyReason::ChecksumUseChanged { old, new: _ } => {
191+
if *old {
192+
s.dirty_because(
193+
unit,
194+
"the prior compilation used checksum freshness and this one does not",
195+
)
196+
} else {
197+
s.dirty_because(unit, "checksum freshness requested, prior compilation did not use checksum freshness")
198+
}
199+
}
186200
DirtyReason::DepInfoOutputChanged { .. } => {
187201
s.dirty_because(unit, "the dependency info output changed")
188202
}

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

Lines changed: 65 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -734,17 +734,10 @@ enum LocalFingerprint {
734734
/// The `dep_info` file, when present, also lists a number of other files
735735
/// for us to look at. If any of those files are newer than this file then
736736
/// we need to recompile.
737-
CheckDepInfo { dep_info: PathBuf },
738-
739-
/// This is used for crate compilations. The `dep_info` file is a relative
740-
/// path anchored at `target_root(...)` to the dep-info file that Cargo
741-
/// generates (which is a custom serialization after parsing rustc's own
742-
/// `dep-info` output).
743737
///
744-
/// The `dep_info` file, when present, also lists a number of other files
745-
/// for us to look at. If any of those files have a different checksum then
746-
/// we need to recompile.
747-
CheckDepInfoChecksums { dep_info: PathBuf },
738+
/// If the `checksum` bool is true then the dep_info file is expected to
739+
/// contain file checksums instead of file mtimes.
740+
CheckDepInfo { dep_info: PathBuf, checksum: bool },
748741

749742
/// This represents a nonempty set of `rerun-if-changed` annotations printed
750743
/// out by a build script. The `output` file is a relative file anchored at
@@ -837,42 +830,56 @@ impl LocalFingerprint {
837830
// matches, and for each file we see if any of them are newer than
838831
// the `dep_info` file itself whose mtime represents the start of
839832
// rustc.
840-
LocalFingerprint::CheckDepInfo { dep_info } => {
833+
LocalFingerprint::CheckDepInfo { dep_info, checksum } => {
841834
let dep_info = target_root.join(dep_info);
842-
let info = match dep_info_shared(pkg_root, target_root, &dep_info, cargo_exe, gctx)?
843-
{
844-
Either::Left(stale) => {
845-
return Ok(Some(stale));
846-
}
847-
Either::Right(info) => info,
835+
let cargo_exe = cargo_exe;
836+
let Some(info) = parse_dep_info(pkg_root, target_root, &dep_info)? else {
837+
return Ok(Some(StaleItem::MissingFile(dep_info.clone())));
848838
};
849-
Ok(find_stale_file(
850-
mtime_cache,
851-
checksum_cache,
852-
&dep_info,
853-
info.files.iter().map(|p| (p, None)),
854-
false,
855-
))
856-
}
857-
858-
LocalFingerprint::CheckDepInfoChecksums { dep_info } => {
859-
let dep_info = target_root.join(dep_info);
860-
let info = match dep_info_shared(pkg_root, target_root, &dep_info, cargo_exe, gctx)?
861-
{
862-
Either::Left(stale) => {
863-
return Ok(Some(stale));
839+
for (key, previous) in info.env.iter() {
840+
let current = if key == CARGO_ENV {
841+
Some(
842+
cargo_exe
843+
.to_str()
844+
.ok_or_else(|| {
845+
format_err!(
846+
"cargo exe path {} must be valid UTF-8",
847+
cargo_exe.display()
848+
)
849+
})?
850+
.to_string(),
851+
)
852+
} else {
853+
gctx.get_env(key).ok()
854+
};
855+
if current == *previous {
856+
continue;
864857
}
865-
Either::Right(info) => info,
866-
};
867-
Ok(find_stale_file(
868-
mtime_cache,
869-
checksum_cache,
870-
&dep_info,
871-
info.files
858+
return Ok(Some(StaleItem::ChangedEnv {
859+
var: key.clone(),
860+
previous: previous.clone(),
861+
current,
862+
}));
863+
}
864+
macro_rules! find_stale_file {
865+
($iter:expr) => {
866+
Ok(find_stale_file(
867+
mtime_cache,
868+
checksum_cache,
869+
&dep_info,
870+
$iter,
871+
*checksum,
872+
))
873+
};
874+
}
875+
if *checksum {
876+
find_stale_file!(info
877+
.files
872878
.iter()
873-
.map(|file| (file.clone(), info.checksum.get(file).cloned())),
874-
true,
875-
))
879+
.map(|file| (file.clone(), info.checksum.get(file).cloned())))
880+
} else {
881+
find_stale_file!(info.files.into_iter().map(|p| (p, None)))
882+
}
876883
}
877884

878885
// We need to verify that no paths listed in `paths` are newer than
@@ -897,48 +904,12 @@ impl LocalFingerprint {
897904
match self {
898905
LocalFingerprint::Precalculated(..) => "precalculated",
899906
LocalFingerprint::CheckDepInfo { .. } => "dep-info",
900-
LocalFingerprint::CheckDepInfoChecksums { .. } => "dep-info-checksums",
901907
LocalFingerprint::RerunIfChanged { .. } => "rerun-if-changed",
902908
LocalFingerprint::RerunIfEnvChanged { .. } => "rerun-if-env-changed",
903909
}
904910
}
905911
}
906912

907-
fn dep_info_shared(
908-
pkg_root: &Path,
909-
target_root: &Path,
910-
dep_info: &PathBuf,
911-
cargo_exe: &Path,
912-
gctx: &GlobalContext,
913-
) -> Result<Either<StaleItem, RustcDepInfo>, anyhow::Error> {
914-
let Some(info) = parse_dep_info(pkg_root, target_root, dep_info)? else {
915-
return Ok(Either::Left(StaleItem::MissingFile(dep_info.clone())));
916-
};
917-
for (key, previous) in info.env.iter() {
918-
let current = if key == CARGO_ENV {
919-
Some(
920-
cargo_exe
921-
.to_str()
922-
.ok_or_else(|| {
923-
format_err!("cargo exe path {} must be valid UTF-8", cargo_exe.display())
924-
})?
925-
.to_string(),
926-
)
927-
} else {
928-
gctx.get_env(key).ok()
929-
};
930-
if current == *previous {
931-
continue;
932-
}
933-
return Ok(Either::Left(StaleItem::ChangedEnv {
934-
var: key.clone(),
935-
previous: previous.clone(),
936-
current,
937-
}));
938-
}
939-
Ok(Either::Right(info))
940-
}
941-
942913
impl Fingerprint {
943914
fn new() -> Fingerprint {
944915
Fingerprint {
@@ -1040,24 +1011,25 @@ impl Fingerprint {
10401011
}
10411012
}
10421013
(
1043-
LocalFingerprint::CheckDepInfo { dep_info: adep },
1044-
LocalFingerprint::CheckDepInfo { dep_info: bdep },
1014+
LocalFingerprint::CheckDepInfo {
1015+
dep_info: adep,
1016+
checksum: checksum_a,
1017+
},
1018+
LocalFingerprint::CheckDepInfo {
1019+
dep_info: bdep,
1020+
checksum: checksum_b,
1021+
},
10451022
) => {
10461023
if adep != bdep {
10471024
return DirtyReason::DepInfoOutputChanged {
10481025
old: bdep.clone(),
10491026
new: adep.clone(),
10501027
};
10511028
}
1052-
}
1053-
(
1054-
LocalFingerprint::CheckDepInfoChecksums { dep_info: adep },
1055-
LocalFingerprint::CheckDepInfoChecksums { dep_info: bdep },
1056-
) => {
1057-
if adep != bdep {
1058-
return DirtyReason::DepInfoOutputChanged {
1059-
old: bdep.clone(),
1060-
new: adep.clone(),
1029+
if checksum_a != checksum_b {
1030+
return DirtyReason::ChecksumUseChanged {
1031+
old: *checksum_b,
1032+
new: *checksum_a,
10611033
};
10621034
}
10631035
}
@@ -1516,11 +1488,10 @@ fn calculate_normal(
15161488
} else {
15171489
let dep_info = dep_info_loc(build_runner, unit);
15181490
let dep_info = dep_info.strip_prefix(&target_root).unwrap().to_path_buf();
1519-
if build_runner.bcx.gctx.cli_unstable().checksum_freshness {
1520-
vec![LocalFingerprint::CheckDepInfoChecksums { dep_info }]
1521-
} else {
1522-
vec![LocalFingerprint::CheckDepInfo { dep_info }]
1523-
}
1491+
vec![LocalFingerprint::CheckDepInfo {
1492+
dep_info,
1493+
checksum: build_runner.bcx.gctx.cli_unstable().checksum_freshness,
1494+
}]
15241495
};
15251496

15261497
// Figure out what the outputs of our unit is, and we'll be storing them

0 commit comments

Comments
 (0)