Skip to content

Commit

Permalink
sysroot strip
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Dec 17, 2024
1 parent addcc8c commit b641d1a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
17 changes: 15 additions & 2 deletions src/cargo/core/compiler/build_runner/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub struct CompilationFiles<'a, 'gctx> {
metas: HashMap<Unit, Metadata>,
/// For each Unit, a list all files produced.
outputs: HashMap<Unit, LazyCell<Arc<Vec<OutputFile>>>>,
/// Sysroot of the currently used compiler.
sysroot: PathBuf,
}

/// Info about a single file emitted by the compiler.
Expand Down Expand Up @@ -178,6 +180,13 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
roots: build_runner.bcx.roots.clone(),
metas,
outputs,
sysroot: build_runner
.bcx
.target_data
.get_info(CompileKind::Host)
.unwrap()
.sysroot
.clone(),
}
}

Expand All @@ -201,7 +210,10 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
/// Gets the short hash based only on the `PackageId`.
/// Used for the metadata when `metadata` returns `None`.
pub fn target_short_hash(&self, unit: &Unit) -> String {
let hashable = unit.pkg.package_id().stable_hash(self.ws.root());
let hashable = unit
.pkg
.package_id()
.stable_hash(self.ws.root(), &self.sysroot);
util::short_hash(&(METADATA_VERSION, hashable))
}

Expand Down Expand Up @@ -606,9 +618,10 @@ fn compute_metadata(

// Unique metadata per (name, source, version) triple. This'll allow us
// to pull crates from anywhere without worrying about conflicts.
let sysroot = &bcx.target_data.get_info(unit.kind).unwrap().sysroot;
unit.pkg
.package_id()
.stable_hash(bcx.ws.root())
.stable_hash(bcx.ws.root(), &sysroot)
.hash(&mut shared_hasher);

// Also mix in enabled features to our metadata. This'll ensure that
Expand Down
29 changes: 22 additions & 7 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,20 @@ impl PackageId {

/// Returns a value that implements a "stable" hashable value.
///
/// Stable hashing removes the path prefix of the workspace from path
/// Stable hashing removes the path prefix of the workspace and sysroot from path
/// packages. This helps with reproducible builds, since this hash is part
/// of the symbol metadata, and we don't want the absolute path where the
/// build is performed to affect the binary output.
pub fn stable_hash(self, workspace: &Path) -> PackageIdStableHash<'_> {
PackageIdStableHash(self, workspace)
pub fn stable_hash<'a>(
self,
workspace: &'a Path,
sysroot: &'a Path,
) -> PackageIdStableHash<'a> {
PackageIdStableHash {
inner: self,
sysroot,
workspace,
}
}

/// Filename of the `.crate` tarball, e.g., `once_cell-1.18.0.crate`.
Expand All @@ -198,13 +206,20 @@ impl PackageId {
}
}

pub struct PackageIdStableHash<'a>(PackageId, &'a Path);
pub struct PackageIdStableHash<'a> {
inner: PackageId,
workspace: &'a Path,
sysroot: &'a Path,
}

impl<'a> Hash for PackageIdStableHash<'a> {
fn hash<S: hash::Hasher>(&self, state: &mut S) {
self.0.inner.name.hash(state);
self.0.inner.version.hash(state);
self.0.inner.source_id.stable_hash(self.1, state);
self.inner.inner.name.hash(state);
self.inner.inner.version.hash(state);
self.inner
.inner
.source_id
.stable_hash(self.workspace, self.sysroot, state);
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/cargo/core/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,14 @@ impl SourceId {
///
/// For paths, remove the workspace prefix so the same source will give the
/// same hash in different locations, helping reproducible builds.
pub fn stable_hash<S: hash::Hasher>(self, workspace: &Path, into: &mut S) {
pub fn stable_hash<S: hash::Hasher>(self, workspace: &Path, sysroot: &Path, into: &mut S) {
if self.is_path() {
if let Ok(p) = self
.inner
.url
.to_file_path()
.unwrap()
.strip_prefix(workspace)
{
self.inner.kind.hash(into);
p.to_str().unwrap().hash(into);
return;
for prefix in [workspace, sysroot] {
if let Ok(p) = self.inner.url.to_file_path().unwrap().strip_prefix(prefix) {
self.inner.kind.hash(into);
p.to_str().unwrap().hash(into);
return;
}
}
}
self.hash(into)
Expand Down Expand Up @@ -799,9 +795,14 @@ mod tests {
#[cfg(windows)]
let ws_root = Path::new(r"C:\\tmp\ws");

#[cfg(not(windows))]
let sysroot = Path::new("/tmp/sysroot");
#[cfg(windows)]
let sysroot = Path::new(r"C:\\tmp\sysroot");

let gen_hash = |source_id: SourceId| {
let mut hasher = StableHasher::new();
source_id.stable_hash(ws_root, &mut hasher);
source_id.stable_hash(ws_root, sysroot, &mut hasher);
Hasher::finish(&hasher)
};

Expand Down

0 comments on commit b641d1a

Please sign in to comment.