| ... | @@ -44,33 +44,28 @@ pub fn build(b: *std.Build) void { | ... | @@ -44,33 +44,28 @@ pub fn build(b: *std.Build) void { |
| 44 | } | 44 | } |
| 45 | | 45 | |
| 46 | fn addFuzzer(b: *std.Build, target: std.Build.ResolvedTarget, comptime name: []const u8, afl_clang_args: []const []const u8) *std.Build.Step.InstallFile { | 46 | fn addFuzzer(b: *std.Build, target: std.Build.ResolvedTarget, comptime name: []const u8, afl_clang_args: []const []const u8) *std.Build.Step.InstallFile { |
| 47 | // The library | | |
| 48 | const fuzz_lib = b.addStaticLibrary(.{ | 47 | const fuzz_lib = b.addStaticLibrary(.{ |
| 49 | .name = "fuzz-" ++ name, | 48 | .name = "fuzz-" ++ name ++ "-lib", |
| 50 | .root_source_file = b.path("fuzz/main.zig"), | 49 | .root_source_file = b.path("fuzz/main.zig"), |
| 51 | .target = target, | 50 | .target = target, |
| 52 | .optimize = .Debug, | 51 | .optimize = .Debug, |
| 53 | }); | 52 | }); |
| 54 | fuzz_lib.want_lto = true; | 53 | fuzz_lib.want_lto = true; |
| 55 | fuzz_lib.bundle_compiler_rt = true; | 54 | fuzz_lib.bundle_compiler_rt = true; |
| 56 | // Seems to be necessary for LLVM >= 15 | 55 | fuzz_lib.use_llvm = true; |
| | 56 | fuzz_lib.use_lld = true; |
| 57 | fuzz_lib.root_module.pic = true; | 57 | fuzz_lib.root_module.pic = true; |
| 58 | | 58 | |
| 59 | deps.addAllTo(fuzz_lib); | 59 | deps.addAllTo(fuzz_lib); |
| 60 | | 60 | |
| 61 | // Setup the output name | | |
| 62 | const fuzz_executable_name = "fuzz-" ++ name; | 61 | const fuzz_executable_name = "fuzz-" ++ name; |
| 63 | const fuzz_exe_path = std.fs.path.join(b.allocator, &.{ b.cache_root.path.?, fuzz_executable_name }) catch @panic("OOM"); | | |
| 64 | | 62 | |
| 65 | // We want `afl-clang-lto -o path/to/output path/to/library` | 63 | const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o" }); |
| 66 | const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o", fuzz_exe_path }); | 64 | const output_path = fuzz_compile.addOutputFileArg(fuzz_executable_name); |
| 67 | // Add the path to the library file to afl-clang-lto's args | | |
| 68 | fuzz_compile.addArtifactArg(fuzz_lib); | 65 | fuzz_compile.addArtifactArg(fuzz_lib); |
| 69 | // Custom args | | |
| 70 | fuzz_compile.addArgs(afl_clang_args); | 66 | fuzz_compile.addArgs(afl_clang_args); |
| 71 | | 67 | |
| 72 | // Install the cached output to the install 'bin' path | 68 | const fuzz_install = b.addInstallBinFile(output_path, fuzz_executable_name); |
| 73 | const fuzz_install = b.addInstallBinFile(.{ .cwd_relative = fuzz_exe_path }, fuzz_executable_name); | | |
| 74 | fuzz_install.step.dependOn(&fuzz_compile.step); | 69 | fuzz_install.step.dependOn(&fuzz_compile.step); |
| 75 | | 70 | |
| 76 | return fuzz_install; | 71 | return fuzz_install; |