Skip to content

Commit 5cccbe7

Browse files
authored
fix: Support opt-out via LIBZ_SYS_STATIC=0
This change refactors the `want_static` logic to additionally support `LIBZ_SYS_STATIC=0` as an explicit opt-out to the `static` feature when `libz-sys` is a transitive dependency and the build environment prefers to dynamic link `libz` instead.
1 parent 80c597a commit 5cccbe7

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

build.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ fn main() {
3030
return;
3131
}
3232

33+
let want_static = should_link_static();
3334
// Don't run pkg-config if we're linking statically (we'll build below) and
3435
// also don't run pkg-config on FreeBSD/DragonFly. That'll end up printing
3536
// `-L /usr/lib` which wreaks havoc with linking to an OpenSSL in /usr/local/lib
3637
// (Ports, etc.)
37-
let want_static =
38-
cfg!(feature = "static") || env::var("LIBZ_SYS_STATIC").unwrap_or(String::new()) == "1";
3938
if !want_static &&
4039
!target.contains("msvc") && // pkg-config just never works here
4140
!(host_and_target_contain("freebsd") ||
@@ -238,3 +237,22 @@ fn zlib_installed(cfg: &mut cc::Build) -> bool {
238237

239238
false
240239
}
240+
241+
// The environment variable `LIBZ_SYS_STATIC` is first checked for a value of `0` (false) or `1` (true),
242+
// before considering the `static` feature when no explicit ENV value was detected.
243+
// When `libz-sys` is a transitive dependency from a crate that forces static linking via the `static` feature,
244+
// this enables the build environment to revert that preference via `LIBZ_SYS_STATIC=0`.
245+
// The default is otherwise `false`.
246+
fn should_link_static() -> bool {
247+
let has_static_env: Option<&'static str> = option_env!("LIBZ_SYS_STATIC");
248+
let has_static_cfg = cfg!(feature = "static");
249+
250+
has_static_env
251+
.and_then(|s: &str| s.parse::<u8>().ok())
252+
.and_then(|b| match b {
253+
0 => Some(false),
254+
1 => Some(true),
255+
_ => None,
256+
})
257+
.unwrap_or(has_static_cfg)
258+
}

0 commit comments

Comments
 (0)