Commit 33b8fdb6bc

Carl Åstholm <carl@astholm.se>
2024-02-17 17:23:08
Turn "simple" standalone test cases into a package
1 parent c3ecc69
Changed files (5)
test/standalone/simple/build.zig
@@ -0,0 +1,123 @@
+const std = @import("std");
+const builtin = @import("builtin");
+
+pub fn build(b: *std.Build) void {
+    const step = b.step("test", "Run simple standalone test cases");
+    b.default_step = step;
+
+    const skip_debug = b.option(bool, "skip_debug", "Skip debug builds") orelse false;
+    const skip_release_safe = b.option(bool, "skip_release_safe", "Skip release-safe builds") orelse false;
+    const skip_release_fast = b.option(bool, "skip_release_fast", "Skip release-fast builds") orelse false;
+    const skip_release_small = b.option(bool, "skip_release_small", "Skip release-small builds") orelse false;
+
+    var optimize_modes_buf: [4]std.builtin.OptimizeMode = undefined;
+    var optimize_modes_len: usize = 0;
+    if (!skip_debug) {
+        optimize_modes_buf[optimize_modes_len] = .Debug;
+        optimize_modes_len += 1;
+    }
+    if (!skip_release_safe) {
+        optimize_modes_buf[optimize_modes_len] = .ReleaseSafe;
+        optimize_modes_len += 1;
+    }
+    if (!skip_release_fast) {
+        optimize_modes_buf[optimize_modes_len] = .ReleaseFast;
+        optimize_modes_len += 1;
+    }
+    if (!skip_release_small) {
+        optimize_modes_buf[optimize_modes_len] = .ReleaseSmall;
+        optimize_modes_len += 1;
+    }
+    const optimize_modes = optimize_modes_buf[0..optimize_modes_len];
+
+    for (cases) |case| {
+        for (optimize_modes) |optimize| {
+            if (!case.all_modes and optimize != .Debug) continue;
+            if (case.os_filter) |os_tag| {
+                if (os_tag != builtin.os.tag) continue;
+            }
+
+            const resolved_target = b.resolveTargetQuery(case.target);
+
+            if (case.is_exe) {
+                const exe = b.addExecutable(.{
+                    .name = std.fs.path.stem(case.src_path),
+                    .root_source_file = .{ .path = case.src_path },
+                    .optimize = optimize,
+                    .target = resolved_target,
+                });
+                if (case.link_libc) exe.linkLibC();
+
+                _ = exe.getEmittedBin();
+
+                step.dependOn(&exe.step);
+            }
+
+            if (case.is_test) {
+                const exe = b.addTest(.{
+                    .name = std.fs.path.stem(case.src_path),
+                    .root_source_file = .{ .path = case.src_path },
+                    .optimize = optimize,
+                    .target = resolved_target,
+                });
+                if (case.link_libc) exe.linkLibC();
+
+                const run = b.addRunArtifact(exe);
+                step.dependOn(&run.step);
+            }
+        }
+    }
+}
+
+const Case = struct {
+    src_path: []const u8,
+    link_libc: bool = false,
+    all_modes: bool = false,
+    target: std.Target.Query = .{},
+    is_test: bool = false,
+    is_exe: bool = true,
+    /// Run only on this OS.
+    os_filter: ?std.Target.Os.Tag = null,
+};
+
+const cases = [_]Case{
+    .{
+        .src_path = "hello_world/hello.zig",
+        .all_modes = true,
+    },
+    .{
+        .src_path = "hello_world/hello_libc.zig",
+        .link_libc = true,
+        .all_modes = true,
+    },
+    .{
+        .src_path = "cat/main.zig",
+    },
+    // https://github.com/ziglang/zig/issues/6025
+    //.{
+    //    .src_path = "issue_9693/main.zig",
+    //},
+    .{
+        .src_path = "brace_expansion.zig",
+        .is_test = true,
+    },
+    .{
+        .src_path = "issue_7030.zig",
+        .target = .{
+            .cpu_arch = .wasm32,
+            .os_tag = .freestanding,
+        },
+    },
+    .{ .src_path = "issue_12471/main.zig" },
+    .{ .src_path = "guess_number/main.zig" },
+    .{ .src_path = "main_return_error/error_u8.zig" },
+    .{ .src_path = "main_return_error/error_u8_non_zero.zig" },
+    .{ .src_path = "noreturn_call/inline.zig" },
+    .{ .src_path = "noreturn_call/as_arg.zig" },
+    .{ .src_path = "std_enums_big_enums.zig" },
+    .{
+        .src_path = "issue_9402/main.zig",
+        .os_filter = .windows,
+        .link_libc = true,
+    },
+};
test/standalone/build.zig
@@ -5,23 +5,66 @@ pub fn build(b: *std.Build) void {
     const step = b.step("test", "Run standalone test cases");
     b.default_step = step;
 
-    const enable_ios_sdk = b.option(bool, "enable-ios-sdk", "Run tests requiring presence of iOS SDK and frameworks") orelse false;
-    const enable_macos_sdk = b.option(bool, "enable-macos-sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse enable_ios_sdk;
-    const enable_symlinks_windows = b.option(bool, "enable-symlinks-windows", "Run tests requiring presence of symlinks on Windows") orelse false;
-
+    const enable_ios_sdk = b.option(bool, "enable_ios_sdk", "Run tests requiring presence of iOS SDK and frameworks") orelse false;
+    const enable_macos_sdk = b.option(bool, "enable_macos_sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse enable_ios_sdk;
+    const enable_symlinks_windows = b.option(bool, "enable_symlinks_windows", "Run tests requiring presence of symlinks on Windows") orelse false;
     const omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows;
 
+    const simple_skip_debug = b.option(bool, "simple_skip_debug", "Simple tests skip debug builds") orelse false;
+    const simple_skip_release_safe = b.option(bool, "simple_skip_release_safe", "Simple tests skip release-safe builds") orelse false;
+    const simple_skip_release_fast = b.option(bool, "simple_skip_release_fast", "Simple tests skip release-fast builds") orelse false;
+    const simple_skip_release_small = b.option(bool, "simple_skip_release_small", "Simple tests skip release-small builds") orelse false;
+
+    const simple_dep = b.dependency("simple", .{
+        .skip_debug = simple_skip_debug,
+        .skip_release_safe = simple_skip_release_safe,
+        .skip_release_fast = simple_skip_release_fast,
+        .skip_release_small = simple_skip_release_small,
+    });
+    const simple_dep_step = simple_dep.builder.default_step;
+    simple_dep_step.name = "standalone_test_cases.simple";
+    step.dependOn(simple_dep_step);
+
+    // Ensure the development tools are buildable.
+    const tools_tests_step = b.step("standalone_test_cases.tools", "Test tools");
+    step.dependOn(tools_tests_step);
+    const tools_target = b.resolveTargetQuery(.{});
+    for ([_][]const u8{
+        // Alphabetically sorted. No need to build `tools/spirv/grammar.zig`.
+        "../../tools/gen_outline_atomics.zig",
+        "../../tools/gen_spirv_spec.zig",
+        "../../tools/gen_stubs.zig",
+        "../../tools/generate_linux_syscalls.zig",
+        "../../tools/process_headers.zig",
+        "../../tools/update-linux-headers.zig",
+        "../../tools/update_clang_options.zig",
+        "../../tools/update_cpu_features.zig",
+        "../../tools/update_glibc.zig",
+        "../../tools/update_spirv_features.zig",
+    }) |tool_src_path| {
+        const tool = b.addTest(.{
+            .name = std.fs.path.stem(tool_src_path),
+            .root_source_file = .{ .path = tool_src_path },
+            .optimize = .Debug,
+            .target = tools_target,
+        });
+        const run = b.addRunArtifact(tool);
+        tools_tests_step.dependOn(&run.step);
+    }
+
     add_dep_steps: for (b.available_deps) |available_dep| {
         const dep_name, const dep_hash = available_dep;
 
+        // The 'simple' dependency was already handled manually above.
+        if (std.mem.eql(u8, dep_name, "simple")) continue;
+
         const all_pkgs = @import("root").dependencies.packages;
         inline for (@typeInfo(all_pkgs).Struct.decls) |decl| {
             const pkg_hash = decl.name;
             if (std.mem.eql(u8, dep_hash, pkg_hash)) {
                 const pkg = @field(all_pkgs, pkg_hash);
                 if (!@hasDecl(pkg, "build_zig")) {
-                    std.debug.print("standalone test case '{s}' is missing a 'build.zig' file\n", .{dep_name});
-                    std.process.exit(1);
+                    std.debug.panic("standalone test case '{s}' is missing a 'build.zig' file", .{dep_name});
                 }
                 const requires_ios_sdk = @hasDecl(pkg.build_zig, "requires_ios_sdk") and
                     pkg.build_zig.requires_ios_sdk;
test/standalone/build.zig.zon
@@ -2,9 +2,9 @@
     .name = "standalone_test_cases",
     .version = "0.0.0",
     .dependencies = .{
-        // "Simple" test cases (those in the 'simple' directory) are configured by the root build
-        // script, outside of this package.
-
+        .simple = .{
+            .path = "simple",
+        },
         .test_runner_path = .{
             .path = "test_runner_path",
         },
test/standalone.zig
@@ -1,69 +0,0 @@
-pub const SimpleCase = struct {
-    src_path: []const u8,
-    link_libc: bool = false,
-    all_modes: bool = false,
-    target: std.Target.Query = .{},
-    is_test: bool = false,
-    is_exe: bool = true,
-    /// Run only on this OS.
-    os_filter: ?std.Target.Os.Tag = null,
-};
-
-pub const simple_cases = [_]SimpleCase{
-    .{
-        .src_path = "test/standalone/simple/hello_world/hello.zig",
-        .all_modes = true,
-    },
-    .{
-        .src_path = "test/standalone/simple/hello_world/hello_libc.zig",
-        .link_libc = true,
-        .all_modes = true,
-    },
-    .{
-        .src_path = "test/standalone/simple/cat/main.zig",
-    },
-    // https://github.com/ziglang/zig/issues/6025
-    //.{
-    //    .src_path = "test/standalone/simple/issue_9693/main.zig",
-    //},
-    .{
-        .src_path = "test/standalone/simple/brace_expansion.zig",
-        .is_test = true,
-    },
-    .{
-        .src_path = "test/standalone/simple/issue_7030.zig",
-        .target = .{
-            .cpu_arch = .wasm32,
-            .os_tag = .freestanding,
-        },
-    },
-
-    .{ .src_path = "test/standalone/simple/issue_12471/main.zig" },
-    .{ .src_path = "test/standalone/simple/guess_number/main.zig" },
-    .{ .src_path = "test/standalone/simple/main_return_error/error_u8.zig" },
-    .{ .src_path = "test/standalone/simple/main_return_error/error_u8_non_zero.zig" },
-    .{ .src_path = "test/standalone/simple/noreturn_call/inline.zig" },
-    .{ .src_path = "test/standalone/simple/noreturn_call/as_arg.zig" },
-    .{ .src_path = "test/standalone/simple/std_enums_big_enums.zig" },
-
-    .{
-        .src_path = "test/standalone/simple/issue_9402/main.zig",
-        .os_filter = .windows,
-        .link_libc = true,
-    },
-
-    // Ensure the development tools are buildable. Alphabetically sorted.
-    // No need to build `tools/spirv/grammar.zig`.
-    .{ .src_path = "tools/gen_outline_atomics.zig" },
-    .{ .src_path = "tools/gen_spirv_spec.zig" },
-    .{ .src_path = "tools/gen_stubs.zig" },
-    .{ .src_path = "tools/generate_linux_syscalls.zig" },
-    .{ .src_path = "tools/process_headers.zig" },
-    .{ .src_path = "tools/update-linux-headers.zig" },
-    .{ .src_path = "tools/update_clang_options.zig" },
-    .{ .src_path = "tools/update_cpu_features.zig" },
-    .{ .src_path = "tools/update_glibc.zig" },
-    .{ .src_path = "tools/update_spirv_features.zig" },
-};
-
-const std = @import("std");
test/tests.zig
@@ -7,7 +7,6 @@ const Step = std.Build.Step;
 
 // Cases
 const compare_output = @import("compare_output.zig");
-const standalone = @import("standalone.zig");
 const stack_traces = @import("stack_traces.zig");
 const assemble_and_link = @import("assemble_and_link.zig");
 const translate_c = @import("translate_c.zig");
@@ -672,44 +671,6 @@ pub fn addStandaloneTests(
 ) *Step {
     const step = b.step("test-standalone", "Run the standalone tests");
 
-    for (standalone.simple_cases) |case| {
-        for (optimize_modes) |optimize| {
-            if (!case.all_modes and optimize != .Debug) continue;
-            if (case.os_filter) |os_tag| {
-                if (os_tag != builtin.os.tag) continue;
-            }
-
-            const resolved_target = b.resolveTargetQuery(case.target);
-
-            if (case.is_exe) {
-                const exe = b.addExecutable(.{
-                    .name = std.fs.path.stem(case.src_path),
-                    .root_source_file = .{ .path = case.src_path },
-                    .optimize = optimize,
-                    .target = resolved_target,
-                });
-                if (case.link_libc) exe.linkLibC();
-
-                _ = exe.getEmittedBin();
-
-                step.dependOn(&exe.step);
-            }
-
-            if (case.is_test) {
-                const exe = b.addTest(.{
-                    .name = std.fs.path.stem(case.src_path),
-                    .root_source_file = .{ .path = case.src_path },
-                    .optimize = optimize,
-                    .target = resolved_target,
-                });
-                if (case.link_libc) exe.linkLibC();
-
-                const run = b.addRunArtifact(exe);
-                step.dependOn(&run.step);
-            }
-        }
-    }
-
     // We can only use dependencies if the compiler was built with support for package management.
     // (zig2 doesn't support it, but we still need to construct a build graph to build stage3.)
     const package_management_available = b.available_deps.len != 0;
@@ -717,9 +678,13 @@ pub fn addStandaloneTests(
     if (package_management_available) {
         const test_cases_dep_name = "standalone_test_cases";
         const test_cases_dep = b.dependency(test_cases_dep_name, .{
-            .@"enable-ios-sdk" = enable_ios_sdk,
-            .@"enable-macos-sdk" = enable_macos_sdk,
-            .@"enable-symlinks-windows" = enable_symlinks_windows,
+            .enable_ios_sdk = enable_ios_sdk,
+            .enable_macos_sdk = enable_macos_sdk,
+            .enable_symlinks_windows = enable_symlinks_windows,
+            .simple_skip_debug = mem.indexOfScalar(OptimizeMode, optimize_modes, .Debug) == null,
+            .simple_skip_release_safe = mem.indexOfScalar(OptimizeMode, optimize_modes, .ReleaseSafe) == null,
+            .simple_skip_release_fast = mem.indexOfScalar(OptimizeMode, optimize_modes, .ReleaseFast) == null,
+            .simple_skip_release_small = mem.indexOfScalar(OptimizeMode, optimize_modes, .ReleaseSmall) == null,
         });
         const test_cases_dep_step = test_cases_dep.builder.default_step;
         test_cases_dep_step.name = b.dupe(test_cases_dep_name);