Skip to content

Commit 1643d82

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 952a03f commit 1643d82

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
@@ -26,6 +26,7 @@ exclude = [
2626
"grpc/src/ruby/*",
2727
"grpc/vsprojects/*",
2828
"grpc/test/core/end2end/*",
29+
"grpc/third_party/zlib/*",
2930
"grpc/third_party/abseil-cpp/*",
3031
"grpc/third_party/benchmark/*",
3132
"grpc/third_party/bloaty/*",
@@ -48,6 +49,7 @@ exclude = [
4849
[dependencies]
4950
libc = "0.2"
5051
openssl-sys = { version = "0.9", optional = true, features = ["vendored"] }
52+
libz-sys = { version = "1.0.25", features = ["static"] }
5153

5254
[features]
5355
default = []

grpc-sys/build.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ fn probe_library(library: &str, cargo_metadata: bool) -> Library {
4141
fn prepare_grpc() {
4242
let mut modules = vec![
4343
"grpc",
44-
"grpc/third_party/zlib",
4544
"grpc/third_party/cares/cares",
4645
"grpc/third_party/address_sorting",
4746
];
@@ -77,7 +76,7 @@ fn trim_start<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
7776
fn build_grpc(cc: &mut Build, library: &str) {
7877
prepare_grpc();
7978

80-
let mut third_party = vec!["cares/cares/lib", "zlib"];
79+
let mut third_party = vec!["cares/cares/lib"];
8180

8281
let dst = {
8382
let mut config = Config::new("grpc");
@@ -151,21 +150,16 @@ fn build_grpc(cc: &mut Build, library: &str) {
151150
} else if cfg!(feature = "secure") {
152151
third_party.extend_from_slice(&["boringssl/ssl", "boringssl/crypto"]);
153152
}
153+
// Uses zlib from libz-sys.
154+
setup_libz(&mut config);
154155
config.build_target(library).uses_cxx11().build()
155156
};
156157

157-
let mut zlib = "z";
158158
let build_dir = format!("{}/build", dst.display());
159159
if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") {
160160
let profile = match &*env::var("PROFILE").unwrap_or("debug".to_owned()) {
161-
"bench" | "release" => {
162-
zlib = "zlibstatic";
163-
"Release"
164-
}
165-
_ => {
166-
zlib = "zlibstaticd";
167-
"Debug"
168-
}
161+
"bench" | "release" => "Release",
162+
_ => "Debug",
169163
};
170164
println!("cargo:rustc-link-search=native={}/{}", build_dir, profile);
171165
for path in third_party {
@@ -184,7 +178,7 @@ fn build_grpc(cc: &mut Build, library: &str) {
184178
}
185179
}
186180

187-
println!("cargo:rustc-link-lib=static={}", zlib);
181+
println!("cargo:rustc-link-lib=static=z");
188182
println!("cargo:rustc-link-lib=static=cares");
189183
println!("cargo:rustc-link-lib=static=gpr");
190184
println!("cargo:rustc-link-lib=static=address_sorting");
@@ -231,6 +225,21 @@ fn figure_ssl_path(build_dir: &str) {
231225
println!("cargo:rustc-link-lib=crypto");
232226
}
233227

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

scripts/reset-submodule

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

0 commit comments

Comments
 (0)