authorgravatar for ypsvlq@gmail.comElaine Gibson <ypsvlq@gmail.com> 2024-03-23 08:03:41+00:00
committergravatar for ypsvlq@gmail.comElaine Gibson <ypsvlq@gmail.com> 2024-03-27 10:05:57+00:00
logadd74427b9adc9e1b0d39a0fba9d001c0953a659
tree2baa8bfa654870e90928280441cbd3efc8cb9f02
parent71d878ba5051dd7bb3db15dcfba0f70cff3c225e

mingw: support -municode


8 files changed, 125 insertions(+), 26 deletions(-)

lib/libc/mingw/crt/crtexewin.c created+68
......@@ -0,0 +1,68 @@
1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6#include <windows.h>
7#include <tchar.h>
8#include <corecrt_startup.h>
9
10#ifndef _UNICODE
11#include <mbctype.h>
12#endif
13
14#define SPACECHAR _T(' ')
15#define DQUOTECHAR _T('\"')
16
17extern IMAGE_DOS_HEADER __ImageBase;
18
19int _tmain (int, _TCHAR **, _TCHAR **);
20int _tmain (int __UNUSED_PARAM(argc),
21 _TCHAR ** __UNUSED_PARAM(argv),
22 _TCHAR ** __UNUSED_PARAM(envp))
23{
24 HINSTANCE hInstance;
25 _TCHAR *lpCmdLine;
26 DWORD nShowCmd;
27
28 hInstance = (HINSTANCE) &__ImageBase;
29
30#ifdef _UNICODE
31 lpCmdLine = _wcmdln;
32#else
33 lpCmdLine = _acmdln;
34#endif
35 if (lpCmdLine)
36 {
37 BOOL inDoubleQuote = FALSE;
38 while (*lpCmdLine > SPACECHAR || (*lpCmdLine && inDoubleQuote))
39 {
40 if (*lpCmdLine == DQUOTECHAR)
41 inDoubleQuote = !inDoubleQuote;
42#ifndef _UNICODE
43 if (_ismbblead (*lpCmdLine))
44 {
45 if (lpCmdLine[1])
46 ++lpCmdLine;
47 }
48#endif
49 ++lpCmdLine;
50 }
51 while (*lpCmdLine && (*lpCmdLine <= SPACECHAR))
52 lpCmdLine++;
53 }
54 else
55 lpCmdLine = _TEXT("");
56
57 {
58 STARTUPINFO StartupInfo;
59 memset (&StartupInfo, 0, sizeof (STARTUPINFO));
60 GetStartupInfo (&StartupInfo);
61 if (StartupInfo.dwFlags & STARTF_USESHOWWINDOW)
62 nShowCmd = StartupInfo.wShowWindow;
63 else
64 nShowCmd = SW_SHOWDEFAULT;
65 }
66
67 return _tWinMain (hInstance, NULL, lpCmdLine, nShowCmd);
68}
lib/libc/mingw/crt/ucrtexewin.c created+14
......@@ -0,0 +1,14 @@
1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6
7#ifndef UNICODE
8#define UNICODE
9#endif
10#ifndef _UNICODE
11#define _UNICODE
12#endif
13
14#include "crtexewin.c"
src/Compilation.zig+5-2
......@@ -173,6 +173,7 @@ global_cache_directory: Directory,
173173libc_include_dir_list: []const []const u8,
174174libc_framework_dir_list: []const []const u8,
175175rc_includes: RcIncludes,
176mingw_unicode_entry_point: bool,
176177thread_pool: *ThreadPool,
177178
178179/// Populated when we build the libc++ static library. A Job to build this is placed in the queue
......@@ -758,7 +759,7 @@ pub const MiscTask = enum {
758759
759760 @"mingw-w64 crt2.o",
760761 @"mingw-w64 dllcrt2.o",
761 @"mingw-w64 mingwex.lib",
762 @"mingw-w64 mingw32.lib",
762763};
763764
764765pub const MiscError = struct {
......@@ -1101,6 +1102,7 @@ pub const CreateOptions = struct {
11011102 test_name_prefix: ?[]const u8 = null,
11021103 test_runner_path: ?[]const u8 = null,
11031104 subsystem: ?std.Target.SubSystem = null,
1105 mingw_unicode_entry_point: bool = false,
11041106 /// (Zig compiler development) Enable dumping linker's state as JSON.
11051107 enable_link_snapshots: bool = false,
11061108 /// (Darwin) Install name of the dylib
......@@ -1427,6 +1429,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
14271429 .libc_include_dir_list = libc_dirs.libc_include_dir_list,
14281430 .libc_framework_dir_list = libc_dirs.libc_framework_dir_list,
14291431 .rc_includes = options.rc_includes,
1432 .mingw_unicode_entry_point = options.mingw_unicode_entry_point,
14301433 .thread_pool = options.thread_pool,
14311434 .clang_passthrough_mode = options.clang_passthrough_mode,
14321435 .clang_preprocessor_mode = options.clang_preprocessor_mode,
......@@ -1768,7 +1771,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
17681771
17691772 const crt_job: Job = .{ .mingw_crt_file = if (is_dyn_lib) .dllcrt2_o else .crt2_o };
17701773 try comp.work_queue.ensureUnusedCapacity(2);
1771 comp.work_queue.writeItemAssumeCapacity(.{ .mingw_crt_file = .mingwex_lib });
1774 comp.work_queue.writeItemAssumeCapacity(.{ .mingw_crt_file = .mingw32_lib });
17721775 comp.work_queue.writeItemAssumeCapacity(crt_job);
17731776
17741777 // When linking mingw-w64 there are some import libs we always need.
src/clang_options_data.zig+8-1
......@@ -6895,7 +6895,14 @@ joinpd1("mdouble="),
68956895joinpd1("mfpmath="),
68966896joinpd1("mhwmult="),
68976897joinpd1("mthreads"),
6898joinpd1("municode"),
6898.{
6899 .name = "municode",
6900 .syntax = .joined,
6901 .zig_equivalent = .mingw_unicode_entry_point,
6902 .pd1 = true,
6903 .pd2 = false,
6904 .psl = false,
6905},
68996906joinpd1("mwindows"),
69006907.{
69016908 .name = "offload=",
src/link/Coff/lld.zig+1-1
......@@ -409,7 +409,7 @@ pub fn linkWithLLD(self: *Coff, arena: Allocator, prog_node: *std.Progress.Node)
409409 try argv.append(try comp.get_libc_crt_file(arena, "crt2.obj"));
410410 }
411411
412 try argv.append(try comp.get_libc_crt_file(arena, "mingwex.lib"));
412 try argv.append(try comp.get_libc_crt_file(arena, "mingw32.lib"));
413413 } else {
414414 const lib_str = switch (comp.config.link_mode) {
415415 .dynamic => "",
src/main.zig+7
......@@ -450,6 +450,7 @@ const usage_build_generic =
450450 \\ -fstructured-cfg (SPIR-V) force SPIR-V kernels to use structured control flow
451451 \\ -fno-structured-cfg (SPIR-V) force SPIR-V kernels to not use structured control flow
452452 \\ -mexec-model=[value] (WASI) Execution model
453 \\ -municode (Windows) Use wmain/wWinMain as entry point
453454 \\
454455 \\Per-Module Compile Options:
455456 \\ -target [name] <arch><sub>-<os>-<abi> see the targets command
......@@ -893,6 +894,7 @@ fn buildOutputType(
893894 var subsystem: ?std.Target.SubSystem = null;
894895 var major_subsystem_version: ?u16 = null;
895896 var minor_subsystem_version: ?u16 = null;
897 var mingw_unicode_entry_point: bool = false;
896898 var enable_link_snapshots: bool = false;
897899 var debug_incremental: bool = false;
898900 var install_name: ?[]const u8 = null;
......@@ -1686,6 +1688,8 @@ fn buildOutputType(
16861688 }
16871689 } else if (mem.startsWith(u8, arg, "-mexec-model=")) {
16881690 create_module.opts.wasi_exec_model = parseWasiExecModel(arg["-mexec-model=".len..]);
1691 } else if (mem.eql(u8, arg, "-municode")) {
1692 mingw_unicode_entry_point = true;
16891693 } else {
16901694 fatal("unrecognized parameter: '{s}'", .{arg});
16911695 }
......@@ -2091,6 +2095,7 @@ fn buildOutputType(
20912095 try force_undefined_symbols.put(arena, it.only_arg, {});
20922096 },
20932097 .force_load_objc => force_load_objc = true,
2098 .mingw_unicode_entry_point => mingw_unicode_entry_point = true,
20942099 .weak_library => try create_module.system_libs.put(arena, it.only_arg, .{
20952100 .needed = false,
20962101 .weak = true,
......@@ -3229,6 +3234,7 @@ fn buildOutputType(
32293234 .rc_source_files = create_module.rc_source_files.items,
32303235 .manifest_file = manifest_file,
32313236 .rc_includes = rc_includes,
3237 .mingw_unicode_entry_point = mingw_unicode_entry_point,
32323238 .link_objects = create_module.link_objects.items,
32333239 .framework_dirs = create_module.framework_dirs.items,
32343240 .frameworks = resolved_frameworks.items,
......@@ -5792,6 +5798,7 @@ pub const ClangArgIterator = struct {
57925798 install_name,
57935799 undefined,
57945800 force_load_objc,
5801 mingw_unicode_entry_point,
57955802 };
57965803
57975804 const Args = struct {
src/mingw.zig+18-22
......@@ -13,7 +13,7 @@ const Cache = std.Build.Cache;
1313pub const CRTFile = enum {
1414 crt2_o,
1515 dllcrt2_o,
16 mingwex_lib,
16 mingw32_lib,
1717};
1818
1919pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progress.Node) !void {
......@@ -28,14 +28,9 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
2828 .crt2_o => {
2929 var args = std.ArrayList([]const u8).init(arena);
3030 try add_cc_args(comp, arena, &args);
31 try args.appendSlice(&[_][]const u8{
32 // Prevents warning: 'used' attribute ignored on a non-definition declaration
33 // pointing at extern _CRTALLOC
34 "-Wno-ignored-attributes",
35 // Uncommenting this makes mingw-w64 look for wmain instead of main.
36 //"-DUNICODE",
37 //"-D_UNICODE",
38 });
31 if (comp.mingw_unicode_entry_point) {
32 try args.appendSlice(&.{ "-DUNICODE", "-D_UNICODE" });
33 }
3934 var files = [_]Compilation.CSourceFile{
4035 .{
4136 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
......@@ -63,12 +58,12 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
6358 return comp.build_crt_file("dllcrt2", .Obj, .@"mingw-w64 dllcrt2.o", prog_node, &files);
6459 },
6560
66 .mingwex_lib => {
61 .mingw32_lib => {
6762 var args = std.ArrayList([]const u8).init(arena);
6863 try add_cc_args(comp, arena, &args);
6964 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena);
7065
71 for (mingwex_generic_src) |dep| {
66 for (mingw32_generic_src) |dep| {
7267 try c_source_files.append(.{
7368 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
7469 "libc", "mingw", dep,
......@@ -79,7 +74,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
7974 }
8075 const target = comp.getTarget();
8176 if (target.cpu.arch == .x86 or target.cpu.arch == .x86_64) {
82 for (mingwex_x86_src) |dep| {
77 for (mingw32_x86_src) |dep| {
8378 try c_source_files.append(.{
8479 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
8580 "libc", "mingw", dep,
......@@ -89,7 +84,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
8984 });
9085 }
9186 if (target.cpu.arch == .x86) {
92 for (mingwex_x86_32_src) |dep| {
87 for (mingw32_x86_32_src) |dep| {
9388 try c_source_files.append(.{
9489 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
9590 "libc", "mingw", dep,
......@@ -100,7 +95,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
10095 }
10196 }
10297 } else if (target.cpu.arch.isARM()) {
103 for (mingwex_arm32_src) |dep| {
98 for (mingw32_arm32_src) |dep| {
10499 try c_source_files.append(.{
105100 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
106101 "libc", "mingw", dep,
......@@ -110,7 +105,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
110105 });
111106 }
112107 } else if (target.cpu.arch.isAARCH64()) {
113 for (mingwex_arm64_src) |dep| {
108 for (mingw32_arm64_src) |dep| {
114109 try c_source_files.append(.{
115110 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
116111 "libc", "mingw", dep,
......@@ -122,7 +117,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
122117 } else {
123118 @panic("unsupported arch");
124119 }
125 return comp.build_crt_file("mingwex", .Lib, .@"mingw-w64 mingwex.lib", prog_node, c_source_files.items);
120 return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items);
126121 },
127122 }
128123}
......@@ -389,14 +384,16 @@ fn findDef(
389384 return error.FileNotFound;
390385}
391386
392const mingwex_generic_src = [_][]const u8{
387const mingw32_generic_src = [_][]const u8{
393388 // mingw32
389 "crt" ++ path.sep_str ++ "crtexewin.c",
394390 "crt" ++ path.sep_str ++ "dll_argv.c",
395391 "crt" ++ path.sep_str ++ "gccmain.c",
396392 "crt" ++ path.sep_str ++ "natstart.c",
397393 "crt" ++ path.sep_str ++ "pseudo-reloc-list.c",
398394 "crt" ++ path.sep_str ++ "wildcard.c",
399395 "crt" ++ path.sep_str ++ "charmax.c",
396 "crt" ++ path.sep_str ++ "ucrtexewin.c",
400397 "crt" ++ path.sep_str ++ "dllargv.c",
401398 "crt" ++ path.sep_str ++ "_newmode.c",
402399 "crt" ++ path.sep_str ++ "tlssup.c",
......@@ -814,7 +811,7 @@ const mingwex_generic_src = [_][]const u8{
814811 "libsrc" ++ path.sep_str ++ "activeds-uuid.c",
815812};
816813
817const mingwex_x86_src = [_][]const u8{
814const mingw32_x86_src = [_][]const u8{
818815 // mingwex
819816 "math" ++ path.sep_str ++ "cbrtl.c",
820817 "math" ++ path.sep_str ++ "erfl.c",
......@@ -873,8 +870,7 @@ const mingwex_x86_src = [_][]const u8{
873870 "math" ++ path.sep_str ++ "nexttowardf.c",
874871};
875872
876const mingwex_x86_32_src = [_][]const u8{
877 // ucrtbase
873const mingw32_x86_32_src = [_][]const u8{
878874 "math" ++ path.sep_str ++ "coshf.c",
879875 "math" ++ path.sep_str ++ "expf.c",
880876 "math" ++ path.sep_str ++ "log10f.c",
......@@ -896,7 +892,7 @@ const mingwex_x86_32_src = [_][]const u8{
896892 "math" ++ path.sep_str ++ "x86" ++ path.sep_str ++ "tanf.c",
897893};
898894
899const mingwex_arm32_src = [_][]const u8{
895const mingw32_arm32_src = [_][]const u8{
900896 "math" ++ path.sep_str ++ "arm-common" ++ path.sep_str ++ "ldexpl.c",
901897 "math" ++ path.sep_str ++ "arm" ++ path.sep_str ++ "_chgsignl.S",
902898 "math" ++ path.sep_str ++ "arm" ++ path.sep_str ++ "s_rint.c",
......@@ -905,7 +901,7 @@ const mingwex_arm32_src = [_][]const u8{
905901 "math" ++ path.sep_str ++ "arm" ++ path.sep_str ++ "sincosf.S",
906902};
907903
908const mingwex_arm64_src = [_][]const u8{
904const mingw32_arm64_src = [_][]const u8{
909905 "math" ++ path.sep_str ++ "arm-common" ++ path.sep_str ++ "ldexpl.c",
910906 "math" ++ path.sep_str ++ "arm64" ++ path.sep_str ++ "_chgsignl.S",
911907 "math" ++ path.sep_str ++ "arm64" ++ path.sep_str ++ "rint.c",
tools/update_clang_options.zig+4
......@@ -532,6 +532,10 @@ const known_options = [_]KnownOpt{
532532 .name = "ObjC",
533533 .ident = "force_load_objc",
534534 },
535 .{
536 .name = "municode",
537 .ident = "mingw_unicode_entry_point",
538 },
535539};
536540
537541const blacklisted_options = [_][]const u8{};