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}:
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.2.13";
28
29 src = let
30 inherit (finalAttrs) version;
31 in fetchurl {
32 urls = [
33 # This URL works for 1.2.13 only; hopefully also for future releases.
34 "https://github.com/madler/zlib/releases/download/v${version}/zlib-${version}.tar.gz"
35 # Stable archive path, but captcha can be encountered, causing hash mismatch.
36 "https://www.zlib.net/fossils/zlib-${version}.tar.gz"
37 ];
38 hash = "sha256-s6JN6XqP28g1uYMxaVAQMLiXcDG8tUs7OsE3QPhGqzA=";
39 };
40
41 postPatch = lib.optionalString stdenv.hostPlatform.isDarwin ''
42 substituteInPlace configure \
43 --replace '/usr/bin/libtool' '${stdenv.cc.targetPrefix}ar' \
44 --replace 'AR="libtool"' 'AR="${stdenv.cc.targetPrefix}ar"' \
45 --replace 'ARFLAGS="-o"' 'ARFLAGS="-r"'
46 '';
47
48 strictDeps = true;
49 outputs = [ "out" "dev" ]
50 ++ lib.optional splitStaticOutput "static";
51 setOutputFlags = false;
52 outputDoc = "dev"; # single tiny man3 page
53
54 dontConfigure = stdenv.hostPlatform.libc == "msvcrt";
55
56 preConfigure = lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
57 export CHOST=${stdenv.hostPlatform.config}
58 '';
59
60 # For zlib's ./configure (as of version 1.2.11), the order
61 # of --static/--shared flags matters!
62 # `--shared --static` builds only static libs, while
63 # `--static --shared` builds both.
64 # So we use the latter order to be able to build both.
65 # Also, giving just `--shared` builds both,
66 # giving just `--static` builds only static,
67 # and giving nothing builds both.
68 # So we have 3 possible ways to build both:
69 # `--static --shared`, `--shared` and giving nothing.
70 # Of these, we choose `--static --shared`, for clarity and simpler
71 # conditions.
72 configureFlags = lib.optional static "--static"
73 ++ lib.optional shared "--shared";
74 # We do the right thing manually, above, so don't need these.
75 dontDisableStatic = true;
76 dontAddStaticConfigureFlags = true;
77
78 # Note we don't need to set `dontDisableStatic`, because static-disabling
79 # works by grepping for `enable-static` in the `./configure` script
80 # (see `pkgs/stdenv/generic/setup.sh`), and zlib's handwritten one does
81 # not have such.
82 # It wouldn't hurt setting `dontDisableStatic = static && !splitStaticOutput`
83 # here (in case zlib ever switches to autoconf in the future),
84 # but we don't do it simply to avoid mass rebuilds.
85
86 postInstall = lib.optionalString splitStaticOutput ''
87 moveToOutput lib/libz.a "$static"
88 ''
89 # jww (2015-01-06): Sometimes this library install as a .so, even on
90 # Darwin; others time it installs as a .dylib. I haven't yet figured out
91 # what causes this difference.
92 + lib.optionalString stdenv.hostPlatform.isDarwin ''
93 for file in $out/lib/*.so* $out/lib/*.dylib* ; do
94 ${stdenv.cc.bintools.targetPrefix}install_name_tool -id "$file" $file
95 done
96 ''
97 # Non-typical naming confuses libtool which then refuses to use zlib's DLL
98 # in some cases, e.g. when compiling libpng.
99 + lib.optionalString (stdenv.hostPlatform.libc == "msvcrt" && shared) ''
100 ln -s zlib1.dll $out/bin/libz.dll
101 '';
102
103 # As zlib takes part in the stdenv building, we don't want references
104 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
105 env.NIX_CFLAGS_COMPILE = lib.optionalString (!stdenv.hostPlatform.isDarwin) "-static-libgcc";
106
107 # We don't strip on static cross-compilation because of reports that native
108 # stripping corrupted the target library; see commit 12e960f5 for the report.
109 dontStrip = stdenv.hostPlatform != stdenv.buildPlatform && static;
110 configurePlatforms = [];
111
112 installFlags = lib.optionals (stdenv.hostPlatform.libc == "msvcrt") [
113 "BINARY_PATH=$(out)/bin"
114 "INCLUDE_PATH=$(dev)/include"
115 "LIBRARY_PATH=$(out)/lib"
116 ];
117
118 enableParallelBuilding = true;
119 doCheck = true;
120
121 makeFlags = [
122 "PREFIX=${stdenv.cc.targetPrefix}"
123 ] ++ lib.optionals (stdenv.hostPlatform.libc == "msvcrt") [
124 "-f" "win32/Makefile.gcc"
125 ] ++ lib.optionals shared [
126 # Note that as of writing (zlib 1.2.11), this flag only has an effect
127 # for Windows as it is specific to `win32/Makefile.gcc`.
128 "SHARED_MODE=1"
129 ];
130
131 passthru.tests.pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage;
132
133 meta = with lib; {
134 homepage = "https://zlib.net";
135 description = "Lossless data-compression library";
136 license = licenses.zlib;
137 platforms = platforms.all;
138 pkgConfigModules = [ "zlib" ];
139 };
140})