authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-08 14:08:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-08 16:43:42-07:00
log8964737ffc1502894f8261bb8a5ab541997f6f65
treec31623d94610c56f94aea180b9c7f3cd82450651
parent328ae41468f3514251ac1b0726c41eca9fbc3fb5

build.zig: add -Dtest-default-only option

handy during development when it is already known that not all tests will pass.

2 files changed, 181 insertions(+), 154 deletions(-)

build.zig+7-2
...@@ -81,12 +81,13 @@ pub fn build(b: *std.Build) !void {...@@ -81,12 +81,13 @@ pub fn build(b: *std.Build) !void {
81 docs_step.dependOn(langref_step);81 docs_step.dependOn(langref_step);
82 docs_step.dependOn(std_docs_step);82 docs_step.dependOn(std_docs_step);
8383
84 const test_default_only = b.option(bool, "test-default-only", "Limit test matrix to exactly one target configuration") orelse false;
84 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;85 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
85 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;86 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse test_default_only;
86 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;87 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;
87 const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release;88 const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release;
88 const skip_release_safe = b.option(bool, "skip-release-safe", "Main test suite skips release-safe builds") orelse skip_release;89 const skip_release_safe = b.option(bool, "skip-release-safe", "Main test suite skips release-safe builds") orelse skip_release;
89 const skip_non_native = b.option(bool, "skip-non-native", "Main test suite skips non-native builds") orelse false;90 const skip_non_native = b.option(bool, "skip-non-native", "Main test suite skips non-native builds") orelse test_default_only;
90 const skip_libc = b.option(bool, "skip-libc", "Main test suite skips tests that link libc") orelse false;91 const skip_libc = b.option(bool, "skip-libc", "Main test suite skips tests that link libc") orelse false;
91 const skip_single_threaded = b.option(bool, "skip-single-threaded", "Main test suite skips tests that are single-threaded") orelse false;92 const skip_single_threaded = b.option(bool, "skip-single-threaded", "Main test suite skips tests that are single-threaded") orelse false;
92 const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false;93 const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false;
...@@ -449,6 +450,7 @@ pub fn build(b: *std.Build) !void {...@@ -449,6 +450,7 @@ pub fn build(b: *std.Build) !void {
449 .include_paths = &.{},450 .include_paths = &.{},
450 .skip_single_threaded = skip_single_threaded,451 .skip_single_threaded = skip_single_threaded,
451 .skip_non_native = skip_non_native,452 .skip_non_native = skip_non_native,
453 .test_default_only = test_default_only,
452 .skip_freebsd = skip_freebsd,454 .skip_freebsd = skip_freebsd,
453 .skip_netbsd = skip_netbsd,455 .skip_netbsd = skip_netbsd,
454 .skip_windows = skip_windows,456 .skip_windows = skip_windows,
...@@ -471,6 +473,7 @@ pub fn build(b: *std.Build) !void {...@@ -471,6 +473,7 @@ pub fn build(b: *std.Build) !void {
471 .include_paths = &.{},473 .include_paths = &.{},
472 .skip_single_threaded = true,474 .skip_single_threaded = true,
473 .skip_non_native = skip_non_native,475 .skip_non_native = skip_non_native,
476 .test_default_only = test_default_only,
474 .skip_freebsd = skip_freebsd,477 .skip_freebsd = skip_freebsd,
475 .skip_netbsd = skip_netbsd,478 .skip_netbsd = skip_netbsd,
476 .skip_windows = skip_windows,479 .skip_windows = skip_windows,
...@@ -492,6 +495,7 @@ pub fn build(b: *std.Build) !void {...@@ -492,6 +495,7 @@ pub fn build(b: *std.Build) !void {
492 .include_paths = &.{},495 .include_paths = &.{},
493 .skip_single_threaded = true,496 .skip_single_threaded = true,
494 .skip_non_native = skip_non_native,497 .skip_non_native = skip_non_native,
498 .test_default_only = test_default_only,
495 .skip_freebsd = skip_freebsd,499 .skip_freebsd = skip_freebsd,
496 .skip_netbsd = skip_netbsd,500 .skip_netbsd = skip_netbsd,
497 .skip_windows = skip_windows,501 .skip_windows = skip_windows,
...@@ -513,6 +517,7 @@ pub fn build(b: *std.Build) !void {...@@ -513,6 +517,7 @@ pub fn build(b: *std.Build) !void {
513 .include_paths = &.{},517 .include_paths = &.{},
514 .skip_single_threaded = skip_single_threaded,518 .skip_single_threaded = skip_single_threaded,
515 .skip_non_native = skip_non_native,519 .skip_non_native = skip_non_native,
520 .test_default_only = test_default_only,
516 .skip_freebsd = skip_freebsd,521 .skip_freebsd = skip_freebsd,
517 .skip_netbsd = skip_netbsd,522 .skip_netbsd = skip_netbsd,
518 .skip_windows = skip_windows,523 .skip_windows = skip_windows,
test/tests.zig+174-152
...@@ -44,7 +44,7 @@ const test_targets = blk: {...@@ -44,7 +44,7 @@ const test_targets = blk: {
44 break :blk [_]TestTarget{44 break :blk [_]TestTarget{
45 // Native Targets45 // Native Targets
4646
47 .{},47 .{}, // 0 index must be all defaults
48 .{48 .{
49 .link_libc = true,49 .link_libc = true,
50 },50 },
...@@ -2224,6 +2224,7 @@ const ModuleTestOptions = struct {...@@ -2224,6 +2224,7 @@ const ModuleTestOptions = struct {
2224 desc: []const u8,2224 desc: []const u8,
2225 optimize_modes: []const OptimizeMode,2225 optimize_modes: []const OptimizeMode,
2226 include_paths: []const []const u8,2226 include_paths: []const []const u8,
2227 test_default_only: bool,
2227 skip_single_threaded: bool,2228 skip_single_threaded: bool,
2228 skip_non_native: bool,2229 skip_non_native: bool,
2229 skip_freebsd: bool,2230 skip_freebsd: bool,
...@@ -2235,13 +2236,21 @@ const ModuleTestOptions = struct {...@@ -2235,13 +2236,21 @@ const ModuleTestOptions = struct {
2235 skip_libc: bool,2236 skip_libc: bool,
2236 max_rss: usize = 0,2237 max_rss: usize = 0,
2237 no_builtin: bool = false,2238 no_builtin: bool = false,
2238 build_options: ?*std.Build.Step.Options = null,2239 build_options: ?*Step.Options = null,
2239};2240};
22402241
2241pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {2242pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
2242 const step = b.step(b.fmt("test-{s}", .{options.name}), options.desc);2243 const step = b.step(b.fmt("test-{s}", .{options.name}), options.desc);
22432244
2244 for_targets: for (test_targets) |test_target| {2245 if (options.test_default_only) {
2246 const test_target = &test_targets[0];
2247 const resolved_target = b.resolveTargetQuery(test_target.target);
2248 const triple_txt = resolved_target.query.zigTriple(b.allocator) catch @panic("OOM");
2249 addOneModuleTest(b, step, test_target, &resolved_target, triple_txt, options);
2250 return step;
2251 }
2252
2253 for_targets: for (&test_targets) |*test_target| {
2245 if (test_target.skip_modules.len > 0) {2254 if (test_target.skip_modules.len > 0) {
2246 for (test_target.skip_modules) |skip_mod| {2255 for (test_target.skip_modules) |skip_mod| {
2247 if (std.mem.eql(u8, options.name, skip_mod)) continue :for_targets;2256 if (std.mem.eql(u8, options.name, skip_mod)) continue :for_targets;
...@@ -2306,167 +2315,180 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {...@@ -2306,167 +2315,180 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
2306 } else false;2315 } else false;
2307 if (!want_this_mode) continue;2316 if (!want_this_mode) continue;
23082317
2309 const libc_suffix = if (test_target.link_libc == true) "-libc" else "";2318 addOneModuleTest(b, step, test_target, &resolved_target, triple_txt, options);
2310 const model_txt = target.cpu.model.name;2319 }
2320 return step;
2321}
23112322
2312 // wasm32-wasi builds need more RAM, idk why2323fn addOneModuleTest(
2313 const max_rss = if (target.os.tag == .wasi)2324 b: *std.Build,
2314 options.max_rss * 22325 step: *Step,
2315 else2326 test_target: *const TestTarget,
2316 options.max_rss;2327 resolved_target: *const std.Build.ResolvedTarget,
2328 triple_txt: []const u8,
2329 options: ModuleTestOptions,
2330) void {
2331 const target = &resolved_target.result;
2332 const libc_suffix = if (test_target.link_libc == true) "-libc" else "";
2333 const model_txt = target.cpu.model.name;
2334
2335 // wasm32-wasi builds need more RAM, idk why
2336 const max_rss = if (target.os.tag == .wasi)
2337 options.max_rss * 2
2338 else
2339 options.max_rss;
2340
2341 const these_tests = b.addTest(.{
2342 .root_module = b.createModule(.{
2343 .root_source_file = b.path(options.root_src),
2344 .optimize = test_target.optimize_mode,
2345 .target = resolved_target.*,
2346 .link_libc = test_target.link_libc,
2347 .pic = test_target.pic,
2348 .strip = test_target.strip,
2349 .single_threaded = test_target.single_threaded,
2350 }),
2351 .max_rss = max_rss,
2352 .filters = options.test_filters,
2353 .use_llvm = test_target.use_llvm,
2354 .use_lld = test_target.use_lld,
2355 .zig_lib_dir = b.path("lib"),
2356 });
2357 these_tests.linkage = test_target.linkage;
2358 if (options.no_builtin) these_tests.root_module.no_builtin = false;
2359 if (options.build_options) |build_options| {
2360 these_tests.root_module.addOptions("build_options", build_options);
2361 }
2362 const single_threaded_suffix = if (test_target.single_threaded == true) "-single" else "";
2363 const backend_suffix = if (test_target.use_llvm == true)
2364 "-llvm"
2365 else if (target.ofmt == std.Target.ObjectFormat.c)
2366 "-cbe"
2367 else if (test_target.use_llvm == false)
2368 "-selfhosted"
2369 else
2370 "";
2371 const use_lld = if (test_target.use_lld == false) "-no-lld" else "";
2372 const linkage_name = if (test_target.linkage) |linkage| switch (linkage) {
2373 inline else => |t| "-" ++ @tagName(t),
2374 } else "";
2375 const use_pic = if (test_target.pic == true) "-pic" else "";
2376
2377 for (options.include_paths) |include_path| these_tests.root_module.addIncludePath(b.path(include_path));
2378
2379 const qualified_name = b.fmt("{s}-{s}-{s}-{t}{s}{s}{s}{s}{s}{s}", .{
2380 options.name,
2381 triple_txt,
2382 model_txt,
2383 test_target.optimize_mode,
2384 libc_suffix,
2385 single_threaded_suffix,
2386 backend_suffix,
2387 use_lld,
2388 linkage_name,
2389 use_pic,
2390 });
23172391
2318 const these_tests = b.addTest(.{2392 if (target.ofmt == std.Target.ObjectFormat.c) {
2319 .root_module = b.createModule(.{2393 var altered_query = test_target.target;
2320 .root_source_file = b.path(options.root_src),2394 altered_query.ofmt = null;
2321 .optimize = test_target.optimize_mode,2395
2322 .target = resolved_target,2396 const compile_c = b.createModule(.{
2323 .link_libc = test_target.link_libc,2397 .root_source_file = null,
2324 .pic = test_target.pic,2398 .link_libc = test_target.link_libc,
2325 .strip = test_target.strip,2399 .target = b.resolveTargetQuery(altered_query),
2326 .single_threaded = test_target.single_threaded,
2327 }),
2328 .max_rss = max_rss,
2329 .filters = options.test_filters,
2330 .use_llvm = test_target.use_llvm,
2331 .use_lld = test_target.use_lld,
2332 .zig_lib_dir = b.path("lib"),
2333 });2400 });
2334 these_tests.linkage = test_target.linkage;2401 const compile_c_exe = b.addExecutable(.{
2335 if (options.no_builtin) these_tests.root_module.no_builtin = false;2402 .name = qualified_name,
2336 if (options.build_options) |build_options| {2403 .root_module = compile_c,
2337 these_tests.root_module.addOptions("build_options", build_options);2404 .zig_lib_dir = b.path("lib"),
2338 }
2339 const single_threaded_suffix = if (test_target.single_threaded == true) "-single" else "";
2340 const backend_suffix = if (test_target.use_llvm == true)
2341 "-llvm"
2342 else if (target.ofmt == std.Target.ObjectFormat.c)
2343 "-cbe"
2344 else if (test_target.use_llvm == false)
2345 "-selfhosted"
2346 else
2347 "";
2348 const use_lld = if (test_target.use_lld == false) "-no-lld" else "";
2349 const linkage_name = if (test_target.linkage) |linkage| switch (linkage) {
2350 inline else => |t| "-" ++ @tagName(t),
2351 } else "";
2352 const use_pic = if (test_target.pic == true) "-pic" else "";
2353
2354 for (options.include_paths) |include_path| these_tests.root_module.addIncludePath(b.path(include_path));
2355
2356 const qualified_name = b.fmt("{s}-{s}-{s}-{s}{s}{s}{s}{s}{s}{s}", .{
2357 options.name,
2358 triple_txt,
2359 model_txt,
2360 @tagName(test_target.optimize_mode),
2361 libc_suffix,
2362 single_threaded_suffix,
2363 backend_suffix,
2364 use_lld,
2365 linkage_name,
2366 use_pic,
2367 });2405 });
23682406
2369 if (target.ofmt == std.Target.ObjectFormat.c) {2407 compile_c.addCSourceFile(.{
2370 var altered_query = test_target.target;2408 .file = these_tests.getEmittedBin(),
2371 altered_query.ofmt = null;2409 .flags = &.{
23722410 // Tracking issue for making the C backend generate C89 compatible code:
2373 const compile_c = b.createModule(.{2411 // https://github.com/ziglang/zig/issues/19468
2374 .root_source_file = null,2412 "-std=c99",
2375 .link_libc = test_target.link_libc,2413 "-Werror",
2376 .target = b.resolveTargetQuery(altered_query),2414
2377 });2415 "-Wall",
2378 const compile_c_exe = b.addExecutable(.{2416 "-Wembedded-directive",
2379 .name = qualified_name,2417 "-Wempty-translation-unit",
2380 .root_module = compile_c,2418 "-Wextra",
2381 .zig_lib_dir = b.path("lib"),2419 "-Wgnu",
2382 });2420 "-Winvalid-utf8",
23832421 "-Wkeyword-macro",
2384 compile_c.addCSourceFile(.{2422 "-Woverlength-strings",
2385 .file = these_tests.getEmittedBin(),2423
2386 .flags = &.{2424 // Tracking issue for making the C backend generate code
2387 // Tracking issue for making the C backend generate C89 compatible code:2425 // that does not trigger warnings:
2388 // https://github.com/ziglang/zig/issues/194682426 // https://github.com/ziglang/zig/issues/19467
2389 "-std=c99",2427
2390 "-Werror",2428 // spotted everywhere
23912429 "-Wno-builtin-requires-header",
2392 "-Wall",2430
2393 "-Wembedded-directive",2431 // spotted on linux
2394 "-Wempty-translation-unit",2432 "-Wno-braced-scalar-init",
2395 "-Wextra",2433 "-Wno-excess-initializers",
2396 "-Wgnu",2434 "-Wno-incompatible-pointer-types-discards-qualifiers",
2397 "-Winvalid-utf8",2435 "-Wno-unused",
2398 "-Wkeyword-macro",2436 "-Wno-unused-parameter",
2399 "-Woverlength-strings",2437
24002438 // spotted on darwin
2401 // Tracking issue for making the C backend generate code2439 "-Wno-incompatible-pointer-types",
2402 // that does not trigger warnings:2440
2403 // https://github.com/ziglang/zig/issues/194672441 // https://github.com/llvm/llvm-project/issues/153314
24042442 "-Wno-unterminated-string-initialization",
2405 // spotted everywhere2443
2406 "-Wno-builtin-requires-header",2444 // In both Zig and C it is legal to return a pointer to a
24072445 // local. The C backend lowers such thing directly, so the
2408 // spotted on linux2446 // corresponding warning in C must be disabled.
2409 "-Wno-braced-scalar-init",2447 "-Wno-return-stack-address",
2410 "-Wno-excess-initializers",2448 },
2411 "-Wno-incompatible-pointer-types-discards-qualifiers",2449 });
2412 "-Wno-unused",2450 compile_c.addIncludePath(b.path("lib")); // for zig.h
2413 "-Wno-unused-parameter",2451 if (target.os.tag == .windows) {
24142452 if (true) {
2415 // spotted on darwin2453 // Unfortunately this requires about 8G of RAM for clang to compile
2416 "-Wno-incompatible-pointer-types",2454 // and our Windows CI runners do not have this much.
24172455 // TODO This is not an appropriate way to work around this problem.
2418 // https://github.com/llvm/llvm-project/issues/1533142456 step.dependOn(&these_tests.step);
2419 "-Wno-unterminated-string-initialization",2457 return;
24202458 }
2421 // In both Zig and C it is legal to return a pointer to a2459 if (test_target.link_libc == false) {
2422 // local. The C backend lowers such thing directly, so the2460 compile_c_exe.subsystem = .Console;
2423 // corresponding warning in C must be disabled.2461 compile_c.linkSystemLibrary("kernel32", .{});
2424 "-Wno-return-stack-address",2462 compile_c.linkSystemLibrary("ntdll", .{});
2425 },2463 }
2426 });2464 if (mem.eql(u8, options.name, "std")) {
2427 compile_c.addIncludePath(b.path("lib")); // for zig.h
2428 if (target.os.tag == .windows) {
2429 if (true) {
2430 // Unfortunately this requires about 8G of RAM for clang to compile
2431 // and our Windows CI runners do not have this much.
2432 step.dependOn(&these_tests.step);
2433 continue;
2434 }
2435 if (test_target.link_libc == false) {2465 if (test_target.link_libc == false) {
2436 compile_c_exe.subsystem = .Console;2466 compile_c.linkSystemLibrary("shell32", .{});
2437 compile_c.linkSystemLibrary("kernel32", .{});2467 compile_c.linkSystemLibrary("advapi32", .{});
2438 compile_c.linkSystemLibrary("ntdll", .{});
2439 }
2440 if (mem.eql(u8, options.name, "std")) {
2441 if (test_target.link_libc == false) {
2442 compile_c.linkSystemLibrary("shell32", .{});
2443 compile_c.linkSystemLibrary("advapi32", .{});
2444 }
2445 compile_c.linkSystemLibrary("crypt32", .{});
2446 compile_c.linkSystemLibrary("ws2_32", .{});
2447 compile_c.linkSystemLibrary("ole32", .{});
2448 }2468 }
2469 compile_c.linkSystemLibrary("crypt32", .{});
2470 compile_c.linkSystemLibrary("ws2_32", .{});
2471 compile_c.linkSystemLibrary("ole32", .{});
2449 }2472 }
2473 }
24502474
2451 const run = b.addRunArtifact(compile_c_exe);2475 const run = b.addRunArtifact(compile_c_exe);
2452 run.skip_foreign_checks = true;2476 run.skip_foreign_checks = true;
2453 run.enableTestRunnerMode();2477 run.enableTestRunnerMode();
2454 run.setName(b.fmt("run test {s}", .{qualified_name}));2478 run.setName(b.fmt("run test {s}", .{qualified_name}));
24552479
2456 step.dependOn(&run.step);2480 step.dependOn(&run.step);
2457 } else if (target.cpu.arch.isSpirV()) {2481 } else if (target.cpu.arch.isSpirV()) {
2458 // Don't run spirv binaries2482 // Don't run spirv binaries
2459 _ = these_tests.getEmittedBin();2483 _ = these_tests.getEmittedBin();
2460 step.dependOn(&these_tests.step);2484 step.dependOn(&these_tests.step);
2461 } else {2485 } else {
2462 const run = b.addRunArtifact(these_tests);2486 const run = b.addRunArtifact(these_tests);
2463 run.skip_foreign_checks = true;2487 run.skip_foreign_checks = true;
2464 run.setName(b.fmt("run test {s}", .{qualified_name}));2488 run.setName(b.fmt("run test {s}", .{qualified_name}));
24652489
2466 step.dependOn(&run.step);2490 step.dependOn(&run.step);
2467 }
2468 }2491 }
2469 return step;
2470}2492}
24712493
2472pub fn wouldUseLlvm(use_llvm: ?bool, query: std.Target.Query, optimize_mode: OptimizeMode) bool {2494pub fn wouldUseLlvm(use_llvm: ?bool, query: std.Target.Query, optimize_mode: OptimizeMode) bool {