Skip to content

Commit a1fbc42

Browse files
committed
Add a way to not use bindgen
Fix tikv#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 4112a22 commit a1fbc42

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

Cargo.toml

+2-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,7 +30,7 @@ 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", "grpcio-sys/use-bindgen"]
3434
protobuf-codec = ["protobuf"]
3535
prost-codec = ["prost", "bytes"]
3636
secure = ["grpcio-sys/secure"]

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.51.0", default-features = false }
72+
bindgen = { version = "0.51.0", default-features = false, optional = true }
6973
boringssl-src = "0.1.0"

grpc-sys/build.rs

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

266266
// Generate the bindings to grpc C-core.
267267
// Try to disable the generation of platform-related bindings.
268-
fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) {
268+
#[cfg(feature = "use-bindgen")]
269+
fn bindgen_grpc(file_path: &PathBuf) {
270+
// create a config to generate binding file
271+
let mut config = bindgen::Builder::default();
272+
if cfg!(feature = "secure") {
273+
config = config.clang_arg("-DGRPC_SYS_SECURE");
274+
}
275+
276+
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
277+
config = config.clang_arg("-D _WIN32_WINNT=0x600");
278+
}
279+
269280
// Search header files with API interface
270281
let mut headers = Vec::new();
271282
for result in WalkDir::new(Path::new("./grpc/include")) {
@@ -329,28 +340,36 @@ fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) {
329340
// Determine if need to update bindings. Supported platforms do not
330341
// need to be updated by default unless the UPDATE_BIND is specified.
331342
// Other platforms use bindgen to generate the bindings every time.
332-
fn config_binding_path(config: bindgen::Builder) {
333-
let file_path: PathBuf;
343+
fn config_binding_path() {
334344
let target = env::var("TARGET").unwrap();
335-
match target.as_str() {
345+
let file_path: PathBuf = match target.as_str() {
336346
"x86_64-unknown-linux-gnu" | "aarch64-unknown-linux-gnu" => {
337347
// Cargo treats nonexistent files changed, so we only emit the rerun-if-changed
338348
// directive when we expect the target-specific pre-generated binding file to be
339349
// present.
340350
println!("cargo:rerun-if-changed=bindings/{}-bindings.rs", &target);
341351

342-
file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
352+
let file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
343353
.join("bindings")
344354
.join(format!("{}-bindings.rs", &target));
345-
if env::var("UPDATE_BIND").map(|s| s == "1").unwrap_or(false) {
346-
bindgen_grpc(config, &file_path);
355+
356+
#[cfg(feature = "use-bindgen")]
357+
if env::var("UPDATE_BIND").is_ok() {
358+
bindgen_grpc(&file_path);
347359
}
360+
361+
file_path
348362
}
349363
_ => {
350-
file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");
351-
bindgen_grpc(config, &file_path);
364+
let file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");
365+
366+
#[cfg(feature = "use-bindgen")]
367+
bindgen_grpc(&file_path);
368+
369+
file_path
352370
}
353371
};
372+
354373
println!(
355374
"cargo:rustc-env=BINDING_PATH={}",
356375
file_path.to_str().unwrap()
@@ -364,12 +383,9 @@ fn main() {
364383

365384
// create a builder to compile grpc_wrap.cc
366385
let mut cc = cc::Build::new();
367-
// create a config to generate binding file
368-
let mut bind_config = bindgen::Builder::default();
369386

370387
let library = if cfg!(feature = "secure") {
371388
cc.define("GRPC_SYS_SECURE", None);
372-
bind_config = bind_config.clang_arg("-DGRPC_SYS_SECURE");
373389
"grpc"
374390
} else {
375391
"grpc_unsecure"
@@ -378,7 +394,6 @@ fn main() {
378394
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
379395
// At lease vista
380396
cc.define("_WIN32_WINNT", Some("0x600"));
381-
bind_config = bind_config.clang_arg("-D _WIN32_WINNT=0x600");
382397
}
383398

384399
if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") {
@@ -399,5 +414,5 @@ fn main() {
399414
cc.warnings_into_errors(true);
400415
cc.compile("libgrpc_wrap.a");
401416

402-
config_binding_path(bind_config);
417+
config_binding_path();
403418
}

0 commit comments

Comments
 (0)