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