1const std = @import("std");
2const deps = @import("./deps.zig");
3
4pub fn build(b: *std.Build) void {
5 const target = b.standardTargetOptions(.{});
6 const mode = b.option(std.builtin.OptimizeMode, "mode", "") orelse .Debug;
7 const disable_llvm = b.option(bool, "disable_llvm", "use the non-llvm zig codegen") orelse false;
8
9 const tests = b.addTest(.{
10 .root_module = b.createModule(.{
11 .root_source_file = b.path("test.zig"),
12 .target = target,
13 .optimize = mode,
14 }),
15 });
16 deps.addAllTo(tests);
17 tests.use_llvm = !disable_llvm;
18 tests.use_lld = !disable_llvm;
19 b.getInstallStep().dependOn(&tests.step);
20
21 const build_options = b.addOptions();
22 build_options.addOption([:0]const u8, "yamltestsuite_root", deps.dirs._hi7zwl8ps6jd);
23 tests.root_module.addImport("build_options", build_options.createModule());
24
25 const test_step = b.step("test", "Run all library tests");
26 const tests_run = b.addRunArtifact(tests);
27 tests_run.setCwd(b.path("."));
28 tests_run.has_side_effects = true;
29 test_step.dependOn(&tests_run.step);
30
31 //
32
33 const fuzz_exe = addFuzzer(b, target, "yaml", &.{});
34 b.getInstallStep().dependOn(&fuzz_exe.step);
35
36 const fuzz_run = b.addSystemCommand(&.{"afl-fuzz"});
37 fuzz_run.step.dependOn(&fuzz_exe.step);
38 fuzz_run.addArgs(&.{ "-i", "fuzz/input" });
39 fuzz_run.addArgs(&.{ "-o", "fuzz/output" });
40 fuzz_run.addArgs(&.{ "-x", "fuzz/yaml.dict" });
41 fuzz_run.addArg("--");
42 fuzz_run.addFileArg(fuzz_exe.source);
43
44 const fuzz_step = b.step("fuzz", "Run AFL++");
45 fuzz_step.dependOn(&fuzz_run.step);
46}
47
48fn addFuzzer(b: *std.Build, target: std.Build.ResolvedTarget, comptime name: []const u8, afl_clang_args: []const []const u8) *std.Build.Step.InstallFile {
49 const fuzz_lib = b.addLibrary(.{
50 .linkage = .static,
51 .name = "fuzz-" ++ name ++ "-lib",
52 .root_module = b.createModule(.{
53 .root_source_file = b.path("fuzz/main.zig"),
54 .target = target,
55 .optimize = .Debug,
56 }),
57 });
58 fuzz_lib.lto = .full;
59 fuzz_lib.bundle_compiler_rt = true;
60 fuzz_lib.use_llvm = true;
61 fuzz_lib.use_lld = true;
62 fuzz_lib.root_module.pic = true;
63 fuzz_lib.root_module.link_libc = true;
64 fuzz_lib.bundle_ubsan_rt = true;
65
66 deps.addAllTo(fuzz_lib);
67
68 const fuzz_executable_name = "fuzz-" ++ name;
69
70 const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-o" });
71 const output_path = fuzz_compile.addOutputFileArg(fuzz_executable_name);
72 fuzz_compile.addArtifactArg(fuzz_lib);
73 fuzz_compile.addArgs(afl_clang_args);
74
75 const fuzz_install = b.addInstallBinFile(output_path, fuzz_executable_name);
76 fuzz_install.step.dependOn(&fuzz_compile.step);
77
78 return fuzz_install;
79}