Skip to content

Commit

Permalink
grpc-sys: Use grpc headers found by pkgconfig.
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Steven Sloboda <[email protected]>
  • Loading branch information
ssloboda committed Dec 28, 2020
1 parent b9ddf27 commit 2c15028
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions grpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn get_env(name: &str) -> Option<String> {
// Generate the bindings to grpc C-core.
// Try to disable the generation of platform-related bindings.
#[cfg(feature = "use-bindgen")]
fn bindgen_grpc(file_path: &PathBuf) {
fn bindgen_grpc(file_path: &PathBuf, grpc_include_dir: &PathBuf) {
// create a config to generate binding file
let mut config = bindgen::Builder::default();
if cfg!(feature = "secure") {
Expand All @@ -281,7 +281,7 @@ fn bindgen_grpc(file_path: &PathBuf) {

// Search header files with API interface
let mut headers = Vec::new();
for result in WalkDir::new(Path::new("./grpc/include")) {
for result in WalkDir::new(grpc_include_dir) {
let dent = result.expect("Error happened when search headers");
if !dent.file_type().is_file() {
continue;
Expand All @@ -307,7 +307,7 @@ fn bindgen_grpc(file_path: &PathBuf) {
let cfg = config
.header("grpc_wrap.cc")
.clang_arg("-xc++")
.clang_arg("-I./grpc/include")
.clang_arg(format!("-I{}", grpc_include_dir.display()))
.clang_arg("-std=c++11")
.rustfmt_bindings(true)
.impl_debug(true)
Expand Down Expand Up @@ -344,7 +344,7 @@ fn bindgen_grpc(file_path: &PathBuf) {
// Determine if need to update bindings. Supported platforms do not
// need to be updated by default unless the UPDATE_BIND is specified.
// Other platforms use bindgen to generate the bindings every time.
fn config_binding_path() {
fn config_binding_path(#[allow(unused_variables)] grpc_include_dir: &PathBuf) {
let target = env::var("TARGET").unwrap();
let file_path: PathBuf = match target.as_str() {
"x86_64-unknown-linux-gnu" | "aarch64-unknown-linux-gnu" => {
Expand All @@ -359,7 +359,7 @@ fn config_binding_path() {

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

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

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

file_path
}
Expand Down Expand Up @@ -400,15 +400,28 @@ fn main() {
cc.define("_WIN32_WINNT", Some("0x600"));
}

if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") {
let grpc_include_dir = if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") {
// Print cargo metadata.
let lib_core = probe_library(library, true);
let grpc_include_dir = lib_core
.include_paths
.iter()
.map(|inc_path| inc_path.join("grpc"))
.find(|grpc_inc_path| grpc_inc_path.is_dir())
.unwrap_or_else(|| {
panic!(
"Could not find grpc include dir in {:#?}",
lib_core.include_paths
)
});
for inc_path in lib_core.include_paths {
cc.include(inc_path);
}
grpc_include_dir
} else {
build_grpc(&mut cc, library);
}
"./grpc/include".into()
};

cc.cpp(true);
if !cfg!(target_env = "msvc") {
Expand All @@ -418,5 +431,5 @@ fn main() {
cc.warnings_into_errors(true);
cc.compile("libgrpc_wrap.a");

config_binding_path();
config_binding_path(&grpc_include_dir);
}

0 comments on commit 2c15028

Please sign in to comment.