| ... | ... | @@ -44,33 +44,28 @@ pub fn build(b: *std.Build) void { |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 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 | 47 | const fuzz_lib = b.addStaticLibrary(.{ |
| 49 | | .name = "fuzz-" ++ name, |
| 48 | .name = "fuzz-" ++ name ++ "-lib", |
| 50 | 49 | .root_source_file = b.path("fuzz/main.zig"), |
| 51 | 50 | .target = target, |
| 52 | 51 | .optimize = .Debug, |
| 53 | 52 | }); |
| 54 | 53 | fuzz_lib.want_lto = true; |
| 55 | 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 | 57 | fuzz_lib.root_module.pic = true; |
| 58 | 58 | |
| 59 | 59 | deps.addAllTo(fuzz_lib); |
| 60 | 60 | |
| 61 | | // Setup the output name |
| 62 | 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` |
| 66 | | const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o", fuzz_exe_path }); |
| 67 | | // Add the path to the library file to afl-clang-lto's args |
| 63 | const fuzz_compile = b.addSystemCommand(&.{ "afl-clang-lto", "-v", "-o" }); |
| 64 | const output_path = fuzz_compile.addOutputFileArg(fuzz_executable_name); |
| 68 | 65 | fuzz_compile.addArtifactArg(fuzz_lib); |
| 69 | | // Custom args |
| 70 | 66 | fuzz_compile.addArgs(afl_clang_args); |
| 71 | 67 | |
| 72 | | // Install the cached output to the install 'bin' path |
| 73 | | const fuzz_install = b.addInstallBinFile(.{ .cwd_relative = fuzz_exe_path }, fuzz_executable_name); |
| 68 | const fuzz_install = b.addInstallBinFile(output_path, fuzz_executable_name); |
| 74 | 69 | fuzz_install.step.dependOn(&fuzz_compile.step); |
| 75 | 70 | |
| 76 | 71 | return fuzz_install; |