@@ -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