nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ stdenv
2, fetchurl
3# Note: If `{ static = false; shared = false; }`, upstream's default is used
4# (which is building both static and shared as of zlib 1.2.11).
5, shared ? true
6, static ? true
7# If true, a separate .static ouput 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 ? static
13}:
14
15# Note: this package is used for bootstrapping fetchurl, and thus
16# cannot use fetchpatch! All mutable patches (generated by GitHub or
17# cgit) that are needed here should be included directly in Nixpkgs as
18# files.
19
20assert splitStaticOutput -> static;
21
22stdenv.mkDerivation (rec {
23 name = "zlib-${version}";
24 version = "1.2.11";
25
26 src = fetchurl {
27 urls =
28 [ "https://www.zlib.net/fossils/${name}.tar.gz" # stable archive path
29 "mirror://sourceforge/libpng/zlib/${version}/${name}.tar.gz"
30 ];
31 sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1";
32 };
33
34 patches = stdenv.lib.optional stdenv.hostPlatform.isCygwin ./disable-cygwin-widechar.patch;
35
36 postPatch = stdenv.lib.optionalString stdenv.hostPlatform.isDarwin ''
37 substituteInPlace configure \
38 --replace '/usr/bin/libtool' 'ar' \
39 --replace 'AR="libtool"' 'AR="ar"' \
40 --replace 'ARFLAGS="-o"' 'ARFLAGS="-r"'
41 '';
42
43 outputs = [ "out" "dev" ]
44 ++ stdenv.lib.optional splitStaticOutput "static";
45 setOutputFlags = false;
46 outputDoc = "dev"; # single tiny man3 page
47
48 # For zlib's ./configure (as of verion 1.2.11), the order
49 # of --static/--shared flags matters!
50 # `--shared --static` builds only static libs, while
51 # `--static --shared` builds both.
52 # So we use the latter order to be able to build both.
53 # Also, giving just `--shared` builds both,
54 # giving just `--static` builds only static,
55 # and giving nothing builds both.
56 # So we have 3 possible ways to build both:
57 # `--static --shared`, `--shared` and giving nothing.
58 # Of these, we choose `--shared`, only because that's
59 # what we did in the past and we can avoid mass rebuilds this way.
60 # As a result, we pass `--static` only when we want just static.
61 configureFlags = stdenv.lib.optional (static && !shared) "--static"
62 ++ stdenv.lib.optional shared "--shared";
63
64 # Note we don't need to set `dontDisableStatic`, because static-disabling
65 # works by grepping for `enable-static` in the `./configure` script
66 # (see `pkgs/stdenv/generic/setup.sh`), and zlib's handwritten one does
67 # not have such.
68 # It wouldn't hurt setting `dontDisableStatic = static && !splitStaticOutput`
69 # here (in case zlib ever switches to autoconf in the future),
70 # but we don't do it simply to avoid mass rebuilds.
71
72 postInstall = stdenv.lib.optionalString splitStaticOutput ''
73 moveToOutput lib/libz.a "$static"
74 ''
75 # jww (2015-01-06): Sometimes this library install as a .so, even on
76 # Darwin; others time it installs as a .dylib. I haven't yet figured out
77 # what causes this difference.
78 + stdenv.lib.optionalString stdenv.hostPlatform.isDarwin ''
79 for file in $out/lib/*.so* $out/lib/*.dylib* ; do
80 ${stdenv.cc.bintools.targetPrefix}install_name_tool -id "$file" $file
81 done
82 ''
83 # Non-typical naming confuses libtool which then refuses to use zlib's DLL
84 # in some cases, e.g. when compiling libpng.
85 + stdenv.lib.optionalString (stdenv.hostPlatform.libc == "msvcrt") ''
86 ln -s zlib1.dll $out/bin/libz.dll
87 '';
88
89 # As zlib takes part in the stdenv building, we don't want references
90 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
91 NIX_CFLAGS_COMPILE = stdenv.lib.optionalString (!stdenv.hostPlatform.isDarwin) "-static-libgcc";
92
93 # We don't strip on static cross-compilation because of reports that native
94 # stripping corrupted the target library; see commit 12e960f5 for the report.
95 dontStrip = stdenv.hostPlatform != stdenv.buildPlatform && static;
96 configurePlatforms = [];
97
98 installFlags = stdenv.lib.optionals (stdenv.hostPlatform.libc == "msvcrt") [
99 "BINARY_PATH=$(out)/bin"
100 "INCLUDE_PATH=$(dev)/include"
101 "LIBRARY_PATH=$(out)/lib"
102 ];
103
104 enableParallelBuilding = true;
105 doCheck = true;
106
107 makeFlags = [
108 "PREFIX=${stdenv.cc.targetPrefix}"
109 ] ++ stdenv.lib.optionals (stdenv.hostPlatform.libc == "msvcrt") [
110 "-f" "win32/Makefile.gcc"
111 ] ++ stdenv.lib.optionals shared [
112 # Note that as of writing (zlib 1.2.11), this flag only has an effect
113 # for Windows as it is specific to `win32/Makefile.gcc`.
114 "SHARED_MODE=1"
115 ];
116
117 passthru = {
118 inherit version;
119 };
120
121 meta = with stdenv.lib; {
122 homepage = "https://zlib.net";
123 description = "Lossless data-compression library";
124 license = licenses.zlib;
125 platforms = platforms.all;
126 };
127} // stdenv.lib.optionalAttrs (stdenv.hostPlatform != stdenv.buildPlatform) {
128 preConfigure = ''
129 export CHOST=${stdenv.hostPlatform.config}
130 '';
131} // stdenv.lib.optionalAttrs (stdenv.hostPlatform.libc == "msvcrt") {
132 dontConfigure = true;
133})