authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-07 17:03:29+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 01:25:38+02:00
logdd84ccda5daf63373b2b5df3ac02025017b7efbf
treec85dbd8048ffdac85962d4e8ac83f94ce2b94cc0
parent95745f77da6cd3ca30aea6aa47253c1e0a4f5a75

wasi: clean up linking logic

Do not try to link WASI libc or emulated subcomponents when not targeting WASI; e.g., when targeting `wasm32-freestanding`.

1 files changed, 20 insertions(+), 23 deletions(-)

src/link/Wasm.zig+20-23
......@@ -574,7 +574,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
574574 null;
575575
576576 const target = self.base.options.target;
577 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
578577
579578 const id_symlink_basename = "lld.id";
580579
......@@ -653,8 +652,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
653652 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});
654653 }
655654 } else {
656 const is_obj = self.base.options.output_mode == .Obj;
657
658655 // Create an LLD command line and invoke it.
659656 var argv = std.ArrayList([]const u8).init(self.base.allocator);
660657 defer argv.deinit();
......@@ -662,10 +659,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
662659 // This is necessary because LLD does not behave properly as a library -
663660 // it calls exit() and does not reset all global data between invocations.
664661 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" });
665 if (is_obj) {
666 try argv.append("-r");
667 }
668
669662 try argv.append("-error-limit=0");
670663
671664 if (self.base.options.lto) {
......@@ -697,25 +690,29 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
697690 full_out_path,
698691 });
699692
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, "crt1.o"));
703 }
704
705 if (!is_obj) {
706 const system_libs = self.base.options.system_libs.keys();
707 for (system_libs) |link_lib| {
708 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
693 if (target.os.tag == .wasi) {
694 if (self.base.options.link_libc and self.base.options.output_mode == .Exe) {
695 // TODO work out if we want standard crt, a reactor or a command
696 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));
709697 }
710698
711 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;
712 for (wasi_emulated_libs) |lib_name| {
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));
715 }
699 const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or
700 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
701 if (is_exe_or_dyn_lib) {
702 const system_libs = self.base.options.system_libs.keys();
703 for (system_libs) |link_lib| {
704 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
705 }
716706
717 if (self.base.options.link_libc) {
718 try argv.append(try comp.get_libc_crt_file(arena, "libc.a"));
707 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));
711 }
712
713 if (self.base.options.link_libc) {
714 try argv.append(try comp.get_libc_crt_file(arena, "libc.a"));
715 }
719716 }
720717 }
721718