Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc-sys: Use grpc headers found by pkgconfig. #505

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 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,15 +281,15 @@ 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.join("grpc")) {
let dent = result.expect("Error happened when search headers");
if !dent.file_type().is_file() {
continue;
}
let mut file = fs::File::open(dent.path()).expect("couldn't open headers");
let mut buf = String::new();
file.read_to_string(&mut buf)
.expect("Coundn't read header content");
.expect("Couldn't read header content");
if buf.contains("GRPCAPI") || buf.contains("GPRAPI") {
headers.push(String::from(dent.path().to_str().unwrap()));
}
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()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be correct. For example, include directory should be "/usr/local/include" instead of "/usr/local/include/grpc".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️

.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(_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()
.find(|grpc_inc_path| grpc_inc_path.is_dir())
BusyJay marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or_else(|| {
panic!(
"Could not find grpc include dir in {:#?}",
lib_core.include_paths
)
})
.into();
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);
}