aboutsummaryrefslogtreecommitdiff
path: root/lib/std/start.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-10-15 18:22:12 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-10-15 18:22:12 -0700
commitc7c38e72793df53e6582b6f8b501e18314ab07e9 (patch)
tree78194609b6f0106b50dfb49602011b62ec8f4476 /lib/std/start.zig
parent51a3d0603c116d99c0a93dd451a69c79dd0cbca2 (diff)
parent3658dd5e89cd16c011bdc52d334c1308f440157b (diff)
downloadzig-c7c38e72793df53e6582b6f8b501e18314ab07e9.tar.gz
zig-c7c38e72793df53e6582b6f8b501e18314ab07e9.zip
Merge branch '5002-fix-entrypoint-with-winmain' of https://github.com/AnthonyYoManz/zig into AnthonyYoManz-5002-fix-entrypoint-with-winmain
Diffstat (limited to 'lib/std/start.zig')
-rw-r--r--lib/std/start.zig68
1 files changed, 61 insertions, 7 deletions
diff --git a/lib/std/start.zig b/lib/std/start.zig
index 71940b12ca..e09f1be765 100644
--- a/lib/std/start.zig
+++ b/lib/std/start.zig
@@ -29,7 +29,15 @@ comptime {
if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
!@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
{
+ @export(WinStartup, .{ .name = "WinMainCRTStartup" });
+ } else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
+ !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
+ {
@export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" });
+ } else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and
+ !@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup"))
+ {
+ @export(wWinMainCRTStartup, .{ .name = "wWinMainCRTStartup" });
}
} else if (builtin.os.tag == .uefi) {
if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
@@ -143,6 +151,17 @@ fn _start() callconv(.Naked) noreturn {
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
}
+fn WinStartup() callconv(.Stdcall) noreturn {
+ @setAlignStack(16);
+ if (!builtin.single_threaded) {
+ _ = @import("start_windows_tls.zig");
+ }
+
+ std.debug.maybeEnableSegfaultHandler();
+
+ std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain));
+}
+
fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
@setAlignStack(16);
if (!builtin.single_threaded) {
@@ -151,7 +170,20 @@ fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
std.debug.maybeEnableSegfaultHandler();
- std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
+ const result = initEventLoopAndCallMain(std.os.windows.INT, callWinMain);
+ std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
+}
+
+fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {
+ @setAlignStack(16);
+ if (!builtin.single_threaded) {
+ _ = @import("start_windows_tls.zig");
+ }
+
+ std.debug.maybeEnableSegfaultHandler();
+
+ const result = initEventLoopAndCallMain(std.os.windows.INT, callWWinMain);
+ std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
}
// TODO https://github.com/ziglang/zig/issues/265
@@ -205,7 +237,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
std.debug.maybeEnableSegfaultHandler();
- return initEventLoopAndCallMain();
+ return initEventLoopAndCallMain(u8, callMain);
}
fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C) i32 {
@@ -220,7 +252,7 @@ const bad_main_ret = "expected return type of main to be 'void', '!void', 'noret
// This is marked inline because for some reason LLVM in release mode fails to inline it,
// and we want fewer call frames in stack traces.
-inline fn initEventLoopAndCallMain() u8 {
+inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn () Out) Out {
if (std.event.Loop.instance) |loop| {
if (!@hasDecl(root, "event_loop")) {
loop.init() catch |err| {
@@ -234,7 +266,7 @@ inline fn initEventLoopAndCallMain() u8 {
var result: u8 = undefined;
var frame: @Frame(callMainAsync) = undefined;
- _ = @asyncCall(&frame, &result, callMainAsync, .{loop});
+ _ = @asyncCall(&frame, &result, callMainAsync, .{u8, mainFunc, loop});
loop.run();
return result;
}
@@ -242,13 +274,13 @@ inline fn initEventLoopAndCallMain() u8 {
// This is marked inline because for some reason LLVM in release mode fails to inline it,
// and we want fewer call frames in stack traces.
- return @call(.{ .modifier = .always_inline }, callMain, .{});
+ return @call(.{ .modifier = .always_inline }, mainFunc, .{});
}
-fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 {
+fn callMainAsync(comptime Out: type, comptime mainProc: fn () Out, loop: *std.event.Loop) callconv(.Async) Out {
// This prevents the event loop from terminating at least until main() has returned.
loop.beginOneEvent();
defer loop.finishOneEvent();
- return callMain();
+ return mainProc();
}
// This is not marked inline because it is called with @asyncCall when
@@ -290,3 +322,25 @@ pub fn callMain() u8 {
else => @compileError(bad_main_ret),
}
}
+
+pub fn callWinMain() std.os.windows.INT {
+ const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
+ const lpCmdLine = std.os.windows.kernel32.GetCommandLineA();
+
+ // There's no (documented) way to get the nCmdShow parameter, so we're
+ // using this fairly standard default.
+ const nCmdShow = std.os.windows.user32.SW_SHOW;
+
+ return root.WinMain(hInstance, null, lpCmdLine, nCmdShow);
+}
+
+pub fn callWWinMain() std.os.windows.INT {
+ const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
+ const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();
+
+ // There's no (documented) way to get the nCmdShow parameter, so we're
+ // using this fairly standard default.
+ const nCmdShow = std.os.windows.user32.SW_SHOW;
+
+ return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);
+}