Skip to content

Commit

Permalink
*: use libz-sys instead of bundled one (#419)
Browse files Browse the repository at this point in the history
* *: use libz-sys instead of bundled one

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 shortcoming is that libz-sys may not be compatible with
grpcio, but I believe the chance is quite small given it's such a small
library. And giving it's such a small library, the benifits like compile
time or package size described above may be too small to be observed.

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

Signed-off-by: Jay Lee <[email protected]>
  • Loading branch information
BusyJay authored Jan 23, 2020
1 parent 952a03f commit 6ed6767
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 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
35 changes: 23 additions & 12 deletions grpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn probe_library(library: &str, cargo_metadata: bool) -> Library {
fn prepare_grpc() {
let mut modules = vec![
"grpc",
"grpc/third_party/zlib",
"grpc/third_party/cares/cares",
"grpc/third_party/address_sorting",
];
Expand Down Expand Up @@ -77,7 +76,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 +150,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 +178,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 +225,23 @@ 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");
// cmake script expect libz.a being under ${DEP_Z_ROOT}/lib, but libz-sys crate put it
// under ${DEP_Z_ROOT}/build. Append the path to CMAKE_PREFIX_PATH to get around it.
let zlib_root = env::var("DEP_Z_ROOT").unwrap();
let prefix_path = if let Ok(prefix_path) = env::var("CMAKE_PREFIX_PATH") {
format!("{};{}/build", prefix_path, zlib_root)
} else {
format!("{}/build", zlib_root)
};
// To avoid linking system library, set lib path explicitly.
println!("cargo:rustc-link-search=native={}/build", zlib_root);
println!("cargo:rustc-link-search=native={}/lib", zlib_root);
env::set_var("CMAKE_PREFIX_PATH", prefix_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
2 changes: 1 addition & 1 deletion src/async/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl SpawnNotify {
pub fn resolve(self, success: bool) {
// it should always be canceled for now.
assert!(success);
poll(&Arc::new(self.clone()), true);
poll(&Arc::new(self), true);
}
}

Expand Down

0 comments on commit 6ed6767

Please sign in to comment.