Skip to content

Commit

Permalink
Use OnceLock instead of lazy_static
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 19, 2023
1 parent 4d88765 commit 20e8959
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
1 change: 0 additions & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ which = "*"

[target.'cfg(target_os="windows")'.dependencies]
cc = "*"
lazy_static = "*"
34 changes: 16 additions & 18 deletions xtask/src/tools/windows.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

lazy_static::lazy_static! {
static ref VISUAL_STUDIO_PATH: Option<PathBuf> = find_visual_studio();
}
static VISUAL_STUDIO_PATH: OnceLock<Option<Box<Path>>> = OnceLock::new();

fn find_visual_studio() -> Option<PathBuf> {
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<PathBuf> {
for path in [
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<PathBuf> {
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)
}

0 comments on commit 20e8959

Please sign in to comment.