Skip to content

Commit

Permalink
Use relative directory for obj files hash (#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmattia authored Nov 8, 2024
1 parent 8120aed commit 15d2f7d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/command_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Miscellaneous helpers for running commands

use std::{
borrow::Cow,
collections::hash_map,
ffi::OsString,
fmt::Display,
Expand Down Expand Up @@ -311,7 +312,21 @@ pub(crate) fn objects_from_files(files: &[Arc<Path>], dst: &Path) -> Result<Vec<
// Hash the dirname. This should prevent conflicts if we have multiple
// object files with the same filename in different subfolders.
let mut hasher = hash_map::DefaultHasher::new();
hasher.write(dirname.to_string().as_bytes());

// Make the dirname relative (if possible) to avoid full system paths influencing the sha
// and making the output system-dependent
//
// NOTE: Here we allow using std::env::var (instead of Build::getenv) because
// CARGO_* variables always trigger a rebuild when changed
#[allow(clippy::disallowed_methods)]
let dirname = if let Some(root) = std::env::var_os("CARGO_MANIFEST_DIR") {
let root = root.to_string_lossy();
Cow::Borrowed(dirname.strip_prefix(&*root).unwrap_or(&dirname))
} else {
dirname
};

hasher.write(dirname.as_bytes());
let obj = dst
.join(format!("{:016x}-{}", hasher.finish(), basename))
.with_extension("o");
Expand Down

0 comments on commit 15d2f7d

Please sign in to comment.