Skip to content

Commit c4f414f

Browse files
authored
Map source absolute paths to OUT_DIR as relative. (#684)
Fixes #683
1 parent 2ce2be8 commit c4f414f

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@
5656
#![allow(deprecated)]
5757
#![deny(missing_docs)]
5858

59-
use std::collections::HashMap;
59+
use std::collections::{hash_map, HashMap};
6060
use std::env;
6161
use std::ffi::{OsStr, OsString};
6262
use std::fmt::{self, Display, Formatter};
6363
use std::fs;
64+
use std::hash::Hasher;
6465
use std::io::{self, BufRead, BufReader, Read, Write};
6566
use std::path::{Component, Path, PathBuf};
6667
use std::process::{Child, Command, Stdio};
@@ -1023,7 +1024,24 @@ impl Build {
10231024

10241025
let mut objects = Vec::new();
10251026
for file in self.files.iter() {
1026-
let obj = dst.join(file).with_extension("o");
1027+
let obj = if file.has_root() {
1028+
// If `file` is an absolute path, prefix the `basename`
1029+
// with the `dirname`'s hash to ensure name uniqueness.
1030+
let basename = file
1031+
.file_name()
1032+
.ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "file_name() failure"))?
1033+
.to_string_lossy();
1034+
let dirname = file
1035+
.parent()
1036+
.ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "parent() failure"))?
1037+
.to_string_lossy();
1038+
let mut hasher = hash_map::DefaultHasher::new();
1039+
hasher.write(dirname.to_string().as_bytes());
1040+
dst.join(format!("{:016x}-{}", hasher.finish(), basename))
1041+
.with_extension("o")
1042+
} else {
1043+
dst.join(file).with_extension("o")
1044+
};
10271045
let obj = if !obj.starts_with(&dst) {
10281046
dst.join(obj.file_name().ok_or_else(|| {
10291047
Error::new(ErrorKind::IOError, "Getting object file details failed.")

0 commit comments

Comments
 (0)