authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-12-15 14:37:50+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-12-16 01:56:55+01:00
log8f330ab70ea4f28b0b5efb349b89a9c9a8b95bf0
treeccd714b87f0f235740778b424294a26bb27a4074
parentaa0377794df692ede9bc19d8e5296ab90ce6df68

mingw: Fix CFLAGS for winpthreads.

It should not use the CRT ones, and it also needs a few flags of its own that I forgot to add in #22156. Follow-up fix for #10989.

1 files changed, 115 insertions(+), 77 deletions(-)

src/mingw.zig+115-77
......@@ -32,7 +32,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
3232 switch (crt_file) {
3333 .crt2_o => {
3434 var args = std.ArrayList([]const u8).init(arena);
35 try add_cc_args(comp, arena, &args);
35 try addCrtCcArgs(comp, arena, &args);
3636 if (comp.mingw_unicode_entry_point) {
3737 try args.appendSlice(&.{ "-DUNICODE", "-D_UNICODE" });
3838 }
......@@ -52,7 +52,7 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
5252
5353 .dllcrt2_o => {
5454 var args = std.ArrayList([]const u8).init(arena);
55 try add_cc_args(comp, arena, &args);
55 try addCrtCcArgs(comp, arena, &args);
5656 var files = [_]Compilation.CSourceFile{
5757 .{
5858 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
......@@ -68,81 +68,109 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
6868 },
6969
7070 .mingw32_lib => {
71 var args = std.ArrayList([]const u8).init(arena);
72 try add_cc_args(comp, arena, &args);
7371 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena);
7472
75 for (mingw32_generic_src) |dep| {
76 try c_source_files.append(.{
77 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
78 "libc", "mingw", dep,
79 }),
80 .extra_flags = args.items,
81 .owner = undefined,
82 });
83 }
84 if (target.cpu.arch.isX86()) {
85 for (mingw32_x86_src) |dep| {
73 {
74 var crt_args = std.ArrayList([]const u8).init(arena);
75 try addCrtCcArgs(comp, arena, &crt_args);
76
77 for (mingw32_generic_src) |dep| {
8678 try c_source_files.append(.{
8779 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
8880 "libc", "mingw", dep,
8981 }),
90 .extra_flags = args.items,
82 .extra_flags = crt_args.items,
9183 .owner = undefined,
9284 });
9385 }
94 if (target.cpu.arch == .x86) {
95 for (mingw32_x86_32_src) |dep| {
86 if (target.cpu.arch.isX86()) {
87 for (mingw32_x86_src) |dep| {
9688 try c_source_files.append(.{
9789 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
9890 "libc", "mingw", dep,
9991 }),
100 .extra_flags = args.items,
92 .extra_flags = crt_args.items,
10193 .owner = undefined,
10294 });
10395 }
96 if (target.cpu.arch == .x86) {
97 for (mingw32_x86_32_src) |dep| {
98 try c_source_files.append(.{
99 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
100 "libc", "mingw", dep,
101 }),
102 .extra_flags = crt_args.items,
103 .owner = undefined,
104 });
105 }
106 }
107 } else if (target.cpu.arch == .thumb) {
108 for (mingw32_arm_src) |dep| {
109 try c_source_files.append(.{
110 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
111 "libc", "mingw", dep,
112 }),
113 .extra_flags = crt_args.items,
114 .owner = undefined,
115 });
116 }
117 for (mingw32_arm32_src) |dep| {
118 try c_source_files.append(.{
119 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
120 "libc", "mingw", dep,
121 }),
122 .extra_flags = crt_args.items,
123 .owner = undefined,
124 });
125 }
126 } else if (target.cpu.arch == .aarch64) {
127 for (mingw32_arm_src) |dep| {
128 try c_source_files.append(.{
129 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
130 "libc", "mingw", dep,
131 }),
132 .extra_flags = crt_args.items,
133 .owner = undefined,
134 });
135 }
136 for (mingw32_arm64_src) |dep| {
137 try c_source_files.append(.{
138 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
139 "libc", "mingw", dep,
140 }),
141 .extra_flags = crt_args.items,
142 .owner = undefined,
143 });
144 }
145 } else {
146 @panic("unsupported arch");
104147 }
105 } else if (target.cpu.arch.isThumb()) {
106 for (mingw32_arm_src) |dep| {
107 try c_source_files.append(.{
108 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
109 "libc", "mingw", dep,
110 }),
111 .extra_flags = args.items,
112 .owner = undefined,
113 });
114 }
115 for (mingw32_arm32_src) |dep| {
116 try c_source_files.append(.{
117 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
118 "libc", "mingw", dep,
119 }),
120 .extra_flags = args.items,
121 .owner = undefined,
122 });
123 }
124 } else if (target.cpu.arch.isAARCH64()) {
125 for (mingw32_arm_src) |dep| {
126 try c_source_files.append(.{
127 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
128 "libc", "mingw", dep,
129 }),
130 .extra_flags = args.items,
131 .owner = undefined,
132 });
148 }
149
150 {
151 var winpthreads_args = std.ArrayList([]const u8).init(arena);
152 try addCcArgs(comp, arena, &winpthreads_args);
153 try winpthreads_args.appendSlice(&[_][]const u8{
154 "-DIN_WINPTHREAD",
155 "-DWIN32_LEAN_AND_MEAN",
156 });
157
158 switch (comp.compilerRtOptMode()) {
159 .Debug, .ReleaseSafe => try winpthreads_args.append("-DWINPTHREAD_DBG"),
160 .ReleaseFast, .ReleaseSmall => {},
133161 }
134 for (mingw32_arm64_src) |dep| {
162
163 for (mingw32_winpthreads_src) |dep| {
135164 try c_source_files.append(.{
136165 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
137166 "libc", "mingw", dep,
138167 }),
139 .extra_flags = args.items,
168 .extra_flags = winpthreads_args.items,
140169 .owner = undefined,
141170 });
142171 }
143 } else {
144 @panic("unsupported arch");
145172 }
173
146174 return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, c_source_files.items, .{
147175 .unwind_tables = unwind_tables,
148176 });
......@@ -150,37 +178,44 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
150178 }
151179}
152180
153fn add_cc_args(
181fn addCcArgs(
154182 comp: *Compilation,
155183 arena: Allocator,
156184 args: *std.ArrayList([]const u8),
157185) error{OutOfMemory}!void {
158186 try args.appendSlice(&[_][]const u8{
159 "-DHAVE_CONFIG_H",
160
161 "-I",
162 try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "mingw", "include" }),
187 "-std=gnu11",
188 "-D__USE_MINGW_ANSI_STDIO=0",
163189
164190 "-isystem",
165191 try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "include", "any-windows-any" }),
166192 });
193}
167194
168 const target = comp.getTarget();
169 if (target.cpu.arch.isThumb()) {
195fn addCrtCcArgs(
196 comp: *Compilation,
197 arena: Allocator,
198 args: *std.ArrayList([]const u8),
199) error{OutOfMemory}!void {
200 try addCcArgs(comp, arena, args);
201
202 if (comp.getTarget().cpu.arch == .thumb) {
170203 try args.append("-mfpu=vfp");
171204 }
172205
173206 try args.appendSlice(&[_][]const u8{
174 "-std=gnu11",
175 "-D_CRTBLD",
176 "-D_SYSCRT=1",
177 "-DCRTDLL=1",
178 "-D_WIN32_WINNT=0x0f00",
179207 // According to Martin Storsjö,
180208 // > the files under mingw-w64-crt are designed to always
181209 // be built with __MSVCRT_VERSION__=0x700
182210 "-D__MSVCRT_VERSION__=0x700",
183 "-D__USE_MINGW_ANSI_STDIO=0",
211 "-D_CRTBLD",
212 "-D_SYSCRT=1",
213 "-D_WIN32_WINNT=0x0f00",
214 "-DCRTDLL=1",
215 "-DHAVE_CONFIG_H",
216
217 "-I",
218 try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "mingw", "include" }),
184219 });
185220}
186221
......@@ -736,19 +771,6 @@ const mingw32_generic_src = [_][]const u8{
736771 "stdio" ++ path.sep_str ++ "ucrt_vsprintf.c",
737772 "stdio" ++ path.sep_str ++ "ucrt_vsscanf.c",
738773 "string" ++ path.sep_str ++ "ucrt__wcstok.c",
739 // winpthreads
740 "winpthreads" ++ path.sep_str ++ "barrier.c",
741 "winpthreads" ++ path.sep_str ++ "clock.c",
742 "winpthreads" ++ path.sep_str ++ "cond.c",
743 "winpthreads" ++ path.sep_str ++ "misc.c",
744 "winpthreads" ++ path.sep_str ++ "mutex.c",
745 "winpthreads" ++ path.sep_str ++ "nanosleep.c",
746 "winpthreads" ++ path.sep_str ++ "ref.c",
747 "winpthreads" ++ path.sep_str ++ "rwlock.c",
748 "winpthreads" ++ path.sep_str ++ "sched.c",
749 "winpthreads" ++ path.sep_str ++ "sem.c",
750 "winpthreads" ++ path.sep_str ++ "spinlock.c",
751 "winpthreads" ++ path.sep_str ++ "thread.c",
752774 // uuid
753775 "libsrc" ++ path.sep_str ++ "ativscp-uuid.c",
754776 "libsrc" ++ path.sep_str ++ "atsmedia-uuid.c",
......@@ -978,6 +1000,22 @@ const mingw32_arm64_src = [_][]const u8{
9781000 "math" ++ path.sep_str ++ "arm64" ++ path.sep_str ++ "sincosf.S",
9791001};
9801002
1003const mingw32_winpthreads_src = [_][]const u8{
1004 // winpthreads
1005 "winpthreads" ++ path.sep_str ++ "barrier.c",
1006 "winpthreads" ++ path.sep_str ++ "clock.c",
1007 "winpthreads" ++ path.sep_str ++ "cond.c",
1008 "winpthreads" ++ path.sep_str ++ "misc.c",
1009 "winpthreads" ++ path.sep_str ++ "mutex.c",
1010 "winpthreads" ++ path.sep_str ++ "nanosleep.c",
1011 "winpthreads" ++ path.sep_str ++ "ref.c",
1012 "winpthreads" ++ path.sep_str ++ "rwlock.c",
1013 "winpthreads" ++ path.sep_str ++ "sched.c",
1014 "winpthreads" ++ path.sep_str ++ "sem.c",
1015 "winpthreads" ++ path.sep_str ++ "spinlock.c",
1016 "winpthreads" ++ path.sep_str ++ "thread.c",
1017};
1018
9811019pub const always_link_libs = [_][]const u8{
9821020 "api-ms-win-crt-conio-l1-1-0",
9831021 "api-ms-win-crt-convert-l1-1-0",