Commit 7fe2a3d104

Luuk de Gram <luuk@degram.dev>
2022-08-20 15:46:39
test/link: add wasm linker-test for archives
Adds a test case that will pull-in compiler-rt symbols, and therefore link with the compiler-rt archive file.
1 parent 1544625
Changed files (4)
lib
test
link
wasm
lib/std/build.zig
@@ -1898,12 +1898,7 @@ pub const LibExeObjStep = struct {
     pub fn runEmulatable(exe: *LibExeObjStep) *EmulatableRunStep {
         assert(exe.kind == .exe or exe.kind == .test_exe);
 
-        const run_step = EmulatableRunStep.create(exe.builder.fmt("run {s}", .{exe.step.name}), exe);
-        if (exe.vcpkg_bin_path) |path| {
-            run_step.addPathDir(path);
-        }
-
-        return run_step;
+        return EmulatableRunStep.create(exe.builder, exe.builder.fmt("run {s}", .{exe.step.name}), exe);
     }
 
     pub fn checkObject(self: *LibExeObjStep, obj_format: std.Target.ObjectFormat) *CheckObjectStep {
test/link/wasm/archive/build.zig
@@ -0,0 +1,27 @@
+const std = @import("std");
+const Builder = std.build.Builder;
+
+pub fn build(b: *Builder) void {
+    const mode = b.standardReleaseOptions();
+
+    const test_step = b.step("test", "Test");
+    test_step.dependOn(b.getInstallStep());
+
+    // The code in question will pull-in compiler-rt,
+    // and therefore link with its archive file.
+    const lib = b.addSharedLibrary("main", "main.zig", .unversioned);
+    lib.setBuildMode(mode);
+    lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
+    lib.use_llvm = false;
+    lib.use_stage1 = false;
+    lib.use_lld = false;
+
+    const check = lib.checkObject(.wasm);
+    check.checkStart("Section import");
+    check.checkNext("entries 1"); // __truncsfhf2 should have been resolved, so only 1 import (compiler-rt's memcpy).
+
+    check.checkStart("Section custom");
+    check.checkNext("name __truncsfhf2"); // Ensure it was imported and resolved
+
+    test_step.dependOn(&check.step);
+}
test/link/wasm/archive/main.zig
@@ -0,0 +1,6 @@
+export fn foo() void {
+    var a: f16 = 2.2;
+    // this will pull-in compiler-rt
+    var b = @trunc(a);
+    _ = b;
+}
test/link.zig
@@ -47,6 +47,11 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
         .build_modes = true,
         .requires_stage2 = true,
     });
+
+    cases.addBuildFile("test/link/wasm/archive/build.zig", .{
+        .build_modes = true,
+        .requires_stage2 = true,
+    });
 }
 
 fn addMachOCases(cases: *tests.StandaloneContext) void {