-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.zig
438 lines (395 loc) · 17.6 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
const std = @import("std");
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardOptimizeOption(.{});
const aro = b.dependency("aro", .{
.target = target,
.optimize = mode,
});
const aro_module = aro.module("aro");
const compressed_mingw_includes = b.dependency("compressed_mingw_includes", .{});
const compressed_mingw_includes_module = compressed_mingw_includes.module("compressed_mingw_includes");
const resinator = b.addModule("resinator", .{
.root_source_file = b.path("src/resinator.zig"),
.imports = &.{
.{ .name = "aro", .module = aro_module },
},
});
const exe = b.addExecutable(.{
.name = "resinator",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = mode,
});
exe.root_module.addImport("aro", aro_module);
exe.root_module.addImport("compressed_mingw_includes", compressed_mingw_includes_module);
b.installArtifact(exe);
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
const exe_tests = b.addTest(.{
.root_source_file = b.path("src/resinator.zig"),
.target = target,
.optimize = mode,
.filter = test_filter,
});
const run_exe_tests = b.addRunArtifact(exe_tests);
const reference_tests = b.addTest(.{
.name = "reference",
.root_source_file = b.path("test/reference.zig"),
.target = target,
.optimize = mode,
.filter = test_filter,
});
reference_tests.root_module.addImport("resinator", resinator);
const run_reference_tests = b.addRunArtifact(reference_tests);
const parser_tests = b.addTest(.{
.name = "parse",
.root_source_file = b.path("test/parse.zig"),
.target = target,
.optimize = mode,
.filter = test_filter,
});
parser_tests.root_module.addImport("resinator", resinator);
const run_parser_tests = b.addRunArtifact(parser_tests);
const compiler_tests = b.addTest(.{
.name = "compile",
.root_source_file = b.path("test/compile.zig"),
.target = target,
.optimize = mode,
.filter = test_filter,
});
compiler_tests.root_module.addImport("resinator", resinator);
const run_compiler_tests = b.addRunArtifact(compiler_tests);
const cvtres_tests = b.addTest(.{
.name = "cvtres",
.root_source_file = b.path("test/cvtres.zig"),
.target = target,
.optimize = mode,
.filter = test_filter,
});
cvtres_tests.root_module.addImport("resinator", resinator);
const run_cvtres_tests = b.addRunArtifact(cvtres_tests);
const run_cli_tests_step = addCliTests(b, exe);
const test_step = b.step("test", "Run all tests");
test_step.dependOn(&run_exe_tests.step);
test_step.dependOn(&run_reference_tests.step);
test_step.dependOn(&run_parser_tests.step);
test_step.dependOn(&run_compiler_tests.step);
test_step.dependOn(&run_cvtres_tests.step);
test_step.dependOn(run_cli_tests_step);
const test_utils_module = b.createModule(.{
.root_source_file = b.path("test/utils.zig"),
.imports = &.{
.{ .name = "resinator", .module = resinator },
},
});
// Tools
const cvtres_strip = b.addExecutable(.{
.name = "cvtres-strip",
.root_source_file = b.path("tools/cvtres-strip.zig"),
.target = target,
.optimize = mode,
});
cvtres_strip.root_module.addImport("utils", test_utils_module);
const install_cvtres_strip = b.addInstallArtifact(cvtres_strip, .{});
const cvtres_strip_step = b.step("cvtres-strip", "Build and install cvtres-strip tool");
cvtres_strip_step.dependOn(&install_cvtres_strip.step);
const tools_step = b.step("tools", "Build and install tools");
tools_step.dependOn(&install_cvtres_strip.step);
// Fuzzy tests
const fuzzy_max_iterations = b.option(u64, "fuzzy-iterations", "The max iterations for fuzzy tests (default: 1000)") orelse 1000;
const fuzzy_debug = b.option(bool, "fuzzy-debug", "When enabled, fuzzy tests will write their inputs to the cache dir (default: false)") orelse false;
const test_options = b.addOptions();
test_options.addOption(u64, "max_iterations", fuzzy_max_iterations);
test_options.addOption(bool, "fuzzy_debug", fuzzy_debug);
const all_fuzzy_tests_step = b.step("test_fuzzy", "Run all fuzz/property-testing-like tests with a max number of iterations for each");
_ = addFuzzyTest(b, "numbers", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "number_expressions", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "ascii_strings", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "numeric_types", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "common_resource_attributes", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "raw_data", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "name_or_ordinal", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "code_pages", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "icons", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "stringtable", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "strings", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "cvtres", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
_ = addFuzzyTest(b, "res", mode, target, resinator, test_utils_module, all_fuzzy_tests_step, test_options);
// Exclude these fuzzy tests from the test_fuzzy step since they don't fully work as tests
// and are more geared towards gathering info at the moment.
_ = addFuzzyTest(b, "bitmaps", mode, target, resinator, test_utils_module, null, test_options);
_ = addFuzzyTest(b, "fonts", mode, target, resinator, test_utils_module, null, test_options);
_ = addFuzzyTest(b, "dlginclude", mode, target, resinator, test_utils_module, null, test_options);
_ = addFuzzyTest(b, "accelerators", mode, target, resinator, test_utils_module, null, test_options);
_ = addFuzzer(b, "fuzz_rc", &.{}, resinator, target);
const fuzz_winafl_exe = b.addExecutable(.{
.name = "fuzz_winafl",
.root_source_file = b.path("test/fuzz_winafl.zig"),
.target = target,
.optimize = mode,
});
fuzz_winafl_exe.root_module.addImport("resinator", resinator);
const fuzz_winafl_compile = b.step("fuzz_winafl", "Build/install fuzz_winafl exe");
const install_fuzz_winafl = b.addInstallArtifact(fuzz_winafl_exe, .{});
fuzz_winafl_compile.dependOn(&install_fuzz_winafl.step);
// release step
{
const release_step = b.step("release", "Build release binaries for all supported targets");
const release_targets = &[_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .linux },
.{ .cpu_arch = .x86_64, .os_tag = .linux },
.{ .cpu_arch = .x86, .os_tag = .linux },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
.{ .cpu_arch = .x86, .os_tag = .windows },
};
for (release_targets) |release_target| {
const resolved_release_target = b.resolveTargetQuery(release_target);
const release_exe = b.addExecutable(.{
.name = "resinator",
.root_source_file = b.path("src/main.zig"),
.target = resolved_release_target,
.optimize = .ReleaseFast,
.single_threaded = true,
.strip = true,
});
release_exe.root_module.addImport("aro", aro_module);
release_exe.root_module.addImport("compressed_mingw_includes", compressed_mingw_includes_module);
const triple = release_target.zigTriple(b.allocator) catch unreachable;
const install_dir = b.pathJoin(&.{ "release", triple });
const release_install = b.addInstallArtifact(
release_exe,
.{ .dest_dir = .{
.override = .{ .custom = install_dir },
} },
);
release_step.dependOn(&release_install.step);
}
}
}
fn addCliTests(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step {
const step = b.step("test-cli", "Test the command line interface");
const test_rcs = b.addWriteFiles();
// Avoid errors from multiple processes trying to extract
// MinGW headers at the same time by having tests that use
// /:auto-includes depend on this cache warming step.
//
// TODO: Make extraction from multiple processes at the same
// time work (e.g. one does the extraction while the
// others wait, or something like that).
const empty_rc = test_rcs.add("empty.rc",
\\
);
const warm_mingw_cache = blk: {
const run = b.addRunArtifact(exe);
run.addArgs(&.{ "/:auto-includes", "gnu" });
run.addArg("--");
run.addFileArg(empty_rc);
_ = run.addOutputFileArg("output.res");
run.expectStdOutEqual("");
run.expectStdErrEqual("");
step.dependOn(&run.step);
break :blk run;
};
const windows_h = test_rcs.add("windows-h.rc",
\\#include "windows.h"
);
{
const run = b.addRunArtifact(exe);
run.addArg("--");
run.addFileArg(windows_h);
_ = run.addOutputFileArg("output.res");
run.expectStdOutEqual("");
run.expectStdErrEqual("");
step.dependOn(&warm_mingw_cache.step);
}
{
const run = b.addRunArtifact(exe);
run.addArgs(&.{ "/:auto-includes", "gnu" });
run.addArg("--");
run.addFileArg(windows_h);
_ = run.addOutputFileArg("output.res");
run.expectStdOutEqual("");
run.expectStdErrEqual("");
step.dependOn(&warm_mingw_cache.step);
}
const predefined_macros = test_rcs.add("predefined-macros.rc",
\\1 RCDATA { RC_INVOKED, _WIN32 }
);
{
const run = b.addRunArtifact(exe);
run.addArgs(&.{ "/:auto-includes", "none" });
run.addArg("--");
run.addFileArg(predefined_macros);
_ = run.addOutputFileArg("output.res");
run.expectStdOutEqual("");
run.expectStdErrEqual("");
step.dependOn(&run.step);
}
// case sensitive, so this doesn't undef RC_INVOKED
{
const run = b.addRunArtifact(exe);
run.addArgs(&.{ "/u", "rc_invoked" });
run.addArgs(&.{ "/:auto-includes", "none" });
run.addArg("--");
run.addFileArg(predefined_macros);
_ = run.addOutputFileArg("output.res");
run.expectStdOutEqual("");
run.expectStdErrEqual("");
step.dependOn(&run.step);
}
// undefing predefined macros works
{
const run = b.addRunArtifact(exe);
run.addArgs(&.{ "/u", "RC_INVOKED" });
run.addArgs(&.{ "/:auto-includes", "none" });
run.addArg("--");
run.addFileArg(predefined_macros);
_ = run.addOutputFileArg("output.res");
run.expectExitCode(1);
run.addCheck(.{ .expect_stderr_match = b.dupe("got 'RC_INVOKED'") });
run.expectStdOutEqual("");
step.dependOn(&run.step);
}
{
const run = b.addRunArtifact(exe);
run.addArgs(&.{ "/u", "_WIN32" });
run.addArgs(&.{ "/:auto-includes", "none" });
run.addArg("--");
run.addFileArg(predefined_macros);
_ = run.addOutputFileArg("output.res");
run.expectExitCode(1);
run.addCheck(.{ .expect_stderr_match = b.dupe("got '_WIN32'") });
run.expectStdOutEqual("");
step.dependOn(&run.step);
}
const disjoint_code_page = test_rcs.add("disjoint-code-page1.rc",
\\#include "empty.h"
\\#pragma code_page(65001)
\\1 RCDATA { "\371" }
);
const disjoint_code_page_with_invalid_code_page_before = test_rcs.add("disjoint-code-page2.rc",
\\#include "empty.h"
\\#pragma code_page(1234567)
\\#pragma foo
\\#pragma code_page(65001)
\\1 RCDATA { "\371" }
);
const empty_h = test_rcs.add("empty.h", "");
_ = empty_h;
{
const run = b.addRunArtifact(exe);
run.addArgs(&.{ "/:auto-includes", "none" });
run.addArg("--");
run.addFileArg(disjoint_code_page);
_ = run.addOutputFileArg("output.res");
run.expectExitCode(0);
run.addCheck(.{ .expect_stderr_match = b.dupe("#pragma code_page as the first thing in the .rc script can cause the input and output code pages to become out-of-sync") });
run.expectStdOutEqual("");
step.dependOn(&run.step);
}
{
const run = b.addRunArtifact(exe);
run.addArgs(&.{"/w"}); // Warn instead of error on invalid code page in pragma, necessary for this test
run.addArgs(&.{ "/:auto-includes", "none" });
run.addArg("--");
run.addFileArg(disjoint_code_page_with_invalid_code_page_before);
_ = run.addOutputFileArg("output.res");
run.expectExitCode(0);
run.addCheck(.{ .expect_stderr_match = b.dupe("#pragma code_page as the first thing in the .rc script can cause the input and output code pages to become out-of-sync") });
run.expectStdOutEqual("");
step.dependOn(&run.step);
}
return step;
}
fn addFuzzyTest(
b: *std.Build,
comptime name: []const u8,
mode: std.builtin.Mode,
target: std.Build.ResolvedTarget,
resinator: *std.Build.Module,
test_utils_module: *std.Build.Module,
all_fuzzy_tests_step: ?*std.Build.Step,
fuzzy_options: *std.Build.Step.Options,
) *std.Build.Step.Compile {
var test_step = b.addTest(.{
.root_source_file = b.path("test/fuzzy_" ++ name ++ ".zig"),
.target = target,
.optimize = mode,
});
test_step.root_module.addImport("resinator", resinator);
// We use an import to avoid pulling in the test cases of the test utils themselves
test_step.root_module.addImport("test_utils", test_utils_module);
test_step.root_module.addOptions("fuzzy_options", fuzzy_options);
const run_test = b.addRunArtifact(test_step);
var test_run_step = b.step("test_fuzzy_" ++ name, "Some fuzz/property-testing-like tests for " ++ name);
test_run_step.dependOn(&run_test.step);
if (all_fuzzy_tests_step) |all_step| {
all_step.dependOn(test_run_step);
}
return test_step;
}
fn addFuzzer(
b: *std.Build,
comptime name: []const u8,
afl_clang_args: []const []const u8,
resinator: *std.Build.Module,
target: std.Build.ResolvedTarget,
) FuzzerSteps {
// The library
const fuzz_lib = b.addStaticLibrary(.{
.name = name ++ "-lib",
.root_source_file = b.path("test/" ++ name ++ ".zig"),
.target = target,
.optimize = .Debug,
});
fuzz_lib.root_module.addImport("resinator", resinator);
fuzz_lib.want_lto = true;
fuzz_lib.bundle_compiler_rt = true;
fuzz_lib.root_module.pic = true;
// Setup the output name
const fuzz_executable_name = name;
// We want `afl-clang-lto -o path/to/output path/to/library`
const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-o" });
const fuzz_exe_path = fuzz_compile.addOutputFileArg(name);
// Add the path to the library file to afl-clang-lto's args
fuzz_compile.addArtifactArg(fuzz_lib);
// Custom args
fuzz_compile.addArgs(afl_clang_args);
// Install the cached output to the install 'bin' path
const fuzz_install = b.addInstallBinFile(fuzz_exe_path, fuzz_executable_name);
fuzz_install.step.dependOn(&fuzz_compile.step);
// Add a top-level step that compiles and installs the fuzz executable
const fuzz_compile_run = b.step(name, "Build executable for fuzz testing '" ++ name ++ "' using afl-clang-lto");
fuzz_compile_run.dependOn(&fuzz_compile.step);
fuzz_compile_run.dependOn(&fuzz_install.step);
// Compile a companion exe for debugging crashes
const fuzz_debug_exe = b.addExecutable(.{
.name = name ++ "-debug",
.root_source_file = b.path("test/" ++ name ++ ".zig"),
.target = target,
.optimize = .Debug,
});
fuzz_debug_exe.root_module.addImport("resinator", resinator);
// Only install fuzz-debug when the fuzz step is run
const install_fuzz_debug_exe = b.addInstallArtifact(fuzz_debug_exe, .{});
fuzz_compile_run.dependOn(&install_fuzz_debug_exe.step);
return FuzzerSteps{
.lib = fuzz_lib,
.debug_exe = fuzz_debug_exe,
};
}
const FuzzerSteps = struct {
lib: *std.Build.Step.Compile,
debug_exe: *std.Build.Step.Compile,
pub fn libExes(self: *const FuzzerSteps) [2]*std.Build.Step.Compile {
return [_]*std.Build.Step.Compile{ self.lib, self.debug_exe };
}
};