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

grpcio: pre-generate bindings for common targets #356

Merged
merged 40 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f7b4b4f
use environment variable to control build
hunterlxt Jul 25, 2019
290c4cd
update
hunterlxt Jul 29, 2019
84225c9
update
hunterlxt Jul 29, 2019
d45975c
update
hunterlxt Jul 29, 2019
8abd525
update
hunterlxt Jul 29, 2019
c9ee115
update
hunterlxt Jul 29, 2019
a32a1c7
update
hunterlxt Jul 29, 2019
00e5122
update
hunterlxt Jul 29, 2019
1962edc
update
hunterlxt Jul 29, 2019
0b723fd
update
hunterlxt Jul 29, 2019
982726d
update
hunterlxt Jul 29, 2019
a42cf69
update
hunterlxt Jul 29, 2019
02aaa91
update
hunterlxt Jul 29, 2019
cce3d18
update
hunterlxt Jul 29, 2019
50b9abe
update
hunterlxt Jul 29, 2019
6e825d0
update
hunterlxt Jul 29, 2019
0481522
noc++
hunterlxt Jul 29, 2019
9afddf2
update script name
hunterlxt Jul 30, 2019
8c41cf3
add macos bindings
mahjonp Jul 30, 2019
46a47ae
cargo fmt
hunterlxt Jul 30, 2019
bf23bb2
update
hunterlxt Jul 30, 2019
cb6cb3c
update
hunterlxt Jul 30, 2019
706cf01
Good Night
hunterlxt Jul 30, 2019
5d75a79
Good Night
hunterlxt Jul 30, 2019
e34a695
Update build.rs
hunterlxt Jul 30, 2019
ce54a95
update
hunterlxt Jul 31, 2019
054ed05
update
hunterlxt Jul 31, 2019
fe857cb
update
hunterlxt Jul 31, 2019
2871e8a
add macos bindings
mahjonp Jul 31, 2019
298aa3e
update
hunterlxt Jul 31, 2019
bdd1454
update
hunterlxt Jul 31, 2019
e5fc34a
update
hunterlxt Jul 31, 2019
6333612
update
hunterlxt Jul 31, 2019
ef52f2d
update by reviews
hunterlxt Aug 2, 2019
036dc37
update
hunterlxt Aug 2, 2019
a1ca387
address comment and remove osx support
BusyJay Aug 5, 2019
696a9dd
update README
BusyJay Aug 5, 2019
ec29b89
fix eol
BusyJay Aug 5, 2019
baf33ed
address comment
BusyJay Aug 5, 2019
537f2ef
address comment
BusyJay Aug 5, 2019
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ matrix:

