From 5cccbe77b02cec69641856025a1744b1ca9558ed Mon Sep 17 00:00:00 2001 From: Brennan Kinney <5098581+polarathene@users.noreply.github.com> Date: Fri, 14 Nov 2025 21:51:28 +1300 Subject: [PATCH 1/2] 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. --- build.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index cd11af8b..5228979f 100644 --- a/build.rs +++ b/build.rs @@ -30,12 +30,11 @@ fn main() { return; } + let want_static = should_link_static(); // Don't run pkg-config if we're linking statically (we'll build below) and // also don't run pkg-config on FreeBSD/DragonFly. That'll end up printing // `-L /usr/lib` which wreaks havoc with linking to an OpenSSL in /usr/local/lib // (Ports, etc.) - let want_static = - cfg!(feature = "static") || env::var("LIBZ_SYS_STATIC").unwrap_or(String::new()) == "1"; if !want_static && !target.contains("msvc") && // pkg-config just never works here !(host_and_target_contain("freebsd") || @@ -238,3 +237,22 @@ fn zlib_installed(cfg: &mut cc::Build) -> bool { false } + +// The environment variable `LIBZ_SYS_STATIC` is first checked for a value of `0` (false) or `1` (true), +// before considering the `static` feature when no explicit ENV value was detected. +// When `libz-sys` is a transitive dependency from a crate that forces static linking via the `static` feature, +// this enables the build environment to revert that preference via `LIBZ_SYS_STATIC=0`. +// The default is otherwise `false`. +fn should_link_static() -> bool { + let has_static_env: Option<&'static str> = option_env!("LIBZ_SYS_STATIC"); + let has_static_cfg = cfg!(feature = "static"); + + has_static_env + .and_then(|s: &str| s.parse::().ok()) + .and_then(|b| match b { + 0 => Some(false), + 1 => Some(true), + _ => None, + }) + .unwrap_or(has_static_cfg) +} From 8cad56c64d210c80edc1a21fdeb2852f94192a19 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 15 Nov 2025 06:46:03 +0100 Subject: [PATCH 2/2] refactor - turn comments into doc-comments for nicer display in editors. --- build.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index 5228979f..2f922154 100644 --- a/build.rs +++ b/build.rs @@ -238,11 +238,11 @@ fn zlib_installed(cfg: &mut cc::Build) -> bool { false } -// The environment variable `LIBZ_SYS_STATIC` is first checked for a value of `0` (false) or `1` (true), -// before considering the `static` feature when no explicit ENV value was detected. -// When `libz-sys` is a transitive dependency from a crate that forces static linking via the `static` feature, -// this enables the build environment to revert that preference via `LIBZ_SYS_STATIC=0`. -// The default is otherwise `false`. +/// The environment variable `LIBZ_SYS_STATIC` is first checked for a value of `0` (false) or `1` (true), +/// before considering the `static` feature when no explicit ENV value was detected. +/// When `libz-sys` is a transitive dependency from a crate that forces static linking via the `static` feature, +/// this enables the build environment to revert that preference via `LIBZ_SYS_STATIC=0`. +/// The default is otherwise `false`. fn should_link_static() -> bool { let has_static_env: Option<&'static str> = option_env!("LIBZ_SYS_STATIC"); let has_static_cfg = cfg!(feature = "static");