Skip to content

Commit

Permalink
*: use libz-sys instead of bundled one
Browse files Browse the repository at this point in the history
When using the built-in cmake to build zlib, it changes the source tree
as madler/zlib#162 describes. This leads to the failure during
[generating the docs][1]. So let's switch to libz-sys instead, which
uses its own custom script to build zlib, and leave source tree as it
is. Switching to libz-sys can also reduce the package size as we can
ignore more sub modules. It should improve compile time if libz-sys is
also a dependency of other crates.

The only shortcomming is that libz-sys may not be compitable with
grpcio, but I believe the chance is quite small given it's such a small
library.

[1]: https://docs.rs/crate/grpcio/0.5.0-alpha.5/builds/196235.

Signed-off-by: Jay Lee <busyjaylee@gmail.com>
  • Loading branch information
BusyJay committed Dec 20, 2019
1 parent 952a03f commit 777d5f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 2 additions & 0 deletions grpc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exclude = [
"grpc/src/ruby/*",
"grpc/vsprojects/*",
"grpc/test/core/end2end/*",
"grpc/third_party/zlib/*",
"grpc/third_party/abseil-cpp/*",
"grpc/third_party/benchmark/*",
"grpc/third_party/bloaty/*",
Expand All @@ -48,6 +49,7 @@ exclude = [
[dependencies]
libc = "0.2"
openssl-sys = { version = "0.9", optional = true, features = ["vendored"] }
libz-sys = { version = "1.0.25", features = ["static"] }

[features]
default = []
Expand Down
30 changes: 19 additions & 11 deletions grpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn trim_start<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
fn build_grpc(cc: &mut Build, library: &str) {
prepare_grpc();

let mut third_party = vec!["cares/cares/lib", "zlib"];
let mut third_party = vec!["cares/cares/lib"];

let dst = {
let mut config = Config::new("grpc");
Expand Down Expand Up @@ -151,21 +151,16 @@ fn build_grpc(cc: &mut Build, library: &str) {
} else if cfg!(feature = "secure") {
third_party.extend_from_slice(&["boringssl/ssl", "boringssl/crypto"]);
}
// Uses zlib from libz-sys.
setup_libz(&mut config);
config.build_target(library).uses_cxx11().build()
};

let mut zlib = "z";
let build_dir = format!("{}/build", dst.display());
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
let profile = match &*env::var("PROFILE").unwrap_or("debug".to_owned()) {
"bench" | "release" => {
zlib = "zlibstatic";
"Release"
}
_ => {
zlib = "zlibstaticd";
"Debug"
}
"bench" | "release" => "Release",
_ => "Debug",
};
println!("cargo:rustc-link-search=native={}/{}", build_dir, profile);
for path in third_party {
Expand All @@ -184,7 +179,7 @@ fn build_grpc(cc: &mut Build, library: &str) {
}
}

println!("cargo:rustc-link-lib=static={}", zlib);
println!("cargo:rustc-link-lib=static=z");
println!("cargo:rustc-link-lib=static=cares");
println!("cargo:rustc-link-lib=static=gpr");
println!("cargo:rustc-link-lib=static=address_sorting");
Expand Down Expand Up @@ -231,6 +226,19 @@ fn figure_ssl_path(build_dir: &str) {
println!("cargo:rustc-link-lib=crypto");
}

fn setup_libz(config: &mut Config) {
config.define("gRPC_ZLIB_PROVIDER", "package");
config.register_dep("Z").very_verbose(true);
env::set_var("CMAKE_PREFIX_PATH", {
let zlib_path = format!("{}/build", env::var("DEP_Z_ROOT").unwrap());
if let Ok(prefix_path) = env::var("CMAKE_PREFIX_PATH") {
format!("{};{}", prefix_path, zlib_path)
} else {
zlib_path
}
});
}

#[cfg(feature = "openssl-vendored")]
fn setup_openssl(config: &mut Config) {
// openssl-sys uses openssl-src to build the library. openssl-src uses
Expand Down
1 change: 0 additions & 1 deletion scripts/reset-submodule
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ git submodule update --init grpc-sys/grpc
cd grpc-sys/grpc
git submodule update --init third_party/boringssl
git submodule update --init third_party/cares/cares
git submodule update --init third_party/zlib
cd third_party/zlib
git clean -f
git reset --hard

0 comments on commit 777d5f9

Please sign in to comment.