authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 09:52:15+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-06-09 09:52:15+02:00
loga6ae5a77dac6466617cdf57357aa79c2b79403b3
tree1e22f27275c7c6ee6928ddd3a7c90f4b66d12ee6
parent2d43db1d767ec56183cd7cf3ee8c5fd929548beb
parent2ee1f7898b27447bb9b21d284842415c7459db4b
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8992 from ziglang/cc-wasm32-wasi-emulations

cc,wasi: include emulated libs in WASI libc and refactor WASI libc handling

5 files changed, 369 insertions(+), 186 deletions(-)

src/Compilation.zig+31-12
......@@ -203,8 +203,8 @@ const Job = union(enum) {
203203 /// needed when not linking libc and using LLVM for code generation because it generates
204204 /// calls to, for example, memcpy and memset.
205205 zig_libc: void,
206 /// WASI libc sysroot
207 wasi_libc_sysroot: void,
206 /// one of WASI libc static objects
207 wasi_libc_crt_file: wasi_libc.CRTFile,
208208
209209 /// Use stage1 C++ code to compile zig code into an object file.
210210 stage1_module: void,
......@@ -279,7 +279,7 @@ pub const MiscTask = enum {
279279 libcxx,
280280 libcxxabi,
281281 libtsan,
282 wasi_libc_sysroot,
282 wasi_libc_crt_file,
283283 compiler_rt,
284284 libssp,
285285 zig_libc,
......@@ -646,6 +646,12 @@ pub const InitOptions = struct {
646646 framework_dirs: []const []const u8 = &[0][]const u8{},
647647 frameworks: []const []const u8 = &[0][]const u8{},
648648 system_libs: []const []const u8 = &[0][]const u8{},
649 /// These correspond to the WASI libc emulated subcomponents including:
650 /// * process clocks
651 /// * getpid
652 /// * mman
653 /// * signal
654 wasi_emulated_libs: []const wasi_libc.CRTFile = &[0]wasi_libc.CRTFile{},
649655 link_libc: bool = false,
650656 link_libcpp: bool = false,
651657 link_libunwind: bool = false,
......@@ -1286,6 +1292,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
12861292 .framework_dirs = options.framework_dirs,
12871293 .system_libs = system_libs,
12881294 .syslibroot = darwin_options.syslibroot,
1295 .wasi_emulated_libs = options.wasi_emulated_libs,
12891296 .lib_dirs = options.lib_dirs,
12901297 .rpath_list = options.rpath_list,
12911298 .strip = strip,
......@@ -1426,8 +1433,19 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14261433 },
14271434 });
14281435 }
1429 if (comp.wantBuildWasiLibcSysrootFromSource()) {
1430 try comp.work_queue.write(&[_]Job{.{ .wasi_libc_sysroot = {} }});
1436 if (comp.wantBuildWasiLibcFromSource()) {
1437 const wasi_emulated_libs = comp.bin_file.options.wasi_emulated_libs;
1438 try comp.work_queue.ensureUnusedCapacity(wasi_emulated_libs.len + 2); // worst-case we need all components
1439 for (wasi_emulated_libs) |crt_file| {
1440 comp.work_queue.writeItemAssumeCapacity(.{
1441 .wasi_libc_crt_file = crt_file,
1442 });
1443 }
1444 // TODO add logic deciding which crt1 we want here.
1445 comp.work_queue.writeAssumeCapacity(&[_]Job{
1446 .{ .wasi_libc_crt_file = .crt1_o },
1447 .{ .wasi_libc_crt_file = .libc_a },
1448 });
14311449 }
14321450 if (comp.wantBuildMinGWFromSource()) {
14331451 const static_lib_jobs = [_]Job{
......@@ -2169,12 +2187,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
21692187 );
21702188 };
21712189 },
2172 .wasi_libc_sysroot => {
2173 wasi_libc.buildWasiLibcSysroot(self) catch |err| {
2190 .wasi_libc_crt_file => |crt_file| {
2191 wasi_libc.buildCRTFile(self, crt_file) catch |err| {
21742192 // TODO Surface more error details.
21752193 try self.setMiscFailure(
2176 .wasi_libc_sysroot,
2177 "unable to build WASI libc sysroot: {s}",
2194 .wasi_libc_crt_file,
2195 "unable to build WASI libc CRT file: {s}",
21782196 .{@errorName(err)},
21792197 );
21802198 };
......@@ -3303,7 +3321,7 @@ pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []cons
33033321 if (comp.wantBuildGLibCFromSource() or
33043322 comp.wantBuildMuslFromSource() or
33053323 comp.wantBuildMinGWFromSource() or
3306 comp.wantBuildWasiLibcSysrootFromSource())
3324 comp.wantBuildWasiLibcFromSource())
33073325 {
33083326 return comp.crt_files.get(basename).?.full_object_path;
33093327 }
......@@ -3343,8 +3361,9 @@ fn wantBuildMuslFromSource(comp: Compilation) bool {
33433361 !comp.getTarget().isWasm();
33443362}
33453363
3346fn wantBuildWasiLibcSysrootFromSource(comp: Compilation) bool {
3347 return comp.wantBuildLibCFromSource() and comp.getTarget().isWasm();
3364fn wantBuildWasiLibcFromSource(comp: Compilation) bool {
3365 return comp.wantBuildLibCFromSource() and comp.getTarget().isWasm() and
3366 comp.getTarget().os.tag == .wasi;
33483367}
33493368
33503369fn wantBuildMinGWFromSource(comp: Compilation) bool {
src/link.zig+2
......@@ -13,6 +13,7 @@ const Type = @import("type.zig").Type;
1313const Cache = @import("Cache.zig");
1414const build_options = @import("build_options");
1515const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
16const wasi_libc = @import("wasi_libc.zig");
1617
1718pub const producer_string = if (std.builtin.is_test) "zig test" else "zig " ++ build_options.version;
1819
......@@ -110,6 +111,7 @@ pub const Options = struct {
110111 framework_dirs: []const []const u8,
111112 frameworks: []const []const u8,
112113 system_libs: std.StringArrayHashMapUnmanaged(void),
114 wasi_emulated_libs: []const wasi_libc.CRTFile,
113115 lib_dirs: []const []const u8,
114116 rpath_list: []const []const u8,
115117
src/link/Wasm.zig+32-16
......@@ -15,6 +15,7 @@ const codegen = @import("../codegen/wasm.zig");
1515const link = @import("../link.zig");
1616const trace = @import("../tracy.zig").trace;
1717const build_options = @import("build_options");
18const wasi_libc = @import("../wasi_libc.zig");
1819const Cache = @import("../Cache.zig");
1920const TypedValue = @import("../TypedValue.zig");
2021
......@@ -574,7 +575,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
574575 null;
575576
576577 const target = self.base.options.target;
577 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
578578
579579 const id_symlink_basename = "lld.id";
580580
......@@ -653,8 +653,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
653653 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});
654654 }
655655 } else {
656 const is_obj = self.base.options.output_mode == .Obj;
657
658656 // Create an LLD command line and invoke it.
659657 var argv = std.ArrayList([]const u8).init(self.base.allocator);
660658 defer argv.deinit();
......@@ -662,10 +660,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
662660 // This is necessary because LLD does not behave properly as a library -
663661 // it calls exit() and does not reset all global data between invocations.
664662 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" });
665 if (is_obj) {
666 try argv.append("-r");
667 }
668
669663 try argv.append("-error-limit=0");
670664
671665 if (self.base.options.lto) {
......@@ -697,16 +691,38 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
697691 full_out_path,
698692 });
699693
700 if (link_in_crt) {
701 // TODO work out if we want standard crt, a reactor or a command
702 try argv.append(try comp.get_libc_crt_file(arena, "crt.o"));
703 }
694 if (target.os.tag == .wasi) {
695 if (self.base.options.link_libc and self.base.options.output_mode == .Exe) {
696 // TODO work out if we want standard crt, a reactor or a command
697 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));
698 }
699
700 const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or
701 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
702 if (is_exe_or_dyn_lib) {
703 const system_libs = self.base.options.system_libs.keys();
704704
705 if (!is_obj and self.base.options.link_libc) {
706 try argv.append(try comp.get_libc_crt_file(arena, switch (self.base.options.link_mode) {
707 .Static => "libc.a",
708 .Dynamic => unreachable,
709 }));
705 for (system_libs) |link_lib| {
706 if (mem.eql(u8, "wasi_snapshot_preview1", link_lib)) {
707 // Any referenced symbol from this lib, will be undefined until
708 // runtime as this lib is provided directly by the runtime.
709 continue;
710 }
711 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
712 }
713
714 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;
715 for (wasi_emulated_libs) |crt_file| {
716 try argv.append(try comp.get_libc_crt_file(
717 arena,
718 wasi_libc.emulatedLibCRFileLibName(crt_file),
719 ));
720 }
721
722 if (self.base.options.link_libc) {
723 try argv.append(try comp.get_libc_crt_file(arena, "libc.a"));
724 }
725 }
710726 }
711727
712728 // Positional arguments to the linker such as object files.
src/main.zig+12
......@@ -15,6 +15,7 @@ const Package = @import("Package.zig");
1515const build_options = @import("build_options");
1616const introspect = @import("introspect.zig");
1717const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
18const wasi_libc = @import("wasi_libc.zig");
1819const translate_c = @import("translate_c.zig");
1920const Cache = @import("Cache.zig");
2021const target_util = @import("target.zig");
......@@ -616,6 +617,9 @@ fn buildOutputType(
616617 var system_libs = std.ArrayList([]const u8).init(gpa);
617618 defer system_libs.deinit();
618619
620 var wasi_emulated_libs = std.ArrayList(wasi_libc.CRTFile).init(gpa);
621 defer wasi_emulated_libs.deinit();
622
619623 var clang_argv = std.ArrayList([]const u8).init(gpa);
620624 defer clang_argv.deinit();
621625
......@@ -1586,6 +1590,13 @@ fn buildOutputType(
15861590 if (std.fs.path.isAbsolute(lib_name)) {
15871591 fatal("cannot use absolute path as a system library: {s}", .{lib_name});
15881592 }
1593 if (target_info.target.os.tag == .wasi) {
1594 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| {
1595 try wasi_emulated_libs.append(crt_file);
1596 _ = system_libs.orderedRemove(i);
1597 continue;
1598 }
1599 }
15891600 i += 1;
15901601 }
15911602 }
......@@ -1895,6 +1906,7 @@ fn buildOutputType(
18951906 .framework_dirs = framework_dirs.items,
18961907 .frameworks = frameworks.items,
18971908 .system_libs = system_libs.items,
1909 .wasi_emulated_libs = wasi_emulated_libs.items,
18981910 .link_libc = link_libc,
18991911 .link_libcpp = link_libcpp,
19001912 .link_libunwind = link_libunwind,
src/wasi_libc.zig+292-158
......@@ -1,4 +1,5 @@
11const std = @import("std");
2const mem = std.mem;
23const path = std.fs.path;
34
45const Allocator = std.mem.Allocator;
......@@ -7,7 +8,44 @@ const build_options = @import("build_options");
78const target_util = @import("target.zig");
89const musl = @import("musl.zig");
910
10pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
11pub const CRTFile = enum {
12 crt1_o,
13 crt1_reactor_o,
14 crt1_command_o,
15 libc_a,
16 libwasi_emulated_process_clocks_a,
17 libwasi_emulated_getpid_a,
18 libwasi_emulated_mman_a,
19 libwasi_emulated_signal_a,
20};
21
22pub fn getEmulatedLibCRTFile(lib_name: []const u8) ?CRTFile {
23 if (mem.eql(u8, lib_name, "wasi-emulated-process-clocks")) {
24 return .libwasi_emulated_process_clocks_a;
25 }
26 if (mem.eql(u8, lib_name, "wasi-emulated-getpid")) {
27 return .libwasi_emulated_getpid_a;
28 }
29 if (mem.eql(u8, lib_name, "wasi-emulated-mman")) {
30 return .libwasi_emulated_mman_a;
31 }
32 if (mem.eql(u8, lib_name, "wasi-emulated-signal")) {
33 return .libwasi_emulated_signal_a;
34 }
35 return null;
36}
37
38pub fn emulatedLibCRFileLibName(crt_file: CRTFile) []const u8 {
39 return switch (crt_file) {
40 .libwasi_emulated_process_clocks_a => "libwasi-emulated-process-clocks.a",
41 .libwasi_emulated_getpid_a => "libwasi-emulated-getpid.a",
42 .libwasi_emulated_mman_a => "libwasi-emulated-mman.a",
43 .libwasi_emulated_signal_a => "libwasi-emulated-signal.a",
44 else => unreachable,
45 };
46}
47
48pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
1149 if (!build_options.have_llvm) {
1250 return error.ZigCompilerNotBuiltWithLLVMExtensions;
1351 }
......@@ -17,190 +55,190 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
1755 defer arena_allocator.deinit();
1856 const arena = &arena_allocator.allocator;
1957
20 {
21 // Compile crt sources.
22 var args = std.ArrayList([]const u8).init(arena);
23 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 }),
58 switch (crt_file) {
59 .crt1_o => {
60 var args = std.ArrayList([]const u8).init(arena);
61 try addCCArgs(comp, arena, &args, false);
62 try addLibcBottomHalfIncludes(comp, arena, &args);
63 return comp.build_crt_file("crt1", .Obj, &[1]Compilation.CSourceFile{
64 .{
65 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
66 "libc", try sanitize(arena, crt1_src_file),
67 }),
68 .extra_flags = args.items,
69 },
70 });
71 },
72 .crt1_reactor_o => {
73 var args = std.ArrayList([]const u8).init(arena);
74 try addCCArgs(comp, arena, &args, false);
75 try addLibcBottomHalfIncludes(comp, arena, &args);
76 return comp.build_crt_file("crt1-reactor", .Obj, &[1]Compilation.CSourceFile{
77 .{
78 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
79 "libc", try sanitize(arena, crt1_reactor_src_file),
80 }),
81 .extra_flags = args.items,
82 },
83 });
84 },
85 .crt1_command_o => {
86 var args = std.ArrayList([]const u8).init(arena);
87 try addCCArgs(comp, arena, &args, false);
88 try addLibcBottomHalfIncludes(comp, arena, &args);
89 return comp.build_crt_file("crt1-command", .Obj, &[1]Compilation.CSourceFile{
90 .{
91 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
92 "libc", try sanitize(arena, crt1_command_src_file),
93 }),
94 .extra_flags = args.items,
95 },
96 });
97 },
98 .libc_a => {
99 var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
100
101 {
102 // Compile dlmalloc.
103 var args = std.ArrayList([]const u8).init(arena);
104 try addCCArgs(comp, arena, &args, true);
105 try args.appendSlice(&[_][]const u8{
106 "-I",
107 try comp.zig_lib_directory.join(arena, &[_][]const u8{
108 "libc",
109 "wasi",
110 "dlmalloc",
111 "include",
112 }),
113 });
33114
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 }),
115 for (dlmalloc_src_files) |file_path| {
116 try libc_sources.append(.{
117 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
118 "libc", try sanitize(arena, file_path),
119 }),
120 .extra_flags = args.items,
121 });
122 }
123 }
43124
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 });
125 {
126 // Compile libc-bottom-half.
127 var args = std.ArrayList([]const u8).init(arena);
128 try addCCArgs(comp, arena, &args, true);
129 try addLibcBottomHalfIncludes(comp, arena, &args);
53130
54 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
55 for (crt_src_files) |file_path| {
56 try comp_sources.append(.{
57 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
58 "libc", try sanitize(arena, file_path),
59 }),
60 .extra_flags = args.items,
61 });
62 }
63 try comp.build_crt_file("crt", .Obj, comp_sources.items);
64 }
131 for (libc_bottom_half_src_files) |file_path| {
132 try libc_sources.append(.{
133 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
134 "libc", try sanitize(arena, file_path),
135 }),
136 .extra_flags = args.items,
137 });
138 }
139 }
65140
66 {
67 // Compile WASI libc (sysroot).
68 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
141 {
142 // Compile libc-top-half.
143 var args = std.ArrayList([]const u8).init(arena);
144 try addCCArgs(comp, arena, &args, true);
145 try addLibcTopHalfIncludes(comp, arena, &args);
146
147 for (libc_top_half_src_files) |file_path| {
148 try libc_sources.append(.{
149 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
150 "libc", try sanitize(arena, file_path),
151 }),
152 .extra_flags = args.items,
153 });
154 }
155 }
69156
70 {
71 // Compile dlmalloc.
157 try comp.build_crt_file("c", .Lib, libc_sources.items);
158 },
159 .libwasi_emulated_process_clocks_a => {
72160 var args = std.ArrayList([]const u8).init(arena);
73161 try addCCArgs(comp, arena, &args, true);
74 try args.appendSlice(&[_][]const u8{
75 "-I",
76 try comp.zig_lib_directory.join(arena, &[_][]const u8{
77 "libc",
78 "wasi",
79 "dlmalloc",
80 "include",
81 }),
82 });
162 try addLibcBottomHalfIncludes(comp, arena, &args);
83163
84 for (dlmalloc_src_files) |file_path| {
85 try comp_sources.append(.{
164 var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
165 for (emulated_process_clocks_src_files) |file_path| {
166 try emu_clocks_sources.append(.{
86167 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
87168 "libc", try sanitize(arena, file_path),
88169 }),
89170 .extra_flags = args.items,
90171 });
91172 }
92 }
93
94 {
95 // Compile libc-bottom-half.
173 try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, emu_clocks_sources.items);
174 },
175 .libwasi_emulated_getpid_a => {
96176 var args = std.ArrayList([]const u8).init(arena);
97177 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 }),
107
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 }),
116
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 });
178 try addLibcBottomHalfIncludes(comp, arena, &args);
127179
128 for (libc_bottom_half_src_files) |file_path| {
129 try comp_sources.append(.{
180 var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
181 for (emulated_getpid_src_files) |file_path| {
182 try emu_getpid_sources.append(.{
130183 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
131184 "libc", try sanitize(arena, file_path),
132185 }),
133186 .extra_flags = args.items,
134187 });
135188 }
136 }
137
138 {
139 // Compile libc-top-half.
189 try comp.build_crt_file("wasi-emulated-getpid", .Lib, emu_getpid_sources.items);
190 },
191 .libwasi_emulated_mman_a => {
140192 var args = std.ArrayList([]const u8).init(arena);
141193 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 }),
152
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 }),
162
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 }),
194 try addLibcBottomHalfIncludes(comp, arena, &args);
172195
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 }),
182
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 });
192
193 for (libc_top_half_src_files) |file_path| {
194 try comp_sources.append(.{
196 var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
197 for (emulated_mman_src_files) |file_path| {
198 try emu_mman_sources.append(.{
195199 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
196200 "libc", try sanitize(arena, file_path),
197201 }),
198202 .extra_flags = args.items,
199203 });
200204 }
201 }
205 try comp.build_crt_file("wasi-emulated-mman", .Lib, emu_mman_sources.items);
206 },
207 .libwasi_emulated_signal_a => {
208 var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
209
210 {
211 var args = std.ArrayList([]const u8).init(arena);
212 try addCCArgs(comp, arena, &args, true);
202213
203 try comp.build_crt_file("c", .Lib, comp_sources.items);
214 for (emulated_signal_bottom_half_src_files) |file_path| {
215 try emu_signal_sources.append(.{
216 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
217 "libc", try sanitize(arena, file_path),
218 }),
219 .extra_flags = args.items,
220 });
221 }
222 }
223
224 {
225 var args = std.ArrayList([]const u8).init(arena);
226 try addCCArgs(comp, arena, &args, true);
227 try addLibcTopHalfIncludes(comp, arena, &args);
228 try args.append("-D_WASI_EMULATED_SIGNAL");
229
230 for (emulated_signal_top_half_src_files) |file_path| {
231 try emu_signal_sources.append(.{
232 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
233 "libc", try sanitize(arena, file_path),
234 }),
235 .extra_flags = args.items,
236 });
237 }
238 }
239
240 try comp.build_crt_file("wasi-emulated-signal", .Lib, emu_signal_sources.items);
241 },
204242 }
205243}
206244
......@@ -251,6 +289,99 @@ fn addCCArgs(
251289 });
252290}
253291
292fn addLibcBottomHalfIncludes(
293 comp: *Compilation,
294 arena: *Allocator,
295 args: *std.ArrayList([]const u8),
296) error{OutOfMemory}!void {
297 try args.appendSlice(&[_][]const u8{
298 "-I",
299 try comp.zig_lib_directory.join(arena, &[_][]const u8{
300 "libc",
301 "wasi",
302 "libc-bottom-half",
303 "headers",
304 "private",
305 }),
306
307 "-I",
308 try comp.zig_lib_directory.join(arena, &[_][]const u8{
309 "libc",
310 "wasi",
311 "libc-bottom-half",
312 "cloudlibc",
313 "src",
314 "include",
315 }),
316
317 "-I",
318 try comp.zig_lib_directory.join(arena, &[_][]const u8{
319 "libc",
320 "wasi",
321 "libc-bottom-half",
322 "cloudlibc",
323 "src",
324 }),
325 });
326}
327
328fn addLibcTopHalfIncludes(
329 comp: *Compilation,
330 arena: *Allocator,
331 args: *std.ArrayList([]const u8),
332) error{OutOfMemory}!void {
333 try args.appendSlice(&[_][]const u8{
334 "-I",
335 try comp.zig_lib_directory.join(arena, &[_][]const u8{
336 "libc",
337 "wasi",
338 "libc-top-half",
339 "musl",
340 "src",
341 "include",
342 }),
343
344 "-I",
345 try comp.zig_lib_directory.join(arena, &[_][]const u8{
346 "libc",
347 "wasi",
348 "libc-top-half",
349 "musl",
350 "src",
351 "internal",
352 }),
353
354 "-I",
355 try comp.zig_lib_directory.join(arena, &[_][]const u8{
356 "libc",
357 "wasi",
358 "libc-top-half",
359 "musl",
360 "arch",
361 "wasm32",
362 }),
363
364 "-I",
365 try comp.zig_lib_directory.join(arena, &[_][]const u8{
366 "libc",
367 "wasi",
368 "libc-top-half",
369 "musl",
370 "arch",
371 "generic",
372 }),
373
374 "-I",
375 try comp.zig_lib_directory.join(arena, &[_][]const u8{
376 "libc",
377 "wasi",
378 "libc-top-half",
379 "headers",
380 "private",
381 }),
382 });
383}
384
254385const dlmalloc_src_files = [_][]const u8{
255386 "wasi/dlmalloc/src/dlmalloc.c",
256387};
......@@ -1005,11 +1136,9 @@ const libc_top_half_src_files = [_][]const u8{
10051136 "wasi/libc-top-half/sources/arc4random.c",
10061137};
10071138
1008const crt_src_files = &[_][]const u8{
1009 "wasi/libc-bottom-half/crt/crt1.c",
1010 "wasi/libc-bottom-half/crt/crt1-command.c",
1011 "wasi/libc-bottom-half/crt/crt1-reactor.c",
1012};
1139const crt1_src_file = "wasi/libc-bottom-half/crt/crt1.c";
1140const crt1_command_src_file = "wasi/libc-bottom-half/crt/crt1-command.c";
1141const crt1_reactor_src_file = "wasi/libc-bottom-half/crt/crt1-reactor.c";
10131142
10141143const emulated_process_clocks_src_files = &[_][]const u8{
10151144 "wasi/libc-bottom-half/clocks/clock.c",
......@@ -1025,6 +1154,11 @@ const emulated_mman_src_files = &[_][]const u8{
10251154 "wasi/libc-bottom-half/mman/mman.c",
10261155};
10271156
1028const emulated_signal_src_files = &[_][]const u8{
1157const emulated_signal_bottom_half_src_files = &[_][]const u8{
10291158 "wasi/libc-bottom-half/signal/signal.c",
10301159};
1160
1161const emulated_signal_top_half_src_files = &[_][]const u8{
1162 "wasi/libc-top-half/musl/src/signal/psignal.c",
1163 "wasi/libc-top-half/musl/src/string/strsignal.c",
1164};