Commit 3ed221f149

Carl Åstholm <carl@astholm.se>
2024-01-16 17:54:57
Promote standalone test cases to packages
This is a prerequisite for removing `b.anonymousDependency()`, but having compiler tests dogfood package management might be a good idea in general.
1 parent 05126fc
test/standalone/build.zig
@@ -0,0 +1,46 @@
+const std = @import("std");
+const builtin = @import("builtin");
+
+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 omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows;
+
+    add_dep_steps: for (b.available_deps) |available_dep| {
+        const dep_name, const dep_hash = available_dep;
+
+        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.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;
+                const requires_macos_sdk = @hasDecl(pkg.build_zig, "requires_macos_sdk") and
+                    pkg.build_zig.requires_macos_sdk;
+                const requires_symlinks = @hasDecl(pkg.build_zig, "requires_symlinks") and
+                    pkg.build_zig.requires_symlinks;
+                if ((requires_symlinks and omit_symlinks) or
+                    (requires_macos_sdk and !enable_macos_sdk) or
+                    (requires_ios_sdk and !enable_ios_sdk))
+                {
+                    continue :add_dep_steps;
+                }
+                break;
+            }
+        } else unreachable;
+
+        const dep = b.dependency(dep_name, .{});
+        const dep_step = dep.builder.default_step;
+        dep_step.name = b.fmt("standalone_test_cases.{s}", .{dep_name});
+        step.dependOn(dep_step);
+    }
+}
test/standalone/build.zig.zon
@@ -0,0 +1,160 @@
+.{
+    .name = "standalone_test_cases",
+    .version = "0.0.0",
+    .dependencies = .{
+        .test_runner_path = .{
+            .path = "test_runner_path",
+        },
+        .test_runner_module_imports = .{
+            .path = "test_runner_module_imports",
+        },
+        // https://github.com/ziglang/zig/issues/17483
+        //.issue_13970 = .{
+        //    .path = "issue_13970",
+        //},
+        .shared_library = .{
+            .path = "shared_library",
+        },
+        .mix_o_files = .{
+            .path = "mix_o_files",
+        },
+        .mix_c_files = .{
+            .path = "mix_c_files",
+        },
+        .global_linkage = .{
+            .path = "global_linkage",
+        },
+        .static_c_lib = .{
+            .path = "static_c_lib",
+        },
+        .issue_339 = .{
+            .path = "issue_339",
+        },
+        .issue_8550 = .{
+            .path = "issue_8550",
+        },
+        .issue_794 = .{
+            .path = "issue_794",
+        },
+        .issue_5825 = .{
+            .path = "issue_5825",
+        },
+        .pkg_import = .{
+            .path = "pkg_import",
+        },
+        .use_alias = .{
+            .path = "use_alias",
+        },
+        .install_raw_hex = .{
+            .path = "install_raw_hex",
+        },
+        // https://github.com/ziglang/zig/issues/17484
+        //.emit_asm_and_bin = .{
+        //    .path = "emit_asm_and_bin",
+        //},
+        // https://github.com/ziglang/zig/issues/17484
+        //.issue_12588 = .{
+        //    .path = "issue_12588",
+        //},
+        .child_process = .{
+            .path = "child_process",
+        },
+        .embed_generated_file = .{
+            .path = "embed_generated_file",
+        },
+        .@"extern" = .{
+            .path = "extern",
+        },
+        .dep_diamond = .{
+            .path = "dep_diamond",
+        },
+        .dep_triangle = .{
+            .path = "dep_triangle",
+        },
+        .dep_recursive = .{
+            .path = "dep_recursive",
+        },
+        .dep_mutually_recursive = .{
+            .path = "dep_mutually_recursive",
+        },
+        .dep_shared_builtin = .{
+            .path = "dep_shared_builtin",
+        },
+        .dirname = .{
+            .path = "dirname",
+        },
+        .empty_env = .{
+            .path = "empty_env",
+        },
+        .issue_11595 = .{
+            .path = "issue_11595",
+        },
+        .load_dynamic_library = .{
+            .path = "load_dynamic_library",
+        },
+        .windows_resources = .{
+            .path = "windows_resources",
+        },
+        .windows_entry_points = .{
+            .path = "windows_entry_points",
+        },
+        .windows_spawn = .{
+            .path = "windows_spawn",
+        },
+        .self_exe_symlink = .{
+            .path = "self_exe_symlink",
+        },
+        .c_compiler = .{
+            .path = "c_compiler",
+        },
+        .pie = .{
+            .path = "pie",
+        },
+        .issue_12706 = .{
+            .path = "issue_12706",
+        },
+        // TODO This test is disabled for doing naughty things in the build script.
+        // The logic needs to get moved to a child process instead of build.zig.
+        //.sigpipe = .{
+        //    .path = "sigpipe",
+        //},
+        // TODO restore this test
+        //.options = .{
+        //    .path = "options",
+        //},
+        .strip_empty_loop = .{
+            .path = "strip_empty_loop",
+        },
+        .strip_struct_init = .{
+            .path = "strip_struct_init",
+        },
+        .cmakedefine = .{
+            .path = "cmakedefine",
+        },
+        .zerolength_check = .{
+            .path = "zerolength_check",
+        },
+        .stack_iterator = .{
+            .path = "stack_iterator",
+        },
+        .coff_dwarf = .{
+            .path = "coff_dwarf",
+        },
+        .compiler_rt_panic = .{
+            .path = "compiler_rt_panic",
+        },
+        .ios = .{
+            .path = "ios",
+        },
+        .depend_on_main_mod = .{
+            .path = "depend_on_main_mod",
+        },
+        .install_headers = .{
+            .path = "install_headers",
+        },
+    },
+    .paths = .{
+        "build.zig",
+        "build.zig.zon",
+    },
+}
test/standalone.zig
@@ -9,11 +9,6 @@ pub const SimpleCase = struct {
     os_filter: ?std.Target.Os.Tag = null,
 };
 
