Skip to content

Commit

Permalink
Fix archiver detection for musl cross compilation (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyXu authored Feb 24, 2025
1 parent 9731605 commit e6ae39a
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ use std::io::{self, Write};
use std::path::{Component, Path, PathBuf};
#[cfg(feature = "parallel")]
use std::process::Child;
use std::process::Command;
use std::process::{Command, Stdio};
use std::sync::{
atomic::{AtomicU8, Ordering::Relaxed},
Arc, RwLock,
Expand Down Expand Up @@ -3226,7 +3226,6 @@ impl Build {
}
});

let default = tool.to_string();
let tool = match tool_opt {
Some(t) => t,
None => {
Expand Down Expand Up @@ -3287,32 +3286,39 @@ impl Build {
self.cmd(&name)
} else if self.get_is_cross_compile()? {
match self.prefix_for_target(&self.get_raw_target()?) {
Some(p) => {
Some(prefix) => {
// GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both.
// Prefer -ar if it exists, as builds of `-gcc-ar` have been observed to be
// outright broken (such as when targeting freebsd with `--disable-lto`
// toolchain where the archiver attempts to load the LTO plugin anyway but
// fails to find one).
//
// The same applies to ranlib.
let mut chosen = default;
for &infix in &["", "-gcc"] {
let target_p = format!("{}{}-{}", p, infix, tool);
if Command::new(&target_p).output().is_ok() {
chosen = target_p;
break;
}
}
let chosen = ["", "-gcc"]
.iter()
.filter_map(|infix| {
let target_p = format!("{prefix}{infix}-{tool}");
let status = Command::new(&target_p)
.arg("--version")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.ok()?;
status.success().then_some(target_p)
})
.next()
.unwrap_or_else(|| tool.to_string());
name = chosen.into();
self.cmd(&name)
}
None => {
name = default.into();
name = tool.into();
self.cmd(&name)
}
}
} else {
name = default.into();
name = tool.into();
self.cmd(&name)
}
}
Expand Down

0 comments on commit e6ae39a

Please sign in to comment.