Skip to content

Commit c320d22

Browse files
committed
fix windows build
Signed-off-by: Jay Lee <[email protected]>
1 parent 83b4730 commit c320d22

File tree

1 file changed

+77
-63
lines changed

1 file changed

+77
-63
lines changed

grpc-sys/build.rs

Lines changed: 77 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -300,76 +300,90 @@ fn get_env(name: &str) -> Option<String> {
300300
// Try to disable the generation of platform-related bindings.
301301
#[cfg(feature = "use-bindgen")]
302302
fn bindgen_grpc(file_path: &PathBuf) {
303-
// create a config to generate binding file
304-
let mut config = bindgen::Builder::default();
305-
if cfg!(feature = "secure") {
306-
config = config.clang_arg("-DGRPC_SYS_SECURE");
307-
}
303+
fn exec(path: &PathBuf) {
304+
// create a config to generate binding file
305+
let mut config = bindgen::Builder::default();
306+
if cfg!(feature = "secure") {
307+
config = config.clang_arg("-DGRPC_SYS_SECURE");
308+
}
308309

309-
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
310-
config = config.clang_arg("-D _WIN32_WINNT=0x600");
311-
}
310+
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
311+
config = config.clang_arg("-D _WIN32_WINNT=0x600");
312+
}
312313

313-
// Search header files with API interface
314-
let mut headers = Vec::new();
315-
for result in WalkDir::new(Path::new("./grpc/include")) {
316-
let dent = result.expect("Error happened when search headers");
317-
if !dent.file_type().is_file() {
318-
continue;
314+
// Search header files with API interface
315+
let mut headers = Vec::new();
316+
for result in WalkDir::new(Path::new("./grpc/include")) {
317+
let dent = result.expect("Error happened when search headers");
318+
if !dent.file_type().is_file() {
319+
continue;
320+
}
321+
let mut file = fs::File::open(dent.path()).expect("couldn't open headers");
322+
let mut buf = String::new();
323+
file.read_to_string(&mut buf)
324+
.expect("Coundn't read header content");
325+
if buf.contains("GRPCAPI") || buf.contains("GPRAPI") {
326+
headers.push(String::from(dent.path().to_str().unwrap()));
327+
}
319328
}
320-
let mut file = fs::File::open(dent.path()).expect("couldn't open headers");
321-
let mut buf = String::new();
322-
file.read_to_string(&mut buf)
323-
.expect("Coundn't read header content");
324-
if buf.contains("GRPCAPI") || buf.contains("GPRAPI") {
325-
headers.push(String::from(dent.path().to_str().unwrap()));
329+
330+
// To control the order of bindings
331+
headers.sort();
332+
for path in headers {
333+
config = config.header(path);
326334
}
327-
}
328335

329-
// To control the order of bindings
330-
headers.sort();
331-
for path in headers {
332-
config = config.header(path);
336+
println!("cargo:rerun-if-env-changed=TEST_BIND");
337+
let gen_tests = env::var("TEST_BIND").map_or(false, |s| s == "1");
338+
339+
let cfg = config
340+
.header("grpc_wrap.cc")
341+
.clang_arg("-xc++")
342+
.clang_arg("-I./grpc/include")
343+
.clang_arg("-std=c++11")
344+
.rustfmt_bindings(true)
345+
.impl_debug(true)
346+
.size_t_is_usize(true)
347+
.disable_header_comment()
348+
.whitelist_function(r"\bgrpc_.*")
349+
.whitelist_function(r"\bgpr_.*")
350+
.whitelist_function(r"\bgrpcwrap_.*")
351+
.whitelist_var(r"\bGRPC_.*")
352+
.whitelist_type(r"\bgrpc_.*")
353+
.whitelist_type(r"\bgpr_.*")
354+
.whitelist_type(r"\bgrpcwrap_.*")
355+
.whitelist_type(r"\bcensus_context.*")
356+
.whitelist_type(r"\bverify_peer_options.*")
357+
.blacklist_type(r"(__)?pthread.*")
358+
.blacklist_function(r"\bgpr_mu_.*")
359+
.blacklist_function(r"\bgpr_cv_.*")
360+
.blacklist_function(r"\bgpr_once_.*")
361+
.blacklist_type(r"gpr_mu")
362+
.blacklist_type(r"gpr_cv")
363+
.blacklist_type(r"gpr_once")
364+
.constified_enum_module(r"grpc_status_code")
365+
.layout_tests(gen_tests)
366+
.default_enum_style(bindgen::EnumVariation::Rust {
367+
non_exhaustive: false,
368+
});
369+
println!("running {}", cfg.command_line_flags().join(" "));
370+
cfg.generate()
371+
.expect("Unable to generate grpc bindings")
372+
.write_to_file(path)
373+
.expect("Couldn't write bindings!");
333374
}
334375

335-
println!("cargo:rerun-if-env-changed=TEST_BIND");
336-
let gen_tests = env::var("TEST_BIND").map_or(false, |s| s == "1");
337-
338-
let cfg = config
339-
.header("grpc_wrap.cc")
340-
.clang_arg("-xc++")
341-
.clang_arg("-I./grpc/include")
342-
.clang_arg("-std=c++11")
343-
.rustfmt_bindings(true)
344-
.impl_debug(true)
345-
.size_t_is_usize(true)
346-
.disable_header_comment()
347-
.whitelist_function(r"\bgrpc_.*")
348-
.whitelist_function(r"\bgpr_.*")
349-
.whitelist_function(r"\bgrpcwrap_.*")
350-
.whitelist_var(r"\bGRPC_.*")
351-
.whitelist_type(r"\bgrpc_.*")
352-
.whitelist_type(r"\bgpr_.*")
353-
.whitelist_type(r"\bgrpcwrap_.*")
354-
.whitelist_type(r"\bcensus_context.*")
355-
.whitelist_type(r"\bverify_peer_options.*")
356-
.blacklist_type(r"(__)?pthread.*")
357-
.blacklist_function(r"\bgpr_mu_.*")
358-
.blacklist_function(r"\bgpr_cv_.*")
359-
.blacklist_function(r"\bgpr_once_.*")
360-
.blacklist_type(r"gpr_mu")
361-
.blacklist_type(r"gpr_cv")
362-
.blacklist_type(r"gpr_once")
363-
.constified_enum_module(r"grpc_status_code")
364-
.layout_tests(gen_tests)
365-
.default_enum_style(bindgen::EnumVariation::Rust {
366-
non_exhaustive: false,
367-
});
368-
println!("running {}", cfg.command_line_flags().join(" "));
369-
cfg.generate()
370-
.expect("Unable to generate grpc bindings")
371-
.write_to_file(file_path)
372-
.expect("Couldn't write bindings!");
376+
// Due to rust-lang/regex#750, spawn a thread with larger stack to
377+
// make it build on Windows.
378+
let path = file_path.to_path_buf();
379+
std::thread::Builder::new()
380+
.stack_size(8 * 1024 * 1024)
381+
.spawn(move || {
382+
exec(&path);
383+
})
384+
.unwrap()
385+
.join()
386+
.unwrap()
373387
}
374388

375389
// Determine if need to update bindings. Supported platforms do not

0 commit comments

Comments
 (0)