authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-05 01:08:47+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 01:25:38+02:00
log6f6182a5f3a4b027bbff6405600fd21b3ef8b86c
treea70d28c3608c940b5ee4cabafe9ec90df54149bf
parent2d43db1d767ec56183cd7cf3ee8c5fd929548beb

zig,cc,wasi: include emulated libs in WASI libc

This commit includes emulated libc sublibs that were not included in the compilation and caching of WASI libc that ships with Zig. The libs include (emulated): process clocks, getpid, mman, and signal. With this change, it is now possible to successfully cross-compile `wasm3` engine to WASI with `zig cc`. For the future though, it might be worth considering splitting WASI libc into libc-proper and modularised emulated libs as it is done in upstream, and then have them included only if the user specifically requests emulation/parts of it.

1 files changed, 164 insertions(+), 105 deletions(-)

src/wasi_libc.zig+164-105
......@@ -21,35 +21,7 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
2121 // Compile crt sources.
2222 var args = std.ArrayList([]const u8).init(arena);
2323 try addCCArgs(comp, arena, &args, false);
24 try args.appendSlice(&[_][]const u8{
25 "-I",
26 try comp.zig_lib_directory.join(arena, &[_][]const u8{
27 "libc",
28 "wasi",
29 "libc-bottom-half",
30 "headers",
31 "private",
32 }),
33
34 "-I",
35 try comp.zig_lib_directory.join(arena, &[_][]const u8{
36 "libc",
37 "wasi",
38 "libc-bottom-half",
39 "cloudlibc",
40 "src",
41 "include",
42 }),
43
44 "-I",
45 try comp.zig_lib_directory.join(arena, &[_][]const u8{
46 "libc",
47 "wasi",
48 "libc-bottom-half",
49 "cloudlibc",
50 "src",
51 }),
52 });
24 try addLibcBottomHalfIncludes(comp, arena, &args);
5325
5426 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
5527 for (crt_src_files) |file_path| {
......@@ -95,37 +67,43 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
9567 // Compile libc-bottom-half.
9668 var args = std.ArrayList([]const u8).init(arena);
9769 try addCCArgs(comp, arena, &args, true);
98 try args.appendSlice(&[_][]const u8{
99 "-I",
100 try comp.zig_lib_directory.join(arena, &[_][]const u8{
101 "libc",
102 "wasi",
103 "libc-bottom-half",
104 "headers",
105 "private",
106 }),
70 try addLibcBottomHalfIncludes(comp, arena, &args);
10771
108 "-I",
109 try comp.zig_lib_directory.join(arena, &[_][]const u8{
110 "libc",
111 "wasi",
112 "libc-bottom-half",
113 "cloudlibc",
114 "src",
115 }),
72 for (libc_bottom_half_src_files) |file_path| {
73 try comp_sources.append(.{
74 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
75 "libc", try sanitize(arena, file_path),
76 }),
77 .extra_flags = args.items,
78 });
79 }
80 }
11681
117 "-I",
118 try comp.zig_lib_directory.join(arena, &[_][]const u8{
119 "libc",
120 "wasi",
121 "libc-bottom-half",
122 "cloudlibc",
123 "src",
124 "include",
125 }),
126 });
82 {
83 // Compile emulated sources depending only on libc-bottom-half.
84 var args = std.ArrayList([]const u8).init(arena);
85 try addCCArgs(comp, arena, &args, true);
86 try addLibcBottomHalfIncludes(comp, arena, &args);
12787
128 for (libc_bottom_half_src_files) |file_path| {
88 for (emulated_process_clocks_src_files) |file_path| {
89 try comp_sources.append(.{
90 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
91 "libc", try sanitize(arena, file_path),
92 }),
93 .extra_flags = args.items,
94 });
95 }
96
97 for (emulated_getpid_src_files) |file_path| {
98 try comp_sources.append(.{
99 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
100 "libc", try sanitize(arena, file_path),
101 }),
102 .extra_flags = args.items,
103 });
104 }
105
106 for (emulated_mman_src_files) |file_path| {
129107 try comp_sources.append(.{
130108 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
131109 "libc", try sanitize(arena, file_path),
......@@ -136,59 +114,42 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
136114 }
137115
138116 {
139 // Compile libc-top-half.
117 // Compile emulated signals (bottom-half).
140118 var args = std.ArrayList([]const u8).init(arena);
141119 try addCCArgs(comp, arena, &args, true);
142 try args.appendSlice(&[_][]const u8{
143 "-I",
144 try comp.zig_lib_directory.join(arena, &[_][]const u8{
145 "libc",
146 "wasi",
147 "libc-top-half",
148 "musl",
149 "src",
150 "include",
151 }),
152120
153 "-I",
154 try comp.zig_lib_directory.join(arena, &[_][]const u8{
155 "libc",
156 "wasi",
157 "libc-top-half",
158 "musl",
159 "src",
160 "internal",
161 }),
121 for (emulated_signal_bottom_half_src_files) |file_path| {
122 try comp_sources.append(.{
123 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
124 "libc", try sanitize(arena, file_path),
125 }),
126 .extra_flags = args.items,
127 });
128 }
129 }
162130
163 "-I",
164 try comp.zig_lib_directory.join(arena, &[_][]const u8{
165 "libc",
166 "wasi",
167 "libc-top-half",
168 "musl",
169 "arch",
170 "wasm32",
171 }),
131 {
132 // Compile emulated signals (top-half).
133 var args = std.ArrayList([]const u8).init(arena);
134 try addCCArgs(comp, arena, &args, true);
135 try addLibcTopHalfIncludes(comp, arena, &args);
136 try args.append("-D_WASI_EMULATED_SIGNAL");
172137
173 "-I",
174 try comp.zig_lib_directory.join(arena, &[_][]const u8{
175 "libc",
176 "wasi",
177 "libc-top-half",
178 "musl",
179 "arch",
180 "generic",
181 }),
138 for (emulated_signal_top_half_src_files) |file_path| {
139 try comp_sources.append(.{
140 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
141 "libc", try sanitize(arena, file_path),
142 }),
143 .extra_flags = args.items,
144 });
145 }
146 }
182147
183 "-I",
184 try comp.zig_lib_directory.join(arena, &[_][]const u8{
185 "libc",
186 "wasi",
187 "libc-top-half",
188 "headers",
189 "private",
190 }),
191 });
148 {
149 // Compile libc-top-half.
150 var args = std.ArrayList([]const u8).init(arena);
151 try addCCArgs(comp, arena, &args, true);
152 try addLibcTopHalfIncludes(comp, arena, &args);
192153
193154 for (libc_top_half_src_files) |file_path| {
194155 try comp_sources.append(.{
......@@ -251,6 +212,99 @@ fn addCCArgs(
251212 });
252213}
253214
215fn addLibcBottomHalfIncludes(
216 comp: *Compilation,
217 arena: *Allocator,
218 args: *std.ArrayList([]const u8),
219) error{OutOfMemory}!void {
220 try args.appendSlice(&[_][]const u8{
221 "-I",
222 try comp.zig_lib_directory.join(arena, &[_][]const u8{
223 "libc",
224 "wasi",
225 "libc-bottom-half",
226 "headers",
227 "private",
228 }),
229
230 "-I",
231 try comp.zig_lib_directory.join(arena, &[_][]const u8{
232 "libc",
233 "wasi",
234 "libc-bottom-half",
235 "cloudlibc",
236 "src",
237 "include",
238 }),
239
240 "-I",
241 try comp.zig_lib_directory.join(arena, &[_][]const u8{
242 "libc",
243 "wasi",
244 "libc-bottom-half",
245 "cloudlibc",
246 "src",
247 }),
248 });
249}
250
251fn addLibcTopHalfIncludes(
252 comp: *Compilation,
253 arena: *Allocator,
254 args: *std.ArrayList([]const u8),
255) error{OutOfMemory}!void {
256 try args.appendSlice(&[_][]const u8{
257 "-I",
258 try comp.zig_lib_directory.join(arena, &[_][]const u8{
259 "libc",
260 "wasi",
261 "libc-top-half",
262 "musl",
263 "src",
264 "include",
265 }),
266
267 "-I",
268 try comp.zig_lib_directory.join(arena, &[_][]const u8{
269 "libc",
270 "wasi",
271 "libc-top-half",
272 "musl",
273 "src",
274 "internal",
275 }),
276
277 "-I",
278 try comp.zig_lib_directory.join(arena, &[_][]const u8{
279 "libc",
280 "wasi",
281 "libc-top-half",
282 "musl",
283 "arch",
284 "wasm32",
285 }),
286
287 "-I",
288 try comp.zig_lib_directory.join(arena, &[_][]const u8{
289 "libc",
290 "wasi",
291 "libc-top-half",
292 "musl",
293 "arch",
294 "generic",
295 }),
296
297 "-I",
298 try comp.zig_lib_directory.join(arena, &[_][]const u8{
299 "libc",
300 "wasi",
301 "libc-top-half",
302 "headers",
303 "private",
304 }),
305 });
306}
307
254308const dlmalloc_src_files = [_][]const u8{
255309 "wasi/dlmalloc/src/dlmalloc.c",
256310};
......@@ -1025,6 +1079,11 @@ const emulated_mman_src_files = &[_][]const u8{
10251079 "wasi/libc-bottom-half/mman/mman.c",
10261080};
10271081
1028const emulated_signal_src_files = &[_][]const u8{
1082const emulated_signal_bottom_half_src_files = &[_][]const u8{
10291083 "wasi/libc-bottom-half/signal/signal.c",
10301084};
1085
1086const emulated_signal_top_half_src_files = &[_][]const u8{
1087 "wasi/libc-top-half/musl/src/signal/psignal.c",
1088 "wasi/libc-top-half/musl/src/string/strsignal.c",
1089};