@@ -267,7 +267,18 @@ fn get_env(name: &str) -> Option<String> {
267
267
268
268
// Generate the bindings to grpc C-core.
269
269
// 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
+
271
282
// Search header files with API interface
272
283
let mut headers = Vec :: new ( ) ;
273
284
for result in WalkDir :: new ( Path :: new ( "./grpc/include" ) ) {
@@ -333,28 +344,36 @@ fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) {
333
344
// Determine if need to update bindings. Supported platforms do not
334
345
// need to be updated by default unless the UPDATE_BIND is specified.
335
346
// 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 ( ) {
338
348
let target = env:: var ( "TARGET" ) . unwrap ( ) ;
339
- match target. as_str ( ) {
349
+ let file_path : PathBuf = match target. as_str ( ) {
340
350
"x86_64-unknown-linux-gnu" | "aarch64-unknown-linux-gnu" => {
341
351
// Cargo treats nonexistent files changed, so we only emit the rerun-if-changed
342
352
// directive when we expect the target-specific pre-generated binding file to be
343
353
// present.
344
354
println ! ( "cargo:rerun-if-changed=bindings/{}-bindings.rs" , & target) ;
345
355
346
- file_path = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) )
356
+ let file_path = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) )
347
357
. join ( "bindings" )
348
358
. 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) ;
351
363
}
364
+
365
+ file_path
352
366
}
353
367
_ => {
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
356
374
}
357
375
} ;
376
+
358
377
println ! (
359
378
"cargo:rustc-env=BINDING_PATH={}" ,
360
379
file_path. to_str( ) . unwrap( )
@@ -368,12 +387,9 @@ fn main() {
368
387
369
388
// create a builder to compile grpc_wrap.cc
370
389
let mut cc = cc:: Build :: new ( ) ;
371
- // create a config to generate binding file
372
- let mut bind_config = bindgen:: Builder :: default ( ) ;
373
390
374
391
let library = if cfg ! ( feature = "secure" ) {
375
392
cc. define ( "GRPC_SYS_SECURE" , None ) ;
376
- bind_config = bind_config. clang_arg ( "-DGRPC_SYS_SECURE" ) ;
377
393
"grpc"
378
394
} else {
379
395
"grpc_unsecure"
@@ -382,7 +398,6 @@ fn main() {
382
398
if get_env ( "CARGO_CFG_TARGET_OS" ) . map_or ( false , |s| s == "windows" ) {
383
399
// At lease vista
384
400
cc. define ( "_WIN32_WINNT" , Some ( "0x600" ) ) ;
385
- bind_config = bind_config. clang_arg ( "-D _WIN32_WINNT=0x600" ) ;
386
401
}
387
402
388
403
if get_env ( "GRPCIO_SYS_USE_PKG_CONFIG" ) . map_or ( false , |s| s == "1" ) {
@@ -403,5 +418,5 @@ fn main() {
403
418
cc. warnings_into_errors ( true ) ;
404
419
cc. compile ( "libgrpc_wrap.a" ) ;
405
420
406
- config_binding_path ( bind_config ) ;
421
+ config_binding_path ( ) ;
407
422
}
0 commit comments