Skip to content

Commit 172a1da

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. Backport of alexcrichton#173.
1 parent 4879299 commit 172a1da

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ members = ['testcrate']
3939
exclude = ['target']
4040

4141
[dependencies]
42-
cc = "1.0"
42+
cc = "1.0.79"

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;
@@ -323,17 +324,25 @@ impl Build {
323324
// prefix, we unset `CROSS_COMPILE` for `./Configure`.
324325
configure.env_remove("CROSS_COMPILE");
325326

326-
// Infer ar/ranlib tools from cross compilers if the it looks like
327-
// we're doing something like `foo-gcc` route that to `foo-ranlib`
328-
// as well.
329-
if path.ends_with("-gcc") && !target.contains("unknown-linux-musl") {
330-
let path = &path[..path.len() - 4];
331-
if env::var_os("RANLIB").is_none() {
332-
configure.env("RANLIB", format!("{}-ranlib", path));
333-
}
334-
if env::var_os("AR").is_none() {
335-
configure.env("AR", format!("{}-ar", path));
336-
}
327+
let ar = cc.get_archiver();
328+
configure.env("AR", ar.get_program());
329+
if ar.get_args().count() == 0 {
330+
// On some platforms (like emscripten on windows), the ar to use may not be a
331+
// single binary, but instead a multi-argument command like `cmd /c emar.bar`.
332+
// We can't convey that through `AR` alone, and so also need to set ARFLAGS.
333+
configure.env(
334+
"ARFLAGS",
335+
ar.get_args().collect::<Vec<_>>().join(OsStr::new(" ")),
336+
);
337+
}
338+
let ranlib = cc.get_ranlib();
339+
configure.env("RANLIB", ranlib.get_program());
340+
if ranlib.get_args().count() == 0 {
341+
// Same thing as for AR -- we may need to set RANLIBFLAGS
342+
configure.env(
343+
"RANLIBFLAGS",
344+
ranlib.get_args().collect::<Vec<_>>().join(OsStr::new(" ")),
345+
);
337346
}
338347

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

0 commit comments

Comments
 (0)