Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cc for ar/ranlib detection #180

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ members = ['testcrate']
exclude = ['target']

[dependencies]
cc = "1.0"
cc = "1.0.79"
31 changes: 20 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate cc;

use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -323,17 +324,25 @@ impl Build {
// prefix, we unset `CROSS_COMPILE` for `./Configure`.
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.
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 ar = cc.get_archiver();
configure.env("AR", ar.get_program());
if ar.get_args().count() == 0 {
// On some platforms (like emscripten on windows), the ar to use may not be a
// single binary, but instead a multi-argument command like `cmd /c emar.bar`.
// We can't convey that through `AR` alone, and so also need to set ARFLAGS.
configure.env(
"ARFLAGS",
ar.get_args().collect::<Vec<_>>().join(OsStr::new(" ")),
);
}
let ranlib = cc.get_ranlib();
configure.env("RANLIB", ranlib.get_program());
if ranlib.get_args().count() == 0 {
// Same thing as for AR -- we may need to set RANLIBFLAGS
configure.env(
"RANLIBFLAGS",
ranlib.get_args().collect::<Vec<_>>().join(OsStr::new(" ")),
);
}

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