1{
2 stdenv,
3 darwin,
4 lib,
5 pkg-config,
6 autoreconfHook,
7 python3,
8 ncurses,
9 findXMLCatalogs,
10 libiconv,
11 # Python limits cross-compilation to an allowlist of host OSes.
12 # https://github.com/python/cpython/blob/dfad678d7024ab86d265d84ed45999e031a03691/configure.ac#L534-L562
13 pythonSupport ?
14 enableShared
15 && (
16 stdenv.hostPlatform == stdenv.buildPlatform
17 || stdenv.hostPlatform.isCygwin
18 || stdenv.hostPlatform.isLinux
19 || stdenv.hostPlatform.isWasi
20 ),
21 icuSupport ? false,
22 icu,
23 zlibSupport ? false,
24 zlib,
25 enableShared ? !stdenv.hostPlatform.isMinGW && !stdenv.hostPlatform.isStatic,
26 enableStatic ? !enableShared,
27 gnome,
28 testers,
29 enableHttp ? false,
30
31 version,
32 extraPatches ? [ ],
33 src,
34 extraMeta ? { },
35 freezeUpdateScript ? false,
36}:
37
38let
39 # libxml2 is a dependency of xcbuild. Avoid an infinite recursion by using a bootstrap stdenv
40 # that does not propagate xcrun.
41 stdenv' = if stdenv.hostPlatform.isDarwin then darwin.bootstrapStdenv else stdenv;
42in
43stdenv'.mkDerivation (finalAttrs: {
44 inherit
45 version
46 src
47 ;
48
49 pname = "libxml2";
50
51 outputs = [
52 "bin"
53 "dev"
54 "out"
55 "devdoc"
56 ]
57 ++ lib.optional pythonSupport "py"
58 ++ lib.optional (enableStatic && enableShared) "static";
59 outputMan = "bin";
60
61 patches = [
62 # Unmerged ABI-breaking patch required to fix the following security issues:
63 # - https://gitlab.gnome.org/GNOME/libxslt/-/issues/139
64 # - https://gitlab.gnome.org/GNOME/libxslt/-/issues/140
65 # See also https://gitlab.gnome.org/GNOME/libxml2/-/issues/906
66 # Source: https://github.com/chromium/chromium/blob/4fb4ae8ce3daa399c3d8ca67f2dfb9deffcc7007/third_party/libxml/chromium/xml-attr-extra.patch
67 ./xml-attr-extra.patch
68 ]
69 ++ extraPatches;
70
71 strictDeps = true;
72
73 nativeBuildInputs = [
74 pkg-config
75 autoreconfHook
76 ];
77
78 buildInputs =
79 lib.optionals pythonSupport [
80 ncurses
81 python3
82 ]
83 ++ lib.optionals zlibSupport [
84 zlib
85 ];
86
87 propagatedBuildInputs = [
88 findXMLCatalogs
89 ]
90 ++ lib.optionals (stdenv.hostPlatform.isDarwin || stdenv.hostPlatform.isMinGW) [
91 libiconv
92 ]
93 ++ lib.optionals icuSupport [
94 icu
95 ];
96
97 configureFlags = [
98 "--exec-prefix=${placeholder "dev"}"
99 (lib.enableFeature enableStatic "static")
100 (lib.enableFeature enableShared "shared")
101 (lib.withFeature icuSupport "icu")
102 (lib.withFeature pythonSupport "python")
103 (lib.optionalString pythonSupport "PYTHON=${python3.pythonOnBuildForHost.interpreter}")
104 ]
105 # avoid rebuilds, can be merged into list in version bumps
106 ++ lib.optional enableHttp "--with-http"
107 ++ lib.optional zlibSupport "--with-zlib";
108
109 installFlags = lib.optionals pythonSupport [
110 "pythondir=\"${placeholder "py"}/${python3.sitePackages}\""
111 "pyexecdir=\"${placeholder "py"}/${python3.sitePackages}\""
112 ];
113
114 enableParallelBuilding = true;
115
116 doCheck = (stdenv.hostPlatform == stdenv.buildPlatform) && stdenv.hostPlatform.libc != "musl";
117 preCheck = lib.optional stdenv.hostPlatform.isDarwin ''
118 export DYLD_LIBRARY_PATH="$PWD/.libs:$DYLD_LIBRARY_PATH"
119 '';
120
121 preConfigure = lib.optionalString (lib.versionAtLeast stdenv.hostPlatform.darwinMinVersion "11") ''
122 MACOSX_DEPLOYMENT_TARGET=10.16
123 '';
124
125 preInstall = lib.optionalString pythonSupport ''
126 substituteInPlace python/libxml2mod.la --replace-fail "$dev/${python3.sitePackages}" "$py/${python3.sitePackages}"
127 '';
128
129 postFixup = ''
130 moveToOutput bin/xml2-config "$dev"
131 moveToOutput lib/xml2Conf.sh "$dev"
132 ''
133 + lib.optionalString (enableStatic && enableShared) ''
134 moveToOutput lib/libxml2.a "$static"
135 '';
136
137 passthru = {
138 inherit pythonSupport;
139
140 updateScript = gnome.updateScript {
141 packageName = "libxml2";
142 versionPolicy = "none";
143 freeze = freezeUpdateScript;
144 };
145 tests = {
146 pkg-config = testers.hasPkgConfigModules {
147 package = finalAttrs.finalPackage;
148 };
149 cmake-config = testers.hasCmakeConfigModules {
150 moduleNames = [ "LibXml2" ];
151 package = finalAttrs.finalPackage;
152 };
153 };
154 };
155
156 meta = {
157 homepage = "https://gitlab.gnome.org/GNOME/libxml2";
158 description = "XML parsing library for C";
159 license = lib.licenses.mit;
160 platforms = lib.platforms.all;
161 pkgConfigModules = [ "libxml-2.0" ];
162 }
163 // extraMeta;
164})