|
| 1 | +const std = @import("std"); |
| 2 | +const builtin = @import("builtin"); |
| 3 | + |
| 4 | +pub fn build(b: *std.Build) void { |
| 5 | + const test_step = b.step("test", "Test it"); |
| 6 | + b.default_step = test_step; |
| 7 | + |
| 8 | + const optimize: std.builtin.OptimizeMode = .Debug; |
| 9 | + const target = b.graph.host; |
| 10 | + |
| 11 | + if (builtin.os.tag != .windows) return; |
| 12 | + |
| 13 | + const expected_console_subsystem = std.fmt.comptimePrint("{}\n", .{@intFromEnum(std.coff.Subsystem.WINDOWS_CUI)}); |
| 14 | + const expected_windows_subsystem = std.fmt.comptimePrint("{}\n", .{@intFromEnum(std.coff.Subsystem.WINDOWS_GUI)}); |
| 15 | + |
| 16 | + // Normal Zig main, no libc linked |
| 17 | + { |
| 18 | + const main_mod = b.createModule(.{ |
| 19 | + .root_source_file = b.path("main.zig"), |
| 20 | + .optimize = optimize, |
| 21 | + .target = target, |
| 22 | + }); |
| 23 | + |
| 24 | + const main_inferred = b.addExecutable(.{ |
| 25 | + .name = "main_inferred", |
| 26 | + .root_module = main_mod, |
| 27 | + }); |
| 28 | + const run_inferred = b.addRunArtifact(main_inferred); |
| 29 | + run_inferred.expectStdErrEqual(expected_console_subsystem); |
| 30 | + run_inferred.expectExitCode(0); |
| 31 | + test_step.dependOn(&run_inferred.step); |
| 32 | + |
| 33 | + const main_console = b.addExecutable(.{ |
| 34 | + .name = "main_console", |
| 35 | + .root_module = main_mod, |
| 36 | + }); |
| 37 | + main_console.subsystem = .console; |
| 38 | + const run_console = b.addRunArtifact(main_console); |
| 39 | + run_console.expectStdErrEqual(expected_console_subsystem); |
| 40 | + run_console.expectExitCode(0); |
| 41 | + test_step.dependOn(&run_console.step); |
| 42 | + |
| 43 | + const main_windows = b.addExecutable(.{ |
| 44 | + .name = "main_windows", |
| 45 | + .root_module = main_mod, |
| 46 | + }); |
| 47 | + main_windows.subsystem = .windows; |
| 48 | + const run_windows = b.addRunArtifact(main_windows); |
| 49 | + run_windows.expectStdErrEqual(expected_windows_subsystem); |
| 50 | + run_windows.expectExitCode(0); |
| 51 | + test_step.dependOn(&run_windows.step); |
| 52 | + } |
| 53 | + |
| 54 | + // Normal Zig main, libc linked |
| 55 | + { |
| 56 | + const main_link_libc_mod = b.createModule(.{ |
| 57 | + .root_source_file = b.path("main.zig"), |
| 58 | + .optimize = optimize, |
| 59 | + .target = target, |
| 60 | + .link_libc = true, |
| 61 | + }); |
| 62 | + |
| 63 | + const main_link_libc_inferred = b.addExecutable(.{ |
| 64 | + .name = "main_link_libc_inferred", |
| 65 | + .root_module = main_link_libc_mod, |
| 66 | + }); |
| 67 | + const run_inferred = b.addRunArtifact(main_link_libc_inferred); |
| 68 | + run_inferred.expectStdErrEqual(expected_console_subsystem); |
| 69 | + run_inferred.expectExitCode(0); |
| 70 | + test_step.dependOn(&run_inferred.step); |
| 71 | + |
| 72 | + const main_link_libc_console = b.addExecutable(.{ |
| 73 | + .name = "main_link_libc_console", |
| 74 | + .root_module = main_link_libc_mod, |
| 75 | + }); |
| 76 | + main_link_libc_console.subsystem = .console; |
| 77 | + const run_console = b.addRunArtifact(main_link_libc_console); |
| 78 | + run_console.expectStdErrEqual(expected_console_subsystem); |
| 79 | + run_console.expectExitCode(0); |
| 80 | + test_step.dependOn(&run_console.step); |
| 81 | + |
| 82 | + const main_link_libc_windows = b.addExecutable(.{ |
| 83 | + .name = "main_link_libc_windows", |
| 84 | + .root_module = main_link_libc_mod, |
| 85 | + }); |
| 86 | + main_link_libc_windows.subsystem = .windows; |
| 87 | + const run_windows = b.addRunArtifact(main_link_libc_windows); |
| 88 | + run_windows.expectStdErrEqual(expected_windows_subsystem); |
| 89 | + run_windows.expectExitCode(0); |
| 90 | + test_step.dependOn(&run_windows.step); |
| 91 | + } |
| 92 | + |
| 93 | + // wWinMain |
| 94 | + { |
| 95 | + const winmain_mod = b.createModule(.{ |
| 96 | + .root_source_file = b.path("winmain.zig"), |
| 97 | + .optimize = optimize, |
| 98 | + .target = target, |
| 99 | + }); |
| 100 | + |
| 101 | + const winmain_inferred = b.addExecutable(.{ |
| 102 | + .name = "winmain_inferred", |
| 103 | + .root_module = winmain_mod, |
| 104 | + }); |
| 105 | + const run_inferred = b.addRunArtifact(winmain_inferred); |
| 106 | + run_inferred.expectStdErrEqual(expected_windows_subsystem); |
| 107 | + run_inferred.expectExitCode(0); |
| 108 | + test_step.dependOn(&run_inferred.step); |
| 109 | + |
| 110 | + const winmain_console = b.addExecutable(.{ |
| 111 | + .name = "winmain_console", |
| 112 | + .root_module = winmain_mod, |
| 113 | + }); |
| 114 | + winmain_console.subsystem = .console; |
| 115 | + const run_console = b.addRunArtifact(winmain_console); |
| 116 | + run_console.expectStdErrEqual(expected_console_subsystem); |
| 117 | + run_console.expectExitCode(0); |
| 118 | + test_step.dependOn(&run_console.step); |
| 119 | + |
| 120 | + const winmain_windows = b.addExecutable(.{ |
| 121 | + .name = "winmain_windows", |
| 122 | + .root_module = winmain_mod, |
| 123 | + }); |
| 124 | + winmain_windows.subsystem = .windows; |
| 125 | + const run_windows = b.addRunArtifact(winmain_windows); |
| 126 | + run_windows.expectStdErrEqual(expected_windows_subsystem); |
| 127 | + run_windows.expectExitCode(0); |
| 128 | + test_step.dependOn(&run_windows.step); |
| 129 | + } |
| 130 | + |
| 131 | + // exported callconv(.c) main, libc must be linked |
| 132 | + { |
| 133 | + const cmain_mod = b.createModule(.{ |
| 134 | + .root_source_file = b.path("cmain.zig"), |
| 135 | + .optimize = optimize, |
| 136 | + .target = target, |
| 137 | + .link_libc = true, |
| 138 | + }); |
| 139 | + |
| 140 | + const cmain_inferred = b.addExecutable(.{ |
| 141 | + .name = "cmain_inferred", |
| 142 | + .root_module = cmain_mod, |
| 143 | + }); |
| 144 | + const run_inferred = b.addRunArtifact(cmain_inferred); |
| 145 | + run_inferred.expectStdErrEqual(expected_console_subsystem); |
| 146 | + run_inferred.expectExitCode(0); |
| 147 | + test_step.dependOn(&run_inferred.step); |
| 148 | + |
| 149 | + const cmain_console = b.addExecutable(.{ |
| 150 | + .name = "cmain_console", |
| 151 | + .root_module = cmain_mod, |
| 152 | + }); |
| 153 | + cmain_console.subsystem = .console; |
| 154 | + const run_console = b.addRunArtifact(cmain_console); |
| 155 | + run_console.expectStdErrEqual(expected_console_subsystem); |
| 156 | + run_console.expectExitCode(0); |
| 157 | + test_step.dependOn(&run_console.step); |
| 158 | + |
| 159 | + const cmain_windows = b.addExecutable(.{ |
| 160 | + .name = "cmain_windows", |
| 161 | + .root_module = cmain_mod, |
| 162 | + }); |
| 163 | + cmain_windows.subsystem = .windows; |
| 164 | + const run_windows = b.addRunArtifact(cmain_windows); |
| 165 | + run_windows.expectStdErrEqual(expected_windows_subsystem); |
| 166 | + run_windows.expectExitCode(0); |
| 167 | + test_step.dependOn(&run_windows.step); |
| 168 | + } |
| 169 | +} |
0 commit comments