authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-19 21:59:21+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-20 16:54:00+02:00
log6d5002028a2fe71b67d8106b30c6602bc8e44121
tree7f8bce9ad733d2ec5a5e472df13e02b001c96fdb
parentf102a5800c90dfec5a4f619dca94af1b7d7e9a62

cc,wasi: link compiled WASI libc with wasm-ld


6 files changed, 31 insertions(+), 8 deletions(-)

lib/std/target.zig-3
......@@ -1307,9 +1307,6 @@ pub const Target = struct {
13071307 }
13081308
13091309 pub fn libPrefix_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {
1310 if (cpu_arch.isWasm()) {
1311 return "";
1312 }
13131310 switch (abi) {
13141311 .msvc => return "",
13151312 else => return "lib",
lib/std/zig.zig+10-1
......@@ -160,8 +160,17 @@ pub fn binNameAlloc(allocator: *std.mem.Allocator, options: BinNameOptions) erro
160160 },
161161 .wasm => switch (options.output_mode) {
162162 .Exe => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, target.exeFileExt() }),
163 .Lib => {
164 switch (options.link_mode orelse .Static) {
165 .Static => return std.fmt.allocPrint(allocator, "{s}{s}.a", .{
166 target.libPrefix(), root_name,
167 }),
168 .Dynamic => return std.fmt.allocPrint(allocator, "{s}{s}.wasm", .{
169 target.libPrefix(), root_name,
170 }),
171 }
172 },
163173 .Obj => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, target.oFileExt() }),
164 .Lib => return std.fmt.allocPrint(allocator, "{s}.wasm", .{root_name}),
165174 },
166175 .c => return std.fmt.allocPrint(allocator, "{s}.c", .{root_name}),
167176 .spirv => return std.fmt.allocPrint(allocator, "{s}.spv", .{root_name}),
src/Compilation.zig+2-1
......@@ -3262,7 +3262,8 @@ fn detectLibCFromLibCInstallation(arena: *Allocator, target: Target, lci: *const
32623262pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []const u8) ![]const u8 {
32633263 if (comp.wantBuildGLibCFromSource() or
32643264 comp.wantBuildMuslFromSource() or
3265 comp.wantBuildMinGWFromSource())
3265 comp.wantBuildMinGWFromSource() or
3266 comp.wantBuildWASILibcSysrootFromSource())
32663267 {
32673268 return comp.crt_files.get(basename).?.full_object_path;
32683269 }
src/link.zig+1-3
......@@ -412,9 +412,7 @@ pub const File = struct {
412412 return;
413413 }
414414 const use_lld = build_options.have_llvm and base.options.use_lld;
415 if (use_lld and base.options.output_mode == .Lib and base.options.link_mode == .Static and
416 !base.options.target.isWasm())
417 {
415 if (use_lld and base.options.output_mode == .Lib and base.options.link_mode == .Static) {
418416 return base.linkAsArchive(comp);
419417 }
420418 switch (base.tag) {
src/link/Wasm.zig+13
......@@ -573,6 +573,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
573573 null;
574574
575575 const target = self.base.options.target;
576 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
576577
577578 const id_symlink_basename = "lld.id";
578579
......@@ -695,6 +696,18 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
695696 full_out_path,
696697 });
697698
699 if (link_in_crt) {
700 // TODO work out if we want standard crt, a reactor or a command
701 try argv.append(try comp.get_libc_crt_file(arena, "crt.o.wasm"));
702 }
703
704 if (!is_obj and self.base.options.link_libc) {
705 try argv.append(try comp.get_libc_crt_file(arena, switch (self.base.options.link_mode) {
706 .Static => "libc.a",
707 .Dynamic => unreachable,
708 }));
709 }
710
698711 // Positional arguments to the linker such as object files.
699712 try argv.appendSlice(self.base.options.objects);
700713
src/wasi_libc.zig+5
......@@ -17,6 +17,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
1717 const arena = &arena_allocator.allocator;
1818
1919 {
20 // Compile crt sources.
2021 var args = std.ArrayList([]const u8).init(arena);
2122 try addCCArgs(comp, arena, &args, false);
2223 try args.appendSlice(&[_][]const u8{
......@@ -62,9 +63,11 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
6263 }
6364
6465 {
66 // Compile WASI libc (sysroot).
6567 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
6668
6769 {
70 // Compile dlmalloc.
6871 var args = std.ArrayList([]const u8).init(arena);
6972 try addCCArgs(comp, arena, &args, true);
7073 try args.appendSlice(&[_][]const u8{
......@@ -88,6 +91,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
8891 }
8992
9093 {
94 // Compile libc-bottom-half.
9195 var args = std.ArrayList([]const u8).init(arena);
9296 try addCCArgs(comp, arena, &args, true);
9397 try args.appendSlice(&[_][]const u8{
......@@ -131,6 +135,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
131135 }
132136
133137 {
138 // Compile libc-top-half.
134139 var args = std.ArrayList([]const u8).init(arena);
135140 try addCCArgs(comp, arena, &args, true);
136141 try args.appendSlice(&[_][]const u8{