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
signaturebadge-question-mark 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) {...@@ -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 wasi_libc.CRTFile = &[0]wasi_libc.CRTFile{},
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 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 });
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+2
...@@ -13,6 +13,7 @@ const Type = @import("type.zig").Type;...@@ -13,6 +13,7 @@ const Type = @import("type.zig").Type;
13const Cache = @import("Cache.zig");13const Cache = @import("Cache.zig");
14const build_options = @import("build_options");14const build_options = @import("build_options");
15const LibCInstallation = @import("libc_installation.zig").LibCInstallation;15const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
16const wasi_libc = @import("wasi_libc.zig");
1617
17pub const producer_string = if (std.builtin.is_test) "zig test" else "zig " ++ build_options.version;18pub const producer_string = if (std.builtin.is_test) "zig test" else "zig " ++ build_options.version;
1819
...@@ -110,6 +111,7 @@ pub const Options = struct {...@@ -110,6 +111,7 @@ pub const Options = struct {
110 framework_dirs: []const []const u8,111 framework_dirs: []const []const u8,
111 frameworks: []const []const u8,112 frameworks: []const []const u8,
112 system_libs: std.StringArrayHashMapUnmanaged(void),113 system_libs: std.StringArrayHashMapUnmanaged(void),
114 wasi_emulated_libs: []const wasi_libc.CRTFile,
113 lib_dirs: []const []const u8,115 lib_dirs: []const []const u8,
114 rpath_list: []const []const u8,116 rpath_list: []const []const u8,
115117
src/link/Wasm.zig+32-16
...@@ -15,6 +15,7 @@ const codegen = @import("../codegen/wasm.zig");...@@ -15,6 +15,7 @@ const codegen = @import("../codegen/wasm.zig");
15const link = @import("../link.zig");15const link = @import("../link.zig");
16const trace = @import("../tracy.zig").trace;16const trace = @import("../tracy.zig").trace;
17const build_options = @import("build_options");17const build_options = @import("build_options");
18const wasi_libc = @import("../wasi_libc.zig");
18const Cache = @import("../Cache.zig");19const Cache = @import("../Cache.zig");
19const TypedValue = @import("../TypedValue.zig");20const TypedValue = @import("../TypedValue.zig");
2021
...@@ -574,7 +575,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -574,7 +575,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
574 null;575 null;
575576
576 const target = self.base.options.target;577 const target = self.base.options.target;
577 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
578578
579 const id_symlink_basename = "lld.id";579 const id_symlink_basename = "lld.id";
580580
...@@ -653,8 +653,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -653,8 +653,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
653 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});653 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});
654 }654 }
655 } else {655 } else {
656 const is_obj = self.base.options.output_mode == .Obj;
657
658 // Create an LLD command line and invoke it.656 // Create an LLD command line and invoke it.
659 var argv = std.ArrayList([]const u8).init(self.base.allocator);657 var argv = std.ArrayList([]const u8).init(self.base.allocator);
660 defer argv.deinit();658 defer argv.deinit();
...@@ -662,10 +660,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -662,10 +660,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
662 // This is necessary because LLD does not behave properly as a library -660 // This is necessary because LLD does not behave properly as a library -
663 // it calls exit() and does not reset all global data between invocations.661 // it calls exit() and does not reset all global data between invocations.
664 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" });662 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" });
665 if (is_obj) {
666 try argv.append("-r");
667 }
668
669 try argv.append("-error-limit=0");663 try argv.append("-error-limit=0");
670664
671 if (self.base.options.lto) {665 if (self.base.options.lto) {
...@@ -697,16 +691,38 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -697,16 +691,38 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
697 full_out_path,691 full_out_path,
698 });692 });
699693
700 if (link_in_crt) {694 if (target.os.tag == .wasi) {
701 // TODO work out if we want standard crt, a reactor or a command695 if (self.base.options.link_libc and self.base.options.output_mode == .Exe) {
702 try argv.append(try comp.get_libc_crt_file(arena, "crt.o"));696 // TODO work out if we want standard crt, a reactor or a command
703 }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) {705 for (system_libs) |link_lib| {
706 try argv.append(try comp.get_libc_crt_file(arena, switch (self.base.options.link_mode) {706 if (mem.eql(u8, "wasi_snapshot_preview1", link_lib)) {
707 .Static => "libc.a",707 // Any referenced symbol from this lib, will be undefined until
708 .Dynamic => unreachable,708 // runtime as this lib is provided directly by the runtime.
709 }));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 }
710 }726 }
711727
712 // Positional arguments to the linker such as object files.728 // Positional arguments to the linker such as object files.
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(wasi_libc.CRTFile).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)) |crt_file| {
1595 try wasi_emulated_libs.append(crt_file);
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+292-158
...@@ -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,44 @@ const build_options = @import("build_options");...@@ -7,7 +8,44 @@ 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 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 {
11 if (!build_options.have_llvm) {49 if (!build_options.have_llvm) {
12 return error.ZigCompilerNotBuiltWithLLVMExtensions;50 return error.ZigCompilerNotBuiltWithLLVMExtensions;
13 }51 }
...@@ -17,190 +55,190 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {...@@ -17,190 +55,190 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
17 defer arena_allocator.deinit();55 defer arena_allocator.deinit();
18 const arena = &arena_allocator.allocator;56 const arena = &arena_allocator.allocator;
1957
20 {58 switch (crt_file) {
21 // Compile crt sources.59 .crt1_o => {
22 var args = std.ArrayList([]const u8).init(arena);60 var args = std.ArrayList([]const u8).init(arena);
23 try addCCArgs(comp, arena, &args, false);61 try addCCArgs(comp, arena, &args, false);
24 try args.appendSlice(&[_][]const u8{62 try addLibcBottomHalfIncludes(comp, arena, &args);
25 "-I",63 return comp.build_crt_file("crt1", .Obj, &[1]Compilation.CSourceFile{
26 try comp.zig_lib_directory.join(arena, &[_][]const u8{64 .{
27 "libc",65 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
28 "wasi",66 "libc", try sanitize(arena, crt1_src_file),
29 "libc-bottom-half",67 }),
30 "headers",68 .extra_flags = args.items,
31 "private",69 },
32 }),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",115 for (dlmalloc_src_files) |file_path| {
35 try comp.zig_lib_directory.join(arena, &[_][]const u8{116 try libc_sources.append(.{
36 "libc",117 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
37 "wasi",118 "libc", try sanitize(arena, file_path),
38 "libc-bottom-half",119 }),
39 "cloudlibc",120 .extra_flags = args.items,
40 "src",121 });
41 "include",122 }
42 }),123 }
43124
44 "-I",125 {
45 try comp.zig_lib_directory.join(arena, &[_][]const u8{126 // Compile libc-bottom-half.
46 "libc",127 var args = std.ArrayList([]const u8).init(arena);
47 "wasi",128 try addCCArgs(comp, arena, &args, true);
48 "libc-bottom-half",129 try addLibcBottomHalfIncludes(comp, arena, &args);
49 "cloudlibc",
50 "src",
51 }),
52 });
53130
54 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);131 for (libc_bottom_half_src_files) |file_path| {
55 for (crt_src_files) |file_path| {132 try libc_sources.append(.{
56 try comp_sources.append(.{133 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
57 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{134 "libc", try sanitize(arena, file_path),
58 "libc", try sanitize(arena, file_path),135 }),
59 }),136 .extra_flags = args.items,
60 .extra_flags = args.items,137 });
61 });138 }
62 }139 }
63 try comp.build_crt_file("crt", .Obj, comp_sources.items);
64 }
65140
66 {141 {
67 // Compile WASI libc (sysroot).142 // Compile libc-top-half.
68 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);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 {157 try comp.build_crt_file("c", .Lib, libc_sources.items);
71 // Compile dlmalloc.158 },
159 .libwasi_emulated_process_clocks_a => {
72 var args = std.ArrayList([]const u8).init(arena);160 var args = std.ArrayList([]const u8).init(arena);
73 try addCCArgs(comp, arena, &args, true);161 try addCCArgs(comp, arena, &args, true);
74 try args.appendSlice(&[_][]const u8{162 try addLibcBottomHalfIncludes(comp, arena, &args);
75 "-I",
76 try comp.zig_lib_directory.join(arena, &[_][]const u8{
77 "libc",
78 "wasi",
79 "dlmalloc",
80 "include",
81 }),
82 });
83163
84 for (dlmalloc_src_files) |file_path| {164 var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
85 try comp_sources.append(.{165 for (emulated_process_clocks_src_files) |file_path| {
166 try emu_clocks_sources.append(.{
86 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{167 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
87 "libc", try sanitize(arena, file_path),168 "libc", try sanitize(arena, file_path),
88 }),169 }),
89 .extra_flags = args.items,170 .extra_flags = args.items,
90 });171 });
91 }172 }
92 }173 try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, emu_clocks_sources.items);
93174 },
94 {175 .libwasi_emulated_getpid_a => {
95 // Compile libc-bottom-half.
96 var args = std.ArrayList([]const u8).init(arena);176 var args = std.ArrayList([]const u8).init(arena);
97 try addCCArgs(comp, arena, &args, true);177 try addCCArgs(comp, arena, &args, true);
98 try args.appendSlice(&[_][]const u8{178 try addLibcBottomHalfIncludes(comp, arena, &args);
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 });
127179
128 for (libc_bottom_half_src_files) |file_path| {180 var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
129 try comp_sources.append(.{181 for (emulated_getpid_src_files) |file_path| {
182 try emu_getpid_sources.append(.{
130 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{183 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
131 "libc", try sanitize(arena, file_path),184 "libc", try sanitize(arena, file_path),
132 }),185 }),
133 .extra_flags = args.items,186 .extra_flags = args.items,
134 });187 });
135 }188 }
136 }189 try comp.build_crt_file("wasi-emulated-getpid", .Lib, emu_getpid_sources.items);
137190 },
138 {191 .libwasi_emulated_mman_a => {
139 // Compile libc-top-half.
140 var args = std.ArrayList([]const u8).init(arena);192 var args = std.ArrayList([]const u8).init(arena);
141 try addCCArgs(comp, arena, &args, true);193 try addCCArgs(comp, arena, &args, true);
142 try args.appendSlice(&[_][]const u8{194 try addLibcBottomHalfIncludes(comp, arena, &args);
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 }),
172195
173 "-I",196 var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
174 try comp.zig_lib_directory.join(arena, &[_][]const u8{197 for (emulated_mman_src_files) |file_path| {
175 "libc",198 try emu_mman_sources.append(.{
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(.{
195 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{199 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
196 "libc", try sanitize(arena, file_path),200 "libc", try sanitize(arena, file_path),
197 }),201 }),
198 .extra_flags = args.items,202 .extra_flags = args.items,
199 });203 });
200 }204 }
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 },
204 }242 }
205}243}
206244
...@@ -251,6 +289,99 @@ fn addCCArgs(...@@ -251,6 +289,99 @@ fn addCCArgs(
251 });289 });
252}290}
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
254const dlmalloc_src_files = [_][]const u8{385const dlmalloc_src_files = [_][]const u8{
255 "wasi/dlmalloc/src/dlmalloc.c",386 "wasi/dlmalloc/src/dlmalloc.c",
256};387};
...@@ -1005,11 +1136,9 @@ const libc_top_half_src_files = [_][]const u8{...@@ -1005,11 +1136,9 @@ const libc_top_half_src_files = [_][]const u8{
1005 "wasi/libc-top-half/sources/arc4random.c",1136 "wasi/libc-top-half/sources/arc4random.c",
1006};1137};
10071138
1008const crt_src_files = &[_][]const u8{1139const crt1_src_file = "wasi/libc-bottom-half/crt/crt1.c";
1009 "wasi/libc-bottom-half/crt/crt1.c",1140const crt1_command_src_file = "wasi/libc-bottom-half/crt/crt1-command.c";
1010 "wasi/libc-bottom-half/crt/crt1-command.c",1141const crt1_reactor_src_file = "wasi/libc-bottom-half/crt/crt1-reactor.c";
1011 "wasi/libc-bottom-half/crt/crt1-reactor.c",
1012};
10131142
1014const emulated_process_clocks_src_files = &[_][]const u8{1143const emulated_process_clocks_src_files = &[_][]const u8{
1015 "wasi/libc-bottom-half/clocks/clock.c",1144 "wasi/libc-bottom-half/clocks/clock.c",
...@@ -1025,6 +1154,11 @@ const emulated_mman_src_files = &[_][]const u8{...@@ -1025,6 +1154,11 @@ const emulated_mman_src_files = &[_][]const u8{
1025 "wasi/libc-bottom-half/mman/mman.c",1154 "wasi/libc-bottom-half/mman/mman.c",
1026};1155};
10271156
1028const emulated_signal_src_files = &[_][]const u8{1157const emulated_signal_bottom_half_src_files = &[_][]const u8{
1029 "wasi/libc-bottom-half/signal/signal.c",1158 "wasi/libc-bottom-half/signal/signal.c",
1030};1159};
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};