Skip to content

Commit c13be3c

Browse files
author
Jon Gjengset
committed
Correctly infer ranlib/ar for cross-gcc toolchains
A second try at alexcrichton#163. The GCC convention is that if the toolchain is named `$target-gnu-gcc`, then ranlib and ar will be available as `$target-gnu-gcc-ranlib` and `$target-gnu-gcc-ar` respectively. The code as written would infer them to be `$target-gnu-{ranlib,ar}`, which will only work if the tools from `binutils` (which follow that convention) are on `$PATH`. I've also updated the logic in line with the `cc` crate to check that the inferred `AR`/`RANLIB` is actually executable before setting it as an override. See also rust-lang/cc-rs#736.
1 parent cbbb491 commit c13be3c

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,25 @@ impl Build {
346346
configure.env_remove("CROSS_COMPILE");
347347

348348
// Infer ar/ranlib tools from cross compilers if the it looks like
349-
// we're doing something like `foo-gcc` route that to `foo-ranlib`
350-
// as well.
349+
// we're doing something like `foo-gcc` route that to `foo-gcc-ranlib`
350+
// or `foo-ranlib` (for `binutils`) as well.
351351
if path.ends_with("-gcc") && !target.contains("unknown-linux-musl") {
352-
let path = &path[..path.len() - 4];
353-
if env::var_os("RANLIB").is_none() {
354-
configure.env("RANLIB", format!("{}-ranlib", path));
355-
}
356-
if env::var_os("AR").is_none() {
357-
configure.env("AR", format!("{}-ar", path));
352+
let suffix = &path[path.len() - 4..];
353+
let path = &path[..path.len() - suffix.len()];
354+
for &tool in &["RANLIB", "AR"] {
355+
if env::var_os(tool).is_some() {
356+
// Respect what came before.
357+
break;
358+
}
359+
360+
for &infix in &[suffix, ""] {
361+
let candidate = format!("{}{}-{}", path, infix, tool.to_lowercase());
362+
// Only choose a tool if it's actually executable
363+
if Command::new(&candidate).output().is_ok() {
364+
configure.env(tool, candidate);
365+
break;
366+
}
367+
}
358368
}
359369
}
360370

0 commit comments

Comments
 (0)