Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
352 lines (314 loc) · 14.1 KB
/
Copy pathbuild.zig
File metadata and controls
352 lines (314 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const std = @import("std");
const version = std.SemanticVersion.parse(@import("build.zig.zon").version) catch unreachable;
const minimum_zig_version = std.SemanticVersion.parse(@import("build.zig.zon").minimum_zig_version) catch unreachable;
pub fn build(b: *std.Build) void {
const upstream = b.dependency("libexpat", .{});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const linkage = b.option(std.builtin.LinkMode, "linkage", "Link mode") orelse .static;
const strip = b.option(bool, "strip", "Omit debug information");
const pic = b.option(bool, "pie", "Produce Position Independent Code");
// Most of these config options have not been tested.
var context_bytes = b.option(i64, "context-bytes", "Define to specify how much context to retain around the current parse point, 0 to disable") orelse 1024;
if (context_bytes < 0) context_bytes = 0;
const CharType = enum {
char,
ushort,
wchar_t,
};
const char_type = b.option(CharType, "char-type", "Character type to use. (default=char)") orelse .char;
const dtd = b.option(bool, "dtd", "Define to make parameter entity parsing functionality available") orelse true;
const ge = b.option(bool, "ge", "Define to make general entity parsing functionality available") orelse true;
const ns = b.option(bool, "ns", "Define to make XML Namespaces functionality available") orelse true;
if (dtd and !ge) {
std.log.err("Option `dtd` requires that `ge` is also enabled.", .{});
std.log.err("Please either enable option `ge` (recommended) or disable `dtd` also.\n", .{});
b.invalid_user_input = true;
return;
}
const need_short_char_arg = char_type == .wchar_t and target.result.os.tag != .windows;
const examples_dir: std.Build.Step.InstallArtifact.Options.Dir = .{ .override = .{ .custom = "examples" } };
b.getInstallStep().dependOn(&b.addInstallFileWithDir(upstream.path("expat/AUTHORS"), .prefix, "AUTHORS").step);
b.getInstallStep().dependOn(&b.addInstallFileWithDir(upstream.path("expat/Changes"), .prefix, "changelog").step);
const has_getrandom: bool = switch (target.result.os.tag) {
.linux => if (target.result.isMuslLibC())
true
else if (target.result.isGnuLibC())
target.result.os.version_range.linux.glibc.order(.{ .major = 2, .minor = 25, .patch = 0 }) != .lt
else
unreachable,
.freebsd => target.result.os.isAtLeast(.freebsd, .{ .major = 12, .minor = 0, .patch = 0 }) orelse false,
.netbsd => target.result.os.isAtLeast(.netbsd, .{ .major = 10, .minor = 0, .patch = 0 }) orelse false,
else => false,
};
const has_getentropy: bool = switch (target.result.os.tag) {
.linux => if (target.result.isMuslLibC())
true
else if (target.result.isGnuLibC())
target.result.os.version_range.linux.glibc.order(.{ .major = 2, .minor = 25, .patch = 0 }) != .lt
else
unreachable,
.macos,
.ios,
.tvos,
.watchos,
.visionos,
=> true,
.freebsd => target.result.os.isAtLeast(.freebsd, .{ .major = 12, .minor = 0, .patch = 0 }) orelse false,
.netbsd => target.result.os.isAtLeast(.netbsd, .{ .major = 10, .minor = 0, .patch = 0 }) orelse false,
.openbsd => target.result.os.isAtLeast(.freebsd, .{ .major = 5, .minor = 6, .patch = 0 }) orelse false,
else => false,
};
const has_arc4random_buf = switch (target.result.os.tag) {
.dragonfly,
.netbsd,
.freebsd,
.openbsd,
.macos,
.ios,
.tvos,
.watchos,
.visionos,
=> true,
.linux => if (target.result.isGnuLibC())
target.result.os.version_range.linux.glibc.order(.{ .major = 2, .minor = 36, .patch = 0 }) != .lt
else
false,
else => false,
};
const xml_dev_urandom = target.result.os.tag != .windows;
const config_header = b.addConfigHeader(.{
.style = .{ .cmake = upstream.path("expat/expat_config.h.cmake") },
.include_path = "expat_config.h",
}, .{
.BYTEORDER = @as(i64, switch (target.result.cpu.arch.endian()) {
.little => 1234,
.big => 4321,
}),
.HAVE_ARC4RANDOM = null,
.HAVE_ARC4RANDOM_BUF = if (has_arc4random_buf) true else null,
.HAVE_DLFCN_H = target.result.os.tag != .windows,
.HAVE_FCNTL_H = true,
.HAVE_GETPAGESIZE = target.result.os.tag.isBSD(),
.HAVE_GETRANDOM = has_getrandom,
.HAVE_INTTYPES_H = true,
.HAVE_MEMORY_H = true,
.HAVE_MMAP = target.result.os.tag != .windows and target.result.os.tag != .wasi,
.HAVE_STDINT_H = true,
.HAVE_STDLIB_H = true,
.HAVE_STRINGS_H = true,
.HAVE_STRING_H = true,
.HAVE_SYSCALL_GETRANDOM = has_getrandom,
.HAVE_SYS_STAT_H = true,
.HAVE_SYS_TYPES_H = true,
.HAVE_UNISTD_H = true,
.PACKAGE_BUGREPORT = "https://github.com/libexpat/libexpat/issues",
.PACKAGE_NAME = "expat",
.PACKAGE_STRING = b.fmt("expat {f}", .{version}),
.PACKAGE_TARNAME = "expat",
.PACKAGE_VERSION = b.fmt("{f}", .{version}),
.STDC_HEADERS = true,
.WORDS_BIGENDIAN = target.result.cpu.arch.endian() == .big,
.XML_CONTEXT_BYTES = context_bytes,
.XML_DEV_URANDOM = xml_dev_urandom,
.XML_DTD = dtd,
.XML_GE = ge,
.XML_NS = ns,
.off_t = null,
});
var expat_sources: std.ArrayList([]const u8) = .empty;
expat_sources.appendSlice(b.allocator, sources) catch @panic("OOM");
// if (has_arc4random) expat_sources.append(b.allocator, "lib/random_arc4random.c") catch @panic("OOM"); // HAVE_ARC4RANDOM
if (has_arc4random_buf) expat_sources.append(b.allocator, "lib/random_arc4random_buf.c") catch @panic("OOM");
if (xml_dev_urandom) expat_sources.append(b.allocator, "lib/random_dev_urandom.c") catch @panic("OOM");
if (has_getentropy) expat_sources.append(b.allocator, "lib/random_getentropy.c") catch @panic("OOM");
if (has_getrandom) expat_sources.append(b.allocator, "lib/random_getrandom.c") catch @panic("OOM");
if (target.result.os.tag == .windows) expat_sources.append(b.allocator, "lib/random_rand_s.c") catch @panic("OOM");
const expat = b.addLibrary(.{
.linkage = linkage,
.name = "expat",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.pic = pic,
}),
});
b.installArtifact(expat);
expat.root_module.addConfigHeader(config_header);
if (char_type != .char) expat.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) expat.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
if (has_getentropy) expat.root_module.addCMacro("HAVE_GETENTROPY", "1");
expat.root_module.addIncludePath(upstream.path("expat/lib"));
expat.installHeader(upstream.path("expat/lib/expat.h"), "expat.h");
expat.installHeader(upstream.path("expat/lib/expat_external.h"), "expat_external.h");
expat.root_module.addCSourceFiles(.{
.files = expat_sources.items,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{ "-std=c99", "-fshort-wchar" } else &.{"-std=c99"},
});
const xmlwf = b.addExecutable(.{
.name = "xmlwf",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.pic = pic,
}),
});
xmlwf.root_module.addConfigHeader(config_header);
xmlwf.root_module.addIncludePath(upstream.path("expat/lib"));
if (char_type != .char) xmlwf.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) xmlwf.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
if (has_getentropy) xmlwf.root_module.addCMacro("HAVE_GETENTROPY", "1");
xmlwf.root_module.addCSourceFiles(.{
.files = xmlwf_sources,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
xmlwf.root_module.linkLibrary(expat);
switch (char_type) {
.char => {},
.ushort => @panic("The xmlwf tool can not be built with option -Dchar-type=ushort. Please pass -Dchar-type=(char|wchar_t)"),
.wchar_t => if (target.result.os.tag != .windows) {
@panic("The xmlwf tool can not be built with option -Dchar-type=wchar_t outside of Windows. Please pass -Dchar-type=char");
},
}
const run_xmlwf_cmd = b.addRunArtifact(xmlwf);
comptime std.debug.assert(minimum_zig_version.order(.{ .major = 0, .minor = 17, .patch = 0 }) == .lt); // remove check below after updating to Zig 0.17.0+
if (@hasDecl(std.Build.Step.Run, "addPassthruArgs")) {
run_xmlwf_cmd.addPassthruArgs();
} else {
if (b.args) |args| run_xmlwf_cmd.addArgs(args);
}
const xmlwf_step = b.step("xmlwf", "run xmlwf");
xmlwf_step.dependOn(&run_xmlwf_cmd.step);
const examples_step = b.step("examples", "Build examples");
for (examples) |source| {
const exe = b.addExecutable(.{
.name = std.fs.path.stem(source),
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.pic = pic,
}),
});
if (char_type != .char) exe.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) exe.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
if (has_getentropy) exe.root_module.addCMacro("HAVE_GETENTROPY", "1");
exe.root_module.addCSourceFile(.{
.file = upstream.path(b.fmt("expat/{s}", .{source})),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
exe.root_module.linkLibrary(expat);
examples_step.dependOn(&b.addInstallArtifact(exe, .{ .dest_dir = examples_dir }).step);
if (char_type == .ushort) {
@panic("Examples can not be built with option -Dchar-type=ushort. Please pass -Dchar-type=(char|wchar_t)");
}
}
const test_step = b.step("test", "Run unit tests");
{
var flags: std.ArrayList([]const u8) = .empty;
if (need_short_char_arg) flags.append(b.allocator, "-fshort-wchar") catch @panic("OOM");
const test_exe = b.addExecutable(.{
.name = "runtests",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.pic = pic,
}),
});
test_exe.root_module.addCMacro("XML_TESTING", "1");
if (char_type != .char) test_exe.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) test_exe.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
test_exe.root_module.addCSourceFiles(.{
.flags = flags.items,
.root = upstream.path("expat"),
.files = runtests_sources,
});
test_exe.root_module.addConfigHeader(config_header);
test_exe.root_module.addIncludePath(upstream.path("expat/lib"));
test_exe.root_module.addCSourceFiles(.{
.files = expat_sources.items,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{ "-std=c99", "-fshort-wchar" } else &.{"-std=c99"},
});
if (char_type == .ushort) {
@panic("The testsuite can not be built with option -Dchar-type=ushort. Please pass -Dchar-type=(char|wchar_t)");
}
const run_test_cmd = b.addRunArtifact(test_exe);
comptime std.debug.assert(minimum_zig_version.order(.{ .major = 0, .minor = 17, .patch = 0 }) == .lt); // remove check below after updating to Zig 0.17.0+
if (@hasDecl(std.Build.Step.Run, "addPassthruArgs")) {
run_test_cmd.addPassthruArgs();
} else {
if (b.args) |args| run_test_cmd.addArgs(args);
}
test_step.dependOn(&run_test_cmd.step);
}
const benchmark = b.addExecutable(.{
.name = "benchmark",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}),
});
if (char_type != .char) benchmark.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) benchmark.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
benchmark.root_module.addCSourceFile(.{
.file = upstream.path("expat/tests/benchmark/benchmark.c"),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
benchmark.root_module.linkLibrary(expat);
const run_benchmark_cmd = b.addRunArtifact(benchmark);
comptime std.debug.assert(minimum_zig_version.order(.{ .major = 0, .minor = 17, .patch = 0 }) == .lt); // remove check below after updating to Zig 0.17.0+
if (@hasDecl(std.Build.Step.Run, "addPassthruArgs")) {
run_benchmark_cmd.addPassthruArgs();
} else {
if (b.args) |args| run_benchmark_cmd.addArgs(args);
}
const benchmark_step = b.step("benchmark", "Run benchmark");
benchmark_step.dependOn(&run_benchmark_cmd.step);
}
const sources: []const []const u8 = &.{
"lib/xcs.c",
"lib/xmlparse.c",
"lib/xmlrole.c",
"lib/xmltok.c",
};
const xmlwf_sources: []const []const u8 = &.{
"lib/xcs.c",
"xmlwf/codepage.c",
"xmlwf/readfilemap.c",
"xmlwf/xmlfile.c",
"xmlwf/xmlwf.c",
};
const examples: []const []const u8 = &.{
"examples/element_declarations.c",
"examples/elements.c",
"examples/outline.c",
};
const runtests_sources: []const []const u8 = &.{
"tests/acc_tests.c",
"tests/alloc_tests.c",
"tests/basic_tests.c",
"tests/chardata.c",
"tests/common.c",
"tests/dummy.c",
"tests/handlers.c",
"tests/hash_tests.c",
"tests/memcheck.c",
"tests/minicheck.c",
"tests/misc_tests.c",
"tests/ns_tests.c",
"tests/nsalloc_tests.c",
"tests/props_tests.c",
"tests/runtests.c",
"tests/structdata.c",
"tests/xcs_tests.c",
};