authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 01:23:05+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 01:25:38+02:00
log2ee1f7898b27447bb9b21d284842415c7459db4b
tree1e22f27275c7c6ee6928ddd3a7c90f4b66d12ee6
parentdd84ccda5daf63373b2b5df3ac02025017b7efbf

cc,wasi: store CRTFile enum in wasi_emulated_libs

* then, in `link/Wasm.zig` map `CRTFile` to full emulated libs name * move logic for removing any mention of WASI snapshot `wasi_snapshot_preview1` from `Compilation.zig` into `link/Wasm.zig`

5 files changed, 31 insertions(+), 17 deletions(-)

src/Compilation.zig+4-10
...@@ -651,7 +651,7 @@ pub const InitOptions = struct {...@@ -651,7 +651,7 @@ pub const InitOptions = struct {
651 /// * getpid651 /// * getpid
652 /// * mman652 /// * mman
653 /// * signal653 /// * signal
654 wasi_emulated_libs: []const []const u8 = &[0][]const u8{},654 wasi_emulated_libs: []const wasi_libc.CRTFile = &[0]wasi_libc.CRTFile{},
655 link_libc: bool = false,655 link_libc: bool = false,
656 link_libcpp: bool = false,656 link_libcpp: bool = false,
657 link_libunwind: bool = false,657 link_libunwind: bool = false,
...@@ -1434,11 +1434,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1434,11 +1434,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1434 });1434 });
1435 }1435 }
1436 if (comp.wantBuildWasiLibcFromSource()) {1436 if (comp.wantBuildWasiLibcFromSource()) {
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;1437 const wasi_emulated_libs = comp.bin_file.options.wasi_emulated_libs;
1439 for (wasi_emulated_libs) |lib_name| {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(.{1440 comp.work_queue.writeItemAssumeCapacity(.{
1441 .wasi_libc_crt_file = wasi_libc.getEmulatedLibCRTFile(lib_name).?,1441 .wasi_libc_crt_file = crt_file,
1442 });1442 });
1443 }1443 }
1444 // TODO add logic deciding which crt1 we want here.1444 // TODO add logic deciding which crt1 we want here.
...@@ -4157,12 +4157,6 @@ pub fn stage1AddLinkLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -4157,12 +4157,6 @@ pub fn stage1AddLinkLib(comp: *Compilation, lib_name: []const u8) !void {
4157 if (comp.bin_file.options.skip_linker_dependencies) return;4157 if (comp.bin_file.options.skip_linker_dependencies) return;
41584158
4159 // This happens when an `extern "foo"` function is referenced by the stage1 backend.4159 // This happens when an `extern "foo"` function is referenced by the stage1 backend.
4160 if (comp.getTarget().os.tag == .wasi and mem.eql(u8, "wasi_snapshot_preview1", lib_name)) {
4161 // Any referenced symbol from this lib, will be undefined until
4162 // runtime as this lib is provided directly by the runtime.
4163 return;
4164 }
4165
4166 // If we haven't seen this library yet and we're targeting Windows, we need to queue up4160 // If we haven't seen this library yet and we're targeting Windows, we need to queue up
4167 // a work item to produce the DLL import library for this.4161 // a work item to produce the DLL import library for this.
4168 const gop = try comp.bin_file.options.system_libs.getOrPut(comp.gpa, lib_name);4162 const gop = try comp.bin_file.options.system_libs.getOrPut(comp.gpa, lib_name);
src/link.zig+2-1
...@@ -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,7 +111,7 @@ pub const Options = struct {...@@ -110,7 +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),
113 wasi_emulated_libs: []const []const u8,114 wasi_emulated_libs: []const wasi_libc.CRTFile,
114 lib_dirs: []const []const u8,115 lib_dirs: []const []const u8,
115 rpath_list: []const []const u8,116 rpath_list: []const []const u8,
116117
src/link/Wasm.zig+12-3
...@@ -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
...@@ -700,14 +701,22 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -700,14 +701,22 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
700 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);701 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
701 if (is_exe_or_dyn_lib) {702 if (is_exe_or_dyn_lib) {
702 const system_libs = self.base.options.system_libs.keys();703 const system_libs = self.base.options.system_libs.keys();
704
703 for (system_libs) |link_lib| {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 }
704 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));711 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
705 }712 }
706713
707 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;714 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;
708 for (wasi_emulated_libs) |lib_name| {715 for (wasi_emulated_libs) |crt_file| {
709 const full_lib_name = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name});716 try argv.append(try comp.get_libc_crt_file(
710 try argv.append(try comp.get_libc_crt_file(arena, full_lib_name));717 arena,
718 wasi_libc.emulatedLibCRFileLibName(crt_file),
719 ));
711 }720 }
712721
713 if (self.base.options.link_libc) {722 if (self.base.options.link_libc) {
src/main.zig+3-3
...@@ -617,7 +617,7 @@ fn buildOutputType(...@@ -617,7 +617,7 @@ fn buildOutputType(
617 var system_libs = std.ArrayList([]const u8).init(gpa);617 var system_libs = std.ArrayList([]const u8).init(gpa);
618 defer system_libs.deinit();618 defer system_libs.deinit();
619619
620 var wasi_emulated_libs = std.ArrayList([]const u8).init(gpa);620 var wasi_emulated_libs = std.ArrayList(wasi_libc.CRTFile).init(gpa);
621 defer wasi_emulated_libs.deinit();621 defer wasi_emulated_libs.deinit();
622622
623 var clang_argv = std.ArrayList([]const u8).init(gpa);623 var clang_argv = std.ArrayList([]const u8).init(gpa);
...@@ -1591,8 +1591,8 @@ fn buildOutputType(...@@ -1591,8 +1591,8 @@ fn buildOutputType(
1591 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});
1592 }1592 }
1593 if (target_info.target.os.tag == .wasi) {1593 if (target_info.target.os.tag == .wasi) {
1594 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |_| {1594 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| {
1595 try wasi_emulated_libs.append(lib_name);1595 try wasi_emulated_libs.append(crt_file);
1596 _ = system_libs.orderedRemove(i);1596 _ = system_libs.orderedRemove(i);
1597 continue;1597 continue;
1598 }1598 }
src/wasi_libc.zig+10
...@@ -35,6 +35,16 @@ pub fn getEmulatedLibCRTFile(lib_name: []const u8) ?CRTFile {...@@ -35,6 +35,16 @@ pub fn getEmulatedLibCRTFile(lib_name: []const u8) ?CRTFile {
35 return null;35 return null;
36}36}
3737
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
38pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {48pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
39 if (!build_options.have_llvm) {49 if (!build_options.have_llvm) {
40 return error.ZigCompilerNotBuiltWithLLVMExtensions;50 return error.ZigCompilerNotBuiltWithLLVMExtensions;