Skip to content

Commit

Permalink
Cache compiler versions detected (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyXu authored Feb 16, 2024
1 parent df59d43 commit 8bed330
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
41 changes: 35 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ pub struct Build {
apple_sdk_root_cache: Arc<Mutex<HashMap<String, OsString>>>,
apple_versions_cache: Arc<Mutex<HashMap<String, String>>>,
emit_rerun_if_env_changed: bool,
cached_compiler_family: Arc<Mutex<HashMap<Box<Path>, ToolFamily>>>,
}

/// Represents the types of errors that may occur while using cc-rs.
Expand Down Expand Up @@ -406,6 +407,7 @@ impl Build {
apple_sdk_root_cache: Arc::new(Mutex::new(HashMap::new())),
apple_versions_cache: Arc::new(Mutex::new(HashMap::new())),
emit_rerun_if_env_changed: true,
cached_compiler_family: Arc::default(),
}
}

Expand Down Expand Up @@ -2603,7 +2605,11 @@ impl Build {

fn get_base_compiler(&self) -> Result<Tool, Error> {
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()?;
Expand Down Expand Up @@ -2639,7 +2645,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));
}
Expand All @@ -2653,12 +2664,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
Expand Down Expand Up @@ -2713,7 +2732,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));
}
Expand All @@ -2730,7 +2753,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());
Expand Down
38 changes: 32 additions & 6 deletions src/tool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{
collections::HashMap,
ffi::OsString,
path::{Path, PathBuf},
process::Command,
sync::Mutex,
};

use crate::command_helpers::{run_output, CargoOutput};
Expand All @@ -28,16 +30,27 @@ pub struct Tool {
}

impl Tool {
pub(crate) fn new(path: PathBuf, cargo_output: &CargoOutput) -> Self {
Tool::with_features(path, None, false, cargo_output)
pub(crate) fn new(
path: PathBuf,
cached_compiler_family: &Mutex<HashMap<Box<Path>, ToolFamily>>,
cargo_output: &CargoOutput,
) -> Self {
Self::with_features(path, None, false, cached_compiler_family, cargo_output)
}

pub(crate) fn with_clang_driver(
path: PathBuf,
clang_driver: Option<&str>,
cached_compiler_family: &Mutex<HashMap<Box<Path>, 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)]
Expand All @@ -60,9 +73,10 @@ impl Tool {
path: PathBuf,
clang_driver: Option<&str>,
cuda: bool,
cached_compiler_family: &Mutex<HashMap<Box<Path>, 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");

Expand Down Expand Up @@ -95,6 +109,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()) {
Expand All @@ -108,10 +134,10 @@ impl Tool {
_ => ToolFamily::Clang,
}
} else {
detect_family(&path, cargo_output)
detect_family(&path)
}
} else {
detect_family(&path, cargo_output)
detect_family(&path)
};

Tool {
Expand Down

0 comments on commit 8bed330

Please sign in to comment.