-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Make entry points/inferred Windows subsystem conform better with conventions #25861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| const std = @import("std"); | ||
| const builtin = @import("builtin"); | ||
|
|
||
| pub fn build(b: *std.Build) void { | ||
| const test_step = b.step("test", "Test it"); | ||
| b.default_step = test_step; | ||
|
|
||
| const optimize: std.builtin.OptimizeMode = .Debug; | ||
| const target = b.graph.host; | ||
|
|
||
| if (builtin.os.tag != .windows) return; | ||
|
|
||
| const expected_console_subsystem = std.fmt.comptimePrint("{}\n", .{@intFromEnum(std.coff.Subsystem.WINDOWS_CUI)}); | ||
| const expected_windows_subsystem = std.fmt.comptimePrint("{}\n", .{@intFromEnum(std.coff.Subsystem.WINDOWS_GUI)}); | ||
|
|
||
| // Normal Zig main, no libc linked | ||
| { | ||
| const main_mod = b.createModule(.{ | ||
| .root_source_file = b.path("main.zig"), | ||
| .optimize = optimize, | ||
| .target = target, | ||
| }); | ||
|
|
||
| const main_inferred = b.addExecutable(.{ | ||
| .name = "main_inferred", | ||
| .root_module = main_mod, | ||
| }); | ||
| const run_inferred = b.addRunArtifact(main_inferred); | ||
| run_inferred.expectStdErrEqual(expected_console_subsystem); | ||
| run_inferred.expectExitCode(0); | ||
| test_step.dependOn(&run_inferred.step); | ||
|
|
||
| const main_console = b.addExecutable(.{ | ||
| .name = "main_console", | ||
| .root_module = main_mod, | ||
| }); | ||
| main_console.subsystem = .console; | ||
| const run_console = b.addRunArtifact(main_console); | ||
| run_console.expectStdErrEqual(expected_console_subsystem); | ||
| run_console.expectExitCode(0); | ||
| test_step.dependOn(&run_console.step); | ||
|
|
||
| const main_windows = b.addExecutable(.{ | ||
| .name = "main_windows", | ||
| .root_module = main_mod, | ||
| }); | ||
| main_windows.subsystem = .windows; | ||
| const run_windows = b.addRunArtifact(main_windows); | ||
| run_windows.expectStdErrEqual(expected_windows_subsystem); | ||
| run_windows.expectExitCode(0); | ||
| test_step.dependOn(&run_windows.step); | ||
| } | ||
|
|
||
| // Normal Zig main, libc linked | ||
| { | ||
| const main_link_libc_mod = b.createModule(.{ | ||
| .root_source_file = b.path("main.zig"), | ||
| .optimize = optimize, | ||
| .target = target, | ||
| .link_libc = true, | ||
| }); | ||
|
|
||
| const main_link_libc_inferred = b.addExecutable(.{ | ||
| .name = "main_link_libc_inferred", | ||
| .root_module = main_link_libc_mod, | ||
| }); | ||
| const run_inferred = b.addRunArtifact(main_link_libc_inferred); | ||
| run_inferred.expectStdErrEqual(expected_console_subsystem); | ||
| run_inferred.expectExitCode(0); | ||
| test_step.dependOn(&run_inferred.step); | ||
|
|
||
| const main_link_libc_console = b.addExecutable(.{ | ||
| .name = "main_link_libc_console", | ||
| .root_module = main_link_libc_mod, | ||
| }); | ||
| main_link_libc_console.subsystem = .console; | ||
| const run_console = b.addRunArtifact(main_link_libc_console); | ||
| run_console.expectStdErrEqual(expected_console_subsystem); | ||
| run_console.expectExitCode(0); | ||
| test_step.dependOn(&run_console.step); | ||
|
|
||
| const main_link_libc_windows = b.addExecutable(.{ | ||
| .name = "main_link_libc_windows", | ||
| .root_module = main_link_libc_mod, | ||
| }); | ||
| main_link_libc_windows.subsystem = .windows; | ||
| const run_windows = b.addRunArtifact(main_link_libc_windows); | ||
| run_windows.expectStdErrEqual(expected_windows_subsystem); | ||
| run_windows.expectExitCode(0); | ||
| test_step.dependOn(&run_windows.step); | ||
| } | ||
|
|
||
| // wWinMain | ||
| { | ||
| const winmain_mod = b.createModule(.{ | ||
| .root_source_file = b.path("winmain.zig"), | ||
| .optimize = optimize, | ||
| .target = target, | ||
| }); | ||
|
|
||
| const winmain_inferred = b.addExecutable(.{ | ||
| .name = "winmain_inferred", | ||
| .root_module = winmain_mod, | ||
| }); | ||
| const run_inferred = b.addRunArtifact(winmain_inferred); | ||
| run_inferred.expectStdErrEqual(expected_windows_subsystem); | ||
| run_inferred.expectExitCode(0); | ||
| test_step.dependOn(&run_inferred.step); | ||
|
|
||
| const winmain_console = b.addExecutable(.{ | ||
| .name = "winmain_console", | ||
| .root_module = winmain_mod, | ||
| }); | ||
| winmain_console.subsystem = .console; | ||
| const run_console = b.addRunArtifact(winmain_console); | ||
| run_console.expectStdErrEqual(expected_console_subsystem); | ||
| run_console.expectExitCode(0); | ||
| test_step.dependOn(&run_console.step); | ||
|
|
||
| const winmain_windows = b.addExecutable(.{ | ||
| .name = "winmain_windows", | ||
| .root_module = winmain_mod, | ||
| }); | ||
| winmain_windows.subsystem = .windows; | ||
| const run_windows = b.addRunArtifact(winmain_windows); | ||
| run_windows.expectStdErrEqual(expected_windows_subsystem); | ||
| run_windows.expectExitCode(0); | ||
| test_step.dependOn(&run_windows.step); | ||
| } | ||
|
|
||
| // exported callconv(.c) main, libc must be linked | ||
| { | ||
| const cmain_mod = b.createModule(.{ | ||
| .root_source_file = b.path("cmain.zig"), | ||
| .optimize = optimize, | ||
| .target = target, | ||
| .link_libc = true, | ||
| }); | ||
|
|
||
| const cmain_inferred = b.addExecutable(.{ | ||
| .name = "cmain_inferred", | ||
| .root_module = cmain_mod, | ||
| }); | ||
| const run_inferred = b.addRunArtifact(cmain_inferred); | ||
| run_inferred.expectStdErrEqual(expected_console_subsystem); | ||
| run_inferred.expectExitCode(0); | ||
| test_step.dependOn(&run_inferred.step); | ||
|
|
||
| const cmain_console = b.addExecutable(.{ | ||
| .name = "cmain_console", | ||
| .root_module = cmain_mod, | ||
| }); | ||
| cmain_console.subsystem = .console; | ||
| const run_console = b.addRunArtifact(cmain_console); | ||
| run_console.expectStdErrEqual(expected_console_subsystem); | ||
| run_console.expectExitCode(0); | ||
| test_step.dependOn(&run_console.step); | ||
|
|
||
| const cmain_windows = b.addExecutable(.{ | ||
| .name = "cmain_windows", | ||
| .root_module = cmain_mod, | ||
| }); | ||
| cmain_windows.subsystem = .windows; | ||
| const run_windows = b.addRunArtifact(cmain_windows); | ||
| run_windows.expectStdErrEqual(expected_windows_subsystem); | ||
| run_windows.expectExitCode(0); | ||
| test_step.dependOn(&run_windows.step); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const std = @import("std"); | ||
|
|
||
| pub export fn main() callconv(.c) c_int { | ||
| std.debug.print("{}\n", .{std.os.windows.peb().ImageSubSystem}); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const std = @import("std"); | ||
|
|
||
| pub fn main() void { | ||
| std.debug.print("{}\n", .{std.os.windows.peb().ImageSubSystem}); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const std = @import("std"); | ||
|
|
||
| pub fn wWinMain( | ||
| inst: std.os.windows.HINSTANCE, | ||
| prev: ?std.os.windows.HINSTANCE, | ||
| cmd_line: std.os.windows.LPWSTR, | ||
| cmd_show: c_int, | ||
| ) std.os.windows.INT { | ||
| _ = inst; | ||
| _ = prev; | ||
| _ = cmd_line; | ||
| _ = cmd_show; | ||
| std.debug.print("{}\n", .{std.os.windows.peb().ImageSubSystem}); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if always exporting the default Zig main as
wmainCRTStartupmight be a problem in some instances. If the user doeszig build-exe foo.zig --subsystem windows, linking would fail under the MSVC linker behavior laid out in my above comment.Is it possible to export the same symbol under two names,
wmainCRTStartupandwWinMainCRTStartup, without any negative side effects? This way it would work regardless of which subsystem is specified at the final link.