Commit a7417f7839

Luuk de Gram <luuk@degram.dev>
2022-07-20 21:51:18
wasm: Enable C-ABI tests for self-hosted compiler
1 parent 6b4d4c7
Changed files (4)
test/stage1/c_abi/build_wasm.zig
@@ -0,0 +1,24 @@
+const std = @import("std");
+const Builder = std.build.Builder;
+
+pub fn build(b: *Builder) void {
+    const rel_opts = b.standardReleaseOptions();
+    const target: std.zig.CrossTarget = .{ .cpu_arch = .wasm32, .os_tag = .wasi };
+    b.use_stage1 = false;
+
+    const c_obj = b.addObject("cfuncs", null);
+    c_obj.addCSourceFile("cfuncs.c", &[_][]const u8{"-std=c99"});
+    c_obj.setBuildMode(rel_opts);
+    c_obj.linkSystemLibrary("c");
+    c_obj.setTarget(target);
+
+    const main = b.addTest("main.zig");
+    main.setBuildMode(rel_opts);
+    main.addObject(c_obj);
+    main.setTarget(target);
+
+    const test_step = b.step("test", "Test the program");
+    test_step.dependOn(&main.step);
+
+    b.default_step.dependOn(test_step);
+}
test/standalone.zig
@@ -41,6 +41,12 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
     if (builtin.cpu.arch == .x86_64) {
         cases.addBuildFile("test/stage1/c_abi/build.zig", .{});
     }
+    // C ABI tests only pass for the Wasm target when using stage2
+    cases.addBuildFile("test/stage1/c_abi/build_wasm.zig", .{
+        .requires_stage2 = true,
+        .use_emulation = true,
+    });
+
     cases.addBuildFile("test/standalone/c_compiler/build.zig", .{
         .build_modes = true,
         .cross_targets = true,
test/tests.zig
@@ -463,6 +463,11 @@ pub fn addStandaloneTests(
     enable_macos_sdk: bool,
     target: std.zig.CrossTarget,
     omit_stage2: bool,
+    enable_darling: bool,
+    enable_qemu: bool,
+    enable_rosetta: bool,
+    enable_wasmtime: bool,
+    enable_wine: bool,
 ) *build.Step {
     const cases = b.allocator.create(StandaloneContext) catch unreachable;
     cases.* = StandaloneContext{
@@ -475,6 +480,11 @@ pub fn addStandaloneTests(
         .enable_macos_sdk = enable_macos_sdk,
         .target = target,
         .omit_stage2 = omit_stage2,
+        .enable_darling = enable_darling,
+        .enable_qemu = enable_qemu,
+        .enable_rosetta = enable_rosetta,
+        .enable_wasmtime = enable_wasmtime,
+        .enable_wine = enable_wine,
     };
 
     standalone.addCases(cases);
@@ -962,6 +972,11 @@ pub const StandaloneContext = struct {
     enable_macos_sdk: bool,
     target: std.zig.CrossTarget,
     omit_stage2: bool,
+    enable_darling: bool = false,
+    enable_qemu: bool = false,
+    enable_rosetta: bool = false,
+    enable_wasmtime: bool = false,
+    enable_wine: bool = false,
 
     pub fn addC(self: *StandaloneContext, root_src: []const u8) void {
         self.addAllArgs(root_src, true);
@@ -976,6 +991,7 @@ pub const StandaloneContext = struct {
         cross_targets: bool = false,
         requires_macos_sdk: bool = false,
         requires_stage2: bool = false,
+        use_emulation: bool = false,
     }) void {
         const b = self.b;
 
@@ -1007,6 +1023,24 @@ pub const StandaloneContext = struct {
             zig_args.append(target_arg) catch unreachable;
         }
 
+        if (features.use_emulation) {
+            if (self.enable_darling) {
+                zig_args.append("-fdarling") catch unreachable;
+            }
+            if (self.enable_qemu) {
+                zig_args.append("-fqemu") catch unreachable;
+            }
+            if (self.enable_rosetta) {
+                zig_args.append("-frosetta") catch unreachable;
+            }
+            if (self.enable_wasmtime) {
+                zig_args.append("-fwasmtime") catch unreachable;
+            }
+            if (self.enable_wine) {
+                zig_args.append("-fwine") catch unreachable;
+            }
+        }
+
         const modes = if (features.build_modes) self.modes else &[1]Mode{.Debug};
         for (modes) |mode| {
             const arg = switch (mode) {
build.zig
@@ -484,7 +484,20 @@ pub fn build(b: *Builder) !void {
     ));
 
     toolchain_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
-    toolchain_step.dependOn(tests.addStandaloneTests(b, test_filter, modes, skip_non_native, enable_macos_sdk, target, omit_stage2));
+    toolchain_step.dependOn(tests.addStandaloneTests(
+        b,
+        test_filter,
+        modes,
+        skip_non_native,
+        enable_macos_sdk,
+        target,
+        omit_stage2,
+        b.enable_darling,
+        b.enable_qemu,
+        b.enable_rosetta,
+        b.enable_wasmtime,
+        b.enable_wine,
+    ));
     toolchain_step.dependOn(tests.addLinkTests(b, test_filter, modes, enable_macos_sdk, omit_stage2));
     toolchain_step.dependOn(tests.addStackTraceTests(b, test_filter, modes));
     toolchain_step.dependOn(tests.addCliTests(b, test_filter, modes));