Skip to content

Commit 5b483a7

Browse files
committed
Cache compiler versions detected
Signed-off-by: Jiahao XU <[email protected]>
1 parent 984406f commit 5b483a7

File tree

1 file changed

+65
-12
lines changed

1 file changed

+65
-12
lines changed

src/lib.rs

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub struct Build {
133133
env_cache: Arc<Mutex<HashMap<String, Option<Arc<str>>>>>,
134134
apple_sdk_root_cache: Arc<Mutex<HashMap<String, OsString>>>,
135135
emit_rerun_if_env_changed: bool,
136+
cached_compiler_family: Arc<Mutex<HashMap<Box<Path>, ToolFamily>>>,
136137
}
137138

138139
/// Represents the types of errors that may occur while using cc-rs.
@@ -339,6 +340,7 @@ impl Build {
339340
env_cache: Arc::new(Mutex::new(HashMap::new())),
340341
apple_sdk_root_cache: Arc::new(Mutex::new(HashMap::new())),
341342
emit_rerun_if_env_changed: true,
343+
cached_compiler_family: Arc::default(),
342344
}
343345
}
344346

@@ -2533,7 +2535,11 @@ impl Build {
25332535

25342536
fn get_base_compiler(&self) -> Result<Tool, Error> {
25352537
if let Some(c) = &self.compiler {
2536-
return Ok(Tool::new((**c).to_owned(), &self.cargo_output));
2538+
return Ok(Tool::new(
2539+
(**c).to_owned(),
2540+
&self.cached_compiler_family,
2541+
&self.cargo_output,
2542+
));
25372543
}
25382544
let host = self.get_host()?;
25392545
let target = self.get_target()?;
@@ -2569,7 +2575,12 @@ impl Build {
25692575
// semi-buggy build scripts which are shared in
25702576
// makefiles/configure scripts (where spaces are far more
25712577
// lenient)
2572-
let mut t = Tool::with_clang_driver(tool, driver_mode, &self.cargo_output);
2578+
let mut t = Tool::with_clang_driver(
2579+
tool,
2580+
driver_mode,
2581+
&self.cached_compiler_family,
2582+
&self.cargo_output,
2583+
);
25732584
if let Some(cc_wrapper) = wrapper {
25742585
t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper));
25752586
}
@@ -2583,12 +2594,20 @@ impl Build {
25832594
let tool = if self.cpp { "em++" } else { "emcc" };
25842595
// Windows uses bat file so we have to be a bit more specific
25852596
if cfg!(windows) {
2586-
let mut t = Tool::new(PathBuf::from("cmd"), &self.cargo_output);
2597+
let mut t = Tool::new(
2598+
PathBuf::from("cmd"),
2599+
&self.cached_compiler_family,
2600+
&self.cargo_output,
2601+
);
25872602
t.args.push("/c".into());
25882603
t.args.push(format!("{}.bat", tool).into());
25892604
Some(t)
25902605
} else {
2591-
Some(Tool::new(PathBuf::from(tool), &self.cargo_output))
2606+
Some(Tool::new(
2607+
PathBuf::from(tool),
2608+
&self.cached_compiler_family,
2609+
&self.cargo_output,
2610+
))
25922611
}
25932612
} else {
25942613
None
@@ -2643,7 +2662,11 @@ impl Build {
26432662
default.to_string()
26442663
};
26452664

2646-
let mut t = Tool::new(PathBuf::from(compiler), &self.cargo_output);
2665+
let mut t = Tool::new(
2666+
PathBuf::from(compiler),
2667+
&self.cached_compiler_family,
2668+
&self.cargo_output,
2669+
);
26472670
if let Some(cc_wrapper) = Self::rustc_wrapper_fallback() {
26482671
t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper));
26492672
}
@@ -2660,7 +2683,13 @@ impl Build {
26602683
Err(_) => PathBuf::from("nvcc"),
26612684
Ok(nvcc) => PathBuf::from(&*nvcc),
26622685
};
2663-
let mut nvcc_tool = Tool::with_features(nvcc, None, self.cuda, &self.cargo_output);
2686+
let mut nvcc_tool = Tool::with_features(
2687+
nvcc,
2688+
None,
2689+
self.cuda,
2690+
&self.cached_compiler_family,
2691+
&self.cargo_output,
2692+
);
26642693
nvcc_tool
26652694
.args
26662695
.push(format!("-ccbin={}", tool.path.display()).into());
@@ -3553,16 +3582,27 @@ impl Default for Build {
35533582
}
35543583

35553584
impl Tool {
3556-
fn new(path: PathBuf, cargo_output: &CargoOutput) -> Self {
3557-
Tool::with_features(path, None, false, cargo_output)
3585+
fn new(
3586+
path: PathBuf,
3587+
cached_compiler_family: &Mutex<HashMap<Box<Path>, ToolFamily>>,
3588+
cargo_output: &CargoOutput,
3589+
) -> Self {
3590+
Self::with_features(path, None, false, cached_compiler_family, cargo_output)
35583591
}
35593592

35603593
fn with_clang_driver(
35613594
path: PathBuf,
35623595
clang_driver: Option<&str>,
3596+
cached_compiler_family: &Mutex<HashMap<Box<Path>, ToolFamily>>,
35633597
cargo_output: &CargoOutput,
35643598
) -> Self {
3565-
Self::with_features(path, clang_driver, false, cargo_output)
3599+
Self::with_features(
3600+
path,
3601+
clang_driver,
3602+
false,
3603+
cached_compiler_family,
3604+
cargo_output,
3605+
)
35663606
}
35673607

35683608
#[cfg(windows)]
@@ -3585,9 +3625,10 @@ impl Tool {
35853625
path: PathBuf,
35863626
clang_driver: Option<&str>,
35873627
cuda: bool,
3628+
cached_compiler_family: &Mutex<HashMap<Box<Path>, ToolFamily>>,
35883629
cargo_output: &CargoOutput,
35893630
) -> Self {
3590-
fn detect_family(path: &Path, cargo_output: &CargoOutput) -> ToolFamily {
3631+
fn detect_family_inner(path: &Path, cargo_output: &CargoOutput) -> ToolFamily {
35913632
let mut cmd = Command::new(path);
35923633
cmd.arg("--version");
35933634

@@ -3620,6 +3661,18 @@ impl Tool {
36203661
ToolFamily::Gnu
36213662
}
36223663
}
3664+
let detect_family = |path: &Path| -> ToolFamily {
3665+
if let Some(family) = cached_compiler_family.lock().unwrap().get(path) {
3666+
return *family;
3667+
}
3668+
3669+
let family = detect_family_inner(path, cargo_output);
3670+
cached_compiler_family
3671+
.lock()
3672+
.unwrap()
3673+
.insert(path.into(), family);
3674+
family
3675+
};
36233676

36243677
// Try to detect family of the tool from its name, falling back to Gnu.
36253678
let family = if let Some(fname) = path.file_name().and_then(|p| p.to_str()) {
@@ -3633,10 +3686,10 @@ impl Tool {
36333686
_ => ToolFamily::Clang,
36343687
}
36353688
} else {
3636-
detect_family(&path, cargo_output)
3689+
detect_family(&path)
36373690
}
36383691
} else {
3639-
detect_family(&path, cargo_output)
3692+
detect_family(&path)
36403693
};
36413694

36423695
Tool {

0 commit comments

Comments
 (0)