| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | |
| 4 | pub fn build(b: *std.Build) void { |
| 5 | const step = b.step("test", "Run simple standalone test cases"); |
| 6 | b.default_step = step; |
| 7 | |
| 8 | const skip_debug = b.option(bool, "skip_debug", "Skip debug builds") orelse false; |
| 9 | const skip_release_safe = b.option(bool, "skip_release_safe", "Skip release-safe builds") orelse false; |
| 10 | const skip_release_fast = b.option(bool, "skip_release_fast", "Skip release-fast builds") orelse false; |
| 11 | const skip_release_small = b.option(bool, "skip_release_small", "Skip release-small builds") orelse false; |
| 12 | |
| 13 | var optimize_modes_buf: [4]std.builtin.Optimize = undefined; |
| 14 | var optimize_modes_len: usize = 0; |
| 15 | if (!skip_debug) { |
| 16 | optimize_modes_buf[optimize_modes_len] = .debug; |
| 17 | optimize_modes_len += 1; |
| 18 | } |
| 19 | if (!skip_release_safe) { |
| 20 | optimize_modes_buf[optimize_modes_len] = .safe; |
| 21 | optimize_modes_len += 1; |
| 22 | } |
| 23 | if (!skip_release_fast) { |
| 24 | optimize_modes_buf[optimize_modes_len] = .fast; |
| 25 | optimize_modes_len += 1; |
| 26 | } |
| 27 | if (!skip_release_small) { |
| 28 | optimize_modes_buf[optimize_modes_len] = .small; |
| 29 | optimize_modes_len += 1; |
| 30 | } |
| 31 | const optimize_modes = optimize_modes_buf[0..optimize_modes_len]; |
| 32 | |
| 33 | for (cases) |case| { |
| 34 | for (optimize_modes) |optimize| { |
| 35 | if (!case.all_modes and optimize != .debug) continue; |
| 36 | if (case.os_filter) |os_tag| { |
| 37 | if (os_tag != builtin.os.tag) continue; |
| 38 | } |
| 39 | |
| 40 | const resolved_target = b.resolveTargetQuery(case.target); |
| 41 | |
| 42 | if (case.is_exe) { |
| 43 | const exe = b.addExecutable(.{ |
| 44 | .name = std.fs.path.stem(case.src_path), |
| 45 | .root_module = b.createModule(.{ |
| 46 | .root_source_file = b.path(case.src_path), |
| 47 | .optimize = optimize, |
| 48 | .target = resolved_target, |
| 49 | }), |
| 50 | }); |
| 51 | if (case.link_libc) exe.root_module.link_libc = true; |
| 52 | |
| 53 | _ = exe.getEmittedBin(); |
| 54 | |
| 55 | step.dependOn(&exe.step); |
| 56 | } |
| 57 | |
| 58 | if (case.is_test) { |
| 59 | const exe = b.addTest(.{ |
| 60 | .name = std.fs.path.stem(case.src_path), |
| 61 | .root_module = b.createModule(.{ |
| 62 | .root_source_file = b.path(case.src_path), |
| 63 | .optimize = optimize, |
| 64 | .target = resolved_target, |
| 65 | }), |
| 66 | }); |
| 67 | if (case.link_libc) exe.root_module.link_libc = true; |
| 68 | |
| 69 | const run = b.addRunArtifact(exe); |
| 70 | step.dependOn(&run.step); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | const Case = struct { |
| 77 | src_path: []const u8, |
| 78 | link_libc: bool = false, |
| 79 | all_modes: bool = false, |
| 80 | target: std.Target.Query = .{}, |
| 81 | is_test: bool = false, |
| 82 | is_exe: bool = true, |
| 83 | /// Run only on this OS. |
| 84 | os_filter: ?std.Target.Os.Tag = null, |
| 85 | }; |
| 86 | |
| 87 | const cases = [_]Case{ |
| 88 | .{ |
| 89 | .src_path = "hello_world/hello.zig", |
| 90 | .all_modes = true, |
| 91 | }, |
| 92 | .{ |
| 93 | .src_path = "hello_world/hello_libc.zig", |
| 94 | .link_libc = true, |
| 95 | .all_modes = true, |
| 96 | }, |
| 97 | .{ |
| 98 | .src_path = "cat/main.zig", |
| 99 | }, |
| 100 | // https://github.com/ziglang/zig/issues/6025 |
| 101 | //.{ |
| 102 | // .src_path = "issue_9693/main.zig", |
| 103 | //}, |
| 104 | .{ |
| 105 | .src_path = "issue_7030.zig", |
| 106 | .target = .{ |
| 107 | .cpu_arch = .wasm32, |
| 108 | .os_tag = .freestanding, |
| 109 | }, |
| 110 | }, |
| 111 | .{ .src_path = "guess_number/main.zig" }, |
| 112 | .{ .src_path = "main_return_error/error_u8.zig" }, |
| 113 | .{ .src_path = "main_return_error/error_u8_non_zero.zig" }, |
| 114 | .{ .src_path = "noreturn_call/inline.zig" }, |
| 115 | .{ .src_path = "noreturn_call/as_arg.zig" }, |
| 116 | .{ .src_path = "std_enums_big_enums.zig" }, |
| 117 | .{ |
| 118 | .src_path = "issue_9402/main.zig", |
| 119 | .os_filter = .windows, |
| 120 | .link_libc = true, |
| 121 | }, |
| 122 | }; |