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 {
651651 /// * getpid
652652 /// * mman
653653 /// * signal
654 wasi_emulated_libs: []const []const u8 = &[0][]const u8{},
654 wasi_emulated_libs: []const wasi_libc.CRTFile = &[0]wasi_libc.CRTFile{},
655655 link_libc: bool = false,
656656 link_libcpp: bool = false,
657657 link_libunwind: bool = false,
......@@ -1434,11 +1434,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14341434 });
14351435 }
14361436 if (comp.wantBuildWasiLibcFromSource()) {
1437 try comp.work_queue.ensureUnusedCapacity(6); // worst-case we need all components
14381437 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| {
14401440 comp.work_queue.writeItemAssumeCapacity(.{
1441 .wasi_libc_crt_file = wasi_libc.getEmulatedLibCRTFile(lib_name).?,
1441 .wasi_libc_crt_file = crt_file,
14421442 });
14431443 }
14441444 // TODO add logic deciding which crt1 we want here.
......@@ -4157,12 +4157,6 @@ pub fn stage1AddLinkLib(comp: *Compilation, lib_name: []const u8) !void {
41574157 if (comp.bin_file.options.skip_linker_dependencies) return;
41584158
41594159 // 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
41664160 // If we haven't seen this library yet and we're targeting Windows, we need to queue up
41674161 // a work item to produce the DLL import library for this.
41684162 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;
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,7 +111,7 @@ pub const Options = struct {
110111 framework_dirs: []const []const u8,
111112 frameworks: []const []const u8,
112113 system_libs: std.StringArrayHashMapUnmanaged(void),
113 wasi_emulated_libs: []const []const u8,
114 wasi_emulated_libs: []const wasi_libc.CRTFile,
114115 lib_dirs: []const []const u8,
115116 rpath_list: []const []const u8,
116117
src/link/Wasm.zig+12-3
......@@ -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
......@@ -700,14 +701,22 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
700701 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
701702 if (is_exe_or_dyn_lib) {
702703 const system_libs = self.base.options.system_libs.keys();
704
703705 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 }
704711 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
705712 }
706713
707714 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;
708 for (wasi_emulated_libs) |lib_name| {
709 const full_lib_name = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name});
710 try argv.append(try comp.get_libc_crt_file(arena, full_lib_name));
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 ));
711720 }
712721
713722 if (self.base.options.link_libc) {
src/main.zig+3-3
......@@ -617,7 +617,7 @@ fn buildOutputType(
617617 var system_libs = std.ArrayList([]const u8).init(gpa);
618618 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);
621621 defer wasi_emulated_libs.deinit();
622622
623623 var clang_argv = std.ArrayList([]const u8).init(gpa);
......@@ -1591,8 +1591,8 @@ fn buildOutputType(
15911591 fatal("cannot use absolute path as a system library: {s}", .{lib_name});
15921592 }
15931593 if (target_info.target.os.tag == .wasi) {
1594 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |_| {
1595 try wasi_emulated_libs.append(lib_name);
1594 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| {
1595 try wasi_emulated_libs.append(crt_file);
15961596 _ = system_libs.orderedRemove(i);
15971597 continue;
15981598 }
src/wasi_libc.zig+10
......@@ -35,6 +35,16 @@ pub fn getEmulatedLibCRTFile(lib_name: []const u8) ?CRTFile {
3535 return null;
3636}
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
3848pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
3949 if (!build_options.have_llvm) {
4050 return error.ZigCompilerNotBuiltWithLLVMExtensions;