From d990c5cd9b05c8388dcd60d2f4386a7dcd168de1 Mon Sep 17 00:00:00 2001 From: harryoooooooooo Date: Tue, 2 Jan 2024 17:52:52 +0800 Subject: [PATCH] grpc-sys: Fix build failure on uncommon linux build targets In the current implemntation, if the TARGET is not listed in build.rs ( e.g. x86_64-cros-linux-gnu) but target_os/target_arch matches (x86_64|aarch64)/(macos|linux), then the path $OUT_DIR/grpc-bindings.rs will be selected but actually not generated. This patch fixes the issue by using the same condition to detect the supported platforms; If supported, select pre-generated path, otherwise select OUT_DIR path and generate the bindings. --- grpc-sys/build.rs | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/grpc-sys/build.rs b/grpc-sys/build.rs index 832a3a38..cd4a75c9 100644 --- a/grpc-sys/build.rs +++ b/grpc-sys/build.rs @@ -481,24 +481,24 @@ fn bindgen_grpc(file_path: &Path) { // need to be updated by default unless the _gen-bindings feature is specified. // Other platforms use bindgen to generate the bindings every time. fn config_binding_path() { - let target = env::var("TARGET").unwrap(); - let file_path: PathBuf = match target.as_str() { - "x86_64-unknown-linux-gnu" - | "x86_64-unknown-linux-musl" - | "aarch64-unknown-linux-musl" - | "aarch64-unknown-linux-gnu" - | "x86_64-apple-darwin" - | "aarch64-apple-darwin" => { - // Cargo treats nonexistent files changed, so we only emit the rerun-if-changed - // directive when we expect the target-specific pre-generated binding file to be - // present. - println!("cargo:rerun-if-changed=bindings/bindings.rs"); - - PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) - .join("bindings") - .join("bindings.rs") - } - _ => PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs"), + let file_path: PathBuf; + + #[cfg(not(any( + feature = "_gen-bindings", + not(all( + any(target_os = "linux", target_os = "macos"), + any(target_arch = "x86_64", target_arch = "aarch64") + )) + )))] + { + // Cargo treats nonexistent files changed, so we only emit the rerun-if-changed + // directive when we expect the target-specific pre-generated binding file to be + // present. + println!("cargo:rerun-if-changed=bindings/bindings.rs"); + + file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) + .join("bindings") + .join("bindings.rs") }; #[cfg(any( @@ -509,6 +509,7 @@ fn config_binding_path() { )) ))] { + file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs"); // On some system (like Windows), stack size of main thread may // be too small. let f = file_path.clone();