Skip to content

Commit cbe79c9

Browse files
committed
fix: simplify fn detect_compiler
1 parent 05ab1ab commit cbe79c9

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/compiler/compiler.rs

+29-23
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ fn is_rustc_like<P: AsRef<Path>>(p: P) -> bool {
983983
/// Returns true if the given path looks like a c compiler program
984984
///
985985
/// This does not check c compilers, it only report programs that are definitely not rustc
986-
fn is_like_c_compiler<P: AsRef<Path>>(p: P) -> bool {
986+
fn is_known_c_compiler<P: AsRef<Path>>(p: P) -> bool {
987987
matches!(
988988
p.as_ref()
989989
.file_stem()
@@ -1020,49 +1020,52 @@ where
10201020
trace!("detect_compiler: {}", executable.display());
10211021
// First, see if this looks like rustc.
10221022

1023-
let mut must_be_rust = false;
1024-
let mut rustc_executable = executable.to_path_buf();
1025-
if is_rustc_like(executable) {
1026-
must_be_rust = true;
1023+
let maybe_rustc_executable = if is_rustc_like(executable) {
1024+
Some(executable.to_path_buf())
10271025
} else if env.iter().any(|(k, _)| k == OsStr::new("CARGO")) {
10281026
// If not, detect the scenario where cargo is configured to wrap rustc with something other than sccache.
10291027
// This happens when sccache is used as a `RUSTC_WRAPPER` and another tool is used as a
10301028
// `RUSTC_WORKSPACE_WRAPPER`. In that case rustc will be the first argument rather than the command.
10311029
//
10321030
// The check for the `CARGO` env acts as a guardrail against false positives.
10331031
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
1034-
if let Some(arg1) = args.iter().next() {
1035-
if is_rustc_like(arg1) {
1036-
must_be_rust = true;
1037-
rustc_executable = PathBuf::from(arg1)
1038-
}
1039-
}
1032+
args.iter()
1033+
.next()
1034+
.filter(|arg1| is_rustc_like(arg1))
1035+
.map(PathBuf::from)
1036+
} else {
1037+
None
10401038
};
10411039

10421040
let pool = pool.clone();
1043-
if !must_be_rust && is_like_c_compiler(&rustc_executable) {
1044-
let executable = executable.to_owned();
1041+
1042+
let rustc_executable = if let Some(ref rustc_executable) = maybe_rustc_executable {
1043+
rustc_executable
1044+
} else if is_known_c_compiler(executable) {
10451045
let cc = detect_c_compiler(creator, executable, args, env.to_vec(), pool).await;
10461046
return cc.map(|c| (c, None));
1047-
}
1047+
} else {
1048+
// Even if it does not look like rustc like it might still be rustc driver
1049+
// so we do full check
1050+
executable
1051+
};
10481052

1049-
// Even if it does not look like rustc like it might still be rustc driver
1050-
// so we do full check
10511053
match resolve_rust_compiler(
10521054
creator.clone(),
10531055
executable,
1054-
rustc_executable,
1056+
rustc_executable.to_path_buf(),
10551057
env,
1056-
cwd.to_owned(),
1058+
cwd.to_path_buf(),
10571059
dist_archive,
10581060
pool.clone(),
10591061
)
10601062
.await
10611063
{
10621064
Ok(res) => Ok(res),
10631065
Err(e) => {
1064-
if !must_be_rust {
1065-
let executable = executable.to_owned();
1066+
// in case we attempted to test for rustc while it didn't look like it, fallback to c compiler detection one lat time
1067+
if maybe_rustc_executable.is_none() {
1068+
let executable = executable.to_path_buf();
10661069
let cc = detect_c_compiler(creator, executable, args, env.to_vec(), pool).await;
10671070
cc.map(|c| (c, None))
10681071
} else {
@@ -1201,15 +1204,16 @@ counted_array!(static ARGS: [ArgInfo<ArgData>; _] = [
12011204
take_arg!("-ccbin", OsString, CanBeSeparated('='), Detect_PassThrough),
12021205
]);
12031206

1204-
async fn detect_c_compiler<T>(
1207+
async fn detect_c_compiler<T, P>(
12051208
creator: T,
1206-
executable: PathBuf,
1209+
executable: P,
12071210
arguments: &[OsString],
12081211
env: Vec<(OsString, OsString)>,
12091212
pool: tokio::runtime::Handle,
12101213
) -> Result<Box<dyn Compiler<T>>>
12111214
where
12121215
T: CommandCreatorSync,
1216+
P: AsRef<Path>,
12131217
{
12141218
trace!("detect_c_compiler");
12151219

@@ -1261,7 +1265,8 @@ compiler_version=__VERSION__
12611265
.to_vec();
12621266
let (tempdir, src) = write_temp_file(&pool, "testfile.c".as_ref(), test).await?;
12631267

1264-
let mut cmd = creator.clone().new_command_sync(&executable);
1268+
let executable = executable.as_ref();
1269+
let mut cmd = creator.clone().new_command_sync(executable);
12651270
cmd.stdout(Stdio::piped())
12661271
.stderr(Stdio::piped())
12671272
.envs(env.iter().map(|s| (&s.0, &s.1)));
@@ -1302,6 +1307,7 @@ compiler_version=__VERSION__
13021307
}
13031308
});
13041309
if let Some(kind) = lines.next() {
1310+
let executable = executable.to_owned();
13051311
let version = lines
13061312
.next()
13071313
// In case the compiler didn't expand the macro.

0 commit comments

Comments
 (0)