Skip to content

Commit 31ef083

Browse files
authored
Correctly infer ranlib/ar from cross-gcc (#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 won't work. I'm not sure why the code that existed was written the way it was -- I don't know of any GCC toolchains where the `-gcc` is stripped out in the name of ranlib/ar. The file listing on Debian's [GCC 6.x] and [GCC 10.x] all show the binaries using the `$target-gnu-gcc-$bin` format, as does Arch Linux's [GCC 12.x]. This error appears to also be present in the `cc` crate. There, the `-gcc` prefix is always stripped ([1][cc1], [2][cc2]), and then `-ar` is [appended]. But its saving grace is that it also checks if the resulting binary name is executable, and if it isn't falls back to the default AR instead, which means the bad heuristic is likely often masked by the presence of another working default AR. This crate does not (but perhaps it should?). [GCC 6.x]: https://packages.debian.org/stretch/gcc [GCC 10.x]: https://packages.debian.org/stable/devel/gcc [GCC 12.x]: https://archlinux.org/packages/core/x86_64/gcc/ [cc1]: https://github.com/rust-lang/cc-rs/blob/8daff16ce11e2753961c9b0ce3398fdeada6d941/src/lib.rs#L2623-L2626 [cc2]: https://github.com/rust-lang/cc-rs/blob/8daff16ce11e2753961c9b0ce3398fdeada6d941/src/lib.rs#L2769 [appended]: https://github.com/rust-lang/cc-rs/blob/8daff16ce11e2753961c9b0ce3398fdeada6d941/src/lib.rs#L2602-L2604
1 parent 048d5f5 commit 31ef083

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,16 @@ 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`
349+
// we're doing something like `foo-gcc` route that to `foo-gcc-ranlib`
350350
// as well.
351351
if path.ends_with("-gcc") && !target.contains("unknown-linux-musl") {
352-
let path = &path[..path.len() - 4];
352+
let suffix = &path[path.len() - 4..];
353+
let path = &path[..path.len() - suffix.len()];
353354
if env::var_os("RANLIB").is_none() {
354-
configure.env("RANLIB", format!("{}-ranlib", path));
355+
configure.env("RANLIB", format!("{}{}-ranlib", path, suffix));
355356
}
356357
if env::var_os("AR").is_none() {
357-
configure.env("AR", format!("{}-ar", path));
358+
configure.env("AR", format!("{}{}-ar", path, suffix));
358359
}
359360
}
360361

0 commit comments

Comments
 (0)