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 {...@@ -1307,9 +1307,6 @@ pub const Target = struct {
1307 }1307 }
13081308
1309 pub fn libPrefix_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {1309 pub fn libPrefix_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {
1310 if (cpu_arch.isWasm()) {
1311 return "";
1312 }
1313 switch (abi) {1310 switch (abi) {
1314 .msvc => return "",1311 .msvc => return "",
1315 else => return "lib",1312 else => return "lib",
lib/std/zig.zig+10-1
...@@ -160,8 +160,17 @@ pub fn binNameAlloc(allocator: *std.mem.Allocator, options: BinNameOptions) erro...@@ -160,8 +160,17 @@ pub fn binNameAlloc(allocator: *std.mem.Allocator, options: BinNameOptions) erro
160 },160 },
161 .wasm => switch (options.output_mode) {161 .wasm => switch (options.output_mode) {
162 .Exe => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, target.exeFileExt() }),162 .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 },
163 .Obj => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, target.oFileExt() }),173 .Obj => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, target.oFileExt() }),
164 .Lib => return std.fmt.allocPrint(allocator, "{s}.wasm", .{root_name}),
165 },174 },
166 .c => return std.fmt.allocPrint(allocator, "{s}.c", .{root_name}),175 .c => return std.fmt.allocPrint(allocator, "{s}.c", .{root_name}),
167 .spirv => return std.fmt.allocPrint(allocator, "{s}.spv", .{root_name}),176 .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...@@ -3262,7 +3262,8 @@ fn detectLibCFromLibCInstallation(arena: *Allocator, target: Target, lci: *const
3262pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []const u8) ![]const u8 {3262pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []const u8) ![]const u8 {
3263 if (comp.wantBuildGLibCFromSource() or3263 if (comp.wantBuildGLibCFromSource() or
3264 comp.wantBuildMuslFromSource() or3264 comp.wantBuildMuslFromSource() or
3265 comp.wantBuildMinGWFromSource())3265 comp.wantBuildMinGWFromSource() or
3266 comp.wantBuildWASILibcSysrootFromSource())
3266 {3267 {
3267 return comp.crt_files.get(basename).?.full_object_path;3268 return comp.crt_files.get(basename).?.full_object_path;
3268 }3269 }
src/link.zig+1-3
...@@ -412,9 +412,7 @@ pub const File = struct {...@@ -412,9 +412,7 @@ pub const File = struct {
412 return;412 return;
413 }413 }
414 const use_lld = build_options.have_llvm and base.options.use_lld;414 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 and415 if (use_lld and base.options.output_mode == .Lib and base.options.link_mode == .Static) {
416 !base.options.target.isWasm())
417 {
418 return base.linkAsArchive(comp);416 return base.linkAsArchive(comp);
419 }417 }
420 switch (base.tag) {418 switch (base.tag) {
src/link/Wasm.zig+13
...@@ -573,6 +573,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -573,6 +573,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
573 null;573 null;
574574
575 const target = self.base.options.target;575 const target = self.base.options.target;
576 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
576577
577 const id_symlink_basename = "lld.id";578 const id_symlink_basename = "lld.id";
578579
...@@ -695,6 +696,18 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -695,6 +696,18 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
695 full_out_path,696 full_out_path,
696 });697 });
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
698 // Positional arguments to the linker such as object files.711 // Positional arguments to the linker such as object files.
699 try argv.appendSlice(self.base.options.objects);712 try argv.appendSlice(self.base.options.objects);
700713
src/wasi_libc.zig+5
...@@ -17,6 +17,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {...@@ -17,6 +17,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
17 const arena = &arena_allocator.allocator;17 const arena = &arena_allocator.allocator;
1818
19 {19 {
20 // Compile crt sources.
20 var args = std.ArrayList([]const u8).init(arena);21 var args = std.ArrayList([]const u8).init(arena);
21 try addCCArgs(comp, arena, &args, false);22 try addCCArgs(comp, arena, &args, false);
22 try args.appendSlice(&[_][]const u8{23 try args.appendSlice(&[_][]const u8{
...@@ -62,9 +63,11 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {...@@ -62,9 +63,11 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
62 }63 }
6364
64 {65 {
66 // Compile WASI libc (sysroot).
65 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);67 var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
6668
67 {69 {
70 // Compile dlmalloc.
68 var args = std.ArrayList([]const u8).init(arena);71 var args = std.ArrayList([]const u8).init(arena);
69 try addCCArgs(comp, arena, &args, true);72 try addCCArgs(comp, arena, &args, true);
70 try args.appendSlice(&[_][]const u8{73 try args.appendSlice(&[_][]const u8{
...@@ -88,6 +91,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {...@@ -88,6 +91,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
88 }91 }
8992
90 {93 {
94 // Compile libc-bottom-half.
91 var args = std.ArrayList([]const u8).init(arena);95 var args = std.ArrayList([]const u8).init(arena);
92 try addCCArgs(comp, arena, &args, true);96 try addCCArgs(comp, arena, &args, true);
93 try args.appendSlice(&[_][]const u8{97 try args.appendSlice(&[_][]const u8{
...@@ -131,6 +135,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {...@@ -131,6 +135,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
131 }135 }
132136
133 {137 {
138 // Compile libc-top-half.
134 var args = std.ArrayList([]const u8).init(arena);139 var args = std.ArrayList([]const u8).init(arena);
135 try addCCArgs(comp, arena, &args, true);140 try addCCArgs(comp, arena, &args, true);
136 try args.appendSlice(&[_][]const u8{141 try args.appendSlice(&[_][]const u8{