Skip to content

Commit ae7f5ec

Browse files
committed
MSVC: Use bundled CMake if present and no other is in path
1 parent bfaf129 commit ae7f5ec

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ impl Config {
514514
// Build up the first cmake command to build the build system.
515515
let executable = self
516516
.getenv_target_os("CMAKE")
517+
.or_else(|| find_cmake_executable(&target))
517518
.unwrap_or(OsString::from("cmake"));
518519
let mut cmd = Command::new(&executable);
519520

@@ -965,3 +966,28 @@ fn getenv_unwrap(v: &str) -> String {
965966
fn fail(s: &str) -> ! {
966967
panic!("\n{}\n\nbuild script failed, must exit now", s)
967968
}
969+
970+
#[cfg(windows)]
971+
fn find_cmake_executable(target: &str) -> Option<OsString> {
972+
use cc::windows_registry::find_tool;
973+
974+
// Try to find cmake.exe bundled with MSVC, but only if there isn't another one in path
975+
let cmake_in_path = env::split_paths(&env::var_os("PATH").unwrap_or(OsString::new()))
976+
.any(|p| p.join("cmake.exe").exists());
977+
if cmake_in_path {
978+
None
979+
} else {
980+
find_tool(target, "devenv").and_then(|t| {
981+
t.path()
982+
.join("..\\CommonExtensions\\Microsoft\\CMake\\CMake\\bin\\cmake.exe")
983+
.canonicalize()
984+
.ok()
985+
.map(OsString::from)
986+
})
987+
}
988+
}
989+
990+
#[cfg(not(windows))]
991+
fn find_cmake_executable(target: &str) -> Option<OsString> {
992+
None
993+
}

0 commit comments

Comments
 (0)