Skip to content

grpc-sys: detect failure early #511

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

Merged
merged 2 commits into from
Mar 2, 2021
Merged
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
37 changes: 34 additions & 3 deletions grpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ fn trim_start<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
}
}

/// If cache is stale, remove it to avoid compilation failure.
fn clean_up_stale_cache(cxx_compiler: String) {
// We don't know the cmake output path before it's configured.
let build_dir = format!("{}/build", env::var("OUT_DIR").unwrap());
let path = format!("{}/CMakeCache.txt", build_dir);
let f = match std::fs::File::open(&path) {
Ok(f) => BufReader::new(f),
// It may be an empty directory.
Err(_) => return,
};
let cache_stale = f.lines().any(|l| {
let l = l.unwrap();
trim_start(&l, "CMAKE_CXX_COMPILER:").map_or(false, |s| {
let mut splits = s.splitn(2, '=');
splits.next();
splits.next().map_or(false, |p| p != cxx_compiler)
})
});
// CMake can't handle compiler change well, it will invalidate cache without respecting command
// line settings and result in configuration failure.
// See https://gitlab.kitware.com/cmake/cmake/-/issues/18959.
if cache_stale {
let _ = fs::remove_dir_all(&build_dir);
}
}

fn build_grpc(cc: &mut cc::Build, library: &str) {
prepare_grpc();

Expand All @@ -75,11 +101,16 @@ fn build_grpc(cc: &mut cc::Build, library: &str) {
println!("cargo:rustc-link-lib=framework=CoreFoundation");
}

if let Some(val) = get_env("CXX") {
config.define("CMAKE_CXX_COMPILER", val);
let cxx_compiler = if let Some(val) = get_env("CXX") {
config.define("CMAKE_CXX_COMPILER", val.clone());
val
} else if env::var("CARGO_CFG_TARGET_ENV").unwrap() == "musl" {
config.define("CMAKE_CXX_COMPILER", "g++");
}
"g++".to_owned()
} else {
format!("{}", cc.get_compiler().path().display())
};
clean_up_stale_cache(cxx_compiler);

// Cross-compile support for iOS
match target.as_str() {
Expand Down