From c13be3c00c6d03eb88c7f56b8d9d4a587cbc3f7c Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 26 Oct 2022 09:38:56 -0700 Subject: [PATCH 1/2] Correctly infer ranlib/ar for cross-gcc toolchains A second try at #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. --- src/lib.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b50247de..53801dd9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -346,15 +346,25 @@ impl Build { configure.env_remove("CROSS_COMPILE"); // Infer ar/ranlib tools from cross compilers if the it looks like - // we're doing something like `foo-gcc` route that to `foo-ranlib` - // as well. + // we're doing something like `foo-gcc` route that to `foo-gcc-ranlib` + // or `foo-ranlib` (for `binutils`) as well. if path.ends_with("-gcc") && !target.contains("unknown-linux-musl") { - let path = &path[..path.len() - 4]; - if env::var_os("RANLIB").is_none() { - configure.env("RANLIB", format!("{}-ranlib", path)); - } - if env::var_os("AR").is_none() { - configure.env("AR", format!("{}-ar", path)); + let suffix = &path[path.len() - 4..]; + let path = &path[..path.len() - suffix.len()]; + for &tool in &["RANLIB", "AR"] { + if env::var_os(tool).is_some() { + // Respect what came before. + break; + } + + for &infix in &[suffix, ""] { + let candidate = format!("{}{}-{}", path, infix, tool.to_lowercase()); + // Only choose a tool if it's actually executable + if Command::new(&candidate).output().is_ok() { + configure.env(tool, candidate); + break; + } + } } } From 26830576bd5e9354dc723707b49a0f8792d323cd Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Mon, 31 Oct 2022 14:33:51 -0700 Subject: [PATCH 2/2] Prefer non-gcc-ar/ranlib Sometimes `gcc-ar` is broken, so when both are available we should prefer non-gcc-ar. See also rust-lang/cc-rs#741. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 53801dd9..aad8015d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -357,7 +357,7 @@ impl Build { break; } - for &infix in &[suffix, ""] { + for &infix in &["", suffix] { let candidate = format!("{}{}-{}", path, infix, tool.to_lowercase()); // Only choose a tool if it's actually executable if Command::new(&candidate).output().is_ok() {