Commit 3ad9349f09

Andrew Kelley <andrew@ziglang.org>
2019-07-05 20:08:56
add std.os.windows.subsystem
The original issue that #2445 wanted to fix was solved in the previous commit. However it also exposed the subsystem in the standard library, which is still useful. So that's done in this commit, and #2445 can be closed.
1 parent 0d84d52
Changed files (1)
std/os/windows.zig
@@ -20,6 +20,33 @@ pub const shell32 = @import("windows/shell32.zig");
 
 pub usingnamespace @import("windows/bits.zig");
 
+/// `builtin` is missing `subsystem` when the subsystem is automatically detected,
+/// so Zig standard library has the subsystem detection logic here. This should generally be
+/// used rather than `builtin.subsystem`.
+/// On non-windows targets, this is `null`.
+pub const subsystem: ?builtin.SubSystem = blk: {
+    if (@hasDecl(builtin, "subsystem")) break :blk builtin.subsystem;
+    switch (builtin.os) {
+        .windows => {
+            if (builtin.is_test) {
+                break :blk builtin.SubSystem.Console;
+            }
+            const root = @import("root");
+            if (@hasDecl(root, "WinMain") or
+                @hasDecl(root, "wWinMain") or
+                @hasDecl(root, "WinMainCRTStartup") or
+                @hasDecl(root, "wWinMainCRTStartup"))
+            {
+                break :blk builtin.SubSystem.Windows;
+            } else {
+                break :blk builtin.SubSystem.Console;
+            }
+        },
+        .uefi => break :blk builtin.SubSystem.EfiApplication,
+        else => break :blk null,
+    }
+};
+
 pub const CreateFileError = error{
     SharingViolation,
     PathAlreadyExists,