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 ] ++ lib.optional splitStaticOutput "static";
58 setOutputFlags = false;
59 outputDoc = "dev"; # single tiny man3 page
60
61 dontConfigure = stdenv.hostPlatform.isMinGW;
62
63 preConfigure = lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
64 export CHOST=${stdenv.hostPlatform.config}
65 '';
66
67 # For zlib's ./configure (as of version 1.2.11), the order
68 # of --static/--shared flags matters!
69 # `--shared --static` builds only static libs, while
70 # `--static --shared` builds both.
71 # So we use the latter order to be able to build both.
72 # Also, giving just `--shared` builds both,
73 # giving just `--static` builds only static,
74 # and giving nothing builds both.
75 # So we have 3 possible ways to build both:
76 # `--static --shared`, `--shared` and giving nothing.
77 # Of these, we choose `--static --shared`, for clarity and simpler
78 # conditions.
79 configureFlags = lib.optional static "--static" ++ lib.optional shared "--shared";
80 # We do the right thing manually, above, so don't need these.
81 dontDisableStatic = true;
82 dontAddStaticConfigureFlags = true;
83
84 # Note we don't need to set `dontDisableStatic`, because static-disabling
85 # works by grepping for `enable-static` in the `./configure` script
86 # (see `pkgs/stdenv/generic/setup.sh`), and zlib's handwritten one does
87 # not have such.
88 # It wouldn't hurt setting `dontDisableStatic = static && !splitStaticOutput`
89 # here (in case zlib ever switches to autoconf in the future),
90 # but we don't do it simply to avoid mass rebuilds.
91
92 postInstall =
93 lib.optionalString splitStaticOutput ''
94 moveToOutput lib/libz.a "$static"
95 ''
96 # jww (2015-01-06): Sometimes this library install as a .so, even on
97 # Darwin; others time it installs as a .dylib. I haven't yet figured out
98 # what causes this difference.
99 + lib.optionalString stdenv.hostPlatform.isDarwin ''
100 for file in $out/lib/*.so* $out/lib/*.dylib* ; do
101 ${stdenv.cc.bintools.targetPrefix}install_name_tool -id "$file" $file
102 done
103 ''
104 # Non-typical naming confuses libtool which then refuses to use zlib's DLL
105 # in some cases, e.g. when compiling libpng.
106 + lib.optionalString (stdenv.hostPlatform.isMinGW && shared) ''
107 ln -s zlib1.dll $out/bin/libz.dll
108 '';
109
110 env =
111 lib.optionalAttrs (!stdenv.hostPlatform.isDarwin) {
112 # As zlib takes part in the stdenv building, we don't want references
113 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
114 NIX_CFLAGS_COMPILE = "-static-libgcc";
115 }
116 // lib.optionalAttrs (stdenv.hostPlatform.linker == "lld") {
117 # lld 16 enables --no-undefined-version by default
118 # This makes configure think it can't build dynamic libraries
119 # this may be removed when a version is packaged with https://github.com/madler/zlib/issues/960 fixed
120 NIX_LDFLAGS = "--undefined-version";
121 };
122
123 # We don't strip on static cross-compilation because of reports that native
124 # stripping corrupted the target library; see commit 12e960f5 for the report.
125 dontStrip = stdenv.hostPlatform != stdenv.buildPlatform && static;
126 configurePlatforms = [ ];
127
128 installFlags = lib.optionals stdenv.hostPlatform.isMinGW [
129 "BINARY_PATH=$(out)/bin"
130 "INCLUDE_PATH=$(dev)/include"
131 "LIBRARY_PATH=$(out)/lib"
132 ];
133
134 enableParallelBuilding = true;
135 doCheck = true;
136
137 makeFlags =
138 [
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})