From 028787ed2f2e15b61e0c4f936fb25a39adfad019 Mon Sep 17 00:00:00 2001 From: Matej Knopp Date: Sun, 27 Feb 2022 21:15:33 +0100 Subject: [PATCH] MSVC: Use bundled CMake if present and no other is in path --- src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7ab9547..e9f1c2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -514,6 +514,7 @@ impl Config { // Build up the first cmake command to build the build system. let executable = self .getenv_target_os("CMAKE") + .or_else(|| find_cmake_executable(&target)) .unwrap_or(OsString::from("cmake")); let mut cmd = Command::new(&executable); @@ -965,3 +966,28 @@ fn getenv_unwrap(v: &str) -> String { fn fail(s: &str) -> ! { panic!("\n{}\n\nbuild script failed, must exit now", s) } + +#[cfg(windows)] +fn find_cmake_executable(target: &str) -> Option { + use cc::windows_registry::find_tool; + + // Try to find cmake.exe bundled with MSVC, but only if there isn't another one in path + let cmake_in_path = env::split_paths(&env::var_os("PATH").unwrap_or(OsString::new())) + .any(|p| p.join("cmake.exe").exists()); + if cmake_in_path { + None + } else { + find_tool(target, "devenv").and_then(|t| { + t.path() + .join("..\\CommonExtensions\\Microsoft\\CMake\\CMake\\bin\\cmake.exe") + .canonicalize() + .ok() + .map(OsString::from) + }) + } +} + +#[cfg(not(windows))] +fn find_cmake_executable(target: &str) -> Option { + None +}