diff --git a/build.zig b/build.zig index cecba0923eb6c8fd63b0093264a2aa2482b672b4..421b28adef3a31fe766d56baf74e200f9aa5b05b 100644 --- a/build.zig +++ b/build.zig @@ -636,6 +636,7 @@ pub fn build(b: *std.Build) !void { test_step.dependOn(tests.addStandaloneTests( b, optimize_modes, + skip_non_native, enable_macos_sdk, enable_ios_sdk, enable_symlinks_windows, diff --git a/test/standalone/build.zig b/test/standalone/build.zig index ab4262c8b90686517cf32f90b7b99263c72d3969..c9648fd90e6e1c33dfd5179806cd2c65da62c089 100644 --- a/test/standalone/build.zig +++ b/test/standalone/build.zig @@ -14,6 +14,7 @@ pub fn build(b: *std.Build) void { 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 skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; const simple_dep = b.dependency("simple", .{ .skip_debug = simple_skip_debug, @@ -103,7 +104,7 @@ pub fn build(b: *std.Build) void { if (std.mem.eql(u8, dep_name, "simple")) continue; const all_pkgs = @import("root").dependencies.packages; - inline for (@typeInfo(all_pkgs).@"struct".decl_names) |pkg_hash| { + const forward_skip_non_native = inline for (@typeInfo(all_pkgs).@"struct".decl_names) |pkg_hash| { if (std.mem.eql(u8, dep_hash, pkg_hash)) { const pkg = @field(all_pkgs, pkg_hash); if (!@hasDecl(pkg, "build_zig")) { @@ -121,11 +122,15 @@ pub fn build(b: *std.Build) void { { continue :add_dep_steps; } - break; + break @hasDecl(pkg.build_zig, "supports_skip_non_native") and + pkg.build_zig.supports_skip_non_native; } } else unreachable; - const dep = b.dependency(dep_name, .{}); + const dep = if (forward_skip_non_native) + b.dependency(dep_name, .{ .skip_non_native = skip_non_native }) + else + 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); diff --git a/test/standalone/coff_dwarf/build.zig b/test/standalone/coff_dwarf/build.zig index 6ff69e948964dac924043d3635a50df44757a279..03ee6723356318683da675b1fdd5c257285f3cd1 100644 --- a/test/standalone/coff_dwarf/build.zig +++ b/test/standalone/coff_dwarf/build.zig @@ -1,20 +1,13 @@ const std = @import("std"); +pub const supports_skip_non_native = true; + /// This tests the path where DWARF information is embedded in a COFF binary pub fn build(b: *std.Build) void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const host = b.graph.host; - // https://github.com/ziglang/zig/issues/25471 - if (host.result.os.tag == .freebsd) return; - - switch (host.result.cpu.arch) { - .aarch64, - .x86, - .x86_64, - => {}, - else => return, - } - const test_step = b.step("test", "Test it"); b.default_step = test_step; @@ -24,6 +17,8 @@ pub fn build(b: *std.Build) void { else => b.resolveTargetQuery(.{ .os_tag = .windows }), }; + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &host.result)) return; + const exe = b.addExecutable(.{ .name = "main", .root_module = b.createModule(.{ diff --git a/test/standalone/compile_asm/build.zig b/test/standalone/compile_asm/build.zig index 1cd675bc9b6a14a7a31f7299255f8c85cf27a0cc..88956cd07b0192ad0e23c5a770cdb56706031766 100644 --- a/test/standalone/compile_asm/build.zig +++ b/test/standalone/compile_asm/build.zig @@ -1,6 +1,10 @@ const std = @import("std"); +pub const supports_skip_non_native = true; + pub fn build(b: *std.Build) !void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const test_step = b.step("test", "Test it"); b.default_step = test_step; @@ -13,6 +17,8 @@ pub fn build(b: *std.Build) !void { }, }); + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) return; + const kernel = b.addExecutable(.{ .name = "kernel", .root_module = b.createModule(.{ diff --git a/test/standalone/elf2/build.zig b/test/standalone/elf2/build.zig index 6bc29c26e382fb8e52b66fb04969b280fb670e0b..1787a15362fe9522b292e4c6fa1366ceee8d8bd7 100644 --- a/test/standalone/elf2/build.zig +++ b/test/standalone/elf2/build.zig @@ -1,4 +1,8 @@ +pub const supports_skip_non_native = true; + pub fn build(b: *Build) void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const test_step = b.step("test", "Test the new ELF linker"); b.default_step = test_step; @@ -15,6 +19,9 @@ pub fn build(b: *Build) void { .cpu_arch = .x86_64, .os_tag = .linux, }); + + if (skip_non_native and !std.zig.target.isNative(&x86_64_linux_target.query, &x86_64_linux_target.result, &b.graph.host.result)) return; + addOne(b, test_step, x86_64_linux_target, false, .static, false, false, "elf2-hello-selfhosted-static"); addOne(b, test_step, x86_64_linux_target, false, .static, true, false, "elf2-hello-selfhosted-static-pie"); addOne(b, test_step, x86_64_linux_target, true, .static, false, false, "elf2-hello-llvm-static"); diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig index 4669e8e440517ecd3af2243b02cba143f83cbae1..02dd8c4efc4774adff72477d6e43712182e59223 100644 --- a/test/standalone/embed_generated_file/build.zig +++ b/test/standalone/embed_generated_file/build.zig @@ -1,6 +1,17 @@ const std = @import("std"); +pub const supports_skip_non_native = true; + pub fn build(b: *std.Build) void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + + const target = b.resolveTargetQuery(.{ + .cpu_arch = .x86, + .os_tag = .freestanding, + }); + + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) return; + const test_step = b.step("test", "Test it"); b.default_step = test_step; @@ -8,10 +19,7 @@ pub fn build(b: *std.Build) void { .name = "bootloader", .root_module = b.createModule(.{ .root_source_file = b.path("bootloader.zig"), - .target = b.resolveTargetQuery(.{ - .cpu_arch = .x86, - .os_tag = .freestanding, - }), + .target = target, .optimize = .small, }), }); diff --git a/test/standalone/entry_point/build.zig b/test/standalone/entry_point/build.zig index 376b599d289165054717fef0e457ce9aa040e2e3..6dc1e9ab6c341f73dc042e98b71c7657c99e7d2b 100644 --- a/test/standalone/entry_point/build.zig +++ b/test/standalone/entry_point/build.zig @@ -1,11 +1,19 @@ +pub const supports_skip_non_native = true; + pub fn build(b: *std.Build) !void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + + const target = b.resolveTargetQuery(try .parse(.{ + .arch_os_abi = "x86_64-linux", + })); + + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) return; + const mod = b.createModule(.{ // Setting the entry point doesn't work properly on all targets right now. Since we're // really just trying to make sure that the compiler *frontend* respects `-fentry` and // includes it in the cache manifest, just test for a target where it works. - .target = b.resolveTargetQuery(try .parse(.{ - .arch_os_abi = "x86_64-linux", - })), + .target = target, .optimize = .fast, // non-Debug build for reproducible output .root_source_file = b.path("main.zig"), }); diff --git a/test/standalone/glibc_compat/build.zig b/test/standalone/glibc_compat/build.zig index 202449a1e233b1940c632db8d87db3ec8645e984..7769deab301a387a3d035a6a0062ab4a9a971a9e 100644 --- a/test/standalone/glibc_compat/build.zig +++ b/test/standalone/glibc_compat/build.zig @@ -1,6 +1,8 @@ const std = @import("std"); const builtin = @import("builtin"); +pub const supports_skip_non_native = true; + // To run executables linked against a specific glibc version, the // run-time glibc version needs to be new enough. Check the host's glibc // version. Note that this does not allow for translation/vm/emulation @@ -8,17 +10,23 @@ const builtin = @import("builtin"); const running_glibc_ver = builtin.os.versionRange().gnuLibCVersion(); pub fn build(b: *std.Build) void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const test_step = b.step("test", "Test"); b.default_step = test_step; for ([_][]const u8{ "aarch64-linux-gnu.2.27", "aarch64-linux-gnu.2.34" }) |t| { + const target = b.resolveTargetQuery(std.Target.Query.parse( + .{ .arch_os_abi = t }, + ) catch unreachable); + + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) continue; + const exe = b.addExecutable(.{ .name = t, .root_module = b.createModule(.{ .root_source_file = null, - .target = b.resolveTargetQuery(std.Target.Query.parse( - .{ .arch_os_abi = t }, - ) catch unreachable), + .target = target, .link_libc = true, }), }); @@ -49,6 +57,8 @@ pub fn build(b: *std.Build) void { .{ .arch_os_abi = t }, ) catch unreachable); + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) continue; + const glibc_ver = target.result.os.version_range.linux.glibc; // only build test if glibc version supports the architecture @@ -103,6 +113,8 @@ pub fn build(b: *std.Build) void { .{ .arch_os_abi = t }, ) catch unreachable); + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) continue; + if (target.result.cpu.arch.isLoongArch()) continue; // https://github.com/Vexu/arocc/issues/1096 const glibc_ver = target.result.os.version_range.linux.glibc; diff --git a/test/standalone/install_raw_hex/build.zig b/test/standalone/install_raw_hex/build.zig index 3fd9e91a9a1184ca2de89beec01ae7dc7bf2c714..bc6a27ebd905cb2e3bdf6547caddd1f3bdb7b49e 100644 --- a/test/standalone/install_raw_hex/build.zig +++ b/test/standalone/install_raw_hex/build.zig @@ -1,7 +1,11 @@ const builtin = @import("builtin"); const std = @import("std"); +pub const supports_skip_non_native = true; + pub fn build(b: *std.Build) void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const test_step = b.step("test", "Test it"); b.default_step = test_step; @@ -12,6 +16,8 @@ pub fn build(b: *std.Build) void { .abi = .gnueabihf, }); + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) return; + const optimize: std.builtin.Optimize = .debug; const elf = b.addExecutable(.{ diff --git a/test/standalone/tsan/build.zig b/test/standalone/tsan/build.zig index 486c773467f3aff0debdb9f101a446b06f9c66f1..50101d38a53ce6e930ddbc2c3f6b6a7ebe5ee53d 100644 --- a/test/standalone/tsan/build.zig +++ b/test/standalone/tsan/build.zig @@ -1,5 +1,7 @@ const std = @import("std"); +pub const supports_skip_non_native = true; + const targets: []const std.Target.Query = &.{ .{ .cpu_arch = .aarch64, .os_tag = .freebsd, .abi = .none }, .{ .cpu_arch = .x86_64, .os_tag = .freebsd, .abi = .none }, @@ -32,6 +34,8 @@ const targets: []const std.Target.Query = &.{ }; pub fn build(b: *std.Build) !void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const test_step = b.step("test", "Test the program"); b.default_step = test_step; @@ -40,6 +44,8 @@ pub fn build(b: *std.Build) !void { for (targets) |query| { const target = b.resolveTargetQuery(query); + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) continue; + switch (target.result.os.tag) { .macos => { // compiling tsan on macos requires system headers that aren't present during cross-compilation diff --git a/test/standalone/windows_entry_points/build.zig b/test/standalone/windows_entry_points/build.zig index 2c75c3e6ba8cda977eb9fb0f1f73f5af3872e5b1..f1dcec2b3f7e0f0f5b37fd01e446464e7a3fc67d 100644 --- a/test/standalone/windows_entry_points/build.zig +++ b/test/standalone/windows_entry_points/build.zig @@ -1,6 +1,10 @@ const std = @import("std"); +pub const supports_skip_non_native = true; + pub fn build(b: *std.Build) void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const test_step = b.step("test", "Test it"); b.default_step = test_step; @@ -10,6 +14,8 @@ pub fn build(b: *std.Build) void { .abi = .gnu, }); + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) return; + { const exe = b.addExecutable(.{ .name = "main", diff --git a/test/standalone/windows_resources/build.zig b/test/standalone/windows_resources/build.zig index 09d91a29ed986247cf47cded3b70b3e2952cb0fd..782b502f0a7cb6f5f0cc20599fa391ef2c29e801 100644 --- a/test/standalone/windows_resources/build.zig +++ b/test/standalone/windows_resources/build.zig @@ -1,6 +1,10 @@ const std = @import("std"); +pub const supports_skip_non_native = true; + pub fn build(b: *std.Build) void { + const skip_non_native = b.option(bool, "skip_non_native", "Skip non-native targets") orelse false; + const test_step = b.step("test", "Test it"); b.default_step = test_step; @@ -10,6 +14,8 @@ pub fn build(b: *std.Build) void { .abi = .gnu, }); + if (skip_non_native and !std.zig.target.isNative(&target.query, &target.result, &b.graph.host.result)) return; + const generated_h_step = b.addWriteFile("generated.h", "#define GENERATED_DEFINE \"foo\""); add(b, b.graph.host, .any, test_step, generated_h_step); diff --git a/test/tests.zig b/test/tests.zig index 46fa29e9312190350e72deed1848fa27336a9a94..989d4482571365a13c165422f94f84ccb6b4c13c 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -2374,6 +2374,7 @@ pub fn addErrorTraceTests(b: *std.Build, options: ErrorTracesContext.Options) *S pub fn addStandaloneTests( b: *std.Build, optimize_modes: []const OptimizeMode, + skip_non_native: bool, enable_macos_sdk: bool, enable_ios_sdk: bool, enable_symlinks_windows: bool, @@ -2388,6 +2389,7 @@ pub fn addStandaloneTests( .simple_skip_release_safe = mem.findScalar(OptimizeMode, optimize_modes, .safe) == null, .simple_skip_release_fast = mem.findScalar(OptimizeMode, optimize_modes, .fast) == null, .simple_skip_release_small = mem.findScalar(OptimizeMode, optimize_modes, .small) == null, + .skip_non_native = skip_non_native, }); const test_cases_dep_step = test_cases_dep.builder.default_step; test_cases_dep_step.name = test_cases_dep_name;