Skip to content

Commit 19cd68a

Browse files
committed
Canonicalize the build directory
Avoid some problems with relative paths by attempting to expand. Still fall back to the originally provide path if canonicalization fails. Fixes: #227
1 parent f09de3f commit 19cd68a

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/lib.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,11 @@ impl Config {
554554
.out_dir
555555
.clone()
556556
.unwrap_or_else(|| PathBuf::from(getenv_unwrap("OUT_DIR")));
557-
let build = dst.join("build");
558-
self.maybe_clear(&build);
559-
let _ = fs::create_dir_all(&build);
557+
558+
let build_dir = try_canonicalize(&dst.join("build"));
559+
560+
self.maybe_clear(&build_dir);
561+
let _ = fs::create_dir_all(&build_dir);
560562

561563
// Add all our dependencies to our cmake paths
562564
let mut cmake_prefix_path = Vec::new();
@@ -582,7 +584,7 @@ impl Config {
582584
cmd.arg("--debug-output");
583585
}
584586

585-
cmd.arg(&self.path).current_dir(&build);
587+
cmd.arg(&self.path).current_dir(&build_dir);
586588
let mut is_ninja = false;
587589
if let Some(ref generator) = generator {
588590
is_ninja = generator.to_string_lossy().contains("Ninja");
@@ -816,7 +818,7 @@ impl Config {
816818
cmd.env(k, v);
817819
}
818820

819-
if self.always_configure || !build.join("CMakeCache.txt").exists() {
821+
if self.always_configure || !build_dir.join("CMakeCache.txt").exists() {
820822
cmd.args(&self.configure_args);
821823
run(cmd.env("CMAKE_PREFIX_PATH", cmake_prefix_path), "cmake");
822824
} else {
@@ -825,15 +827,15 @@ impl Config {
825827

826828
// And build!
827829
let mut cmd = self.cmake_build_command(&target);
828-
cmd.current_dir(&build);
830+
cmd.current_dir(&build_dir);
829831

830832
for (k, v) in c_compiler.env().iter().chain(&self.env) {
831833
cmd.env(k, v);
832834
}
833835

834836
// If the generated project is Makefile based we should carefully transfer corresponding CARGO_MAKEFLAGS
835837
let mut use_jobserver = false;
836-
if fs::metadata(build.join("Makefile")).is_ok() {
838+
if fs::metadata(build_dir.join("Makefile")).is_ok() {
837839
match env::var_os("CARGO_MAKEFLAGS") {
838840
// Only do this on non-windows, non-bsd, and non-macos (unless a named pipe
839841
// jobserver is available)
@@ -859,7 +861,7 @@ impl Config {
859861
}
860862
}
861863

862-
cmd.arg("--build").arg(&build);
864+
cmd.arg("--build").arg(&build_dir);
863865

864866
if !self.no_build_target {
865867
let target = self
@@ -995,7 +997,8 @@ impl Config {
995997
// CMake will apparently store canonicalized paths which normally
996998
// isn't relevant to us but we canonicalize it here to ensure
997999
// we're both checking the same thing.
998-
let path = fs::canonicalize(&self.path).unwrap_or_else(|_| self.path.clone());
1000+
let path = try_canonicalize(&self.path);
1001+
9991002
let mut f = match File::open(dir.join("CMakeCache.txt")) {
10001003
Ok(f) => f,
10011004
Err(..) => return,
@@ -1129,6 +1132,12 @@ fn uses_named_pipe_jobserver(makeflags: &OsStr) -> bool {
11291132
.contains("--jobserver-auth=fifo:")
11301133
}
11311134

1135+
/// Attempt to canonicalize; fall back to the original path if unsuccessful, in case `cmake` knows
1136+
/// something we don't.
1137+
fn try_canonicalize(path: &Path) -> PathBuf {
1138+
path.canonicalize().unwrap_or_else(|_| path.to_owned())
1139+
}
1140+
11321141
#[cfg(test)]
11331142
mod tests {
11341143
use super::uses_named_pipe_jobserver;

0 commit comments

Comments
 (0)