zlib: simplify arguments & explain --shared & --static (#490608)

This commit is contained in:
Doron Behar
2026-02-21 17:22:37 +00:00
committed by GitHub
+28 -38
View File
@@ -3,25 +3,31 @@
stdenv,
fetchurl,
shared ? !stdenv.hostPlatform.isStatic,
static ? true,
# If true, a separate .static output is created and the .a is moved there.
# If false, and if `{ static = true; }`, the .a stays in the main output.
splitStaticOutput ? shared && static && !stdenv.hostPlatform.isWindows,
# zlib's hand-written `./configure` script is a bit buggy, and it always
# builds the static library. The `--enable-shared` argument (alias to
# `--shared`) makes it build the shared library too. The `--static` argument
# is particularly confusing - if used after `--shared` it disables the build
# of the shared library. See also:
#
# - https://github.com/madler/zlib/pull/394
#
# Since --static is aliased to --disable-shared, we use this flag in the
# `configureFlags` below, along with a few more relevant arguments to
# `stdenv.mkDerivation`.
#
# We avoid the closure size penalty of this buggy upstream behavior by using
# a dedicated output for the static library, which is fortuenatly also
# supported by the `--libdir` argument to `./configure`.
splitStaticOutput ? shared && !stdenv.hostPlatform.isWindows,
testers,
minizip,
}:
# Without either the build will actually still succeed because the build
# system makes an arbitrary choice, but we shouldn't be so indecisive.
assert shared || static;
# Note: this package is used for bootstrapping fetchurl, and thus
# cannot use fetchpatch! All mutable patches (generated by GitHub or
# cgit) that are needed here should be included directly in Nixpkgs as
# files.
assert splitStaticOutput -> static;
stdenv.mkDerivation (finalAttrs: {
pname = "zlib";
version = "1.3.2";
@@ -40,17 +46,14 @@ stdenv.mkDerivation (finalAttrs: {
hash = "sha256-uzKaCizQJ00FUZ1hxmfAYuBpkNcuEl7i36jeZPARnRY=";
};
postPatch =
lib.optionalString stdenv.hostPlatform.isDarwin ''
substituteInPlace configure \
--replace '/usr/bin/libtool' '${stdenv.cc.targetPrefix}ar' \
--replace 'AR="libtool"' 'AR="${stdenv.cc.targetPrefix}ar"' \
--replace 'ARFLAGS="-o"' 'ARFLAGS="-r"'
''
+ lib.optionalString stdenv.hostPlatform.isCygwin ''
substituteInPlace win32/zlib.def \
--replace-fail 'gzopen_w' ""
'';
postPatch = ''
substituteInPlace configure \
--replace-fail '/usr/bin/libtool' '${stdenv.cc.targetPrefix}ar' \
--replace-fail 'AR="libtool"' 'AR="${stdenv.cc.targetPrefix}ar"' \
--replace-fail 'ARFLAGS="-o"' 'ARFLAGS="-r"'
substituteInPlace win32/zlib.def \
--replace-fail 'gzopen_w' ""
'';
strictDeps = true;
outputs = [
@@ -71,22 +74,9 @@ stdenv.mkDerivation (finalAttrs: {
"--includedir=${placeholder "dev"}/include"
"--sharedlibdir=${placeholder "out"}/lib"
"--libdir=${placeholder (if splitStaticOutput then "static" else "out")}/lib"
]
# For zlib's ./configure (as of version 1.2.11), the order
# of --static/--shared flags matters!
# `--shared --static` builds only static libs, while
# `--static --shared` builds both.
# So we use the latter order to be able to build both.
# Also, giving just `--shared` builds both,
# giving just `--static` builds only static,
# and giving nothing builds both.
# So we have 3 possible ways to build both:
# `--static --shared`, `--shared` and giving nothing.
# Of these, we choose `--static --shared`, for clarity and simpler
# conditions.
++ lib.optional static "--static"
++ lib.optional shared "--shared";
# We do the right thing manually, above, so don't need these.
# See comment near splitStaticOutput argument
(lib.enableFeature shared "shared")
];
dontDisableStatic = true;
dontAddStaticConfigureFlags = true;
@@ -130,7 +120,7 @@ stdenv.mkDerivation (finalAttrs: {
# We don't strip on static cross-compilation because of reports that native
# stripping corrupted the target library; see commit 12e960f5 for the report.
dontStrip = stdenv.hostPlatform != stdenv.buildPlatform && static;
dontStrip = stdenv.hostPlatform != stdenv.buildPlatform;
configurePlatforms = [ ];
installFlags = lib.optionals (stdenv.hostPlatform.isMinGW || stdenv.hostPlatform.isCygwin) [