-pub const BuildCase = struct {
-    build_root: []const u8,
-    import: type,
-};
-
 pub const simple_cases = [_]SimpleCase{
     .{
         .src_path = "test/standalone/hello_world/hello.zig",
@@ -71,205 +66,4 @@ pub const simple_cases = [_]SimpleCase{
     .{ .src_path = "tools/update_spirv_features.zig" },
 };
 
-pub const build_cases = [_]BuildCase{
-    .{
-        .build_root = "test/standalone/test_runner_path",
-        .import = @import("standalone/test_runner_path/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/test_runner_module_imports",
-        .import = @import("standalone/test_runner_module_imports/build.zig"),
-    },
-    // https://github.com/ziglang/zig/issues/17483
-    //.{
-    //    .build_root = "test/standalone/issue_13970",
-    //    .import = @import("standalone/issue_13970/build.zig"),
-    //},
-    .{
-        .build_root = "test/standalone/shared_library",
-        .import = @import("standalone/shared_library/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/mix_o_files",
-        .import = @import("standalone/mix_o_files/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/mix_c_files",
-        .import = @import("standalone/mix_c_files/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/global_linkage",
-        .import = @import("standalone/global_linkage/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/static_c_lib",
-        .import = @import("standalone/static_c_lib/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/issue_339",
-        .import = @import("standalone/issue_339/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/issue_8550",
-        .import = @import("standalone/issue_8550/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/issue_794",
-        .import = @import("standalone/issue_794/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/issue_5825",
-        .import = @import("standalone/issue_5825/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/pkg_import",
-        .import = @import("standalone/pkg_import/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/use_alias",
-        .import = @import("standalone/use_alias/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/install_raw_hex",
-        .import = @import("standalone/install_raw_hex/build.zig"),
-    },
-    // https://github.com/ziglang/zig/issues/17484
-    //.{
-    //    .build_root = "test/standalone/emit_asm_and_bin",
-    //    .import = @import("standalone/emit_asm_and_bin/build.zig"),
-    //},
-    // https://github.com/ziglang/zig/issues/17484
-    //.{
-    //    .build_root = "test/standalone/issue_12588",
-    //    .import = @import("standalone/issue_12588/build.zig"),
-    //},
-    .{
-        .build_root = "test/standalone/child_process",
-        .import = @import("standalone/child_process/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/embed_generated_file",
-        .import = @import("standalone/embed_generated_file/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/extern",
-        .import = @import("standalone/extern/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/dep_diamond",
-        .import = @import("standalone/dep_diamond/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/dep_triangle",
-        .import = @import("standalone/dep_triangle/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/dep_recursive",
-        .import = @import("standalone/dep_recursive/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/dep_mutually_recursive",
-        .import = @import("standalone/dep_mutually_recursive/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/dep_shared_builtin",
-        .import = @import("standalone/dep_shared_builtin/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/dirname",
-        .import = @import("standalone/dirname/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/empty_env",
-        .import = @import("standalone/empty_env/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/issue_11595",
-        .import = @import("standalone/issue_11595/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/load_dynamic_library",
-        .import = @import("standalone/load_dynamic_library/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/windows_resources",
-        .import = @import("standalone/windows_resources/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/windows_entry_points",
-        .import = @import("standalone/windows_entry_points/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/windows_spawn",
-        .import = @import("standalone/windows_spawn/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/self_exe_symlink",
-        .import = @import("standalone/self_exe_symlink/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/c_compiler",
-        .import = @import("standalone/c_compiler/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/pie",
-        .import = @import("standalone/pie/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/issue_12706",
-        .import = @import("standalone/issue_12706/build.zig"),
-    },
-    // TODO This test is disabled for doing naughty things in the build script.
-    // The logic needs to get moved to a child process instead of build.zig.
-    //.{
-    //    .build_root = "test/standalone/sigpipe",
-    //    .import = @import("standalone/sigpipe/build.zig"),
-    //},
-    // TODO restore this test
-    //.{
-    //    .build_root = "test/standalone/options",
-    //    .import = @import("standalone/options/build.zig"),
-    //},
-    .{
-        .build_root = "test/standalone/strip_empty_loop",
-        .import = @import("standalone/strip_empty_loop/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/strip_struct_init",
-        .import = @import("standalone/strip_struct_init/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/cmakedefine",
-        .import = @import("standalone/cmakedefine/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/zerolength_check",
-        .import = @import("standalone/zerolength_check/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/stack_iterator",
-        .import = @import("standalone/stack_iterator/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/coff_dwarf",
-        .import = @import("standalone/coff_dwarf/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/compiler_rt_panic",
-        .import = @import("standalone/compiler_rt_panic/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/ios",
-        .import = @import("standalone/ios/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/depend_on_main_mod",
-        .import = @import("standalone/depend_on_main_mod/build.zig"),
-    },
-    .{
-        .build_root = "test/standalone/install_headers",
-        .import = @import("standalone/install_headers/build.zig"),
-    },
-};
-
 const std = @import("std");
test/tests.zig
@@ -668,11 +668,9 @@ pub fn addStandaloneTests(
     optimize_modes: []const OptimizeMode,
     enable_macos_sdk: bool,
     enable_ios_sdk: bool,
-    omit_stage2: bool,
     enable_symlinks_windows: bool,
 ) *Step {
     const step = b.step("test-standalone", "Run the standalone tests");
-    const omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows;
 
     for (standalone.simple_cases) |case| {
         for (optimize_modes) |optimize| {
@@ -712,29 +710,15 @@ pub fn addStandaloneTests(
         }
     }
 
-    inline for (standalone.build_cases) |case| {
-        const requires_stage2 = @hasDecl(case.import, "requires_stage2") and
-            case.import.requires_stage2;
-        const requires_symlinks = @hasDecl(case.import, "requires_symlinks") and
-            case.import.requires_symlinks;
-        const requires_macos_sdk = @hasDecl(case.import, "requires_macos_sdk") and
-            case.import.requires_macos_sdk;
-        const requires_ios_sdk = @hasDecl(case.import, "requires_ios_sdk") and
-            case.import.requires_ios_sdk;
-        const bad =
-            (requires_stage2 and omit_stage2) or
-            (requires_symlinks and omit_symlinks) or
-            (requires_macos_sdk and !enable_macos_sdk) or
-            (requires_ios_sdk and !enable_ios_sdk);
-        if (!bad) {
-            const dep = b.anonymousDependency(case.build_root, case.import, .{});
-            const dep_step = dep.builder.default_step;
-            assert(mem.startsWith(u8, dep.builder.dep_prefix, "test."));
-            const dep_prefix_adjusted = dep.builder.dep_prefix["test.".len..];
-            dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
-            step.dependOn(dep_step);
-        }
-    }
+    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,
+    });
+    const test_cases_dep_step = test_cases_dep.builder.default_step;
+    test_cases_dep_step.name = b.dupe(test_cases_dep_name);
+    step.dependOn(test_cases_dep.builder.default_step);
 
     return step;
 }
build.zig
@@ -521,7 +521,6 @@ pub fn build(b: *std.Build) !void {
         optimization_modes,
         enable_macos_sdk,
         enable_ios_sdk,
-        false,
         enable_symlinks_windows,
     ));
     test_step.dependOn(tests.addCAbiTests(b, skip_non_native, skip_release));
build.zig.zon
@@ -0,0 +1,12 @@
+// The Zig compiler is not intended to be consumed as a package.
+// The sole purpose of this manifest file is to test the compiler.
+.{
+    .name = "zig",
+    .version = "0.0.0",
+    .dependencies = .{
+        .standalone_test_cases = .{
+            .path = "test/standalone",
+        },
+    },
+    .paths = .{""},
+}