authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-22 16:13:47+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-22 16:13:47+02:00
log9d311e9960952838edb19a422e5023c670f9995d
treec3539c2eb0a5bfcef528ed9d7197ea3efb8d6c02
parent2b0d322ea003907dc2111864cea259e5e0043328
parentbd325d1bd94361024f755c8bac3011a5cdffe573
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8857 from ziglang/cc-wasm-ar

wasm: produce static lib by default and rename produced object files to simply .o from .o.wasm

5 files changed, 21 insertions(+), 15 deletions(-)

doc/docgen.zig+17
......@@ -284,6 +284,7 @@ const Code = struct {
284284 link_objects: []const []const u8,
285285 target_str: ?[]const u8,
286286 link_libc: bool,
287 link_mode: ?std.builtin.LinkMode,
287288 disable_cache: bool,
288289
289290 const Id = union(enum) {
......@@ -533,6 +534,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
533534 defer link_objects.deinit();
534535 var target_str: ?[]const u8 = null;
535536 var link_libc = false;
537 var link_mode: ?std.builtin.LinkMode = null;
536538 var disable_cache = false;
537539
538540 const source_token = while (true) {
......@@ -562,6 +564,8 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
562564 target_str = "wasm32-wasi";
563565 } else if (mem.eql(u8, end_tag_name, "link_libc")) {
564566 link_libc = true;
567 } else if (mem.eql(u8, end_tag_name, "link_mode_dynamic")) {
568 link_mode = .Dynamic;
565569 } else if (mem.eql(u8, end_tag_name, "code_end")) {
566570 _ = try eatToken(tokenizer, Token.Id.BracketClose);
567571 break content_tok;
......@@ -585,6 +589,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
585589 .link_objects = link_objects.toOwnedSlice(),
586590 .target_str = target_str,
587591 .link_libc = link_libc,
592 .link_mode = link_mode,
588593 .disable_cache = disable_cache,
589594 },
590595 });
......@@ -1468,6 +1473,18 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: any
14681473 try test_args.appendSlice(&[_][]const u8{ "-target", triple });
14691474 try out.print(" -target {s}", .{triple});
14701475 }
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 }
14711488 const result = exec(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
14721489 const escaped_stderr = try escapeHtml(allocator, result.stderr);
14731490 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>
1003610036 {#header_open|WebAssembly#}
1003710037 <p>Zig supports building for WebAssembly out of the box.</p>
1003810038 {#header_open|Freestanding#}
10039 <p>For host environments like the web browser and nodejs, build as a library using the freestanding OS target.
10040 Here's an example of running Zig code compiled to WebAssembly with nodejs.</p>
10039 <p>For host environments like the web browser and nodejs, build as a dynamic library using the freestanding
10040 OS target. Here's an example of running Zig code compiled to WebAssembly with nodejs.</p>
1004110041 {#code_begin|lib|math#}
1004210042 {#target_wasm#}
10043 {#link_mode_dynamic#}
1004310044extern fn print(i32) void;
1004410045
1004510046export fn add(a: i32, b: i32) void {
lib/std/target.zig-6
......@@ -1259,9 +1259,6 @@ pub const Target = struct {
12591259 }
12601260
12611261 pub fn oFileExt_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {
1262 if (cpu_arch.isWasm()) {
1263 return ".o.wasm";
1264 }
12651262 switch (abi) {
12661263 .msvc => return ".obj",
12671264 else => return ".o",
......@@ -1289,9 +1286,6 @@ pub const Target = struct {
12891286 }
12901287
12911288 pub fn staticLibSuffix_cpu_arch_abi(cpu_arch: Cpu.Arch, abi: Abi) [:0]const u8 {
1292 if (cpu_arch.isWasm()) {
1293 return ".wasm";
1294 }
12951289 switch (abi) {
12961290 .msvc => return ".lib",
12971291 else => return ".a",
src/link/Wasm.zig+1-1
......@@ -698,7 +698,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
698698
699699 if (link_in_crt) {
700700 // 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"));
701 try argv.append(try comp.get_libc_crt_file(arena, "crt.o"));
702702 }
703703
704704 if (!is_obj and self.base.options.link_libc) {
src/main.zig-6
......@@ -1548,12 +1548,6 @@ 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
15571551 // Now that we have target info, we can find out if any of the system libraries
15581552 // are part of libc or libc++. We remove them from the list and communicate their
15591553 // existence via flags instead.