Skip to content

Commit

Permalink
Cache compiler versions detected
Browse files Browse the repository at this point in the history
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Jan 29, 2024
1 parent 984406f commit 5b483a7
Showing 1 changed file with 65 additions and 12 deletions.
77 changes: 65 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub struct Build {
env_cache: Arc<Mutex<HashMap<String, Option<Arc<str>>>>>,
apple_sdk_root_cache: Arc<Mutex<HashMap<String, OsString>>>,
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 @@ -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(),
}
}

Expand Down Expand Up @@ -2533,7 +2535,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 @@ -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));
}
Expand All @@ -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
Expand Down Expand Up @@ -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));
}
Expand All @@ -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());
Expand Down Expand Up @@ -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<HashMap<Box<Path>, 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<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 @@ -3585,9 +3625,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 @@ -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()) {
Expand All @@ -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 {
Expand Down

0 comments on commit 5b483a7

Please sign in to comment.