From 3ad9349f09e5afdacd64b8ab257ae1d09859f48a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 5 Jul 2019 14:08:56 -0400 Subject: [PATCH] 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. --- std/os/windows.zig | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/std/os/windows.zig b/std/os/windows.zig index 3609d92d8036a723656902d702ede0c322a54879..2377fc00e742c378e0dec38642843dd58505f408 100644 --- a/std/os/windows.zig +++ b/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, -- 2.54.0