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