master
 1const std = @import("std");
 2
 3pub fn build(b: *std.Build) !void {
 4    const test_step = b.step("test", "Test the program");
 5    b.default_step = test_step;
 6
 7    const is_macos = b.graph.host.result.os.tag == .macos;
 8
 9    for ([_]struct { std.Target.Os.Tag, []const std.Target.Cpu.Arch }{
10        // .s390x and mips64(el) fail to build
11        .{ .linux, &.{ .aarch64, .aarch64_be, .loongarch64, .powerpc64, .powerpc64le, .riscv64, .x86_64 } },
12        .{ .macos, &.{ .aarch64, .x86_64 } },
13
14        // powerpc64, powerpc64le, and riscv64 are not supported by TSan yet.
15        .{ .freebsd, &.{ .aarch64, .x86_64 } },
16
17        .{ .netbsd, &.{.x86_64} },
18
19        // TSan doesn't have full support for windows yet.
20        // .{ .windows, &.{ .aarch64, .x86_64 } },
21    }) |entry| {
22        switch (entry[0]) {
23            // compiling tsan on macos requires system headers that aren't present during cross-compilation
24            .macos => {
25                if (!is_macos) continue;
26                const target = b.resolveTargetQuery(.{});
27                const exe = b.addExecutable(.{
28                    .name = b.fmt("tsan_{s}_{s}", .{ @tagName(entry[0]), @tagName(target.result.cpu.arch) }),
29                    .root_module = b.createModule(.{
30                        .root_source_file = b.path("main.zig"),
31                        .target = target,
32                        .optimize = .Debug,
33                        .sanitize_thread = true,
34                    }),
35                });
36                const install_exe = b.addInstallArtifact(exe, .{});
37                test_step.dependOn(&install_exe.step);
38            },
39            else => for (entry[1]) |arch| {
40                const target = b.resolveTargetQuery(.{
41                    .os_tag = entry[0],
42                    .cpu_arch = arch,
43                });
44                const exe = b.addExecutable(.{
45                    .name = b.fmt("tsan_{s}_{s}", .{ @tagName(entry[0]), @tagName(arch) }),
46                    .root_module = b.createModule(.{
47                        .root_source_file = b.path("main.zig"),
48                        .target = target,
49                        .optimize = .Debug,
50                        .sanitize_thread = true,
51                    }),
52                });
53                const install_exe = b.addInstallArtifact(exe, .{});
54                test_step.dependOn(&install_exe.step);
55            },
56        }
57    }
58}