1const std = @import("std");
2const builtin = @import("builtin");
3
4pub fn build(b: *std.Build) !void {
5 const test_step = b.step("test", "Test it");
6 b.default_step = test_step;
7
8 const optimize: std.builtin.Optimize = .debug;
9 const target = b.graph.host;
10
11 if (builtin.os.tag != .windows) return;
12
13 const echo_args = b.addExecutable(.{
14 .name = "echo-args",
15 .root_module = b.createModule(.{
16 .root_source_file = b.path("echo-args.zig"),
17 .optimize = optimize,
18 .target = target,
19 }),
20 });
21
22 const bat_files = b.addWriteFiles();
23 {
24 const echo_args_basename = "echo-args.exe";
25 const preamble = std.fmt.allocPrint(b.allocator,
26 \\@echo off
27 \\"{s}"
28 , .{echo_args_basename}) catch @panic("OOM");
29 // Trailing newline intentionally omitted above so we can add args.
30
31 _ = bat_files.add("args1.bat", std.mem.concat(b.allocator, u8, &.{ preamble, " %*" }) catch @panic("OOM"));
32 _ = bat_files.add("args2.bat", std.mem.concat(b.allocator, u8, &.{ preamble, " %1 %2 %3 %4 %5 %6 %7 %8 %9" }) catch @panic("OOM"));
33 _ = bat_files.add("args3.bat", std.mem.concat(b.allocator, u8, &.{ preamble, " \"%~1\" \"%~2\" \"%~3\" \"%~4\" \"%~5\" \"%~6\" \"%~7\" \"%~8\" \"%~9\"" }) catch @panic("OOM"));
34
35 _ = bat_files.addCopyFile(echo_args.getEmittedBin(), echo_args_basename);
36 }
37
38 const test_exe = b.addExecutable(.{
39 .name = "test",
40 .root_module = b.createModule(.{
41 .root_source_file = b.path("test.zig"),
42 .optimize = optimize,
43 .target = target,
44 }),
45 });
46
47 const run = b.addRunArtifact(test_exe);
48 run.setCwd(bat_files.getDirectory());
49 run.expectExitCode(0);
50 run.skip_foreign_checks = true;
51
52 test_step.dependOn(&run.step);
53
54 const fuzz = b.addExecutable(.{
55 .name = "fuzz",
56 .root_module = b.createModule(.{
57 .root_source_file = b.path("fuzz.zig"),
58 .optimize = optimize,
59 .target = target,
60 }),
61 });
62
63 const fuzz_max_iterations = b.option(u64, "iterations", "The max fuzz iterations (default: 100)") orelse 100;
64 const fuzz_iterations_arg = std.fmt.allocPrint(b.allocator, "{}", .{fuzz_max_iterations}) catch @panic("oom");
65
66 const fuzz_seed = b.option(u64, "seed", "Seed to use for the PRNG (default: random)") orelse seed: {
67 var buf: [8]u8 = undefined;
68 b.graph.io.random(&buf);
69 break :seed std.mem.readInt(u64, &buf, builtin.cpu.arch.endian());
70 };
71 const fuzz_seed_arg = std.fmt.allocPrint(b.allocator, "{}", .{fuzz_seed}) catch @panic("oom");
72
73 const fuzz_run = b.addRunArtifact(fuzz);
74 fuzz_run.setCwd(bat_files.getDirectory());
75 fuzz_run.addArgs(&.{ fuzz_iterations_arg, fuzz_seed_arg });
76 fuzz_run.expectExitCode(0);
77 fuzz_run.skip_foreign_checks = true;
78
79 test_step.dependOn(&fuzz_run.step);
80}