authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-11-07 18:56:52-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-11-07 18:56:52-08:00
log74d2536715c4d4c27d7075b9675fa330e5be02cf
treef0d213d829e1f6236a1974ecd078d21863837d51
parentbf15c791faa7bf5a029ec58432b3e8e16c691dff
parent54f2a7c833bf2ca0b370ebe2470bb0dd71206cba
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #25158 from castholm/subsystem

Misc. Windows subsystem refactorings

11 files changed, 75 insertions(+), 147 deletions(-)

lib/std/Build/Step/Compile.zig+2-11
......@@ -171,7 +171,7 @@ lto: ?std.zig.LtoMode = null,
171171
172172dll_export_fns: ?bool = null,
173173
174subsystem: ?std.Target.SubSystem = null,
174subsystem: ?std.zig.Subsystem = null,
175175
176176/// (Windows) When targeting the MinGW ABI, use the unicode entry point (wmain/wWinMain)
177177mingw_unicode_entry_point: bool = false,
......@@ -1764,16 +1764,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
17641764
17651765 if (compile.subsystem) |subsystem| {
17661766 try zig_args.append("--subsystem");
1767 try zig_args.append(switch (subsystem) {
1768 .Console => "console",
1769 .Windows => "windows",
1770 .Posix => "posix",
1771 .Native => "native",
1772 .EfiApplication => "efi_application",
1773 .EfiBootServiceDriver => "efi_boot_service_driver",
1774 .EfiRom => "efi_rom",
1775 .EfiRuntimeDriver => "efi_runtime_driver",
1776 });
1767 try zig_args.append(@tagName(subsystem));
17771768 }
17781769
17791770 if (compile.mingw_unicode_entry_point) {
lib/std/Target.zig+2-10
......@@ -1138,16 +1138,8 @@ pub fn toCoffMachine(target: *const Target) std.coff.IMAGE.FILE.MACHINE {
11381138 };
11391139}
11401140
1141pub const SubSystem = enum {
1142 Console,
1143 Windows,
1144 Posix,
1145 Native,
1146 EfiApplication,
1147 EfiBootServiceDriver,
1148 EfiRom,
1149 EfiRuntimeDriver,
1150};
1141/// Deprecated; use 'std.zig.Subsystem' instead. To be removed after 0.16.0 is tagged.
1142pub const SubSystem = std.zig.Subsystem;
11511143
11521144pub const Cpu = struct {
11531145 /// Architecture
lib/std/builtin.zig-26
......@@ -6,32 +6,6 @@ const root = @import("root");
66
77pub const assembly = @import("builtin/assembly.zig");
88
9/// `explicit_subsystem` is missing when the subsystem is automatically detected,
10/// so Zig standard library has the subsystem detection logic here. This should generally be
11/// used rather than `explicit_subsystem`.
12/// On non-Windows targets, this is `null`.
13pub const subsystem: ?std.Target.SubSystem = blk: {
14 if (@hasDecl(builtin, "explicit_subsystem")) break :blk builtin.explicit_subsystem;
15 switch (builtin.os.tag) {
16 .windows => {
17 if (builtin.is_test) {
18 break :blk std.Target.SubSystem.Console;
19 }
20 if (@hasDecl(root, "main") or
21 @hasDecl(root, "WinMain") or
22 @hasDecl(root, "wWinMain") or
23 @hasDecl(root, "WinMainCRTStartup") or
24 @hasDecl(root, "wWinMainCRTStartup"))
25 {
26 break :blk std.Target.SubSystem.Windows;
27 } else {
28 break :blk std.Target.SubSystem.Console;
29 }
30 },
31 else => break :blk null,
32 }
33};
34
359/// This data structure is used by the Zig language code generation and
3610/// therefore must be kept in sync with the compiler implementation.
3711pub const StackTrace = struct {
lib/std/start.zig+9-12
......@@ -734,24 +734,21 @@ pub fn call_wWinMain() std.os.windows.INT {
734734 // - u32 in PEB.ProcessParameters.dwShowWindow
735735 // Since STARTUPINFO is the bottleneck for the allowed values, we use `u16` as the
736736 // type which can coerce into i32/c_int/u32 depending on how the user defines their wWinMain
737 // (the Win32 docs show wWinMain with `int` as the type for nCmdShow).
738 const nCmdShow: u16 = nCmdShow: {
739 // This makes Zig match the nCmdShow behavior of a C program with a WinMain symbol:
737 // (the Win32 docs show wWinMain with `int` as the type for nShowCmd).
738 const nShowCmd: u16 = nShowCmd: {
739 // This makes Zig match the nShowCmd behavior of a C program with a WinMain symbol:
740740 // - With STARTF_USESHOWWINDOW set in STARTUPINFO.dwFlags of the CreateProcess call:
741 // - Compiled with subsystem:console -> nCmdShow is always SW_SHOWDEFAULT
742 // - Compiled with subsystem:windows -> nCmdShow is STARTUPINFO.wShowWindow from
743 // the parent CreateProcess call
741 // - nShowCmd is STARTUPINFO.wShowWindow from the parent CreateProcess call
744742 // - With STARTF_USESHOWWINDOW unset:
745 // - nCmdShow is always SW_SHOWDEFAULT
743 // - nShowCmd is always SW_SHOWDEFAULT
746744 const SW_SHOWDEFAULT = 10;
747745 const STARTF_USESHOWWINDOW = 1;
748 // root having a wWinMain means that std.builtin.subsystem will always have a non-null value.
749 if (std.builtin.subsystem.? == .Windows and peb.ProcessParameters.dwFlags & STARTF_USESHOWWINDOW != 0) {
750 break :nCmdShow @truncate(peb.ProcessParameters.dwShowWindow);
746 if (peb.ProcessParameters.dwFlags & STARTF_USESHOWWINDOW != 0) {
747 break :nShowCmd @truncate(peb.ProcessParameters.dwShowWindow);
751748 }
752 break :nCmdShow SW_SHOWDEFAULT;
749 break :nShowCmd SW_SHOWDEFAULT;
753750 };
754751
755752 // second parameter hPrevInstance, MSDN: "This parameter is always NULL"
756 return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);
753 return root.wWinMain(hInstance, null, lpCmdLine, nShowCmd);
757754}
lib/std/zig.zig+28
......@@ -349,6 +349,34 @@ pub const BuildId = union(enum) {
349349
350350pub const LtoMode = enum { none, full, thin };
351351
352pub const Subsystem = enum {
353 console,
354 windows,
355 posix,
356 native,
357 efi_application,
358 efi_boot_service_driver,
359 efi_rom,
360 efi_runtime_driver,
361
362 /// Deprecated; use '.console' instead. To be removed after 0.16.0 is tagged.
363 pub const Console: Subsystem = .console;
364 /// Deprecated; use '.windows' instead. To be removed after 0.16.0 is tagged.
365 pub const Windows: Subsystem = .windows;
366 /// Deprecated; use '.posix' instead. To be removed after 0.16.0 is tagged.
367 pub const Posix: Subsystem = .posix;
368 /// Deprecated; use '.native' instead. To be removed after 0.16.0 is tagged.
369 pub const Native: Subsystem = .native;
370 /// Deprecated; use '.efi_application' instead. To be removed after 0.16.0 is tagged.
371 pub const EfiApplication: Subsystem = .efi_application;
372 /// Deprecated; use '.efi_boot_service_driver' instead. To be removed after 0.16.0 is tagged.
373 pub const EfiBootServiceDriver: Subsystem = .efi_boot_service_driver;
374 /// Deprecated; use '.efi_rom' instead. To be removed after 0.16.0 is tagged.
375 pub const EfiRom: Subsystem = .efi_rom;
376 /// Deprecated; use '.efi_runtime_driver' instead. To be removed after 0.16.0 is tagged.
377 pub const EfiRuntimeDriver: Subsystem = .efi_runtime_driver;
378};
379
352380/// Renders a `std.Target.Cpu` value into a textual representation that can be parsed
353381/// via the `-mcpu` flag passed to the Zig compiler.
354382/// Appends the result to `buffer`.
src/Compilation.zig+1-1
......@@ -1766,7 +1766,7 @@ pub const CreateOptions = struct {
17661766 reference_trace: ?u32 = null,
17671767 test_filters: []const []const u8 = &.{},
17681768 test_runner_path: ?[]const u8 = null,
1769 subsystem: ?std.Target.SubSystem = null,
1769 subsystem: ?std.zig.Subsystem = null,
17701770 mingw_unicode_entry_point: bool = false,
17711771 /// (Zig compiler development) Enable dumping linker's state as JSON.
17721772 enable_link_snapshots: bool = false,
src/link.zig+1-1
......@@ -448,7 +448,7 @@ pub const File = struct {
448448 allow_shlib_undefined: ?bool,
449449 allow_undefined_version: bool,
450450 enable_new_dtags: ?bool,
451 subsystem: ?std.Target.SubSystem,
451 subsystem: ?std.zig.Subsystem,
452452 linker_script: ?[]const u8,
453453 version_script: ?[]const u8,
454454 soname: ?[]const u8,
src/link/Lld.zig+22-59
......@@ -19,7 +19,7 @@ const Coff = struct {
1919 minor_subsystem_version: u16,
2020 lib_directories: []const Cache.Directory,
2121 module_definition_file: ?[]const u8,
22 subsystem: ?std.Target.SubSystem,
22 subsystem: ?std.zig.Subsystem,
2323 /// These flags are populated by `codegen.llvm.updateExports` to allow us to guess the subsystem.
2424 lld_export_flags: struct {
2525 c_main: bool,
......@@ -554,7 +554,7 @@ fn coffLink(lld: *Lld, arena: Allocator) !void {
554554 try argv.append(try allocPrint(arena, "-DEF:{s}", .{def}));
555555 }
556556
557 const resolved_subsystem: ?std.Target.SubSystem = blk: {
557 const resolved_subsystem: ?std.zig.Subsystem = blk: {
558558 if (coff.subsystem) |explicit| break :blk explicit;
559559 switch (target.os.tag) {
560560 .windows => {
......@@ -565,13 +565,13 @@ fn coffLink(lld: *Lld, arena: Allocator) !void {
565565 coff.lld_export_flags.winmain_crt_startup or
566566 coff.lld_export_flags.wwinmain_crt_startup)
567567 {
568 break :blk .Console;
568 break :blk .console;
569569 }
570570 if (coff.lld_export_flags.winmain or coff.lld_export_flags.wwinmain)
571 break :blk .Windows;
571 break :blk .windows;
572572 }
573573 },
574 .uefi => break :blk .EfiApplication,
574 .uefi => break :blk .efi_application,
575575 else => {},
576576 }
577577 break :blk null;
......@@ -580,60 +580,23 @@ fn coffLink(lld: *Lld, arena: Allocator) !void {
580580 const Mode = enum { uefi, win32 };
581581 const mode: Mode = mode: {
582582 if (resolved_subsystem) |subsystem| {
583 const subsystem_suffix = try allocPrint(arena, ",{d}.{d}", .{
584 coff.major_subsystem_version, coff.minor_subsystem_version,
585 });
586
587 switch (subsystem) {
588 .Console => {
589 try argv.append(try allocPrint(arena, "-SUBSYSTEM:console{s}", .{
590 subsystem_suffix,
591 }));
592 break :mode .win32;
593 },
594 .EfiApplication => {
595 try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_application{s}", .{
596 subsystem_suffix,
597 }));
598 break :mode .uefi;
599 },
600 .EfiBootServiceDriver => {
601 try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_boot_service_driver{s}", .{
602 subsystem_suffix,
603 }));
604 break :mode .uefi;
605 },
606 .EfiRom => {
607 try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_rom{s}", .{
608 subsystem_suffix,
609 }));
610 break :mode .uefi;
611 },
612 .EfiRuntimeDriver => {
613 try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_runtime_driver{s}", .{
614 subsystem_suffix,
615 }));
616 break :mode .uefi;
617 },
618 .Native => {
619 try argv.append(try allocPrint(arena, "-SUBSYSTEM:native{s}", .{
620 subsystem_suffix,
621 }));
622 break :mode .win32;
623 },
624 .Posix => {
625 try argv.append(try allocPrint(arena, "-SUBSYSTEM:posix{s}", .{
626 subsystem_suffix,
627 }));
628 break :mode .win32;
629 },
630 .Windows => {
631 try argv.append(try allocPrint(arena, "-SUBSYSTEM:windows{s}", .{
632 subsystem_suffix,
633 }));
634 break :mode .win32;
635 },
636 }
583 try argv.append(try allocPrint(arena, "-SUBSYSTEM:{s},{d}.{d}", .{
584 @tagName(subsystem),
585 coff.major_subsystem_version,
586 coff.minor_subsystem_version,
587 }));
588 break :mode switch (subsystem) {
589 .console,
590 .windows,
591 .posix,
592 .native,
593 => .win32,
594 .efi_application,
595 .efi_boot_service_driver,
596 .efi_rom,
597 .efi_runtime_driver,
598 => .uefi,
599 };
637600 } else if (target.os.tag == .uefi) {
638601 break :mode .uefi;
639602 } else {
src/main.zig+7-24
......@@ -893,7 +893,7 @@ fn buildOutputType(
893893 var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
894894 var override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena);
895895 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
896 var subsystem: ?std.Target.SubSystem = null;
896 var subsystem: ?std.zig.Subsystem = null;
897897 var major_subsystem_version: ?u16 = null;
898898 var minor_subsystem_version: ?u16 = null;
899899 var mingw_unicode_entry_point: bool = false;
......@@ -1135,7 +1135,7 @@ fn buildOutputType(
11351135 }
11361136 n_jobs = num;
11371137 } else if (mem.eql(u8, arg, "--subsystem")) {
1138 subsystem = try parseSubSystem(args_iter.nextOrFatal());
1138 subsystem = try parseSubsystem(args_iter.nextOrFatal());
11391139 } else if (mem.eql(u8, arg, "-O")) {
11401140 mod_opts.optimize_mode = parseOptimizeMode(args_iter.nextOrFatal());
11411141 } else if (mem.cutPrefix(u8, arg, "-fentry=")) |rest| {
......@@ -2415,7 +2415,7 @@ fn buildOutputType(
24152415 } else if (mem.eql(u8, arg, "-rpath") or mem.eql(u8, arg, "--rpath") or mem.eql(u8, arg, "-R")) {
24162416 try create_module.rpath_list.append(arena, linker_args_it.nextOrFatal());
24172417 } else if (mem.eql(u8, arg, "--subsystem")) {
2418 subsystem = try parseSubSystem(linker_args_it.nextOrFatal());
2418 subsystem = try parseSubsystem(linker_args_it.nextOrFatal());
24192419 } else if (mem.eql(u8, arg, "-I") or
24202420 mem.eql(u8, arg, "--dynamic-linker") or
24212421 mem.eql(u8, arg, "-dynamic-linker"))
......@@ -2743,7 +2743,7 @@ fn buildOutputType(
27432743 try symbol_wrap_set.put(arena, next_arg, {});
27442744 } else if (mem.startsWith(u8, arg, "/subsystem:")) {
27452745 var split_it = mem.splitBackwardsScalar(u8, arg, ':');
2746 subsystem = try parseSubSystem(split_it.first());
2746 subsystem = try parseSubsystem(split_it.first());
27472747 } else if (mem.startsWith(u8, arg, "/implib:")) {
27482748 var split_it = mem.splitBackwardsScalar(u8, arg, ':');
27492749 emit_implib = .{ .yes = split_it.first() };
......@@ -6657,26 +6657,10 @@ fn warnAboutForeignBinaries(
66576657 }
66586658}
66596659
6660fn parseSubSystem(next_arg: []const u8) !std.Target.SubSystem {
6661 if (mem.eql(u8, next_arg, "console")) {
6662 return .Console;
6663 } else if (mem.eql(u8, next_arg, "windows")) {
6664 return .Windows;
6665 } else if (mem.eql(u8, next_arg, "posix")) {
6666 return .Posix;
6667 } else if (mem.eql(u8, next_arg, "native")) {
6668 return .Native;
6669 } else if (mem.eql(u8, next_arg, "efi_application")) {
6670 return .EfiApplication;
6671 } else if (mem.eql(u8, next_arg, "efi_boot_service_driver")) {
6672 return .EfiBootServiceDriver;
6673 } else if (mem.eql(u8, next_arg, "efi_rom")) {
6674 return .EfiRom;
6675 } else if (mem.eql(u8, next_arg, "efi_runtime_driver")) {
6676 return .EfiRuntimeDriver;
6677 } else {
6660fn parseSubsystem(arg: []const u8) !std.zig.Subsystem {
6661 return std.meta.stringToEnum(std.zig.Subsystem, arg) orelse
66786662 fatal("invalid: --subsystem: '{s}'. Options are:\n{s}", .{
6679 next_arg,
6663 arg,
66806664 \\ console
66816665 \\ windows
66826666 \\ posix
......@@ -6687,7 +6671,6 @@ fn parseSubSystem(next_arg: []const u8) !std.Target.SubSystem {
66876671 \\ efi_runtime_driver
66886672 \\
66896673 });
6690 }
66916674}
66926675
66936676/// Model a header searchlist as a group.
test/standalone/issue_5825/build.zig+1-1
......@@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void {
3131 .target = target,
3232 }),
3333 });
34 exe.subsystem = .Console;
34 exe.subsystem = .console;
3535 exe.root_module.linkSystemLibrary("kernel32", .{});
3636 exe.root_module.linkSystemLibrary("ntdll", .{});
3737 exe.root_module.addObject(obj);
test/standalone/windows_entry_points/build.zig+2-2
......@@ -53,7 +53,7 @@ pub fn build(b: *std.Build) void {
5353 .link_libc = true,
5454 }),
5555 });
56 // Note: `exe.subsystem = .Windows;` is not necessary
56 // Note: `exe.subsystem = .windows;` is not necessary
5757 exe.root_module.addCSourceFile(.{ .file = b.path("winmain.c") });
5858
5959 _ = exe.getEmittedBin();
......@@ -71,7 +71,7 @@ pub fn build(b: *std.Build) void {
7171 }),
7272 });
7373 exe.mingw_unicode_entry_point = true;
74 // Note: `exe.subsystem = .Windows;` is not necessary
74 // Note: `exe.subsystem = .windows;` is not necessary
7575 exe.root_module.addCSourceFile(.{ .file = b.path("wwinmain.c") });
7676
7777 _ = exe.getEmittedBin();