1{ lib, stdenv
2, fetchurl
3, shared ? !stdenv.hostPlatform.isStatic
4, static ? true
5# If true, a separate .static ouput is created and the .a is moved there.
6# In this case `pkg-config` auto detection does not currently work if the
7# .static output is given as `buildInputs` to another package (#66461), because
8# the `.pc` file lists only the main output's lib dir.
9# If false, and if `{ static = true; }`, the .a stays in the main output.
10, splitStaticOutput ? shared && static
11, testers
12, minizip
13}:
14
15# Without either the build will actually still succeed because the build
16# system makes an arbitrary choice, but we shouldn't be so indecisive.
17assert shared || static;
18
19# Note: this package is used for bootstrapping fetchurl, and thus
20# cannot use fetchpatch! All mutable patches (generated by GitHub or
21# cgit) that are needed here should be included directly in Nixpkgs as
22# files.
23
24assert splitStaticOutput -> static;
25
26stdenv.mkDerivation (finalAttrs: {
27 pname = "zlib";
28 version = "1.3.1";
29
30 src = let
31 inherit (finalAttrs) version;
32 in fetchurl {
33 urls = [
34 # This URL works for 1.2.13 only; hopefully also for future releases.
35 "https://github.com/madler/zlib/releases/download/v${version}/zlib-${version}.tar.gz"
36 # Stable archive path, but captcha can be encountered, causing hash mismatch.
37 "https://www.zlib.net/fossils/zlib-${version}.tar.gz"
38 ];
39 hash = "sha256-mpOyt9/ax3zrpaVYpYDnRmfdb+3kWFuR7vtg8Dty3yM=";
40 };
41
42 postPatch = lib.optionalString stdenv.hostPlatform.isDarwin ''
43 substituteInPlace configure \
44 --replace '/usr/bin/libtool' '${stdenv.cc.targetPrefix}ar' \
45 --replace 'AR="libtool"' 'AR="${stdenv.cc.targetPrefix}ar"' \
46 --replace 'ARFLAGS="-o"' 'ARFLAGS="-r"'
47 '';
48
49 strictDeps = true;
50 outputs = [ "out" "dev" ]
51 ++ lib.optional splitStaticOutput "static";
52 setOutputFlags = false;
53 outputDoc = "dev"; # single tiny man3 page
54
55 dontConfigure = stdenv.hostPlatform.isMinGW;
56
57 preConfigure = lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
58 export CHOST=${stdenv.hostPlatform.config}
59 '';
60
61 # For zlib's ./configure (as of version 1.2.11), the order
62 # of --static/--shared flags matters!
63 # `--shared --static` builds only static libs, while
64 # `--static --shared` builds both.
65 # So we use the latter order to be able to build both.
66 # Also, giving just `--shared` builds both,
67 # giving just `--static` builds only static,
68 # and giving nothing builds both.
69 # So we have 3 possible ways to build both:
70 # `--static --shared`, `--shared` and giving nothing.
71 # Of these, we choose `--static --shared`, for clarity and simpler
72 # conditions.
73 configureFlags = lib.optional static "--static"
74 ++ lib.optional shared "--shared";
75 # We do the right thing manually, above, so don't need these.
76 dontDisableStatic = true;
77 dontAddStaticConfigureFlags = true;
78
79 # Note we don't need to set `dontDisableStatic`, because static-disabling
80 # works by grepping for `enable-static` in the `./configure` script
81 # (see `pkgs/stdenv/generic/setup.sh`), and zlib's handwritten one does
82 # not have such.
83 # It wouldn't hurt setting `dontDisableStatic = static && !splitStaticOutput`
84 # here (in case zlib ever switches to autoconf in the future),
85 # but we don't do it simply to avoid mass rebuilds.
86
87 postInstall = lib.optionalString splitStaticOutput ''
88 moveToOutput lib/libz.a "$static"
89 ''
90 # jww (2015-01-06): Sometimes this library install as a .so, even on
91 # Darwin; others time it installs as a .dylib. I haven't yet figured out
92 # what causes this difference.
93 + lib.optionalString stdenv.hostPlatform.isDarwin ''
94 for file in $out/lib/*.so* $out/lib/*.dylib* ; do
95 ${stdenv.cc.bintools.targetPrefix}install_name_tool -id "$file" $file
96 done
97 ''
98 # Non-typical naming confuses libtool which then refuses to use zlib's DLL
99 # in some cases, e.g. when compiling libpng.
100 + lib.optionalString (stdenv.hostPlatform.isMinGW && shared) ''
101 ln -s zlib1.dll $out/bin/libz.dll
102 '';
103
104 env = lib.optionalAttrs (!stdenv.hostPlatform.isDarwin) {
105 # As zlib takes part in the stdenv building, we don't want references
106 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
107 NIX_CFLAGS_COMPILE = "-static-libgcc";
108 } // lib.optionalAttrs (stdenv.hostPlatform.linker == "lld") {
109 # lld 16 enables --no-undefined-version by defualt
110 # This makes configure think it can't build dynamic libraries
111 # this may be removed when a version is packaged with https://github.com/madler/zlib/issues/960 fixed
112 NIX_LDFLAGS = "--undefined-version";
113 };
114
115 # We don't strip on static cross-compilation because of reports that native
116 # stripping corrupted the target library; see commit 12e960f5 for the report.
117 dontStrip = stdenv.hostPlatform != stdenv.buildPlatform && static;
118 configurePlatforms = [];
119
120 installFlags = lib.optionals stdenv.hostPlatform.isMinGW [
121 "BINARY_PATH=$(out)/bin"
122 "INCLUDE_PATH=$(dev)/include"
123 "LIBRARY_PATH=$(out)/lib"
124 ];
125
126 enableParallelBuilding = true;
127 doCheck = true;
128
129 makeFlags = [
130 "PREFIX=${stdenv.cc.targetPrefix}"
131 ] ++ lib.optionals stdenv.hostPlatform.isMinGW [
132 "-f" "win32/Makefile.gcc"
133 ] ++ lib.optionals shared [
134 # Note that as of writing (zlib 1.2.11), this flag only has an effect
135 # for Windows as it is specific to `win32/Makefile.gcc`.
136 "SHARED_MODE=1"
137 ];
138
139 passthru.tests = {
140 pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage;
141 # uses `zlib` derivation:
142 inherit minizip;
143 };
144
145 meta = with lib; {
146 homepage = "https://zlib.net";
147 description = "Lossless data-compression library";
148 license = licenses.zlib;
149 platforms = platforms.all;
150 pkgConfigModules = [ "zlib" ];
151 };
152})