Skip to content

Commit

Permalink
fixup! Attempt supporting spaces in CC path
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Aug 3, 2024
1 parent 25d039b commit e71b14d
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions jemalloc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,30 @@ fn expect_env(name: &str) -> String {

/// Attempts to turn the given path into a short path. Returns none in case of
/// error.
fn to_short_path(path: &Path) -> Option<PathBuf> {
let path = match Command::new("cmd").args(&["/c", "@echo %~s1"]).arg(path).output() {
fn to_short_path(orig_path: &Path) -> Option<PathBuf> {
let path = match Command::new("cmd").args(["/c", "@echo %~s1"]).arg(orig_path).output() {
Ok(v) if v.status.success() => v.stdout,
Ok(v) => warn!("Failed to expand {} to a short path, the command exited with status {}", compiler_path.display(), v.status),
Err(err) => warn!("Failed to expand {} to a short path: {}", compiler_path.display(), err),
Ok(v) => {
warning!("Failed to expand {} to a short path, the command exited with status {}", orig_path.display(), v.status);
return None;
}
Err(err) => {
warning!("Failed to expand {} to a short path: {}", orig_path.display(), err);
return None;
}
};

// TODO: Turn the Vec<u8> into an OsString so we can lossly turn it into a
// PathBuf.
PathBuf::from(String::from_utf8(path))
let path = match String::from_utf8(path) {
Ok(v) => v,
Err(_err) => {
warning!("Short path of {} is not valid utf-8", orig_path.display());
return None;
}
};

Some(PathBuf::from(path))
}

// TODO: split main functions and remove following allow.
Expand Down Expand Up @@ -168,15 +182,15 @@ fn main() {
.map(|s| s.to_str().unwrap())
.collect::<Vec<_>>()
.join(" ");
let mut compiler_path = compiler.path();
let mut compiler_path = compiler.path().to_owned();
if cfg!(windows) && compiler_path.to_string_lossy().contains(' ') {
// Autoconf does not support spaces in the compiler path. This is not
// usually a problem, except on windows where the MSVC compiler (cl.exe)
// is usually found in C:\Program Files, which has a space. Fortunately,
// windows has a trick that can be used: the path can be turned into a
// "short path", turning C:\Program Files into C:\PROGRA~2. Through this
// trick, we can attempt to eliminate the spaces.
if let Some(short_path) = to_short_path(compiler_path) {
if let Some(short_path) = to_short_path(&compiler_path) {
compiler_path = short_path;
}
}
Expand Down

0 comments on commit e71b14d

Please sign in to comment.