Skip to content

Commit 47502d7

Browse files
ChrisDentonknopp
andcommitted
MSVC: Fallback to bundled CMake if present
Co-Authored-By: Matej Knopp <matej.knopp@gmail.com>
1 parent 060922d commit 47502d7

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/lib.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,9 @@ impl Config {
897897
dst
898898
}
899899

900-
fn cmake_executable(&mut self) -> OsString {
900+
fn cmake_executable(&mut self, target: &str) -> OsString {
901901
self.getenv_target_os("CMAKE")
902+
.or_else(|| find_cmake_executable(target))
902903
.unwrap_or_else(|| OsString::from("cmake"))
903904
}
904905

@@ -912,10 +913,10 @@ impl Config {
912913
.getenv_target_os("EMCMAKE")
913914
.unwrap_or_else(|| OsString::from("emcmake"));
914915
let mut cmd = Command::new(emcmake);
915-
cmd.arg(self.cmake_executable());
916+
cmd.arg(self.cmake_executable(target));
916917
cmd
917918
} else {
918-
Command::new(self.cmake_executable())
919+
Command::new(self.cmake_executable(target))
919920
}
920921
}
921922

@@ -925,10 +926,10 @@ impl Config {
925926
.getenv_target_os("EMMAKE")
926927
.unwrap_or_else(|| OsString::from("emmake"));
927928
let mut cmd = Command::new(emmake);
928-
cmd.arg(self.cmake_executable());
929+
cmd.arg(self.cmake_executable(target));
929930
cmd
930931
} else {
931-
Command::new(self.cmake_executable())
932+
Command::new(self.cmake_executable(target))
932933
}
933934
}
934935

@@ -1166,6 +1167,31 @@ fn try_canonicalize(path: &Path) -> PathBuf {
11661167
path
11671168
}
11681169

1170+
#[cfg(windows)]
1171+
fn find_cmake_executable(target: &str) -> Option<OsString> {
1172+
use cc::windows_registry::find_tool;
1173+
1174+
// Try to find cmake.exe bundled with MSVC, but only if there isn't another one in path
1175+
let cmake_in_path = env::split_paths(&env::var_os("PATH").unwrap_or(OsString::new()))
1176+
.any(|p| p.join("cmake.exe").exists());
1177+
if cmake_in_path {
1178+
None
1179+
} else {
1180+
find_tool(target, "devenv").and_then(|t| {
1181+
t.path()
1182+
.join("..\\CommonExtensions\\Microsoft\\CMake\\CMake\\bin\\cmake.exe")
1183+
.canonicalize()
1184+
.ok()
1185+
.map(OsString::from)
1186+
})
1187+
}
1188+
}
1189+
1190+
#[cfg(not(windows))]
1191+
fn find_cmake_executable(_target: &str) -> Option<OsString> {
1192+
None
1193+
}
1194+
11691195
#[cfg(test)]
11701196
mod tests {
11711197
use super::uses_named_pipe_jobserver;

0 commit comments

Comments
 (0)