Commit 8fa91939a8

Veikka Tuominen <git@vexu.eu>
2022-10-21 22:01:49
build.zig: separate C ABI tests from standalone tests
1 parent 12a2ccf
test/c_abi/build.zig
@@ -1,22 +0,0 @@
-const Builder = @import("std").build.Builder;
-
-pub fn build(b: *Builder) void {
-    const rel_opts = b.standardReleaseOptions();
-    const target = b.standardTargetOptions(.{});
-
-    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.target = target;
-
-    const main = b.addTest("main.zig");
-    main.setBuildMode(rel_opts);
-    main.addObject(c_obj);
-    main.target = target;
-
-    const test_step = b.step("test", "Test the program");
-    test_step.dependOn(&main.step);
-
-    b.default_step.dependOn(test_step);
-}
test/c_abi/build_wasm.zig
@@ -1,24 +0,0 @@
-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
@@ -44,23 +44,6 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
     if (builtin.os.tag != .wasi) {
         cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{});
     }
-    // C ABI compatibility issue: https://github.com/ziglang/zig/issues/1481
-    if (builtin.cpu.arch == .x86_64) {
-        if (builtin.zig_backend == .stage1 or builtin.zig_backend == .stage2_llvm) {
-            cases.addBuildFile("test/c_abi/build.zig", .{});
-        }
-    }
-    if (builtin.cpu.arch.isAARCH64() and builtin.zig_backend == .stage2_llvm) {
-        cases.addBuildFile("test/c_abi/build.zig", .{});
-    }
-    if (builtin.cpu.arch == .i386 and builtin.zig_backend == .stage2_llvm) {
-        cases.addBuildFile("test/c_abi/build.zig", .{});
-    }
-    // C ABI tests only pass for the Wasm target when using stage2
-    cases.addBuildFile("test/c_abi/build_wasm.zig", .{
-        .requires_stage2 = true,
-        .use_emulation = true,
-    });
 
     cases.addBuildFile("test/standalone/c_compiler/build.zig", .{
         .build_modes = true,
test/tests.zig
@@ -1268,3 +1268,82 @@ fn printInvocation(args: []const []const u8) void {
     }
     std.debug.print("\n", .{});
 }
+
+const c_abi_targets = [_]CrossTarget{
+    .{},
+    .{
+        .cpu_arch = .x86_64,
+        .os_tag = .linux,
+        .abi = .musl,
+    },
+    .{
+        .cpu_arch = .i386,
+        .os_tag = .linux,
+        .abi = .musl,
+    },
+    .{
+        .cpu_arch = .aarch64,
+        .os_tag = .linux,
+        .abi = .musl,
+    },
+    .{
+        .cpu_arch = .arm,
+        .os_tag = .linux,
+        .abi = .musleabihf,
+    },
+    .{
+        .cpu_arch = .mips,
+        .os_tag = .linux,
+        .abi = .musl,
+    },
+    .{
+        .cpu_arch = .riscv64,
+        .os_tag = .linux,
+        .abi = .musl,
+    },
+    .{
+        .cpu_arch = .wasm32,
+        .os_tag = .wasi,
+        .abi = .musl,
+    },
+    .{
+        .cpu_arch = .powerpc,
+        .os_tag = .linux,
+        .abi = .musl,
+    },
+    .{
+        .cpu_arch = .powerpc64le,
+        .os_tag = .linux,
+        .abi = .none,
+    },
+};
+
+pub fn addCAbiTests(b: *build.Builder, skip_non_native: bool) *build.Step {
+    const step = b.step("test-c-abi", "Run the C ABI tests");
+
+    for (c_abi_targets) |c_abi_target| {
+        if (skip_non_native and !c_abi_target.isNative())
+            continue;
+
+        const test_step = b.addTest("test/c_abi/main.zig");
+        test_step.setTarget(c_abi_target);
+        if (c_abi_target.abi != null and c_abi_target.abi.?.isMusl()) {
+            // TODO NativeTargetInfo insists on dynamically linking musl
+            // for some reason?
+            test_step.target_info.dynamic_linker.max_byte = null;
+        }
+        test_step.linkLibC();
+        test_step.addCSourceFile("test/c_abi/cfuncs.c", &.{"-std=c99"});
+
+        const triple_prefix = c_abi_target.zigTriple(b.allocator) catch unreachable;
+        test_step.setNamePrefix(b.fmt("{s}-{s} ", .{
+            "test-c-abi",
+            triple_prefix,
+        }));
+
+        test_step.use_stage1 = false;
+
+        step.dependOn(&test_step.step);
+    }
+    return step;
+}
build.zig
@@ -508,6 +508,7 @@ pub fn build(b: *Builder) !void {
         b.enable_wasmtime,
         b.enable_wine,
     ));
+    test_step.dependOn(tests.addCAbiTests(b, skip_non_native));
     test_step.dependOn(tests.addLinkTests(b, test_filter, modes, enable_macos_sdk, skip_stage2_tests));
     test_step.dependOn(tests.addStackTraceTests(b, test_filter, modes));
     test_step.dependOn(tests.addCliTests(b, test_filter, modes));