1{ stdenv
2, lib
3, fetchurl
4, zlib
5, pkg-config
6, autoreconfHook
7, xz
8, libintl
9, python
10, gettext
11, ncurses
12, findXMLCatalogs
13, libiconv
14# Python limits cross-compilation to an allowlist of host OSes.
15# https://github.com/python/cpython/blob/dfad678d7024ab86d265d84ed45999e031a03691/configure.ac#L534-L562
16, pythonSupport ? enableShared &&
17 (stdenv.hostPlatform == stdenv.buildPlatform || stdenv.hostPlatform.isCygwin || stdenv.hostPlatform.isLinux || stdenv.hostPlatform.isWasi)
18, icuSupport ? false
19, icu
20, enableShared ? stdenv.hostPlatform.libc != "msvcrt" && !stdenv.hostPlatform.isStatic
21, enableStatic ? !enableShared
22, gnome
23}:
24
25let
26 # Newer versions fail with minimal python, probably because
27 # https://gitlab.gnome.org/GNOME/libxml2/-/commit/b706824b612adb2c8255819c9a55e78b52774a3c
28 # This case is encountered "temporarily" during stdenv bootstrapping on darwin.
29 # Beware that the old version has known security issues, so the final set shouldn't use it.
30 oldVer = python.pname == "python3-minimal";
31in
32 assert oldVer -> stdenv.isDarwin; # reduce likelihood of using old libxml2 unintentionally
33
34let
35libxml = stdenv.mkDerivation rec {
36 pname = "libxml2";
37 version = "2.10.4";
38
39 outputs = [ "bin" "dev" "out" "doc" ]
40 ++ lib.optional pythonSupport "py"
41 ++ lib.optional (enableStatic && enableShared) "static";
42 outputMan = "bin";
43
44 src = fetchurl {
45 url = "mirror://gnome/sources/libxml2/${lib.versions.majorMinor version}/libxml2-${version}.tar.xz";
46 sha256 = "7QyRxYRQCPGTZznk7uIDVTHByUdCxlQfRO5m2IWUjUU=";
47 };
48
49 patches = [
50 # Upstream bugs:
51 # https://bugzilla.gnome.org/show_bug.cgi?id=789714
52 # https://gitlab.gnome.org/GNOME/libxml2/issues/64
53 # Patch from https://bugzilla.opensuse.org/show_bug.cgi?id=1065270 ,
54 # but only the UTF-8 part.
55 # Can also be mitigated by fixing malformed XML inputs, such as in
56 # https://gitlab.gnome.org/GNOME/gnumeric/merge_requests/3 .
57 # Other discussion:
58 # https://github.com/itstool/itstool/issues/22
59 # https://github.com/NixOS/nixpkgs/pull/63174
60 # https://github.com/NixOS/nixpkgs/pull/72342
61 ./utf8-xmlErrorFuncHandler.patch
62 ];
63
64 strictDeps = true;
65
66 nativeBuildInputs = [
67 pkg-config
68 autoreconfHook
69 ];
70
71 buildInputs = lib.optionals pythonSupport [
72 python
73 ] ++ lib.optionals (pythonSupport && python?isPy2 && python.isPy2) [
74 gettext
75 ] ++ lib.optionals (pythonSupport && python?isPy3 && python.isPy3) [
76 ncurses
77 ] ++ lib.optionals (stdenv.isDarwin && pythonSupport && python?isPy2 && python.isPy2) [
78 libintl
79 ] ++ lib.optionals stdenv.isFreeBSD [
80 # Libxml2 has an optional dependency on liblzma. However, on impure
81 # platforms, it may end up using that from /usr/lib, and thus lack a
82 # RUNPATH for that, leading to undefined references for its users.
83 xz
84 ];
85
86 propagatedBuildInputs = [
87 zlib
88 findXMLCatalogs
89 ] ++ lib.optionals stdenv.isDarwin [
90 libiconv
91 ] ++ lib.optionals icuSupport [
92 icu
93 ];
94
95 configureFlags = [
96 "--exec-prefix=${placeholder "dev"}"
97 (lib.enableFeature enableStatic "static")
98 (lib.enableFeature enableShared "shared")
99 (lib.withFeature icuSupport "icu")
100 (lib.withFeature pythonSupport "python")
101 (lib.optionalString pythonSupport "PYTHON=${python.pythonForBuild.interpreter}")
102 ];
103
104 installFlags = lib.optionals pythonSupport [
105 "pythondir=\"${placeholder "py"}/${python.sitePackages}\""
106 "pyexecdir=\"${placeholder "py"}/${python.sitePackages}\""
107 ];
108
109 enableParallelBuilding = true;
110
111 doCheck =
112 (stdenv.hostPlatform == stdenv.buildPlatform) &&
113 stdenv.hostPlatform.libc != "musl";
114 preCheck = lib.optional stdenv.isDarwin ''
115 export DYLD_LIBRARY_PATH="$PWD/.libs:$DYLD_LIBRARY_PATH"
116 '';
117
118 preConfigure = lib.optionalString (lib.versionAtLeast stdenv.hostPlatform.darwinMinVersion "11") ''
119 MACOSX_DEPLOYMENT_TARGET=10.16
120 '';
121
122 preInstall = lib.optionalString pythonSupport ''
123 substituteInPlace python/libxml2mod.la --replace "$dev/${python.sitePackages}" "$py/${python.sitePackages}"
124 '';
125
126 postFixup = ''
127 moveToOutput bin/xml2-config "$dev"
128 moveToOutput lib/xml2Conf.sh "$dev"
129 '' + lib.optionalString (enableStatic && enableShared) ''
130 moveToOutput lib/libxml2.a "$static"
131 '';
132
133 passthru = {
134 inherit version;
135 pythonSupport = pythonSupport;
136
137 updateScript = gnome.updateScript {
138 packageName = pname;
139 versionPolicy = "none";
140 };
141 };
142
143 meta = with lib; {
144 homepage = "https://gitlab.gnome.org/GNOME/libxml2";
145 description = "XML parsing library for C";
146 license = licenses.mit;
147 platforms = platforms.all;
148 maintainers = with maintainers; [ eelco jtojnar ];
149 };
150};
151in
152if oldVer then
153 libxml.overrideAttrs (attrs: rec {
154 version = "2.10.1";
155 src = fetchurl {
156 url = "mirror://gnome/sources/libxml2/${lib.versions.majorMinor version}/libxml2-${version}.tar.xz";
157 sha256 = "21a9e13cc7c4717a6c36268d0924f92c3f67a1ece6b7ff9d588958a6db9fb9d8";
158 };
159 })
160else
161 libxml