Skip to content

Commit 32fcbe3

Browse files
author
Jon Gjengset
committed
Use cc for ar/ranlib detection
Note that `Command::get_program` and `Command::get_args` both stabilized in Rust 1.57.0, and so implicitly bump this crate's MSRV. Depends on rust-lang/cc-rs#763. Replaces #164.
1 parent 26d1a05 commit 32fcbe3

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ exclude = ['target']
4343

4444
[dependencies]
4545
cc = "1.0"
46+
47+
[patch.crates-io]
48+
cc = { git = "https://github.com/jonhoo/cc-rs.git", branch = "ranlib" }

src/lib.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate cc;
22

33
use std::env;
4+
use std::ffi::OsStr;
45
use std::fs;
56
use std::path::{Path, PathBuf};
67
use std::process::Command;
@@ -345,17 +346,25 @@ impl Build {
345346
// prefix, we unset `CROSS_COMPILE` for `./Configure`.
346347
configure.env_remove("CROSS_COMPILE");
347348

348-
// 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.
351-
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));
358-
}
349+
let ar = cc.get_archiver();
350+
configure.env("AR", ar.get_program());
351+
if ar.get_args().count() == 0 {
352+
// On some platforms (like emscripten on windows), the ar to use may not be a
353+
// single binary, but instead a multi-argument command like `cmd /c emar.bar`.
354+
// We can't convey that through `AR` alone, and so also need to set ARFLAGS.
355+
configure.env(
356+
"ARFLAGS",
357+
ar.get_args().collect::<Vec<_>>().join(OsStr::new(" ")),
358+
);
359+
}
360+
let ranlib = cc.get_ranlib();
361+
configure.env("RANLIB", ranlib.get_program());
362+
if ranlib.get_args().count() == 0 {
363+
// Same thing as for AR -- we may need to set RANLIBFLAGS
364+
configure.env(
365+
"RANLIBFLAGS",
366+
ranlib.get_args().collect::<Vec<_>>().join(OsStr::new(" ")),
367+
);
359368
}
360369

361370
// Make sure we pass extra flags like `-ffunction-sections` and

0 commit comments

Comments
 (0)