diff --git a/doc/docgen.zig b/doc/docgen.zig index 8dd299a6c5..7971d15796 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -284,6 +284,7 @@ const Code = struct { link_objects: []const []const u8, target_str: ?[]const u8, link_libc: bool, + link_mode: ?std.builtin.LinkMode, disable_cache: bool, const Id = union(enum) { @@ -533,6 +534,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc { defer link_objects.deinit(); var target_str: ?[]const u8 = null; var link_libc = false; + var link_mode: ?std.builtin.LinkMode = null; var disable_cache = false; const source_token = while (true) { @@ -562,6 +564,8 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc { target_str = "wasm32-wasi"; } else if (mem.eql(u8, end_tag_name, "link_libc")) { link_libc = true; + } else if (mem.eql(u8, end_tag_name, "link_mode_dynamic")) { + link_mode = .Dynamic; } else if (mem.eql(u8, end_tag_name, "code_end")) { _ = try eatToken(tokenizer, Token.Id.BracketClose); break content_tok; @@ -585,6 +589,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc { .link_objects = link_objects.toOwnedSlice(), .target_str = target_str, .link_libc = link_libc, + .link_mode = link_mode, .disable_cache = disable_cache, }, }); @@ -1468,6 +1473,18 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any try test_args.appendSlice(&[_][]const u8{ "-target", triple }); try out.print(" -target {s}", .{triple}); } + if (code.link_mode) |link_mode| { + switch (link_mode) { + .Static => { + try test_args.append("-static"); + try out.print(" -static", .{}); + }, + .Dynamic => { + try test_args.append("-dynamic"); + try out.print(" -dynamic", .{}); + }, + } + } const result = exec(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{}); const escaped_stderr = try escapeHtml(allocator, result.stderr); const escaped_stdout = try escapeHtml(allocator, result.stdout); diff --git a/doc/langref.html.in b/doc/langref.html.in index d67f97f4c6..8ebe7fb594 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -10036,10 +10036,11 @@ all your base are belong to us {#header_open|WebAssembly#}
Zig supports building for WebAssembly out of the box.
{#header_open|Freestanding#} -For host environments like the web browser and nodejs, build as a library using the freestanding OS target. - Here's an example of running Zig code compiled to WebAssembly with nodejs.
+For host environments like the web browser and nodejs, build as a dynamic library using the freestanding + OS target. Here's an example of running Zig code compiled to WebAssembly with nodejs.
{#code_begin|lib|math#} {#target_wasm#} + {#link_mode_dynamic#} extern fn print(i32) void; export fn add(a: i32, b: i32) void { diff --git a/src/main.zig b/src/main.zig index 86d017b3b7..b388329508 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1548,12 +1548,6 @@ fn buildOutputType( link_libcpp = true; } - if (cross_target.getCpuArch().isWasm() and output_mode == .Lib and link_mode == null) { - // If link_mode is unspecified, always link as dynamic library when targeting Wasm, - // so that wasm-ld is invoked rather than standard archiver. - link_mode = .Dynamic; - } - // Now that we have target info, we can find out if any of the system libraries // are part of libc or libc++. We remove them from the list and communicate their // existence via flags instead.