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