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.
This commit is contained in:
Luuk de Gram 2022-10-23 20:00:27 +02:00
parent b14f605dd7
commit 2f41109cc4
No known key found for this signature in database
GPG Key ID: A8CFE58E4DC7D664
6 changed files with 73 additions and 0 deletions

View File

@ -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,

View File

@ -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);
}

View File

@ -0,0 +1 @@
export fn foo() void {}

View File

@ -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);
}

View File

@ -0,0 +1,3 @@
int foo() {
return 5;
}

View File

@ -0,0 +1 @@
extern fn foo() c_int;