addons:
apt:
update: true
sources:
- llvm-toolchain-xenial-7
packages:
Expand Down Expand Up @@ -100,6 +101,7 @@ before_script:
script:
- if [[ $TRAVIS_OS_NAME == "linux" ]] && [[ $TRAVIS_RUST_VERSION == "stable" ]]; then
rustup component add rustfmt && cargo fmt --all -- --check;
scripts/generate-bindings.sh && git diff --exit-code HEAD;
fi
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then scripts/lint-grpc-sys.sh && git diff-index --quiet HEAD; fi
- if [[ $TRAVIS_RUST_VERSION == "stable" ]]; then rustup component add clippy && cargo clippy --all -- -D clippy::all && cargo clippy --all --no-default-features --features prost-codec -- -D clippy::all; fi
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ This project is still under development. The following features with the check m
- CMake >= 3.8.0
- Rust >= 1.19.0
- binutils >= 2.22
- LLVM and Clang >= 3.9 recommended by bindgen
- By default, the [secure feature](#feature-secure) is enabled, therefore Go (>=1.7) is required.
- LLVM and Clang >= 3.9 if you need to generate bindings at compile time.
- By default, the [secure feature](#feature-secure) is provided by boringssl, which requires Go (>=1.7) to build. You can also use openssl instead by enabling [openssl feature](#feature-openssl).

For Linux and MacOS, you also need to install gcc (or clang) too.

Bindings are pre-generated for x86_64 Linux. For other platforms, bindings are generated at compile time.

For Windows, you also need to install following software:

- Active State Perl
Expand All @@ -41,7 +43,7 @@ For Windows, you also need to install following software:
## Build

```
$ git submodule update --init --recursive # if you just cloned the repository
$ ./scripts/reset-submodule.cmd # if you just cloned the repository
$ cargo build
```

Expand Down Expand Up @@ -113,11 +115,21 @@ your `Cargo.toml`'s features list for `gprcio`. E.g.:
grpcio = { version = "0.4.4", features = ["openssl"] }
```


## Performance

See [benchmark](https://github.com/pingcap/grpc-rs/tree/master/benchmark) to find out how to run a benchmark by yourself.

Cross Compile
-------------
See [cross_compile](cross_compile.md)

Contributing
------------

Make sure to format and test the code before sending a PR.

If the content in grpc-sys/grpc is updated, you may need to regenerate bindings:

```
$ ./scripts/generate-bindings.sh
```
2 changes: 1 addition & 1 deletion grpc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ cc = "1.0"
cmake = "0.1.40"
pkg-config = "0.3"
walkdir = "2.2.9"
bindgen = { version = "0.50.0", default-features = false }
bindgen = { version = "0.51.0", default-features = false }
6,097 changes: 6,097 additions & 0 deletions grpc-sys/bindings/x86_64-unknown-linux-gnu-bindings.rs

Large diffs are not rendered by default.

75 changes: 59 additions & 16 deletions grpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,11 @@ fn get_env(name: &str) -> Option<String> {
}
}

/// Generate the bindings to C-core
fn bindgen_grpc(mut config: bindgen::Builder) {
// Generate the bindings to grpc C-core.
// Try to disable the generation of platform-related bindings.
fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) {
// Search header files with API interface
let mut headers = Vec::new();
for result in WalkDir::new(Path::new("./grpc/include")) {
let dent = result.expect("Error happened when search headers");
if !dent.file_type().is_file() {
Expand All @@ -249,40 +251,80 @@ fn bindgen_grpc(mut config: bindgen::Builder) {
file.read_to_string(&mut buf)
.expect("Coundn't read header content");
if buf.contains("GRPCAPI") || buf.contains("GPRAPI") {
config = config.header(dent.path().to_str().unwrap());
headers.push(String::from(dent.path().to_str().unwrap()));
}
}

config = config.header("grpc_wrap.cc");
// To control the order of bindings
headers.sort();
for path in headers {
config = config.header(path);
}

let bindings = config
config
.header("grpc_wrap.cc")
.clang_arg("-xc++")
.clang_arg("-I./grpc/include")
.clang_arg("-std=c++11")
.whitelist_recursively(false)
.whitelist_function(r"\bgrpc_.*")
.whitelist_function(r"\bgpr_.*")
.whitelist_function(r"\bgrpcwrap_.*")
.whitelist_var(r"\bGRPC_.*")
.whitelist_type(r"\bgrpc_.*")
.whitelist_type(r"\bgpr_.*")
.whitelist_type(r"\bgrpcwrap_.*")
.whitelist_type(r"\bcensus_context.*")
.whitelist_type(r"\bverify_peer_options.*")
.blacklist_function(r"\bgpr_mu_.*")
.blacklist_function(r"\bgpr_cv_.*")
.blacklist_function(r"\bgpr_once_.*")
.blacklist_type(r"gpr_mu")
.blacklist_type(r"gpr_cv")
.blacklist_type(r"gpr_once")
.constified_enum_module(r"grpc_status_code")
.no_copy("grpc_slice")
.default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: false,
})
.constified_enum_module(r"grpc_status_code")
.no_copy("grpc_slice")
.generate()
.expect("Unable to generate grpc bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("grpc-bindings.rs"))
.expect("Unable to generate grpc bindings")
.write_to_file(file_path)
.expect("Couldn't write bindings!");
}

fn main() {
let mut cc = Build::new();
let mut bind_config = bindgen::Builder::default();
// 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(config: bindgen::Builder) {
let file_path: PathBuf;
match env::var("TARGET").unwrap_or("".to_owned()).as_str() {
"x86_64-unknown-linux-gnu" => {
file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join("bindings")
.join("x86_64-unknown-linux-gnu-bindings.rs");
if env::var("UPDATE_BIND").map(|s| s == "1").unwrap_or(false) {
bindgen_grpc(config, &file_path);
}
}
_ => {
file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");
hunterlxt marked this conversation as resolved.
Show resolved Hide resolved
bindgen_grpc(config, &file_path);
}
};
println!(
"cargo:rustc-env=BINDING_PATH={}",
file_path.to_str().unwrap()
);
}

fn main() {
println!("cargo:rerun-if-changed=grpc_wrap.cc");
println!("cargo:rerun-if-changed=grpc");
println!("rerun-if-env-changed=UPDATE_BIND");
hunterlxt marked this conversation as resolved.
Show resolved Hide resolved

let mut cc = Build::new();
let mut bind_config = bindgen::Builder::default();

let library = if cfg!(feature = "secure") {
cc.define("GRPC_SYS_SECURE", None);
Expand Down Expand Up @@ -317,5 +359,6 @@ fn main() {
cc.warnings_into_errors(true);

cc.compile("libgrpc_wrap.a");
bindgen_grpc(bind_config);

config_binding_path(bind_config);
}
12 changes: 12 additions & 0 deletions grpc-sys/src/grpc_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ impl gpr_timespec {
}
}

impl Copy for gpr_timespec {}

impl Clone for gpr_timespec {
fn clone(&self) -> Self {
gpr_timespec {
tv_sec: self.tv_sec,
tv_nsec: self.tv_nsec,
clock_type: self.clock_type,
}
}
}

impl From<Duration> for gpr_timespec {
fn from(dur: Duration) -> gpr_timespec {
gpr_timespec {
Expand Down
2 changes: 1 addition & 1 deletion grpc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#![allow(non_upper_case_globals)]
#[allow(clippy::all)]
mod bindings {
include!(concat!(env!("OUT_DIR"), "/grpc-bindings.rs"));
include!(env!("BINDING_PATH"));
}
mod grpc_wrap;

Expand Down
9 changes: 9 additions & 0 deletions scripts/generate-bindings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# NOTE:
# This script is only used when you want to generate bindings yourself.
# The generated bindings will overwrite grpc-sys/bindings/*

export UPDATE_BIND=1
cargo build -p grpcio-sys --target x86_64-unknown-linux-gnu
rustfmt grpc-sys/bindings/*