aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/posix/build.zig
blob: 52ec99628db2b0558ad8a17ca1ae055d7301481d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const std = @import("std");
const builtin = @import("builtin");

const Case = struct {
    src_path: []const u8,
    set_env_vars: bool = false,
};

const cases = [_]Case{
    .{
        .src_path = "cwd.zig",
    },
    .{
        .src_path = "getenv.zig",
        .set_env_vars = true,
    },
    .{
        .src_path = "sigaction.zig",
    },
    .{
        .src_path = "relpaths.zig",
    },
};

pub fn build(b: *std.Build) void {
    const test_step = b.step("test", "Run POSIX standalone test cases");
    b.default_step = test_step;

    const optimize = b.standardOptimizeOption(.{});

    const default_target = b.resolveTargetQuery(.{});

    // Run each test case built against libc-less, glibc, and musl.
    for (cases) |case| {
        const run_def = run_exe(b, optimize, &case, default_target, false);
        test_step.dependOn(&run_def.step);

        if (default_target.result.os.tag == .linux) {
            const gnu_target = b.resolveTargetQuery(.{ .abi = .gnu });
            const musl_target = b.resolveTargetQuery(.{ .abi = .musl });

            const run_gnu = run_exe(b, optimize, &case, gnu_target, true);
            const run_musl = run_exe(b, optimize, &case, musl_target, true);

            test_step.dependOn(&run_gnu.step);
            test_step.dependOn(&run_musl.step);
        } else {
            const run_libc = run_exe(b, optimize, &case, default_target, true);
            test_step.dependOn(&run_libc.step);
        }
    }
}

fn run_exe(b: *std.Build, optimize: std.builtin.OptimizeMode, case: *const Case, target: std.Build.ResolvedTarget, link_libc: bool) *std.Build.Step.Run {
    const exe_name = b.fmt("test-posix-{s}{s}{s}", .{
        std.fs.path.stem(case.src_path),
        if (link_libc) "-libc" else "",
        if (link_libc and target.result.isGnuLibC()) "-gnu" else if (link_libc and target.result.isMuslLibC()) "-musl" else "",
    });

    const exe = b.addExecutable(.{
        .name = exe_name,
        .root_module = b.createModule(.{
            .root_source_file = b.path(case.src_path),
            .link_libc = link_libc,
            .optimize = optimize,
            .target = target,
        }),
    });

    const run_cmd = b.addRunArtifact(exe);

    if (case.set_env_vars) {
        run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_1EQ", "test=variable");
        run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_3EQ", "=test=variable=");
        run_cmd.setEnvironmentVariable("ZIG_TEST_POSIX_EMPTY", "");
    }

    return run_cmd;
}