master
 1const std = @import("std");
 2const builtin = @import("builtin");
 3
 4const Case = struct {
 5    src_path: []const u8,
 6    set_env_vars: bool = false,
 7};
 8
 9const cases = [_]Case{
10    .{
11        .src_path = "cwd.zig",
12    },
13    .{
14        .src_path = "getenv.zig",
15        .set_env_vars = true,
16    },
17    .{
18        .src_path = "sigaction.zig",
19    },
20    .{
21        .src_path = "relpaths.zig",
22    },
23};
24
25pub fn build(b: *std.Build) void {
26    const test_step = b.step("test", "Run POSIX standalone test cases");
27    b.default_step = test_step;
28
29    const optimize = b.standardOptimizeOption(.{});
30
31    const default_target = b.resolveTargetQuery(.{});
32
33    // Run each test case built against libc-less, glibc, and musl.
34    for (cases) |case| {
35        const run_def = run_exe(b, optimize, &case, default_target, false);
36        test_step.dependOn(&run_def.step);
37
38        if (default_target.result.os.tag == .linux) {
39            const gnu_target = b.resolveTargetQuery(.{ .abi = .gnu });
40            const musl_target = b.resolveTargetQuery(.{ .abi = .musl });
41
42            const run_gnu = run_exe(b, optimize, &case, gnu_target, true);
43            const run_musl = run_exe(b, optimize, &case, musl_target, true);
44
45            test_step.dependOn(&run_gnu.step);
46            test_step.dependOn(&run_musl.step);
47        } else {
48            const run_libc = run_exe(b, optimize, &case, default_target, true);
49            test_step.dependOn(&run_libc.step);
50        }
51    }
52}
53
54fn run_exe(b: *std.Build, optimize: std.builtin.OptimizeMode, case: *const Case, target: std.Build.ResolvedTarget, link_libc: bool) *std.Build.Step.Run {
55    const exe_name = b.fmt("test-posix-{s}{s}{s}", .{
56        std.fs.path.stem(case.src_path),
57        if (link_libc) "-libc" else "",
58        if (link_libc and target.result.isGnuLibC()) "-gnu" else if (link_libc and target.result.isMuslLibC()) "-musl" else "",
59    });
60
61    const exe = b.addExecutable(.{
62        .name = exe_name,
63        .root_module = b.createModule(.{
64            .root_source_file = b.path(case.src_path),
65            .link_libc = link_libc,
66            .optimize = optimize,
67            .target = target,
68        }),
69    });
70
71    const run_cmd = b.addRunArtifact(exe);
72
73    if (case.set_env_vars) {
74        run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_1EQ", "test=variable");
75        run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_3EQ", "=test=variable=");
76        run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_EMPTY", "");
77    }
78
79    return run_cmd;
80}