Skip to content

Commit

Permalink
Fix archiver detection for musl cross compilation
Browse files Browse the repository at this point in the history
Fixed #1399
  • Loading branch information
NobodyXu authored Feb 15, 2025
1 parent 0dd683c commit 1005cd7
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
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,33 @@ 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"]
.into_iter()
.filter_map(|infix| {
let target_p = format!("{prefix}{infix}-{tool}");
let status = Command::new(&target_p).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 1005cd7

Please sign in to comment.