diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index f49a9cc4..9f8531fe 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -18,4 +18,3 @@ which = "*" [target.'cfg(target_os="windows")'.dependencies] cc = "*" -lazy_static = "*" diff --git a/xtask/src/tools/windows.rs b/xtask/src/tools/windows.rs index 1b939235..bcc76e82 100644 --- a/xtask/src/tools/windows.rs +++ b/xtask/src/tools/windows.rs @@ -1,19 +1,21 @@ use std::path::{Path, PathBuf}; +use std::sync::OnceLock; -lazy_static::lazy_static! { - static ref VISUAL_STUDIO_PATH: Option = find_visual_studio(); -} +static VISUAL_STUDIO_PATH: OnceLock>> = OnceLock::new(); -fn find_visual_studio() -> Option { - let mut buffer = cc::windows_registry::find_tool("x86_64-pc-windows-msvc", "cl.exe")? - .path() - .to_path_buf(); +fn find_visual_studio() -> Option<&'static Path> { + VISUAL_STUDIO_PATH + .get_or_init(|| { + let tool = cc::windows_registry::find_tool("x86_64-pc-windows-msvc", "cl.exe")?; + let mut path = tool.path(); - for _ in 0..8 { - buffer.pop(); - } + for _ in 0..8 { + path = path.parent().unwrap(); + } - Some(buffer) + Some(path.into()) + }) + .as_deref() } pub fn find_cmake() -> Option { @@ -21,22 +23,18 @@ pub fn find_cmake() -> Option { r"C:\Program Files\CMake\bin\cmake.exe", r"C:\Program Files (x86)\CMake\bin\cmake.exe", ] { - if Path::new(path).is_file() { + if Path::is_file(path.as_ref()) { return Some(PathBuf::from(path)); } } - let cmake_path = VISUAL_STUDIO_PATH - .as_deref()? - .join(r"Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"); + let cmake_path = find_visual_studio()?.join(r"Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"); cmake_path.is_file().then_some(cmake_path) } pub fn find_ninja() -> Option { - let ninja_path = VISUAL_STUDIO_PATH - .as_deref()? - .join(r"Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe"); + let ninja_path = find_visual_studio()?.join(r"Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe"); ninja_path.is_file().then_some(ninja_path) }