Skip to content

Commit 0fdadb6

Browse files
committed
grpc-sys: Use grpc headers found by pkgconfig.
Previously the bundled grpc headers were always used to generate bindings even in the case where pkgconfig was used to locate gRPC to link against. This caused errors at runtime, e.g., SEGFAULT, rather than a compile-time error. To fix this, use the headers found by pkgconfig to generate bindings rather than than the bundled headers if pkgconfig is already being used.
1 parent b9ddf27 commit 0fdadb6

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

grpc-sys/build.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn get_env(name: &str) -> Option<String> {
268268
// Generate the bindings to grpc C-core.
269269
// Try to disable the generation of platform-related bindings.
270270
#[cfg(feature = "use-bindgen")]
271-
fn bindgen_grpc(file_path: &PathBuf) {
271+
fn bindgen_grpc(file_path: &PathBuf, grpc_include_dir: &PathBuf) {
272272
// create a config to generate binding file
273273
let mut config = bindgen::Builder::default();
274274
if cfg!(feature = "secure") {
@@ -281,7 +281,7 @@ fn bindgen_grpc(file_path: &PathBuf) {
281281

282282
// Search header files with API interface
283283
let mut headers = Vec::new();
284-
for result in WalkDir::new(Path::new("./grpc/include")) {
284+
for result in WalkDir::new(grpc_include_dir) {
285285
let dent = result.expect("Error happened when search headers");
286286
if !dent.file_type().is_file() {
287287
continue;
@@ -307,7 +307,7 @@ fn bindgen_grpc(file_path: &PathBuf) {
307307
let cfg = config
308308
.header("grpc_wrap.cc")
309309
.clang_arg("-xc++")
310-
.clang_arg("-I./grpc/include")
310+
.clang_arg(format!("-I{}", grpc_include_dir.display()))
311311
.clang_arg("-std=c++11")
312312
.rustfmt_bindings(true)
313313
.impl_debug(true)
@@ -344,7 +344,7 @@ fn bindgen_grpc(file_path: &PathBuf) {
344344
// Determine if need to update bindings. Supported platforms do not
345345
// need to be updated by default unless the UPDATE_BIND is specified.
346346
// Other platforms use bindgen to generate the bindings every time.
347-
fn config_binding_path() {
347+
fn config_binding_path(grpc_include_dir: &PathBuf) {
348348
let target = env::var("TARGET").unwrap();
349349
let file_path: PathBuf = match target.as_str() {
350350
"x86_64-unknown-linux-gnu" | "aarch64-unknown-linux-gnu" => {
@@ -359,7 +359,7 @@ fn config_binding_path() {
359359

360360
#[cfg(feature = "use-bindgen")]
361361
if env::var("UPDATE_BIND").is_ok() {
362-
bindgen_grpc(&file_path);
362+
bindgen_grpc(&file_path, grpc_include_dir);
363363
}
364364

365365
file_path
@@ -368,7 +368,7 @@ fn config_binding_path() {
368368
let file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");
369369

370370
#[cfg(feature = "use-bindgen")]
371-
bindgen_grpc(&file_path);
371+
bindgen_grpc(&file_path, grpc_include_dir);
372372

373373
file_path
374374
}
@@ -400,15 +400,23 @@ fn main() {
400400
cc.define("_WIN32_WINNT", Some("0x600"));
401401
}
402402

403-
if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") {
403+
let grpc_include_dir = if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") {
404404
// Print cargo metadata.
405405
let lib_core = probe_library(library, true);
406+
let grpc_include_dir = lib_core
407+
.include_paths
408+
.iter()
409+
.map(|inc_path| inc_path.join("grpc"))
410+
.find(|grpc_inc_path| grpc_inc_path.is_dir())
411+
.expect(&format!("Could not find grpc include dir in {:#?}", lib_core.include_paths));
406412
for inc_path in lib_core.include_paths {
407413
cc.include(inc_path);
408414
}
415+
grpc_include_dir
409416
} else {
410417
build_grpc(&mut cc, library);
411-
}
418+
"./grpc/include".into()
419+
};
412420

413421
cc.cpp(true);
414422
if !cfg!(target_env = "msvc") {
@@ -418,5 +426,5 @@ fn main() {
418426
cc.warnings_into_errors(true);
419427
cc.compile("libgrpc_wrap.a");
420428

421-
config_binding_path();
429+
config_binding_path(&grpc_include_dir);
422430
}

0 commit comments

Comments
 (0)