aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2021-05-19 21:59:21 +0200
committerJakub Konka <kubkon@jakubkonka.com>2021-05-20 16:54:00 +0200
commit6d5002028a2fe71b67d8106b30c6602bc8e44121 (patch)
tree7f8bce9ad733d2ec5a5e472df13e02b001c96fdb /src
parentf102a5800c90dfec5a4f619dca94af1b7d7e9a62 (diff)
downloadzig-6d5002028a2fe71b67d8106b30c6602bc8e44121.tar.gz
zig-6d5002028a2fe71b67d8106b30c6602bc8e44121.zip
cc,wasi: link compiled WASI libc with wasm-ld
Diffstat (limited to 'src')
-rw-r--r--src/Compilation.zig3
-rw-r--r--src/link.zig4
-rw-r--r--src/link/Wasm.zig13
-rw-r--r--src/wasi_libc.zig5
4 files changed, 21 insertions, 4 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig
index 5c856b6217..1d1f2f01b8 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -3262,7 +3262,8 @@ fn detectLibCFromLibCInstallation(arena: *Allocator, target: Target, lci: *const
pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []const u8) ![]const u8 {
if (comp.wantBuildGLibCFromSource() or
comp.wantBuildMuslFromSource() or
- comp.wantBuildMinGWFromSource())
+ comp.wantBuildMinGWFromSource() or
+ comp.wantBuildWASILibcSysrootFromSource())
{
return comp.crt_files.get(basename).?.full_object_path;
}
diff --git a/src/link.zig b/src/link.zig
index 33b73a3360..7338593a0e 100644
--- a/src/link.zig
+++ b/src/link.zig
@@ -412,9 +412,7 @@ pub const File = struct {
return;
}
const use_lld = build_options.have_llvm and base.options.use_lld;
- if (use_lld and base.options.output_mode == .Lib and base.options.link_mode == .Static and
- !base.options.target.isWasm())
- {
+ if (use_lld and base.options.output_mode == .Lib and base.options.link_mode == .Static) {
return base.linkAsArchive(comp);
}
switch (base.tag) {
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig
index 41b08b09d6..8e296b2b6a 100644
--- a/src/link/Wasm.zig
+++ b/src/link/Wasm.zig
@@ -573,6 +573,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
null;
const target = self.base.options.target;
+ const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
const id_symlink_basename = "lld.id";
@@ -695,6 +696,18 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
full_out_path,
});
+ if (link_in_crt) {
+ // TODO work out if we want standard crt, a reactor or a command
+ try argv.append(try comp.get_libc_crt_file(arena, "crt.o.wasm"));
+ }
+
+ if (!is_obj and self.base.options.link_libc) {
+ try argv.append(try comp.get_libc_crt_file(arena, switch (self.base.options.link_mode) {
+ .Static => "libc.a",
+ .Dynamic => unreachable,
+ }));
+ }
+
// Positional arguments to the linker such as object files.
try argv.appendSlice(self.base.options.objects);
diff --git a/src/wasi_libc.zig b/src/wasi_libc.zig
index 5b7738204b..e2d3652928 100644
--- a/src/wasi_libc.zig
+++ b/src/wasi_libc.zig
@@ -17,6 +17,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
const arena = &arena_allocator.allocator;
{
+ // Compile crt sources.
var args = std.ArrayList([]const u8).init(arena);
try addCCArgs(comp, arena, &args, false);
try args.appendSlice(&[_][]const u8{
@@ -62,9 +63,11 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
}
{
+ // Compile WASI libc (sysroot).
var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
{
+ // Compile dlmalloc.
var args = std.ArrayList([]const u8).init(arena);
try addCCArgs(comp, arena, &args, true);
try args.appendSlice(&[_][]const u8{
@@ -88,6 +91,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
}
{
+ // Compile libc-bottom-half.
var args = std.ArrayList([]const u8).init(arena);
try addCCArgs(comp, arena, &args, true);
try args.appendSlice(&[_][]const u8{
@@ -131,6 +135,7 @@ pub fn buildWASILibcSysroot(comp: *Compilation) !void {
}
{
+ // Compile libc-top-half.
var args = std.ArrayList([]const u8).init(arena);
try addCCArgs(comp, arena, &args, true);
try args.appendSlice(&[_][]const u8{