Skip to content

Commit 9c4b04e

Browse files
committed
*: 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]>
1 parent 0c16249 commit 9c4b04e

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

grpc-sys/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ exclude = [
2727
"grpc/src/ruby/*",
2828
"grpc/vsprojects/*",
2929
"grpc/test/core/end2end/*",
30+
"grpc/third_party/zlib/*",
3031
"grpc/third_party/abseil-cpp/*",
3132
"grpc/third_party/benchmark/*",
3233
"grpc/third_party/bloaty/*",
@@ -49,6 +50,7 @@ exclude = [
4950
[dependencies]
5051
libc = "0.2"
5152
openssl-sys = { version = "0.9", optional = true, features = ["vendored"] }
53+
libz-sys = { version = "1.0.25", features = ["static"] }
5254

5355
[features]
5456
default = []

grpc-sys/build.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ fn probe_library(library: &str, cargo_metadata: bool) -> Library {
3131
fn prepare_grpc() {
3232
let mut modules = vec![
3333
"grpc",
34-
"grpc/third_party/zlib",
3534
"grpc/third_party/cares/cares",
3635
"grpc/third_party/address_sorting",
3736
];
@@ -67,7 +66,7 @@ fn trim_start<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
6766
fn build_grpc(cc: &mut Build, library: &str) {
6867
prepare_grpc();
6968

70-
let mut third_party = vec!["cares/cares/lib", "zlib"];
69+
let mut third_party = vec!["cares/cares/lib"];
7170

7271
let dst = {
7372
let mut config = Config::new("grpc");
@@ -147,21 +146,16 @@ fn build_grpc(cc: &mut Build, library: &str) {
147146
.cflag("-fno-omit-frame-pointer")
148147
.cxxflag("-fno-omit-frame-pointer");
149148
}
149+
// Uses zlib from libz-sys.
150+
setup_libz(&mut config);
150151
config.build_target(library).uses_cxx11().build()
151152
};
152153

153-
let mut zlib = "z";
154154
let build_dir = format!("{}/build", dst.display());
155155
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
156156
let profile = match &*env::var("PROFILE").unwrap_or("debug".to_owned()) {
157-
"bench" | "release" => {
158-
zlib = "zlibstatic";
159-
"Release"
160-
}
161-
_ => {
162-
zlib = "zlibstaticd";
163-
"Debug"
164-
}
157+
"bench" | "release" => "Release",
158+
_ => "Debug",
165159
};
166160
println!("cargo:rustc-link-search=native={}/{}", build_dir, profile);
167161
for path in third_party {
@@ -180,7 +174,7 @@ fn build_grpc(cc: &mut Build, library: &str) {
180174
}
181175
}
182176

183-
println!("cargo:rustc-link-lib=static={}", zlib);
177+
println!("cargo:rustc-link-lib=static=z");
184178
println!("cargo:rustc-link-lib=static=cares");
185179
println!("cargo:rustc-link-lib=static=gpr");
186180
println!("cargo:rustc-link-lib=static=address_sorting");
@@ -225,6 +219,21 @@ fn figure_ssl_path(build_dir: &str) {
225219
println!("cargo:rustc-link-lib=crypto");
226220
}
227221

222+
fn setup_libz(config: &mut Config) {
223+
config.define("gRPC_ZLIB_PROVIDER", "package");
224+
config.register_dep("Z");
225+
// cmake script expect libz.a being under ${DEP_Z_ROOT}/lib, but libz-sys crate put it
226+
// under ${DEP_Z_ROOT}/build. Append the path to CMAKE_PREFIX_PATH to get around it.
227+
env::set_var("CMAKE_PREFIX_PATH", {
228+
let zlib_path = format!("{}/build", env::var("DEP_Z_ROOT").unwrap());
229+
if let Ok(prefix_path) = env::var("CMAKE_PREFIX_PATH") {
230+
format!("{};{}", prefix_path, zlib_path)
231+
} else {
232+
zlib_path
233+
}
234+
});
235+
}
236+
228237
#[cfg(feature = "openssl-vendored")]
229238
fn setup_openssl(config: &mut Config) {
230239
// openssl-sys uses openssl-src to build the library. openssl-src uses

scripts/reset-submodule.cmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ git submodule update --init grpc-sys/grpc
22
cd grpc-sys/grpc
33
git submodule update --init third_party/boringssl
44
git submodule update --init third_party/cares/cares
5-
git submodule update --init third_party/zlib
65
cd third_party/zlib
76
git clean -f
87
git reset --hard

0 commit comments

Comments
 (0)