authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-07 11:02:11+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 01:25:38+02:00
log4e4722a65e3bc3d6bb4518df3082ffea7c2a7c5d
tree02bf3ff87229ec8df06bd8296050e318e1cb90cc
parent93a4403271ef62d8ae2cd10f58cc23d4b7bbdf4d

cc,wasi: build referenced-only emulated components

Move parsing of system libs into `main.zig` next to where we decide if we should link libC, and, if targeting WASI, if the specified libname equals one of the emulated components, save it on the side and remove it from the system libs. Then, build *only* those parts of WASI libc that were preserved in the previous step. This also fixes building of different crt1 bits needed to support reactors and commands.

5 files changed, 227 insertions(+), 154 deletions(-)

src/Compilation.zig+31-12
...@@ -203,8 +203,8 @@ const Job = union(enum) {...@@ -203,8 +203,8 @@ const Job = union(enum) {
203 /// needed when not linking libc and using LLVM for code generation because it generates203 /// needed when not linking libc and using LLVM for code generation because it generates
204 /// calls to, for example, memcpy and memset.204 /// calls to, for example, memcpy and memset.
205 zig_libc: void,205 zig_libc: void,
206 /// WASI libc sysroot206 /// one of WASI libc static objects
207 wasi_libc_sysroot: void,207 wasi_libc_crt_file: wasi_libc.CRTFile,
208208
209 /// Use stage1 C++ code to compile zig code into an object file.209 /// Use stage1 C++ code to compile zig code into an object file.
210 stage1_module: void,210 stage1_module: void,
...@@ -279,7 +279,7 @@ pub const MiscTask = enum {...@@ -279,7 +279,7 @@ pub const MiscTask = enum {
279 libcxx,279 libcxx,
280 libcxxabi,280 libcxxabi,
281 libtsan,281 libtsan,
282 wasi_libc_sysroot,282 wasi_libc_crt_file,
283 compiler_rt,283 compiler_rt,
284 libssp,284 libssp,
285 zig_libc,285 zig_libc,
...@@ -646,6 +646,12 @@ pub const InitOptions = struct {...@@ -646,6 +646,12 @@ pub const InitOptions = struct {
646 framework_dirs: []const []const u8 = &[0][]const u8{},646 framework_dirs: []const []const u8 = &[0][]const u8{},
647 frameworks: []const []const u8 = &[0][]const u8{},647 frameworks: []const []const u8 = &[0][]const u8{},
648 system_libs: []const []const u8 = &[0][]const u8{},648 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 []const u8 = &[0][]const u8{},
649 link_libc: bool = false,655 link_libc: bool = false,
650 link_libcpp: bool = false,656 link_libcpp: bool = false,
651 link_libunwind: bool = false,657 link_libunwind: bool = false,
...@@ -1286,6 +1292,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1286,6 +1292,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1286 .framework_dirs = options.framework_dirs,1292 .framework_dirs = options.framework_dirs,
1287 .system_libs = system_libs,1293 .system_libs = system_libs,
1288 .syslibroot = darwin_options.syslibroot,1294 .syslibroot = darwin_options.syslibroot,
1295 .wasi_emulated_libs = options.wasi_emulated_libs,
1289 .lib_dirs = options.lib_dirs,1296 .lib_dirs = options.lib_dirs,
1290 .rpath_list = options.rpath_list,1297 .rpath_list = options.rpath_list,
1291 .strip = strip,1298 .strip = strip,
...@@ -1426,8 +1433,19 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1426,8 +1433,19 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1426 },1433 },
1427 });1434 });
1428 }1435 }
1429 if (comp.wantBuildWasiLibcSysrootFromSource()) {1436 if (comp.wantBuildWasiLibcFromSource()) {
1430 try comp.work_queue.write(&[_]Job{.{ .wasi_libc_sysroot = {} }});1437 try comp.work_queue.ensureUnusedCapacity(6); // worst-case we need all components
1438 const wasi_emulated_libs = comp.bin_file.options.wasi_emulated_libs;
1439 for (wasi_emulated_libs) |lib_name| {
1440 comp.work_queue.writeItemAssumeCapacity(.{
1441 .wasi_libc_crt_file = wasi_libc.getEmulatedLibCRTFile(lib_name).?,
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 });
1431 }1449 }
1432 if (comp.wantBuildMinGWFromSource()) {1450 if (comp.wantBuildMinGWFromSource()) {
1433 const static_lib_jobs = [_]Job{1451 const static_lib_jobs = [_]Job{
...@@ -2169,12 +2187,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor...@@ -2169,12 +2187,12 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
2169 );2187 );
2170 };2188 };
2171 },2189 },
2172 .wasi_libc_sysroot => {2190 .wasi_libc_crt_file => |crt_file| {
2173 wasi_libc.buildWasiLibcSysroot(self) catch |err| {2191 wasi_libc.buildCRTFile(self, crt_file) catch |err| {
2174 // TODO Surface more error details.2192 // TODO Surface more error details.
2175 try self.setMiscFailure(2193 try self.setMiscFailure(
2176 .wasi_libc_sysroot,2194 .wasi_libc_crt_file,
2177 "unable to build WASI libc sysroot: {s}",2195 "unable to build WASI libc CRT file: {s}",
2178 .{@errorName(err)},2196 .{@errorName(err)},
2179 );2197 );
2180 };2198 };
...@@ -3303,7 +3321,7 @@ pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []cons...@@ -3303,7 +3321,7 @@ pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []cons
3303 if (comp.wantBuildGLibCFromSource() or3321 if (comp.wantBuildGLibCFromSource() or
3304 comp.wantBuildMuslFromSource() or3322 comp.wantBuildMuslFromSource() or
3305 comp.wantBuildMinGWFromSource() or3323 comp.wantBuildMinGWFromSource() or
3306 comp.wantBuildWasiLibcSysrootFromSource())3324 comp.wantBuildWasiLibcFromSource())
3307 {3325 {
3308 return comp.crt_files.get(basename).?.full_object_path;3326 return comp.crt_files.get(basename).?.full_object_path;
3309 }3327 }
...@@ -3343,8 +3361,9 @@ fn wantBuildMuslFromSource(comp: Compilation) bool {...@@ -3343,8 +3361,9 @@ fn wantBuildMuslFromSource(comp: Compilation) bool {
3343 !comp.getTarget().isWasm();3361 !comp.getTarget().isWasm();
3344}3362}
33453363
3346fn wantBuildWasiLibcSysrootFromSource(comp: Compilation) bool {3364fn wantBuildWasiLibcFromSource(comp: Compilation) bool {
3347 return comp.wantBuildLibCFromSource() and comp.getTarget().isWasm();3365 return comp.wantBuildLibCFromSource() and comp.getTarget().isWasm() and
3366 comp.getTarget().os.tag == .wasi;
3348}3367}
33493368
3350fn wantBuildMinGWFromSource(comp: Compilation) bool {3369fn wantBuildMinGWFromSource(comp: Compilation) bool {
src/link.zig+1
...@@ -110,6 +110,7 @@ pub const Options = struct {...@@ -110,6 +110,7 @@ pub const Options = struct {
110 framework_dirs: []const []const u8,110 framework_dirs: []const []const u8,
111 frameworks: []const []const u8,111 frameworks: []const []const u8,
112 system_libs: std.StringArrayHashMapUnmanaged(void),112 system_libs: std.StringArrayHashMapUnmanaged(void),
113 wasi_emulated_libs: []const []const u8,
113 lib_dirs: []const []const u8,114 lib_dirs: []const []const u8,
114 rpath_list: []const []const u8,115 rpath_list: []const []const u8,
115116
src/link/Wasm.zig+8-7
...@@ -699,18 +699,19 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -699,18 +699,19 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
699699
700 if (link_in_crt) {700 if (link_in_crt) {
701 // TODO work out if we want standard crt, a reactor or a command701 // 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"));702 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));
703 }703 }
704704
705 if (!is_obj) {705 if (!is_obj) {
706 const system_libs = self.base.options.system_libs.keys();706 const system_libs = self.base.options.system_libs.keys();
707 for (system_libs) |link_lib| {707 for (system_libs) |link_lib| {
708 const full_name = try std.fmt.allocPrint(arena, "lib{s}.a", .{link_lib});708 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
709 if (comp.crt_files.get(full_name)) |crt| {709 }
710 try argv.append(crt.full_object_path);710
711 } else {711 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;
712 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));712 for (wasi_emulated_libs) |lib_name| {
713 }713 const full_lib_name = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name});
714 try argv.append(try comp.get_libc_crt_file(arena, full_lib_name));
714 }715 }
715716
716 if (self.base.options.link_libc) {717 if (self.base.options.link_libc) {
src/main.zig+12
...@@ -15,6 +15,7 @@ const Package = @import("Package.zig");...@@ -15,6 +15,7 @@ const Package = @import("Package.zig");
15const build_options = @import("build_options");15const build_options = @import("build_options");
16const introspect = @import("introspect.zig");16const introspect = @import("introspect.zig");
17const LibCInstallation = @import("libc_installation.zig").LibCInstallation;17const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
18const wasi_libc = @import("wasi_libc.zig");
18const translate_c = @import("translate_c.zig");19const translate_c = @import("translate_c.zig");
19const Cache = @import("Cache.zig");20const Cache = @import("Cache.zig");
20const target_util = @import("target.zig");21const target_util = @import("target.zig");
...@@ -616,6 +617,9 @@ fn buildOutputType(...@@ -616,6 +617,9 @@ fn buildOutputType(
616 var system_libs = std.ArrayList([]const u8).init(gpa);617 var system_libs = std.ArrayList([]const u8).init(gpa);
617 defer system_libs.deinit();618 defer system_libs.deinit();
618619
620 var wasi_emulated_libs = std.ArrayList([]const u8).init(gpa);
621 defer wasi_emulated_libs.deinit();
622
619 var clang_argv = std.ArrayList([]const u8).init(gpa);623 var clang_argv = std.ArrayList([]const u8).init(gpa);
620 defer clang_argv.deinit();624 defer clang_argv.deinit();
621625
...@@ -1586,6 +1590,13 @@ fn buildOutputType(...@@ -1586,6 +1590,13 @@ fn buildOutputType(
1586 if (std.fs.path.isAbsolute(lib_name)) {1590 if (std.fs.path.isAbsolute(lib_name)) {
1587 fatal("cannot use absolute path as a system library: {s}", .{lib_name});1591 fatal("cannot use absolute path as a system library: {s}", .{lib_name});
1588 }1592 }
1593 if (target_info.target.os.tag == .wasi) {
1594 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |_| {
1595 try wasi_emulated_libs.append(lib_name);
1596 _ = system_libs.orderedRemove(i);
1597 continue;
1598 }
1599 }
1589 i += 1;1600 i += 1;
1590 }1601 }
1591 }1602 }
...@@ -1895,6 +1906,7 @@ fn buildOutputType(...@@ -1895,6 +1906,7 @@ fn buildOutputType(
1895 .framework_dirs = framework_dirs.items,1906 .framework_dirs = framework_dirs.items,
1896 .frameworks = frameworks.items,1907 .frameworks = frameworks.items,
1897 .system_libs = system_libs.items,1908 .system_libs = system_libs.items,
1909 .wasi_emulated_libs = wasi_emulated_libs.items,
1898 .link_libc = link_libc,1910 .link_libc = link_libc,
1899 .link_libcpp = link_libcpp,1911 .link_libcpp = link_libcpp,
1900 .link_libunwind = link_libunwind,1912 .link_libunwind = link_libunwind,
src/wasi_libc.zig+175-135
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const mem = std.mem;
2const path = std.fs.path;3const path = std.fs.path;
34
4const Allocator = std.mem.Allocator;5const Allocator = std.mem.Allocator;
...@@ -7,7 +8,34 @@ const build_options = @import("build_options");...@@ -7,7 +8,34 @@ const build_options = @import("build_options");
7const target_util = @import("target.zig");8const target_util = @import("target.zig");
8const musl = @import("musl.zig");9const 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 buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
11 if (!build_options.have_llvm) {39 if (!build_options.have_llvm) {
12 return error.ZigCompilerNotBuiltWithLLVMExtensions;40 return error.ZigCompilerNotBuiltWithLLVMExtensions;
13 }41 }
...@@ -17,176 +45,190 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {...@@ -17,176 +45,190 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
17 defer arena_allocator.deinit();45 defer arena_allocator.deinit();
18 const arena = &arena_allocator.allocator;46 const arena = &arena_allocator.allocator;
1947
20 {48 switch (crt_file) {
21 // Compile crt sources.49 .crt1_o => {
22 var args = std.ArrayList([]const u8).init(arena);
23 try addCCArgs(comp, arena, &args, false);
24 try addLibcBottomHalfIncludes(comp, arena, &args);
25
26 var crt_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
27 for (crt_src_files) |file_path| {
28 try crt_sources.append(.{
29 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
30 "libc", try sanitize(arena, file_path),
31 }),
32 .extra_flags = args.items,
33 });
34 }
35 try comp.build_crt_file("crt", .Obj, crt_sources.items);
36 }
37
38 {
39 // Compile WASI libc (sysroot).
40 var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
41
42 {
43 // Compile dlmalloc.
44 var args = std.ArrayList([]const u8).init(arena);50 var args = std.ArrayList([]const u8).init(arena);
45 try addCCArgs(comp, arena, &args, true);51 try addCCArgs(comp, arena, &args, false);
46 try args.appendSlice(&[_][]const u8{52 try addLibcBottomHalfIncludes(comp, arena, &args);
47 "-I",53 return comp.build_crt_file("crt1", .Obj, &[1]Compilation.CSourceFile{
48 try comp.zig_lib_directory.join(arena, &[_][]const u8{54 .{
49 "libc",55 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
50 "wasi",56 "libc", try sanitize(arena, crt1_src_file),
51 "dlmalloc",57 }),
52 "include",58 .extra_flags = args.items,
53 }),59 },
54 });60 });
5561 },
56 for (dlmalloc_src_files) |file_path| {62 .crt1_reactor_o => {
57 try libc_sources.append(.{63 var args = std.ArrayList([]const u8).init(arena);
64 try addCCArgs(comp, arena, &args, false);
65 try addLibcBottomHalfIncludes(comp, arena, &args);
66 return comp.build_crt_file("crt1-reactor", .Obj, &[1]Compilation.CSourceFile{
67 .{
58 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{68 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
59 "libc", try sanitize(arena, file_path),69 "libc", try sanitize(arena, crt1_reactor_src_file),
60 }),70 }),
61 .extra_flags = args.items,71 .extra_flags = args.items,
62 });72 },
63 }73 });
64 }74 },
6575 .crt1_command_o => {
66 {
67 // Compile libc-bottom-half.
68 var args = std.ArrayList([]const u8).init(arena);76 var args = std.ArrayList([]const u8).init(arena);
69 try addCCArgs(comp, arena, &args, true);77 try addCCArgs(comp, arena, &args, false);
70 try addLibcBottomHalfIncludes(comp, arena, &args);78 try addLibcBottomHalfIncludes(comp, arena, &args);
7179 return comp.build_crt_file("crt1-command", .Obj, &[1]Compilation.CSourceFile{
72 for (libc_bottom_half_src_files) |file_path| {80 .{
73 try libc_sources.append(.{
74 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{81 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
75 "libc", try sanitize(arena, file_path),82 "libc", try sanitize(arena, crt1_command_src_file),
76 }),83 }),
77 .extra_flags = args.items,84 .extra_flags = args.items,
85 },
86 });
87 },
88 .libc_a => {
89 var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
90
91 {
92 // Compile dlmalloc.
93 var args = std.ArrayList([]const u8).init(arena);
94 try addCCArgs(comp, arena, &args, true);
95 try args.appendSlice(&[_][]const u8{
96 "-I",
97 try comp.zig_lib_directory.join(arena, &[_][]const u8{
98 "libc",
99 "wasi",
100 "dlmalloc",
101 "include",
102 }),
78 });103 });
104
105 for (dlmalloc_src_files) |file_path| {
106 try libc_sources.append(.{
107 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
108 "libc", try sanitize(arena, file_path),
109 }),
110 .extra_flags = args.items,
111 });
112 }
79 }113 }
80 }
81114
82 {115 {
83 // Compile libc-top-half.116 // Compile libc-bottom-half.
117 var args = std.ArrayList([]const u8).init(arena);
118 try addCCArgs(comp, arena, &args, true);
119 try addLibcBottomHalfIncludes(comp, arena, &args);
120
121 for (libc_bottom_half_src_files) |file_path| {
122 try libc_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 }
130
131 {
132 // Compile libc-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
137 for (libc_top_half_src_files) |file_path| {
138 try libc_sources.append(.{
139 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
140 "libc", try sanitize(arena, file_path),
141 }),
142 .extra_flags = args.items,
143 });
144 }
145 }
146
147 try comp.build_crt_file("c", .Lib, libc_sources.items);
148 },
149 .libwasi_emulated_process_clocks_a => {
84 var args = std.ArrayList([]const u8).init(arena);150 var args = std.ArrayList([]const u8).init(arena);
85 try addCCArgs(comp, arena, &args, true);151 try addCCArgs(comp, arena, &args, true);
86 try addLibcTopHalfIncludes(comp, arena, &args);152 try addLibcBottomHalfIncludes(comp, arena, &args);
87153
88 for (libc_top_half_src_files) |file_path| {154 var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
89 try libc_sources.append(.{155 for (emulated_process_clocks_src_files) |file_path| {
156 try emu_clocks_sources.append(.{
90 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{157 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
91 "libc", try sanitize(arena, file_path),158 "libc", try sanitize(arena, file_path),
92 }),159 }),
93 .extra_flags = args.items,160 .extra_flags = args.items,
94 });161 });
95 }162 }
96 }163 try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, emu_clocks_sources.items);
97164 },
98 try comp.build_crt_file("c", .Lib, libc_sources.items);165 .libwasi_emulated_getpid_a => {
99 }
100
101 {
102 // Compile emulated process clocks.
103 var args = std.ArrayList([]const u8).init(arena);
104 try addCCArgs(comp, arena, &args, true);
105 try addLibcBottomHalfIncludes(comp, arena, &args);
106
107 var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
108 for (emulated_process_clocks_src_files) |file_path| {
109 try emu_clocks_sources.append(.{
110 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
111 "libc", try sanitize(arena, file_path),
112 }),
113 .extra_flags = args.items,
114 });
115 }
116 try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, emu_clocks_sources.items);
117 }
118
119 {
120 // Compile emulated getpid.
121 var args = std.ArrayList([]const u8).init(arena);
122 try addCCArgs(comp, arena, &args, true);
123 try addLibcBottomHalfIncludes(comp, arena, &args);
124
125 var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
126 for (emulated_getpid_src_files) |file_path| {
127 try emu_getpid_sources.append(.{
128 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
129 "libc", try sanitize(arena, file_path),
130 }),
131 .extra_flags = args.items,
132 });
133 }
134 try comp.build_crt_file("wasi-emulated-getpid", .Lib, emu_getpid_sources.items);
135 }
136
137 {
138 // Compile emulated mman.
139 var args = std.ArrayList([]const u8).init(arena);
140 try addCCArgs(comp, arena, &args, true);
141 try addLibcBottomHalfIncludes(comp, arena, &args);
142
143 var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
144 for (emulated_mman_src_files) |file_path| {
145 try emu_mman_sources.append(.{
146 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
147 "libc", try sanitize(arena, file_path),
148 }),
149 .extra_flags = args.items,
150 });
151 }
152 try comp.build_crt_file("wasi-emulated-mman", .Lib, emu_mman_sources.items);
153 }
154
155 {
156 // Compile emulated signals.
157 var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
158
159 {
160 var args = std.ArrayList([]const u8).init(arena);166 var args = std.ArrayList([]const u8).init(arena);
161 try addCCArgs(comp, arena, &args, true);167 try addCCArgs(comp, arena, &args, true);
168 try addLibcBottomHalfIncludes(comp, arena, &args);
162169
163 for (emulated_signal_bottom_half_src_files) |file_path| {170 var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
164 try emu_signal_sources.append(.{171 for (emulated_getpid_src_files) |file_path| {
172 try emu_getpid_sources.append(.{
165 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{173 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
166 "libc", try sanitize(arena, file_path),174 "libc", try sanitize(arena, file_path),
167 }),175 }),
168 .extra_flags = args.items,176 .extra_flags = args.items,
169 });177 });
170 }178 }
171 }179 try comp.build_crt_file("wasi-emulated-getpid", .Lib, emu_getpid_sources.items);
172180 },
173 {181 .libwasi_emulated_mman_a => {
174 var args = std.ArrayList([]const u8).init(arena);182 var args = std.ArrayList([]const u8).init(arena);
175 try addCCArgs(comp, arena, &args, true);183 try addCCArgs(comp, arena, &args, true);
176 try addLibcTopHalfIncludes(comp, arena, &args);184 try addLibcBottomHalfIncludes(comp, arena, &args);
177 try args.append("-D_WASI_EMULATED_SIGNAL");
178185
179 for (emulated_signal_top_half_src_files) |file_path| {186 var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
180 try emu_signal_sources.append(.{187 for (emulated_mman_src_files) |file_path| {
188 try emu_mman_sources.append(.{
181 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{189 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
182 "libc", try sanitize(arena, file_path),190 "libc", try sanitize(arena, file_path),
183 }),191 }),
184 .extra_flags = args.items,192 .extra_flags = args.items,
185 });193 });
186 }194 }
187 }195 try comp.build_crt_file("wasi-emulated-mman", .Lib, emu_mman_sources.items);
196 },
197 .libwasi_emulated_signal_a => {
198 var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
199
200 {
201 var args = std.ArrayList([]const u8).init(arena);
202 try addCCArgs(comp, arena, &args, true);
203
204 for (emulated_signal_bottom_half_src_files) |file_path| {
205 try emu_signal_sources.append(.{
206 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
207 "libc", try sanitize(arena, file_path),
208 }),
209 .extra_flags = args.items,
210 });
211 }
212 }
188213
189 try comp.build_crt_file("wasi-emulated-signal", .Lib, emu_signal_sources.items);214 {
215 var args = std.ArrayList([]const u8).init(arena);
216 try addCCArgs(comp, arena, &args, true);
217 try addLibcTopHalfIncludes(comp, arena, &args);
218 try args.append("-D_WASI_EMULATED_SIGNAL");
219
220 for (emulated_signal_top_half_src_files) |file_path| {
221 try emu_signal_sources.append(.{
222 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
223 "libc", try sanitize(arena, file_path),
224 }),
225 .extra_flags = args.items,
226 });
227 }
228 }
229
230 try comp.build_crt_file("wasi-emulated-signal", .Lib, emu_signal_sources.items);
231 },
190 }232 }
191}233}
192234
...@@ -1084,11 +1126,9 @@ const libc_top_half_src_files = [_][]const u8{...@@ -1084,11 +1126,9 @@ const libc_top_half_src_files = [_][]const u8{
1084 "wasi/libc-top-half/sources/arc4random.c",1126 "wasi/libc-top-half/sources/arc4random.c",
1085};1127};
10861128
1087const crt_src_files = &[_][]const u8{1129const crt1_src_file = "wasi/libc-bottom-half/crt/crt1.c";
1088 "wasi/libc-bottom-half/crt/crt1.c",1130const crt1_command_src_file = "wasi/libc-bottom-half/crt/crt1-command.c";
1089 "wasi/libc-bottom-half/crt/crt1-command.c",1131const crt1_reactor_src_file = "wasi/libc-bottom-half/crt/crt1-reactor.c";
1090 "wasi/libc-bottom-half/crt/crt1-reactor.c",
1091};
10921132
1093const emulated_process_clocks_src_files = &[_][]const u8{1133const emulated_process_clocks_src_files = &[_][]const u8{
1094 "wasi/libc-bottom-half/clocks/clock.c",1134 "wasi/libc-bottom-half/clocks/clock.c",