-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
301 lines (267 loc) · 11.1 KB
/
build.zig
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
const std = @import("std");
const version: std.SemanticVersion = .{ .major = 2, .minor = 6, .patch = 4 };
pub fn build(b: *std.Build) void {
const upstream = b.dependency("libexpat", .{});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// 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 false;
const ge = b.option(bool, "ge", "Define to make general entity parsing functionality available") orelse false;
const ns = b.option(bool, "ns", "Define to make XML Namespaces functionality available") orelse false;
const attr_info = b.option(bool, "attr-info", "Define to allow retrieving the byte offsets for attribute names and values") orelse false;
const large_size = b.option(bool, "large-size", "Make XML_GetCurrent* functions return <(unsigned) long long> rather than <(unsigned) long>") orelse false;
const min_size = b.option(bool, "min-size", "Get a smaller (but slower) parser (in particular avoid multiple copies of the tokenizer)") orelse false;
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 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 = switch (target.result.os.tag) {
.dragonfly,
.netbsd,
.freebsd,
.solaris,
.openbsd,
.macos,
.ios,
.tvos,
.watchos,
// .visionos, // not available on Zig 0.12
=> true,
else => false,
},
.HAVE_DLFCN_H = true,
.HAVE_FCNTL_H = true,
.HAVE_GETPAGESIZE = true,
.HAVE_GETRANDOM = switch (target.result.os.tag) {
.linux, .freebsd => true,
else => false,
},
.HAVE_INTTYPES_H = true,
.HAVE_LIBBSD = null,
.HAVE_MEMORY_H = true,
.HAVE_MMAP = true,
.HAVE_STDINT_H = true,
.HAVE_STDLIB_H = true,
.HAVE_STRINGS_H = true,
.HAVE_STRING_H = true,
.HAVE_SYSCALL_GETRANDOM = target.result.os.tag == .linux,
.HAVE_SYS_STAT_H = true,
.HAVE_SYS_TYPES_H = true,
.HAVE_UNISTD_H = true,
.PACKAGE = "expat",
.PACKAGE_BUGREPORT = "https://github.com/libexpat/libexpat/issues",
.PACKAGE_NAME = "expat",
.PACKAGE_STRING = b.fmt("expat {}", .{version}),
.PACKAGE_TARNAME = "expat",
.PACKAGE_URL = "",
.PACKAGE_VERSION = b.fmt("{}", .{version}),
.STDC_HEADERS = true,
.WORDS_BIGENDIAN = null,
.XML_ATTR_INFO = attr_info,
.XML_CONTEXT_BYTES = context_bytes,
.XML_DEV_URANDOM = target.result.os.tag != .windows,
.XML_DTD = dtd,
.XML_GE = ge,
.XML_NS = ns,
.off_t = null,
});
const expat = b.addStaticLibrary(.{
.name = "expat",
.target = target,
.optimize = optimize,
.link_libc = true,
});
b.installArtifact(expat);
expat.linkLibC();
expat.addConfigHeader(config_header);
if (large_size) expat.root_module.addCMacro("XML_LARGE_SIZE", "1");
if (min_size) expat.root_module.addCMacro("XML_MIN_SIZE", "1");
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");
expat.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.addCSourceFiles(.{
.files = sources,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{ "-std=c99", "-fshort-wchar" } else &.{"-std=c99"},
});
const xmlwf = b.addExecutable(.{
.name = "xmlwf",
.target = target,
.optimize = optimize,
});
xmlwf.addConfigHeader(config_header);
xmlwf.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");
xmlwf.addCSourceFiles(.{
.files = xmlwf_sources,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
xmlwf.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);
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),
.target = target,
.optimize = optimize,
});
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");
exe.addCSourceFile(.{
.file = upstream.path(b.fmt("expat/{s}", .{source})),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
exe.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");
for (
[_][]const u8{ "runtests", "runtests_cxx" },
[_][]const []const u8{ runtests_sources, runtests_cxx_sources },
[_]bool{ false, true },
) |name, test_sources, link_cpp| {
var flags: std.ArrayListUnmanaged([]const u8) = .{};
if (link_cpp) flags.append(b.allocator, "-std=c++11") catch @panic("OOM");
if (need_short_char_arg) flags.append(b.allocator, "-fshort-wchar") catch @panic("OOM");
const test_exe = b.addExecutable(.{
.name = name,
.target = target,
.optimize = optimize,
});
test_exe.linkLibC();
if (link_cpp) test_exe.linkLibCpp();
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.addCSourceFiles(.{
.flags = flags.items,
.root = upstream.path("expat"),
.files = test_sources,
});
test_exe.addConfigHeader(config_header);
test_exe.addIncludePath(upstream.path("expat/lib"));
if (large_size) test_exe.root_module.addCMacro("XML_LARGE_SIZE", "1");
if (min_size) test_exe.root_module.addCMacro("XML_MIN_SIZE", "1");
test_exe.addCSourceFiles(.{
.files = sources,
.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);
if (b.args) |args| {
run_test_cmd.addArgs(args);
}
test_step.dependOn(&run_test_cmd.step);
}
const benchmark = b.addExecutable(.{
.name = "benchmark",
.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.addCSourceFile(.{
.file = upstream.path("expat/tests/benchmark/benchmark.c"),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
benchmark.linkLibrary(expat);
const run_benchmark_cmd = b.addRunArtifact(benchmark);
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/xmlparse.c",
"lib/xmlrole.c",
"lib/xmltok.c",
};
const xmlwf_sources: []const []const u8 = &.{
"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/memcheck.c",
"tests/minicheck.c",
"tests/misc_tests.c",
"tests/ns_tests.c",
"tests/nsalloc_tests.c",
"tests/runtests.c",
"tests/structdata.c",
};
const runtests_cxx_sources: []const []const u8 = &.{
"tests/acc_tests_cxx.cpp",
"tests/alloc_tests_cxx.cpp",
"tests/basic_tests_cxx.cpp",
"tests/chardata_cxx.cpp",
"tests/common_cxx.cpp",
"tests/dummy_cxx.cpp",
"tests/handlers_cxx.cpp",
"tests/memcheck_cxx.cpp",
"tests/minicheck_cxx.cpp",
"tests/misc_tests_cxx.cpp",
"tests/ns_tests_cxx.cpp",
"tests/nsalloc_tests_cxx.cpp",
"tests/runtests_cxx.cpp",
"tests/structdata_cxx.cpp",
};