add unstable support for outputting file checksums for use in cargo

This commit is contained in:
Jacob Kiesel
2024-06-22 01:27:59 -06:00
parent bfe5e8cef6
commit bb5a8276be
16 changed files with 321 additions and 28 deletions

View File

@@ -175,6 +175,7 @@ pub struct SourceMapInputs {
pub file_loader: Box<dyn FileLoader + Send + Sync>,
pub path_mapping: FilePathMapping,
pub hash_kind: SourceFileHashAlgorithm,
pub checksum_hash_kind: Option<SourceFileHashAlgorithm>,
}
pub struct SourceMap {
@@ -187,6 +188,12 @@ pub struct SourceMap {
/// The algorithm used for hashing the contents of each source file.
hash_kind: SourceFileHashAlgorithm,
/// Similar to `hash_kind`, however this algorithm is used for checksums to determine if a crate is fresh.
/// `cargo` is the primary user of these.
///
/// If this is equal to `hash_kind` then the checksum won't be computed twice.
checksum_hash_kind: Option<SourceFileHashAlgorithm>,
}
impl SourceMap {
@@ -195,17 +202,19 @@ impl SourceMap {
file_loader: Box::new(RealFileLoader),
path_mapping,
hash_kind: SourceFileHashAlgorithm::Md5,
checksum_hash_kind: None,
})
}
pub fn with_inputs(
SourceMapInputs { file_loader, path_mapping, hash_kind }: SourceMapInputs,
SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind }: SourceMapInputs,
) -> SourceMap {
SourceMap {
files: Default::default(),
file_loader: IntoDynSyncSend(file_loader),
path_mapping,
hash_kind,
checksum_hash_kind,
}
}
@@ -307,7 +316,8 @@ impl SourceMap {
match self.source_file_by_stable_id(stable_id) {
Some(lrc_sf) => Ok(lrc_sf),
None => {
let source_file = SourceFile::new(filename, src, self.hash_kind)?;
let source_file =
SourceFile::new(filename, src, self.hash_kind, self.checksum_hash_kind)?;
// Let's make sure the file_id we generated above actually matches
// the ID we generate for the SourceFile we just created.
@@ -326,6 +336,7 @@ impl SourceMap {
&self,
filename: FileName,
src_hash: SourceFileHash,
checksum_hash: Option<SourceFileHash>,
stable_id: StableSourceFileId,
source_len: u32,
cnum: CrateNum,
@@ -340,6 +351,7 @@ impl SourceMap {
name: filename,
src: None,
src_hash,
checksum_hash,
external_src: FreezeLock::new(ExternalSource::Foreign {
kind: ExternalSourceKind::AbsentOk,
metadata_index,