Skip to content

Add a way to not use bindgen #499

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

Merged
merged 4 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ jobs:
- uses: actions/checkout@v2
- run: which go && go version && which cargo && cargo version && clang --version && openssl version
- run: scripts/reset-submodule.cmd
- run: cargo build --no-default-features
- run: cargo build --no-default-features --features protobuf-codec
- run: cargo build --no-default-features --features prost-codec
- run: cargo build --no-default-features --features use-bindgen
- run: cargo build --no-default-features --features "protobuf-codec use-bindgen"
- run: cargo build --no-default-features --features "prost-codec use-bindgen"
- run: cargo build
- run: cargo test --all

Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ autoexamples = false
all-features = true

[dependencies]
grpcio-sys = { path = "grpc-sys", version = "0.7" }
grpcio-sys = { path = "grpc-sys", version = "0.7", default-features = false }
libc = "0.2"
futures = "0.3"
protobuf = { version = "2.0", optional = true }
Expand All @@ -30,13 +30,14 @@ parking_lot = "0.11"
members = ["proto", "benchmark", "compiler", "interop", "tests-and-examples"]

[features]
default = ["protobuf-codec", "secure"]
default = ["protobuf-codec", "secure", "use-bindgen"]
protobuf-codec = ["protobuf"]
prost-codec = ["prost", "bytes"]
secure = ["grpcio-sys/secure"]
openssl = ["secure", "grpcio-sys/openssl"]
openssl-vendored = ["secure", "grpcio-sys/openssl-vendored"]
no-omit-frame-pointer = ["grpcio-sys/no-omit-frame-pointer"]
use-bindgen = ["grpcio-sys/use-bindgen"]

[profile.release]
debug = true
Expand Down
8 changes: 6 additions & 2 deletions grpc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ openssl-sys = { version = "0.9", optional = true, features = ["vendored"] }
libz-sys = { version = "1.0.25", features = ["static"] }

[features]
default = []
default = ["use-bindgen"]
secure = []
openssl = ["secure"]
openssl-vendored = ["openssl", "openssl-sys"]
no-omit-frame-pointer = []
# If this feature is disabled, bindgen will not be used and the previously generated bindings will
# be compiled instead. This only work for the supported targets and will make compilation fails for
# the other ones.
use-bindgen = ["bindgen"]

[build-dependencies]
cc = "1.0"
cmake = "0.1"
pkg-config = "0.3"
walkdir = "2.2.9"
# Because of rust-lang/cargo#5237, bindgen should not be upgraded util a minor or major release.
bindgen = { version = "0.56.0", default-features = false, features = ["runtime"] }
bindgen = { version = "0.56.0", default-features = false, optional = true, features = ["runtime"] }
boringssl-src = "0.1.0"
43 changes: 29 additions & 14 deletions grpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,18 @@ fn get_env(name: &str) -> Option<String> {

// 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) {
#[cfg(feature = "use-bindgen")]
fn bindgen_grpc(file_path: &PathBuf) {
// create a config to generate binding file
let mut config = bindgen::Builder::default();
if cfg!(feature = "secure") {
config = config.clang_arg("-DGRPC_SYS_SECURE");
}

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

// Search header files with API interface
let mut headers = Vec::new();
for result in WalkDir::new(Path::new("./grpc/include")) {
Expand Down Expand Up @@ -333,28 +344,36 @@ fn bindgen_grpc(mut config: bindgen::Builder, 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(config: bindgen::Builder) {
let file_path: PathBuf;
fn config_binding_path() {
let target = env::var("TARGET").unwrap();
match target.as_str() {
let file_path: PathBuf = match target.as_str() {
"x86_64-unknown-linux-gnu" | "aarch64-unknown-linux-gnu" => {
// Cargo treats nonexistent files changed, so we only emit the rerun-if-changed
// directive when we expect the target-specific pre-generated binding file to be
// present.
println!("cargo:rerun-if-changed=bindings/{}-bindings.rs", &target);

file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
let file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join("bindings")
.join(format!("{}-bindings.rs", &target));
if env::var("UPDATE_BIND").map(|s| s == "1").unwrap_or(false) {
bindgen_grpc(config, &file_path);

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

file_path
}
_ => {
file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");
bindgen_grpc(config, &file_path);
let file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("grpc-bindings.rs");

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

file_path
}
};

println!(
"cargo:rustc-env=BINDING_PATH={}",
file_path.to_str().unwrap()
Expand All @@ -368,12 +387,9 @@ fn main() {

// create a builder to compile grpc_wrap.cc
let mut cc = cc::Build::new();
// create a config to generate binding file
let mut bind_config = bindgen::Builder::default();

let library = if cfg!(feature = "secure") {
cc.define("GRPC_SYS_SECURE", None);
bind_config = bind_config.clang_arg("-DGRPC_SYS_SECURE");
"grpc"
} else {
"grpc_unsecure"
Expand All @@ -382,7 +398,6 @@ fn main() {
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
// At lease vista
cc.define("_WIN32_WINNT", Some("0x600"));
bind_config = bind_config.clang_arg("-D _WIN32_WINNT=0x600");
}

if get_env("GRPCIO_SYS_USE_PKG_CONFIG").map_or(false, |s| s == "1") {
Expand All @@ -403,5 +418,5 @@ fn main() {
cc.warnings_into_errors(true);
cc.compile("libgrpc_wrap.a");

config_binding_path(bind_config);
config_binding_path();
}