Skip to content

Commit b9ddf27

Browse files
authored
Add a way to not use bindgen (#499)
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 <[email protected]>
1 parent 2292ba4 commit b9ddf27

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ jobs:
7575
- uses: actions/checkout@v2
7676
- run: which go && go version && which cargo && cargo version && clang --version && openssl version
7777
- run: scripts/reset-submodule.cmd
78-
- run: cargo build --no-default-features
79-
- run: cargo build --no-default-features --features protobuf-codec
80-
- run: cargo build --no-default-features --features prost-codec
78+
- run: cargo build --no-default-features --features use-bindgen
79+
- run: cargo build --no-default-features --features "protobuf-codec use-bindgen"
80+
- run: cargo build --no-default-features --features "prost-codec use-bindgen"
8181
- run: cargo build
8282
- run: cargo test --all
8383

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ autoexamples = false
1717
all-features = true
1818

1919
[dependencies]
20-
grpcio-sys = { path = "grpc-sys", version = "0.7" }
20+
grpcio-sys = { path = "grpc-sys", version = "0.7", default-features = false }
2121
libc = "0.2"
2222
futures = "0.3"
2323
protobuf = { version = "2.0", optional = true }
@@ -30,13 +30,14 @@ parking_lot = "0.11"
3030
members = ["proto", "benchmark", "compiler", "interop", "tests-and-examples"]
3131

3232
[features]
33-
default = ["protobuf-codec", "secure"]
33+
default = ["protobuf-codec", "secure", "use-bindgen"]
3434
protobuf-codec = ["protobuf"]
3535
prost-codec = ["prost", "bytes"]
3636
secure = ["grpcio-sys/secure"]
3737
openssl = ["secure", "grpcio-sys/openssl"]
3838
openssl-vendored = ["secure", "grpcio-sys/openssl-vendored"]
3939
no-omit-frame-pointer = ["grpcio-sys/no-omit-frame-pointer"]
40+
use-bindgen = ["grpcio-sys/use-bindgen"]
4041

4142
[profile.release]
4243
debug = true

grpc-sys/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,21 @@ openssl-sys = { version = "0.9", optional = true, features = ["vendored"] }
5353
libz-sys = { version = "1.0.25", features = ["static"] }
5454

5555
[features]
56-
default = []
56+
default = ["use-bindgen"]
5757
secure = []
5858
openssl = ["secure"]
5959
openssl-vendored = ["openssl", "openssl-sys"]
6060
no-omit-frame-pointer = []
61+
# If this feature is disabled, bindgen will not be used and the previously generated bindings will
62+
# be compiled instead. This only work for the supported targets and will make compilation fails for
63+
# the other ones.
64+
use-bindgen = ["bindgen"]
6165

6266
[build-dependencies]
6367
cc = "1.0"
6468
cmake = "0.1"
6569
pkg-config = "0.3"
6670
walkdir = "2.2.9"
6771
# Because of rust-lang/cargo#5237, bindgen should not be upgraded util a minor or major release.
68-
bindgen = { version = "0.56.0", default-features = false, features = ["runtime"] }
72+
bindgen = { version = "0.56.0", default-features = false, optional = true, features = ["runtime"] }
6973
boringssl-src = "0.1.0"

grpc-sys/build.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,18 @@ fn get_env(name: &str) -> Option<String> {
267267

268268
// Generate the bindings to grpc C-core.
269269
// Try to disable the generation of platform-related bindings.
270-
fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) {
270+
#[cfg(feature = "use-bindgen")]
271+
fn bindgen_grpc(file_path: &PathBuf) {
272+
// create a config to generate binding file
273+
let mut config = bindgen::Builder::default();
274+
if cfg!(feature = "secure") {
275+
config = config.clang_arg("-DGRPC_SYS_SECURE");
276+
}
277+
278+
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
279+
config = config.clang_arg("-D _WIN32_WINNT=0x600");
280+
}
281+
271282
// Search header files with API interface
272283
let mut headers = Vec::new();
273284
for result in WalkDir::new(Path::new("./grpc/include")) {
@@ -333,28 +344,36 @@ fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) {
333344
// Determine if need to update bindings. Supported platforms do not
334345
// need to be updated by default unless the UPDATE_BIND is specified.
335346
// Other platforms use bindgen to generate the bindings every time.
336-
fn config_binding_path(config: bindgen::Builder) {
337-
let file_path: PathBuf;
347+
fn config_binding_path() {
338348
let target = env::var("TARGET").unwrap();
339-
match target.as_str() {
349+
let file_path: PathBuf = match target.as_str() {
340350
"x86_64-unknown-linux-gnu" | "aarch64-unknown-linux-gnu" => {
341351
// Cargo treats nonexistent files changed, so we only emit the rerun-if-changed
342352
// directive when we expect the target-specific pre-generated binding file to be
343353
// present.
344354
println!("cargo:rerun-if-changed=bindings/{}-bindings.rs", &target);
345355

346-
file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
356+
let file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
347357
.join("bindings")
348358
.join(format!("{}-bindings.rs", &target));
349-
if env::var("UPDATE_BIND").map(|s| s == "1").unwrap_or(false) {
350-
bindgen_grpc(config, &file_path);
359+
360+
#[cfg(feature = "use-bindgen")]
361+
if env::var("UPDATE_BIND").is_ok() {
362+
bindgen_grpc(&file_path);
351363
}
364+
365+
file_path
352366
}
353367
_ => {
354-
file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");
355-
bindgen_grpc(config, &file_path);
368+
let file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");
369+
370+
#[cfg(feature = "use-bindgen")]
371+
bindgen_grpc(&file_path);
372+
373+
file_path
356374
}
357375
};
376+
358377
println!(
359378
"cargo:rustc-env=BINDING_PATH={}",
360379
file_path.to_str().unwrap()
@@ -368,12 +387,9 @@ fn main() {
368387

369388
// create a builder to compile grpc_wrap.cc
370389
let mut cc = cc::Build::new();
371-
// create a config to generate binding file
372-
let mut bind_config = bindgen::Builder::default();
373390

374391
let library = if cfg!(feature = "secure") {
375392
cc.define("GRPC_SYS_SECURE", None);
376-
bind_config = bind_config.clang_arg("-DGRPC_SYS_SECURE");
377393
"grpc"
378394
} else {
379395
"grpc_unsecure"
@@ -382,7 +398,6 @@ fn main() {
382398
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
383399
// At lease vista
384400
cc.define("_WIN32_WINNT", Some("0x600"));
385-
bind_config = bind_config.clang_arg("-D _WIN32_WINNT=0x600");
386401
}
387402

388403
if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") {
@@ -403,5 +418,5 @@ fn main() {
403418
cc.warnings_into_errors(true);
404419
cc.compile("libgrpc_wrap.a");
405420

406-
config_binding_path(bind_config);
421+
config_binding_path();
407422
}

0 commit comments

Comments
 (0)