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