From 5b483a73074fc5f59a4a3da5d176b6a9e6cae444 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 18 Nov 2023 23:46:43 +1100 Subject: [PATCH] Cache compiler versions detected Signed-off-by: Jiahao XU --- src/lib.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 107910f72..04aced545 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,6 +133,7 @@ pub struct Build { env_cache: Arc>>>>, apple_sdk_root_cache: Arc>>, emit_rerun_if_env_changed: bool, + cached_compiler_family: Arc, ToolFamily>>>, } /// Represents the types of errors that may occur while using cc-rs. @@ -339,6 +340,7 @@ impl Build { env_cache: Arc::new(Mutex::new(HashMap::new())), apple_sdk_root_cache: Arc::new(Mutex::new(HashMap::new())), emit_rerun_if_env_changed: true, + cached_compiler_family: Arc::default(), } } @@ -2533,7 +2535,11 @@ impl Build { fn get_base_compiler(&self) -> Result { if let Some(c) = &self.compiler { - return Ok(Tool::new((**c).to_owned(), &self.cargo_output)); + return Ok(Tool::new( + (**c).to_owned(), + &self.cached_compiler_family, + &self.cargo_output, + )); } let host = self.get_host()?; let target = self.get_target()?; @@ -2569,7 +2575,12 @@ impl Build { // semi-buggy build scripts which are shared in // makefiles/configure scripts (where spaces are far more // lenient) - let mut t = Tool::with_clang_driver(tool, driver_mode, &self.cargo_output); + let mut t = Tool::with_clang_driver( + tool, + driver_mode, + &self.cached_compiler_family, + &self.cargo_output, + ); if let Some(cc_wrapper) = wrapper { t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper)); } @@ -2583,12 +2594,20 @@ impl Build { let tool = if self.cpp { "em++" } else { "emcc" }; // Windows uses bat file so we have to be a bit more specific if cfg!(windows) { - let mut t = Tool::new(PathBuf::from("cmd"), &self.cargo_output); + let mut t = Tool::new( + PathBuf::from("cmd"), + &self.cached_compiler_family, + &self.cargo_output, + ); t.args.push("/c".into()); t.args.push(format!("{}.bat", tool).into()); Some(t) } else { - Some(Tool::new(PathBuf::from(tool), &self.cargo_output)) + Some(Tool::new( + PathBuf::from(tool), + &self.cached_compiler_family, + &self.cargo_output, + )) } } else { None @@ -2643,7 +2662,11 @@ impl Build { default.to_string() }; - let mut t = Tool::new(PathBuf::from(compiler), &self.cargo_output); + let mut t = Tool::new( + PathBuf::from(compiler), + &self.cached_compiler_family, + &self.cargo_output, + ); if let Some(cc_wrapper) = Self::rustc_wrapper_fallback() { t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper)); } @@ -2660,7 +2683,13 @@ impl Build { Err(_) => PathBuf::from("nvcc"), Ok(nvcc) => PathBuf::from(&*nvcc), }; - let mut nvcc_tool = Tool::with_features(nvcc, None, self.cuda, &self.cargo_output); + let mut nvcc_tool = Tool::with_features( + nvcc, + None, + self.cuda, + &self.cached_compiler_family, + &self.cargo_output, + ); nvcc_tool .args .push(format!("-ccbin={}", tool.path.display()).into()); @@ -3553,16 +3582,27 @@ impl Default for Build { } impl Tool { - fn new(path: PathBuf, cargo_output: &CargoOutput) -> Self { - Tool::with_features(path, None, false, cargo_output) + fn new( + path: PathBuf, + cached_compiler_family: &Mutex, ToolFamily>>, + cargo_output: &CargoOutput, + ) -> Self { + Self::with_features(path, None, false, cached_compiler_family, cargo_output) } fn with_clang_driver( path: PathBuf, clang_driver: Option<&str>, + cached_compiler_family: &Mutex, ToolFamily>>, cargo_output: &CargoOutput, ) -> Self { - Self::with_features(path, clang_driver, false, cargo_output) + Self::with_features( + path, + clang_driver, + false, + cached_compiler_family, + cargo_output, + ) } #[cfg(windows)] @@ -3585,9 +3625,10 @@ impl Tool { path: PathBuf, clang_driver: Option<&str>, cuda: bool, + cached_compiler_family: &Mutex, ToolFamily>>, cargo_output: &CargoOutput, ) -> Self { - fn detect_family(path: &Path, cargo_output: &CargoOutput) -> ToolFamily { + fn detect_family_inner(path: &Path, cargo_output: &CargoOutput) -> ToolFamily { let mut cmd = Command::new(path); cmd.arg("--version"); @@ -3620,6 +3661,18 @@ impl Tool { ToolFamily::Gnu } } + let detect_family = |path: &Path| -> ToolFamily { + if let Some(family) = cached_compiler_family.lock().unwrap().get(path) { + return *family; + } + + let family = detect_family_inner(path, cargo_output); + cached_compiler_family + .lock() + .unwrap() + .insert(path.into(), family); + family + }; // Try to detect family of the tool from its name, falling back to Gnu. let family = if let Some(fname) = path.file_name().and_then(|p| p.to_str()) { @@ -3633,10 +3686,10 @@ impl Tool { _ => ToolFamily::Clang, } } else { - detect_family(&path, cargo_output) + detect_family(&path) } } else { - detect_family(&path, cargo_output) + detect_family(&path) }; Tool {