Commit 2f41109cc4

Luuk de Gram <luuk@degram.dev>
2022-10-23 20:00:27
test/link: add Wasm linker tests for features
Adds a test for inferring features based on a different object file. Also provides a test case where cpu features are explicitly set on a library where the end result will output said target features.
1 parent b14f605
Changed files (6)
test
link
wasm
basic-features
infer-features
test/link/wasm/basic-features/build.zig
@@ -0,0 +1,23 @@
+const std = @import("std");
+
+pub fn build(b: *std.build.Builder) void {
+    const mode = b.standardReleaseOptions();
+
+    // Library with explicitly set cpu features
+    const lib = b.addSharedLibrary("lib", "main.zig", .unversioned);
+    lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
+    lib.target.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp };
+    lib.target.cpu_features_add.addFeature(0); // index 0 == atomics (see std.Target.wasm.Features)
+    lib.setBuildMode(mode);
+    lib.use_llvm = false;
+    lib.use_lld = false;
+
+    // Verify the result contains the features explicitly set on the target for the library.
+    const check = lib.checkObject(.wasm);
+    check.checkStart("name target_features");
+    check.checkNext("features 1");
+    check.checkNext("+ atomics");
+
+    const test_step = b.step("test", "Run linker test");
+    test_step.dependOn(&check.step);
+}
test/link/wasm/basic-features/main.zig
@@ -0,0 +1,1 @@
+export fn foo() void {}
test/link/wasm/infer-features/build.zig
@@ -0,0 +1,37 @@
+const std = @import("std");
+
+pub fn build(b: *std.build.Builder) void {
+    const mode = b.standardReleaseOptions();
+
+    // Wasm Object file which we will use to infer the features from
+    const c_obj = b.addObject("c_obj", null);
+    c_obj.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
+    c_obj.target.cpu_model = .{ .explicit = &std.Target.wasm.cpu.bleeding_edge };
+    c_obj.addCSourceFile("foo.c", &.{});
+    c_obj.setBuildMode(mode);
+
+    // Wasm library that doesn't have any features specified. This will
+    // infer its featureset from other linked object files.
+    const lib = b.addSharedLibrary("lib", "main.zig", .unversioned);
+    lib.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
+    lib.target.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp };
+    lib.setBuildMode(mode);
+    lib.use_llvm = false;
+    lib.use_lld = false;
+    lib.addObject(c_obj);
+
+    // Verify the result contains the features from the C Object file.
+    const check = lib.checkObject(.wasm);
+    check.checkStart("name target_features");
+    check.checkNext("features 7");
+    check.checkNext("+ atomics");
+    check.checkNext("+ bulk-memory");
+    check.checkNext("+ mutable-globals");
+    check.checkNext("+ nontrapping-fptoint");
+    check.checkNext("+ sign-ext");
+    check.checkNext("+ simd128");
+    check.checkNext("+ tail-call");
+
+    const test_step = b.step("test", "Run linker test");
+    test_step.dependOn(&check.step);
+}
test/link/wasm/infer-features/foo.c
@@ -0,0 +1,3 @@
+int foo() {
+  return 5;
+}
test/link/wasm/infer-features/main.zig
@@ -0,0 +1,1 @@
+extern fn foo() c_int;
test/link.zig
@@ -33,6 +33,10 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
         .requires_stage2 = true,
     });
 
+    cases.addBuildFile("test/link/wasm/basic-features/build.zig", .{
+        .requires_stage2 = true,
+    });
+
     cases.addBuildFile("test/link/wasm/bss/build.zig", .{
         .build_modes = false,
         .requires_stage2 = true,
@@ -44,6 +48,10 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
         .use_emulation = true,
     });
 
+    cases.addBuildFile("test/link/wasm/infer-features/build.zig", .{
+        .requires_stage2 = true,
+    });
+
     cases.addBuildFile("test/link/wasm/producers/build.zig", .{
         .build_modes = true,
         .requires_stage2 = true,