|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const version: std.SemanticVersion = .{ .major = 2, .minor = 13, .patch = 5 }; |
| 4 | + |
| 5 | +pub fn build(b: *std.Build) void { |
| 6 | + const upstream = b.dependency("libxml2", .{}); |
| 7 | + const target = b.standardTargetOptions(.{}); |
| 8 | + const optimize = b.standardOptimizeOption(.{}); |
| 9 | + |
| 10 | + // Most of these config options have not been tested. |
| 11 | + |
| 12 | + const minimum = b.option(bool, "minimum", "build a minimally sized library (default=false)") orelse false; |
| 13 | + const legacy = b.option(bool, "legacy", "maximum ABI compatibility (default=false)") orelse false; |
| 14 | + const tls = b.option(bool, "tls", "thread-local storage (default=false)") orelse false; |
| 15 | + |
| 16 | + var c14n = b.option(bool, "c14n", "Canonical XML 1.0 support (default=true)") orelse !minimum; |
| 17 | + const catalog = b.option(bool, "catalog", "XML Catalogs support (default=true)") orelse !minimum; |
| 18 | + const debug = b.option(bool, "debug", "debugging module (default=true)") orelse !minimum; |
| 19 | + const ftp = b.option(bool, "ftp", "FTP support (default=false)") orelse false; |
| 20 | + const history = b.option(bool, "history", "history support for xmllint shell (default=false)") orelse false; |
| 21 | + var readline = b.option(bool, "readline", "use readline in DIR for shell (default=off)") orelse !minimum and history; |
| 22 | + const html = b.option(bool, "html", "HTML parser (default=true)") orelse !minimum; |
| 23 | + const http = b.option(bool, "http", "HTTP support (default=false)") orelse legacy; |
| 24 | + const iconv = b.option(bool, "iconv", "iconv support (default=on)") orelse !minimum; |
| 25 | + const icu = b.option(bool, "icu", "ICU support (default=false)") orelse false; |
| 26 | + const iso8859x = b.option(bool, "iso8859x", "ISO-8859-X support if no iconv (default=true)") orelse !minimum; |
| 27 | + const lzma = b.option(bool, "lzma", "use liblzma in DIR (default=off)") orelse legacy; |
| 28 | + // const modules = b.option(bool, "modules", "dynamic modules support (default=true)") orelse !minimum; |
| 29 | + var output = b.option(bool, "output", "serialization support (default=true)") orelse !minimum; |
| 30 | + var pattern = b.option(bool, "pattern", "xmlPattern selection interface (default=true)") orelse !minimum; |
| 31 | + var push = b.option(bool, "push", "push parser interfaces (default=true)") orelse !minimum; |
| 32 | + // const python = b.option(bool, "python", "Python bindings (default=true)") orelse !minimum; |
| 33 | + var reader = b.option(bool, "reader", "xmlReader parsing interface (default=true)") orelse !minimum; |
| 34 | + var regexps = b.option(bool, "regexps", "regular expressions support (default=true)") orelse !minimum; |
| 35 | + const sax1 = b.option(bool, "sax1", "older SAX1 interface (default=true)") orelse !minimum; |
| 36 | + var schemas = b.option(bool, "schemas", "XML Schemas 1.0 and RELAX NG support (default=true)") orelse !minimum; |
| 37 | + var schematron = b.option(bool, "schematron", "Schematron support (default=true)") orelse !minimum; |
| 38 | + const threads = b.option(bool, "threads", "multithreading support (default=true)") orelse !minimum; |
| 39 | + const thread_alloc = b.option(bool, "thread-alloc", "per-thread malloc hooks (default=false)") orelse false; |
| 40 | + const valid = b.option(bool, "valid", "DTD validation support (default=true)") orelse !minimum; |
| 41 | + var writer = b.option(bool, "writer", "xmlWriter serialization interface (default=true)") orelse !minimum; |
| 42 | + var xinclude = b.option(bool, "xinclude", "XInclude 1.0 support (default=true)") orelse !minimum; |
| 43 | + var xpath = b.option(bool, "xpath", "XPath 1.0 support (default=true)") orelse !minimum; |
| 44 | + var xptr = b.option(bool, "xptr", "XPointer support (default=true)") orelse !minimum; |
| 45 | + const xptr_locs = b.option(bool, "xptr-locs", "XPointer ranges and points (default=false)") orelse false; |
| 46 | + const zlib = b.option(bool, "zlib", "use libz in DIR") orelse legacy; |
| 47 | + |
| 48 | + if (c14n) { |
| 49 | + if (!output) { |
| 50 | + std.log.warn("-Dc14n overrides -Doutput=false", .{}); |
| 51 | + output = true; |
| 52 | + } |
| 53 | + if (!xpath) { |
| 54 | + std.log.warn("-Dc14n overrides -Dxpath=false", .{}); |
| 55 | + xpath = true; |
| 56 | + } |
| 57 | + } |
| 58 | + if (schemas) { |
| 59 | + if (!pattern) { |
| 60 | + std.log.warn("-Dschemas overrides -Dpattern=false", .{}); |
| 61 | + pattern = true; |
| 62 | + } |
| 63 | + if (!regexps) { |
| 64 | + std.log.warn("-Dschemas overrides -Dregexps=false", .{}); |
| 65 | + regexps = true; |
| 66 | + } |
| 67 | + } |
| 68 | + if (schematron) { |
| 69 | + if (!pattern) { |
| 70 | + std.log.warn("-Dschematron overrides -Dpattern=false", .{}); |
| 71 | + pattern = true; |
| 72 | + } |
| 73 | + if (!xpath) { |
| 74 | + std.log.warn("-Dschematron overrides -Dxpath=false", .{}); |
| 75 | + xpath = true; |
| 76 | + } |
| 77 | + } |
| 78 | + if (reader) { |
| 79 | + if (!push) { |
| 80 | + std.log.warn("-Dreader overrides -Dpush=false", .{}); |
| 81 | + push = true; |
| 82 | + } |
| 83 | + } |
| 84 | + if (writer) { |
| 85 | + if (!output) { |
| 86 | + std.log.warn("-Dwriter overrides -Doutput=false", .{}); |
| 87 | + output = true; |
| 88 | + } |
| 89 | + if (!push) { |
| 90 | + std.log.warn("-Dwriter overrides -Dpush=false", .{}); |
| 91 | + push = true; |
| 92 | + } |
| 93 | + } |
| 94 | + if (xinclude) { |
| 95 | + if (!xpath) { |
| 96 | + std.log.warn("-Dxinclude overrides -Dxpath=false", .{}); |
| 97 | + xpath = true; |
| 98 | + } |
| 99 | + } |
| 100 | + if (xptr_locs) { |
| 101 | + if (!xpath) { |
| 102 | + std.log.warn("-Dxptr-locs overrides -Dxptr=false", .{}); |
| 103 | + xpath = true; |
| 104 | + } |
| 105 | + } |
| 106 | + if (xptr) { |
| 107 | + if (!xpath) { |
| 108 | + std.log.warn("-Dxptr overrides -Dxpath=false", .{}); |
| 109 | + xpath = true; |
| 110 | + } |
| 111 | + } |
| 112 | + if (history) { |
| 113 | + if (!readline) { |
| 114 | + std.log.warn("-Dhistory overrides -Dreadline=false", .{}); |
| 115 | + readline = true; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (!minimum) { |
| 120 | + if (!output) { |
| 121 | + c14n = false; |
| 122 | + writer = false; |
| 123 | + } |
| 124 | + if (!pattern) { |
| 125 | + schemas = false; |
| 126 | + schematron = false; |
| 127 | + } |
| 128 | + if (!push) { |
| 129 | + reader = false; |
| 130 | + writer = false; |
| 131 | + } |
| 132 | + if (!regexps) { |
| 133 | + schemas = false; |
| 134 | + } |
| 135 | + if (!xpath) { |
| 136 | + c14n = false; |
| 137 | + schematron = false; |
| 138 | + xinclude = false; |
| 139 | + xptr = false; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + const xml_version_header = b.addConfigHeader(.{ |
| 144 | + .include_path = "libxml/xmlversion.h", |
| 145 | + .style = .{ .cmake = upstream.path("include/libxml/xmlversion.h.in") }, |
| 146 | + }, .{ |
| 147 | + .VERSION = b.fmt("{}", .{version}), |
| 148 | + .LIBXML_VERSION_NUMBER = @as(i64, version.major * 10000 + version.major * 100 + version.patch), |
| 149 | + .LIBXML_VERSION_EXTRA = "", |
| 150 | + .WITH_THREADS = threads, |
| 151 | + .WITH_THREAD_ALLOC = threads and thread_alloc, |
| 152 | + .WITH_TREE = true, |
| 153 | + .WITH_OUTPUT = output, |
| 154 | + .WITH_PUSH = push, |
| 155 | + .WITH_READER = reader, |
| 156 | + .WITH_PATTERN = pattern, |
| 157 | + .WITH_WRITER = writer, |
| 158 | + .WITH_SAX1 = sax1, |
| 159 | + .WITH_FTP = ftp, |
| 160 | + .WITH_HTTP = http, |
| 161 | + .WITH_VALID = valid, |
| 162 | + .WITH_HTML = html, |
| 163 | + .WITH_LEGACY = legacy, |
| 164 | + .WITH_C14N = c14n, |
| 165 | + .WITH_CATALOG = catalog, |
| 166 | + .WITH_XPATH = xpath, |
| 167 | + .WITH_XPTR = xptr, |
| 168 | + .WITH_XPTR_LOCS = xptr_locs, |
| 169 | + .WITH_XINCLUDE = xinclude, |
| 170 | + .WITH_ICONV = iconv, |
| 171 | + .WITH_ICU = icu, |
| 172 | + .WITH_ISO8859X = iso8859x, |
| 173 | + .WITH_DEBUG = debug, |
| 174 | + .WITH_REGEXPS = regexps, |
| 175 | + .WITH_SCHEMAS = schemas, |
| 176 | + .WITH_SCHEMATRON = schematron, |
| 177 | + .WITH_MODULES = false, |
| 178 | + .MODULE_EXTENSION = null, |
| 179 | + .WITH_ZLIB = zlib, |
| 180 | + .WITH_LZMA = lzma, |
| 181 | + }); |
| 182 | + |
| 183 | + const config_header = b.addConfigHeader(.{}, .{ |
| 184 | + .HAVE_STDINT_H = true, |
| 185 | + .HAVE_FCNTL_H = true, |
| 186 | + .HAVE_UNISTD_H = true, |
| 187 | + .HAVE_SYS_STAT_H = true, |
| 188 | + .HAVE_SYS_MMAN_H = true, |
| 189 | + .HAVE_SYS_TIME_H = true, |
| 190 | + .HAVE_SYS_TIMEB_H = true, |
| 191 | + .HAVE_SYS_RANDOM_H = true, |
| 192 | + .HAVE_DL_H = true, |
| 193 | + .HAVE_DLFCN_H = true, |
| 194 | + .HAVE_GLOB_H = true, |
| 195 | + .HAVE_DECL_GETENTROPY = true, |
| 196 | + .HAVE_DECL_GLOB = true, |
| 197 | + .HAVE_DECL_MMAP = true, |
| 198 | + .HAVE_POLL_H = true, |
| 199 | + .HAVE_ATTRIBUTE_DESTRUCTOR = true, |
| 200 | + .ATTRIBUTE_DESTRUCTOR = .@"__attribute__((destructor))", |
| 201 | + }); |
| 202 | + if (tls) { |
| 203 | + config_header.addValues(.{ .XML_THREAD_LOCAL = ._Thread_local }); |
| 204 | + } |
| 205 | + |
| 206 | + const xml_lib = b.addStaticLibrary(.{ |
| 207 | + .name = "xml", |
| 208 | + .target = target, |
| 209 | + .optimize = optimize, |
| 210 | + .version = version, |
| 211 | + .link_libc = true, |
| 212 | + }); |
| 213 | + b.installArtifact((xml_lib)); |
| 214 | + xml_lib.addConfigHeader(config_header); |
| 215 | + xml_lib.addConfigHeader(xml_version_header); |
| 216 | + xml_lib.installHeader(xml_version_header.getOutput(), "libxml/xmlversion.h"); |
| 217 | + xml_lib.addIncludePath(upstream.path("include")); |
| 218 | + xml_lib.addCSourceFiles(.{ .files = xml_src, .root = upstream.path(""), .flags = xml_flags }); |
| 219 | + xml_lib.installHeadersDirectory(upstream.path("include/libxml"), "libxml", .{}); |
| 220 | + if (target.result.os.tag != .windows) xml_lib.root_module.addCMacro("HAVE_PTHREAD_H", "1"); |
| 221 | + if (target.result.os.tag == .windows) xml_lib.root_module.addCMacro("LIBXML_STATIC", "1"); |
| 222 | + if (c14n) xml_lib.addCSourceFile(.{ .file = upstream.path("c14n.c"), .flags = xml_flags }); |
| 223 | + if (catalog) xml_lib.addCSourceFile(.{ .file = upstream.path("catalog.c"), .flags = xml_flags }); |
| 224 | + if (debug) xml_lib.addCSourceFile(.{ .file = upstream.path("debugXML.c"), .flags = xml_flags }); |
| 225 | + if (ftp) xml_lib.addCSourceFile(.{ .file = upstream.path("nanoftp.c"), .flags = xml_flags }); |
| 226 | + if (html) xml_lib.addCSourceFiles(.{ .files = &.{ "HTMLparser.c", "HTMLtree.c" }, .root = upstream.path(""), .flags = xml_flags }); |
| 227 | + if (http) xml_lib.addCSourceFile(.{ .file = upstream.path("nanohttp.c"), .flags = xml_flags }); |
| 228 | + if (legacy) xml_lib.addCSourceFile(.{ .file = upstream.path("legacy.c"), .flags = xml_flags }); |
| 229 | + if (lzma) xml_lib.addCSourceFile(.{ .file = upstream.path("xzlib.c"), .flags = xml_flags }); |
| 230 | + // if (modules) xml_lib.addCSourceFile(.{ .file = upstream.path("xmlmodule.c"), .flags = xml_flags }); |
| 231 | + if (output) xml_lib.addCSourceFile(.{ .file = upstream.path("xmlsave.c"), .flags = xml_flags }); |
| 232 | + if (pattern) xml_lib.addCSourceFile(.{ .file = upstream.path("pattern.c"), .flags = xml_flags }); |
| 233 | + if (reader) xml_lib.addCSourceFile(.{ .file = upstream.path("xmlreader.c"), .flags = xml_flags }); |
| 234 | + if (regexps) xml_lib.addCSourceFiles(.{ .files = &.{ "xmlregexp.c", "xmlunicode.c" }, .root = upstream.path(""), .flags = xml_flags }); |
| 235 | + if (schemas) xml_lib.addCSourceFiles(.{ .files = &.{ "relaxng.c", "xmlschemas.c", "xmlschemastypes.c" }, .root = upstream.path(""), .flags = xml_flags }); |
| 236 | + if (schematron) xml_lib.addCSourceFile(.{ .file = upstream.path("schematron.c"), .flags = xml_flags }); |
| 237 | + if (writer) xml_lib.addCSourceFile(.{ .file = upstream.path("xmlwriter.c"), .flags = xml_flags }); |
| 238 | + if (xinclude) xml_lib.addCSourceFile(.{ .file = upstream.path("xinclude.c"), .flags = xml_flags }); |
| 239 | + if (xpath) xml_lib.addCSourceFile(.{ .file = upstream.path("xpath.c"), .flags = xml_flags }); |
| 240 | + if (xptr) xml_lib.addCSourceFiles(.{ .files = &.{ "xlink.c", "xpointer.c" }, .root = upstream.path(""), .flags = xml_flags }); |
| 241 | + if (readline) { |
| 242 | + xml_lib.linkSystemLibrary("readline"); |
| 243 | + xml_lib.root_module.addCMacro("HAVE_LIBREADLINE", "1"); |
| 244 | + } |
| 245 | + if (history) { |
| 246 | + xml_lib.linkSystemLibrary("history"); |
| 247 | + xml_lib.root_module.addCMacro("HAVE_LIBHISTORY", "1"); |
| 248 | + } |
| 249 | + if (zlib) xml_lib.linkSystemLibrary("zlib"); |
| 250 | + if (lzma) xml_lib.linkSystemLibrary("lzma"); |
| 251 | + if (icu) xml_lib.linkSystemLibrary("icu-i18n"); |
| 252 | + if (iconv) xml_lib.linkSystemLibrary("iconv"); |
| 253 | + if (target.result.os.tag == .windows) xml_lib.linkSystemLibrary("bcrypt"); |
| 254 | + if (http and target.result.os.tag == .windows) xml_lib.linkSystemLibrary("ws2_32"); |
| 255 | +} |
| 256 | + |
| 257 | +pub const xml_src: []const []const u8 = &.{ |
| 258 | + "buf.c", |
| 259 | + "chvalid.c", |
| 260 | + "dict.c", |
| 261 | + "entities.c", |
| 262 | + "encoding.c", |
| 263 | + "error.c", |
| 264 | + "globals.c", |
| 265 | + "hash.c", |
| 266 | + "list.c", |
| 267 | + "parser.c", |
| 268 | + "parserInternals.c", |
| 269 | + "SAX2.c", |
| 270 | + "threads.c", |
| 271 | + "tree.c", |
| 272 | + "uri.c", |
| 273 | + "valid.c", |
| 274 | + "xmlIO.c", |
| 275 | + "xmlmemory.c", |
| 276 | + "xmlstring.c", |
| 277 | +}; |
| 278 | + |
| 279 | +pub const xml_flags: []const []const u8 = &.{ |
| 280 | + "-pedantic", |
| 281 | + "-Wall", |
| 282 | + "-Wextra", |
| 283 | + "-Wshadow", |
| 284 | + "-Wpointer-arith", |
| 285 | + "-Wcast-align", |
| 286 | + "-Wwrite-strings", |
| 287 | + "-Wstrict-prototypes", |
| 288 | + "-Wmissing-prototypes", |
| 289 | + |
| 290 | + "-Wno-long-long", |
| 291 | + "-Wno-format-extra-args", |
| 292 | + "-Wno-array-bounds", |
| 293 | +}; |
0 commit comments