diff --git a/src/lib.rs b/src/lib.rs index ee53aa5e8..52b402115 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,11 +56,12 @@ #![allow(deprecated)] #![deny(missing_docs)] -use std::collections::HashMap; +use std::collections::{hash_map, HashMap}; use std::env; use std::ffi::{OsStr, OsString}; use std::fmt::{self, Display, Formatter}; use std::fs; +use std::hash::Hasher; use std::io::{self, BufRead, BufReader, Read, Write}; use std::path::{Component, Path, PathBuf}; use std::process::{Child, Command, Stdio}; @@ -1014,6 +1015,21 @@ impl Build { }; } } + // ... and prefix the `basename` with the `dirname`'s hash + // to ensure name uniqueness. + let basename = file + .file_name() + .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "file_name() failure"))? + .to_string_lossy(); + let dirname = file + .parent() + .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "parent() failure"))? + .to_string_lossy(); + let mut hasher = hash_map::DefaultHasher::new(); + hasher.write(dirname.to_string().as_bytes()); + if dst.pop() { + dst.push(format!("{:016x}-{}", hasher.finish(), basename)); + } dst.with_extension("o") } else { dst.join(file).with_extension("o")