authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-21 20:24:13+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-21 20:24:13+02:00
logfbd96907c9f53f01c211e6e3bbf15e53aaeb79f0
tree1ec06e6689a76031e6a39ee330c04bfc031809fc
parent4b69bd61e41f1a49bb0b00ac00a7e499ab7974a7

wasm: build static archive unless -dynamic specified

This matches the behaviour for other targets in that ``` zig build-lib math.zig -target wasm32-freestanding ``` produces now `libmath.a` while ``` zig build-lib math.zig -dynamic -target wasm32-freestanding ``` is required to create a loadable Wasm module.

3 files changed, 20 insertions(+), 8 deletions(-)

doc/docgen.zig+17
...@@ -284,6 +284,7 @@ const Code = struct {...@@ -284,6 +284,7 @@ const Code = struct {
284 link_objects: []const []const u8,284 link_objects: []const []const u8,
285 target_str: ?[]const u8,285 target_str: ?[]const u8,
286 link_libc: bool,286 link_libc: bool,
287 link_mode: ?std.builtin.LinkMode,
287 disable_cache: bool,288 disable_cache: bool,
288289
289 const Id = union(enum) {290 const Id = union(enum) {
...@@ -533,6 +534,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {...@@ -533,6 +534,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
533 defer link_objects.deinit();534 defer link_objects.deinit();
534 var target_str: ?[]const u8 = null;535 var target_str: ?[]const u8 = null;
535 var link_libc = false;536 var link_libc = false;
537 var link_mode: ?std.builtin.LinkMode = null;
536 var disable_cache = false;538 var disable_cache = false;
537539
538 const source_token = while (true) {540 const source_token = while (true) {
...@@ -562,6 +564,8 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {...@@ -562,6 +564,8 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
562 target_str = "wasm32-wasi";564 target_str = "wasm32-wasi";
563 } else if (mem.eql(u8, end_tag_name, "link_libc")) {565 } else if (mem.eql(u8, end_tag_name, "link_libc")) {
564 link_libc = true;566 link_libc = true;
567 } else if (mem.eql(u8, end_tag_name, "link_mode_dynamic")) {
568 link_mode = .Dynamic;
565 } else if (mem.eql(u8, end_tag_name, "code_end")) {569 } else if (mem.eql(u8, end_tag_name, "code_end")) {
566 _ = try eatToken(tokenizer, Token.Id.BracketClose);570 _ = try eatToken(tokenizer, Token.Id.BracketClose);
567 break content_tok;571 break content_tok;
...@@ -585,6 +589,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {...@@ -585,6 +589,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
585 .link_objects = link_objects.toOwnedSlice(),589 .link_objects = link_objects.toOwnedSlice(),
586 .target_str = target_str,590 .target_str = target_str,
587 .link_libc = link_libc,591 .link_libc = link_libc,
592 .link_mode = link_mode,
588 .disable_cache = disable_cache,593 .disable_cache = disable_cache,
589 },594 },
590 });595 });
...@@ -1468,6 +1473,18 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any...@@ -1468,6 +1473,18 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
1468 try test_args.appendSlice(&[_][]const u8{ "-target", triple });1473 try test_args.appendSlice(&[_][]const u8{ "-target", triple });
1469 try out.print(" -target {s}", .{triple});1474 try out.print(" -target {s}", .{triple});
1470 }1475 }
1476 if (code.link_mode) |link_mode| {
1477 switch (link_mode) {
1478 .Static => {
1479 try test_args.append("-static");
1480 try out.print(" -static", .{});
1481 },
1482 .Dynamic => {
1483 try test_args.append("-dynamic");
1484 try out.print(" -dynamic", .{});
1485 },
1486 }
1487 }
1471 const result = exec(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});1488 const result = exec(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
1472 const escaped_stderr = try escapeHtml(allocator, result.stderr);1489 const escaped_stderr = try escapeHtml(allocator, result.stderr);
1473 const escaped_stdout = try escapeHtml(allocator, result.stdout);1490 const escaped_stdout = try escapeHtml(allocator, result.stdout);
doc/langref.html.in+3-2
...@@ -10036,10 +10036,11 @@ all your base are belong to us</code></pre>...@@ -10036,10 +10036,11 @@ all your base are belong to us</code></pre>
10036 {#header_open|WebAssembly#}10036 {#header_open|WebAssembly#}
10037 <p>Zig supports building for WebAssembly out of the box.</p>10037 <p>Zig supports building for WebAssembly out of the box.</p>
10038 {#header_open|Freestanding#}10038 {#header_open|Freestanding#}
10039 <p>For host environments like the web browser and nodejs, build as a library using the freestanding OS target.10039 <p>For host environments like the web browser and nodejs, build as a dynamic library using the freestanding
10040 Here's an example of running Zig code compiled to WebAssembly with nodejs.</p>10040 OS target. Here's an example of running Zig code compiled to WebAssembly with nodejs.</p>
10041 {#code_begin|lib|math#}10041 {#code_begin|lib|math#}
10042 {#target_wasm#}10042 {#target_wasm#}
10043 {#link_mode_dynamic#}
10043extern fn print(i32) void;10044extern fn print(i32) void;
1004410045
10045export fn add(a: i32, b: i32) void {10046export fn add(a: i32, b: i32) void {
src/main.zig-6
...@@ -1548,12 +1548,6 @@ fn buildOutputType(...@@ -1548,12 +1548,6 @@ fn buildOutputType(
1548 link_libcpp = true;1548 link_libcpp = true;
1549 }1549 }
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
1557 // Now that we have target info, we can find out if any of the system libraries1551 // Now that we have target info, we can find out if any of the system libraries
1558 // are part of libc or libc++. We remove them from the list and communicate their1552 // are part of libc or libc++. We remove them from the list and communicate their
1559 // existence via flags instead.1553 // existence via flags instead.