diff --git a/lib/std/start.zig b/lib/std/start.zig index 8aef63332d9d..082969bbf8a6 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -210,6 +210,14 @@ extern "kernel32" fn ExitProcess(exit_code: u32) callconv(.C) noreturn; //////////////////////////////////////////////////////////////////////////////// +/// Contains any logic that should be run on exe startup to bully Windows +/// into being a sane environment +inline fn setupWindows() void { + if (std.options.windows_force_utf8_codepage) { + _ = std.os.windows.kernel32.SetConsoleOutputCP(65001); // use UTF-8 codepage + } +} + fn _DllMainCRTStartup( hinstDLL: std.os.windows.HINSTANCE, fdwReason: std.os.windows.DWORD, @@ -382,6 +390,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn { _ = @import("start_windows_tls.zig"); } + setupWindows(); std.debug.maybeEnableSegfaultHandler(); std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain()); @@ -393,6 +402,7 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn { _ = @import("start_windows_tls.zig"); } + setupWindows(); std.debug.maybeEnableSegfaultHandler(); const result: std.os.windows.INT = initEventLoopAndCallWinMain(); @@ -495,6 +505,9 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 { std.os.argv = argv[0..argc]; std.os.environ = envp; + if (builtin.os.tag == .windows) { + setupWindows(); + } std.debug.maybeEnableSegfaultHandler(); return initEventLoopAndCallMain(); diff --git a/lib/std/std.zig b/lib/std/std.zig index ba52784b458a..8ee7ca445458 100644 --- a/lib/std/std.zig +++ b/lib/std/std.zig @@ -159,6 +159,11 @@ pub const options = struct { options_override.crypto_always_getrandom else false; + + pub const windows_force_utf8_codepage: bool = if (@hasDecl(options_override, "windows_force_utf8_codepage")) + options_override.windows_force_utf8_codepage + else + true; }; // This forces the start.zig file to be imported, and the comptime logic inside that