Skip to content

Commit 15d2f7d

Browse files
authored
Use relative directory for obj files hash (#1270)
1 parent 8120aed commit 15d2f7d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/command_helpers.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Miscellaneous helpers for running commands
22
33
use std::{
4+
borrow::Cow,
45
collections::hash_map,
56
ffi::OsString,
67
fmt::Display,
@@ -311,7 +312,21 @@ pub(crate) fn objects_from_files(files: &[Arc<Path>], dst: &Path) -> Result<Vec<
311312
// Hash the dirname. This should prevent conflicts if we have multiple
312313
// object files with the same filename in different subfolders.
313314
let mut hasher = hash_map::DefaultHasher::new();
314-
hasher.write(dirname.to_string().as_bytes());
315+
316+
// Make the dirname relative (if possible) to avoid full system paths influencing the sha
317+
// and making the output system-dependent
318+
//
319+
// NOTE: Here we allow using std::env::var (instead of Build::getenv) because
320+
// CARGO_* variables always trigger a rebuild when changed
321+
#[allow(clippy::disallowed_methods)]
322+
let dirname = if let Some(root) = std::env::var_os("CARGO_MANIFEST_DIR") {
323+
let root = root.to_string_lossy();
324+
Cow::Borrowed(dirname.strip_prefix(&*root).unwrap_or(&dirname))
325+
} else {
326+
dirname
327+
};
328+
329+
hasher.write(dirname.as_bytes());
315330
let obj = dst
316331
.join(format!("{:016x}-{}", hasher.finish(), basename))
317332
.with_extension("o");

0 commit comments

Comments
 (0)