authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-15 18:22:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-15 18:22:12-07:00
logc7c38e72793df53e6582b6f8b501e18314ab07e9
tree78194609b6f0106b50dfb49602011b62ec8f4476
parent51a3d0603c116d99c0a93dd451a69c79dd0cbca2
parent3658dd5e89cd16c011bdc52d334c1308f440157b

Merge branch '5002-fix-entrypoint-with-winmain' of https://github.com/AnthonyYoManz/zig into AnthonyYoManz-5002-fix-entrypoint-with-winmain


3 files changed, 65 insertions(+), 10 deletions(-)

lib/std/os/windows/kernel32.zig+1
......@@ -84,6 +84,7 @@ pub extern "kernel32" fn FormatMessageW(dwFlags: DWORD, lpSource: ?LPVOID, dwMes
8484pub extern "kernel32" fn FreeEnvironmentStringsW(penv: [*:0]u16) callconv(.Stdcall) BOOL;
8585
8686pub extern "kernel32" fn GetCommandLineA() callconv(.Stdcall) LPSTR;
87pub extern "kernel32" fn GetCommandLineW() callconv(.Stdcall) LPWSTR;
8788
8889pub extern "kernel32" fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) callconv(.Stdcall) BOOL;
8990
lib/std/start.zig+61-7
......@@ -28,8 +28,16 @@ comptime {
2828 } else if (builtin.os.tag == .windows) {
2929 if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
3030 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
31 {
32 @export(WinStartup, .{ .name = "WinMainCRTStartup" });
33 } else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
34 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
3135 {
3236 @export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" });
37 } else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and
38 !@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup"))
39 {
40 @export(wWinMainCRTStartup, .{ .name = "wWinMainCRTStartup" });
3341 }
3442 } else if (builtin.os.tag == .uefi) {
3543 if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
......@@ -143,6 +151,17 @@ fn _start() callconv(.Naked) noreturn {
143151 @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
144152}
145153
154fn WinStartup() callconv(.Stdcall) noreturn {
155 @setAlignStack(16);
156 if (!builtin.single_threaded) {
157 _ = @import("start_windows_tls.zig");
158 }
159
160 std.debug.maybeEnableSegfaultHandler();
161
162 std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain));
163}
164
146165fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
147166 @setAlignStack(16);
148167 if (!builtin.single_threaded) {
......@@ -151,7 +170,20 @@ fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
151170
152171 std.debug.maybeEnableSegfaultHandler();
153172
154 std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
173 const result = initEventLoopAndCallMain(std.os.windows.INT, callWinMain);
174 std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
175}
176
177fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {
178 @setAlignStack(16);
179 if (!builtin.single_threaded) {
180 _ = @import("start_windows_tls.zig");
181 }
182
183 std.debug.maybeEnableSegfaultHandler();
184
185 const result = initEventLoopAndCallMain(std.os.windows.INT, callWWinMain);
186 std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
155187}
156188
157189// TODO https://github.com/ziglang/zig/issues/265
......@@ -205,7 +237,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
205237
206238 std.debug.maybeEnableSegfaultHandler();
207239
208 return initEventLoopAndCallMain();
240 return initEventLoopAndCallMain(u8, callMain);
209241}
210242
211243fn 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
220252
221253// This is marked inline because for some reason LLVM in release mode fails to inline it,
222254// and we want fewer call frames in stack traces.
223inline fn initEventLoopAndCallMain() u8 {
255inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn () Out) Out {
224256 if (std.event.Loop.instance) |loop| {
225257 if (!@hasDecl(root, "event_loop")) {
226258 loop.init() catch |err| {
......@@ -234,7 +266,7 @@ inline fn initEventLoopAndCallMain() u8 {
234266
235267 var result: u8 = undefined;
236268 var frame: @Frame(callMainAsync) = undefined;
237 _ = @asyncCall(&frame, &result, callMainAsync, .{loop});
269 _ = @asyncCall(&frame, &result, callMainAsync, .{u8, mainFunc, loop});
238270 loop.run();
239271 return result;
240272 }
......@@ -242,13 +274,13 @@ inline fn initEventLoopAndCallMain() u8 {
242274
243275 // This is marked inline because for some reason LLVM in release mode fails to inline it,
244276 // and we want fewer call frames in stack traces.
245 return @call(.{ .modifier = .always_inline }, callMain, .{});
277 return @call(.{ .modifier = .always_inline }, mainFunc, .{});
246278}
247fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 {
279fn callMainAsync(comptime Out: type, comptime mainProc: fn () Out, loop: *std.event.Loop) callconv(.Async) Out {
248280 // This prevents the event loop from terminating at least until main() has returned.
249281 loop.beginOneEvent();
250282 defer loop.finishOneEvent();
251 return callMain();
283 return mainProc();
252284}
253285
254286// This is not marked inline because it is called with @asyncCall when
......@@ -290,3 +322,25 @@ pub fn callMain() u8 {
290322 else => @compileError(bad_main_ret),
291323 }
292324}
325
326pub fn callWinMain() std.os.windows.INT {
327 const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
328 const lpCmdLine = std.os.windows.kernel32.GetCommandLineA();
329
330 // There's no (documented) way to get the nCmdShow parameter, so we're
331 // using this fairly standard default.
332 const nCmdShow = std.os.windows.user32.SW_SHOW;
333
334 return root.WinMain(hInstance, null, lpCmdLine, nCmdShow);
335}
336
337pub fn callWWinMain() std.os.windows.INT {
338 const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
339 const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();
340
341 // There's no (documented) way to get the nCmdShow parameter, so we're
342 // using this fairly standard default.
343 const nCmdShow = std.os.windows.user32.SW_SHOW;
344
345 return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);
346}
test/stack_traces.zig+3-3
......@@ -282,10 +282,10 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
282282 \\source.zig:10:8: [address] in main (test)
283283 \\ foo();
284284 \\ ^
285 \\start.zig:269:29: [address] in std.start.posixCallMainAndExit (test)
285 \\start.zig:301:29: [address] in std.start.posixCallMainAndExit (test)
286286 \\ return root.main();
287287 \\ ^
288 \\start.zig:143:5: [address] in std.start._start (test)
288 \\start.zig:151:5: [address] in std.start._start (test)
289289 \\ @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
290290 \\ ^
291291 \\
......@@ -294,7 +294,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
294294 switch (std.Target.current.cpu.arch) {
295295 .aarch64 => "", // TODO disabled; results in segfault
296296 else =>
297 \\start.zig:143:5: [address] in std.start._start (test)
297 \\start.zig:151:5: [address] in std.start._start (test)
298298 \\ @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
299299 \\ ^
300300 \\