authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-20 10:27:35+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-20 16:54:00+02:00
log3a5d0f7700dc28b523e17ba461874b807786b939
tree3f5c2aa584c7b652fa410cb6b9a214af300b963b
parentf291b8b9bccf223418eaeea2efc8ca87851e59eb

wasm: link dynamically by default when targeting wasm

This matches the behaviour of other languages and leaves us the ability to create actual static Wasm archives with ``` zig build-lib -static some.zig ``` which can then be combined with other Wasm object files and linked into either a Wasm lib or executable using `wasm-ld`. Update langref to reflect the fact we now ship WASI libc.

6 files changed, 20 insertions(+), 14 deletions(-)

doc/langref.html.in+2
......@@ -10280,6 +10280,7 @@ Operating Systems:
1028010280 ps4
1028110281 elfiamcu
1028210282 tvos
10283 wasi
1028310284 watchos
1028410285 mesa3d
1028510286 contiki
......@@ -10348,6 +10349,7 @@ Available libcs:
1034810349 sparc-linux-gnu
1034910350 sparcv9-linux-gnu
1035010351 wasm32-freestanding-musl
10352 wasm32-wasi-musl
1035110353 x86_64-linux-gnu
1035210354 x86_64-linux-gnux32
1035310355 x86_64-linux-musl</code></pre>
lib/std/zig.zig+1-3
......@@ -165,9 +165,7 @@ pub fn binNameAlloc(allocator: *std.mem.Allocator, options: BinNameOptions) erro
165165 .Static => return std.fmt.allocPrint(allocator, "{s}{s}.a", .{
166166 target.libPrefix(), root_name,
167167 }),
168 .Dynamic => return std.fmt.allocPrint(allocator, "{s}{s}.wasm", .{
169 target.libPrefix(), root_name,
170 }),
168 .Dynamic => return std.fmt.allocPrint(allocator, "{s}.wasm", .{root_name}),
171169 }
172170 },
173171 .Obj => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, target.oFileExt() }),
src/Compilation.zig+9-10
......@@ -770,8 +770,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
770770 .Lib => is_dyn_lib,
771771 .Exe => true,
772772 };
773 const needs_c_symbols = !options.skip_linker_dependencies and
774 (is_exe_or_dyn_lib or (options.target.isWasm() and options.output_mode != .Obj));
773
774 const needs_c_symbols = !options.skip_linker_dependencies and is_exe_or_dyn_lib;
775775
776776 const comp: *Compilation = comp: {
777777 // For allocations that have the same lifetime as Compilation. This arena is used only during this
......@@ -1418,7 +1418,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14181418 },
14191419 });
14201420 }
1421 if (comp.wantBuildWASILibcSysrootFromSource()) {
1421 if (comp.wantBuildWasiLibcSysrootFromSource()) {
14221422 try comp.work_queue.write(&[_]Job{.{ .wasi_libc_sysroot = {} }});
14231423 }
14241424 if (comp.wantBuildMinGWFromSource()) {
......@@ -1462,7 +1462,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14621462 // Once it is capable this condition should be removed.
14631463 if (build_options.is_stage1) {
14641464 if (comp.bin_file.options.include_compiler_rt) {
1465 if (is_exe_or_dyn_lib or comp.getTarget().isWasm()) {
1465 if (is_exe_or_dyn_lib) {
14661466 try comp.work_queue.writeItem(.{ .compiler_rt_lib = {} });
14671467 } else {
14681468 try comp.work_queue.writeItem(.{ .compiler_rt_obj = {} });
......@@ -2147,7 +2147,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
21472147 };
21482148 },
21492149 .wasi_libc_sysroot => {
2150 wasi_libc.buildWASILibcSysroot(self) catch |err| {
2150 wasi_libc.buildWasiLibcSysroot(self) catch |err| {
21512151 // TODO Surface more error details.
21522152 try self.setMiscFailure(
21532153 .wasi_libc_sysroot,
......@@ -3263,7 +3263,7 @@ pub fn get_libc_crt_file(comp: *Compilation, arena: *Allocator, basename: []cons
32633263 if (comp.wantBuildGLibCFromSource() or
32643264 comp.wantBuildMuslFromSource() or
32653265 comp.wantBuildMinGWFromSource() or
3266 comp.wantBuildWASILibcSysrootFromSource())
3266 comp.wantBuildWasiLibcSysrootFromSource())
32673267 {
32683268 return comp.crt_files.get(basename).?.full_object_path;
32693269 }
......@@ -3303,7 +3303,7 @@ fn wantBuildMuslFromSource(comp: Compilation) bool {
33033303 !comp.getTarget().isWasm();
33043304}
33053305
3306fn wantBuildWASILibcSysrootFromSource(comp: Compilation) bool {
3306fn wantBuildWasiLibcSysrootFromSource(comp: Compilation) bool {
33073307 return comp.wantBuildLibCFromSource() and comp.getTarget().isWasm();
33083308}
33093309
......@@ -3610,11 +3610,10 @@ fn buildOutputFromZig(
36103610 };
36113611 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
36123612 const target = comp.getTarget();
3613 const fixed_output_mode = if (target.cpu.arch.isWasm()) .Obj else output_mode;
36143613 const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{
36153614 .root_name = root_name,
36163615 .target = target,
3617 .output_mode = fixed_output_mode,
3616 .output_mode = output_mode,
36183617 });
36193618 defer comp.gpa.free(bin_basename);
36203619
......@@ -3629,7 +3628,7 @@ fn buildOutputFromZig(
36293628 .target = target,
36303629 .root_name = root_name,
36313630 .root_pkg = &root_pkg,
3632 .output_mode = fixed_output_mode,
3631 .output_mode = output_mode,
36333632 .thread_pool = comp.thread_pool,
36343633 .libc_installation = comp.bin_file.options.libc_installation,
36353634 .emit_bin = emit_bin,
src/main.zig+6
......@@ -1548,6 +1548,12 @@ fn buildOutputType(
15481548 link_libcpp = true;
15491549 }
15501550
1551 if (cross_target.getCpuArch().isWasm() and output_mode == .Lib and link_mode == null) {
1552 // If link_mode is unspecified, always link as dynamic library when targeting Wasm,
1553 // so that wasm-ld is invoked rather than standard archiver.
1554 link_mode = .Dynamic;
1555 }
1556
15511557 // Now that we have target info, we can find out if any of the system libraries
15521558 // are part of libc or libc++. We remove them from the list and communicate their
15531559 // existence via flags instead.
src/target.zig+1
......@@ -146,6 +146,7 @@ pub fn libcNeedsLibUnwind(target: std.Target) bool {
146146 .watchos,
147147 .tvos,
148148 .freestanding,
149 .wasi, // Wasm/WASI currently doesn't offer support for libunwind, so don't link it.
149150 => false,
150151
151152 .windows => target.abi != .msvc,
src/wasi_libc.zig+1-1
......@@ -6,7 +6,7 @@ const Compilation = @import("Compilation.zig");
66const build_options = @import("build_options");
77const target_util = @import("target.zig");
88
9pub fn buildWASILibcSysroot(comp: *Compilation) !void {
9pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
1010 if (!build_options.have_llvm) {
1111 return error.ZigCompilerNotBuiltWithLLVMExtensions;
1212 }