From a1fbc42421c6c72f4181b5099c410e554039ee75 Mon Sep 17 00:00:00 2001 From: Hugues de Valon Date: Thu, 19 Nov 2020 14:47:32 +0000 Subject: [PATCH 1/2] Add a way to not use bindgen Fix #489 Add the `use-bindgen` feature which is activated by default. If disabled, then the previously generated bindings will be used instead of generating new ones. It will fail compilation if this feature is disabled for the non supported targets. If enabled, the behaviour is the same as before. Signed-off-by: Hugues de Valon --- Cargo.toml | 4 ++-- grpc-sys/Cargo.toml | 8 ++++++-- grpc-sys/build.rs | 43 +++++++++++++++++++++++++++++-------------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 18457aa83..9114a26d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ autoexamples = false all-features = true [dependencies] -grpcio-sys = { path = "grpc-sys", version = "0.7" } +grpcio-sys = { path = "grpc-sys", version = "0.7", default-features = false } libc = "0.2" futures = "0.3" protobuf = { version = "2.0", optional = true } @@ -30,7 +30,7 @@ parking_lot = "0.11" members = ["proto", "benchmark", "compiler", "interop", "tests-and-examples"] [features] -default = ["protobuf-codec", "secure"] +default = ["protobuf-codec", "secure", "grpcio-sys/use-bindgen"] protobuf-codec = ["protobuf"] prost-codec = ["prost", "bytes"] secure = ["grpcio-sys/secure"] diff --git a/grpc-sys/Cargo.toml b/grpc-sys/Cargo.toml index b19135ecd..60e1d2fe8 100644 --- a/grpc-sys/Cargo.toml +++ b/grpc-sys/Cargo.toml @@ -53,11 +53,15 @@ openssl-sys = { version = "0.9", optional = true, features = ["vendored"] } libz-sys = { version = "1.0.25", features = ["static"] } [features] -default = [] +default = ["use-bindgen"] secure = [] openssl = ["secure"] openssl-vendored = ["openssl", "openssl-sys"] no-omit-frame-pointer = [] +# If this feature is disabled, bindgen will not be used and the previously generated bindings will +# be compiled instead. This only work for the supported targets and will make compilation fails for +# the other ones. +use-bindgen = ["bindgen"] [build-dependencies] cc = "1.0" @@ -65,5 +69,5 @@ cmake = "0.1" pkg-config = "0.3" walkdir = "2.2.9" # Because of rust-lang/cargo#5237, bindgen should not be upgraded util a minor or major release. -bindgen = { version = "0.51.0", default-features = false } +bindgen = { version = "0.51.0", default-features = false, optional = true } boringssl-src = "0.1.0" diff --git a/grpc-sys/build.rs b/grpc-sys/build.rs index 347323939..546433fcc 100644 --- a/grpc-sys/build.rs +++ b/grpc-sys/build.rs @@ -265,7 +265,18 @@ fn get_env(name: &str) -> Option { // Generate the bindings to grpc C-core. // Try to disable the generation of platform-related bindings. -fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) { +#[cfg(feature = "use-bindgen")] +fn bindgen_grpc(file_path: &PathBuf) { + // create a config to generate binding file + let mut config = bindgen::Builder::default(); + if cfg!(feature = "secure") { + config = config.clang_arg("-DGRPC_SYS_SECURE"); + } + + if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") { + config = config.clang_arg("-D _WIN32_WINNT=0x600"); + } + // Search header files with API interface let mut headers = Vec::new(); for result in WalkDir::new(Path::new("./grpc/include")) { @@ -329,28 +340,36 @@ fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) { // Determine if need to update bindings. Supported platforms do not // need to be updated by default unless the UPDATE_BIND is specified. // Other platforms use bindgen to generate the bindings every time. -fn config_binding_path(config: bindgen::Builder) { - let file_path: PathBuf; +fn config_binding_path() { let target = env::var("TARGET").unwrap(); - match target.as_str() { + let file_path: PathBuf = match target.as_str() { "x86_64-unknown-linux-gnu" | "aarch64-unknown-linux-gnu" => { // 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", &target); - file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) + let file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) .join("bindings") .join(format!("{}-bindings.rs", &target)); - if env::var("UPDATE_BIND").map(|s| s == "1").unwrap_or(false) { - bindgen_grpc(config, &file_path); + + #[cfg(feature = "use-bindgen")] + if env::var("UPDATE_BIND").is_ok() { + bindgen_grpc(&file_path); } + + file_path } _ => { - file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs"); - bindgen_grpc(config, &file_path); + let file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs"); + + #[cfg(feature = "use-bindgen")] + bindgen_grpc(&file_path); + + file_path } }; + println!( "cargo:rustc-env=BINDING_PATH={}", file_path.to_str().unwrap() @@ -364,12 +383,9 @@ fn main() { // create a builder to compile grpc_wrap.cc let mut cc = cc::Build::new(); - // create a config to generate binding file - let mut bind_config = bindgen::Builder::default(); let library = if cfg!(feature = "secure") { cc.define("GRPC_SYS_SECURE", None); - bind_config = bind_config.clang_arg("-DGRPC_SYS_SECURE"); "grpc" } else { "grpc_unsecure" @@ -378,7 +394,6 @@ fn main() { if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") { // At lease vista cc.define("_WIN32_WINNT", Some("0x600")); - bind_config = bind_config.clang_arg("-D _WIN32_WINNT=0x600"); } if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") { @@ -399,5 +414,5 @@ fn main() { cc.warnings_into_errors(true); cc.compile("libgrpc_wrap.a"); - config_binding_path(bind_config); + config_binding_path(); } From 89ca200bcb30cf50e38a3a6f583c1448118f9f54 Mon Sep 17 00:00:00 2001 From: Hugues de Valon Date: Fri, 20 Nov 2020 10:09:27 +0000 Subject: [PATCH 2/2] Add top-level Cargo feature Also fix the CI script for Mac Signed-off-by: Hugues de Valon --- .github/workflows/ci.yml | 6 +++--- Cargo.toml | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c4228576..2a07fcfd7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,9 +75,9 @@ jobs: - uses: actions/checkout@v2 - run: which go && go version && which cargo && cargo version && clang --version && openssl version - run: scripts/reset-submodule.cmd - - run: cargo build --no-default-features - - run: cargo build --no-default-features --features protobuf-codec - - run: cargo build --no-default-features --features prost-codec + - run: cargo build --no-default-features --features use-bindgen + - run: cargo build --no-default-features --features "protobuf-codec use-bindgen" + - run: cargo build --no-default-features --features "prost-codec use-bindgen" - run: cargo build - run: cargo test --all diff --git a/Cargo.toml b/Cargo.toml index 9114a26d5..70b1b3996 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,13 +30,14 @@ parking_lot = "0.11" members = ["proto", "benchmark", "compiler", "interop", "tests-and-examples"] [features] -default = ["protobuf-codec", "secure", "grpcio-sys/use-bindgen"] +default = ["protobuf-codec", "secure", "use-bindgen"] protobuf-codec = ["protobuf"] prost-codec = ["prost", "bytes"] secure = ["grpcio-sys/secure"] openssl = ["secure", "grpcio-sys/openssl"] openssl-vendored = ["secure", "grpcio-sys/openssl-vendored"] no-omit-frame-pointer = ["grpcio-sys/no-omit-frame-pointer"] +use-bindgen = ["grpcio-sys/use-bindgen"] [profile.release] debug = true