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 {
8181 docs_step.dependOn(langref_step);
8282 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;
8485 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;
8687 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;
8788 const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release;
8889 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;
9091 const skip_libc = b.option(bool, "skip-libc", "Main test suite skips tests that link libc") orelse false;
9192 const skip_single_threaded = b.option(bool, "skip-single-threaded", "Main test suite skips tests that are single-threaded") orelse false;
9293 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 {
449450 .include_paths = &.{},
450451 .skip_single_threaded = skip_single_threaded,
451452 .skip_non_native = skip_non_native,
453 .test_default_only = test_default_only,
452454 .skip_freebsd = skip_freebsd,
453455 .skip_netbsd = skip_netbsd,
454456 .skip_windows = skip_windows,
......@@ -471,6 +473,7 @@ pub fn build(b: *std.Build) !void {
471473 .include_paths = &.{},
472474 .skip_single_threaded = true,
473475 .skip_non_native = skip_non_native,
476 .test_default_only = test_default_only,
474477 .skip_freebsd = skip_freebsd,
475478 .skip_netbsd = skip_netbsd,
476479 .skip_windows = skip_windows,
......@@ -492,6 +495,7 @@ pub fn build(b: *std.Build) !void {
492495 .include_paths = &.{},
493496 .skip_single_threaded = true,
494497 .skip_non_native = skip_non_native,
498 .test_default_only = test_default_only,
495499 .skip_freebsd = skip_freebsd,
496500 .skip_netbsd = skip_netbsd,
497501 .skip_windows = skip_windows,
......@@ -513,6 +517,7 @@ pub fn build(b: *std.Build) !void {
513517 .include_paths = &.{},
514518 .skip_single_threaded = skip_single_threaded,
515519 .skip_non_native = skip_non_native,
520 .test_default_only = test_default_only,
516521 .skip_freebsd = skip_freebsd,
517522 .skip_netbsd = skip_netbsd,
518523 .skip_windows = skip_windows,
test/tests.zig+174-152
......@@ -44,7 +44,7 @@ const test_targets = blk: {
4444 break :blk [_]TestTarget{
4545 // Native Targets
4646
47 .{},
47 .{}, // 0 index must be all defaults
4848 .{
4949 .link_libc = true,
5050 },
......@@ -2224,6 +2224,7 @@ const ModuleTestOptions = struct {
22242224 desc: []const u8,
22252225 optimize_modes: []const OptimizeMode,
22262226 include_paths: []const []const u8,
2227 test_default_only: bool,
22272228 skip_single_threaded: bool,
22282229 skip_non_native: bool,
22292230 skip_freebsd: bool,
......@@ -2235,13 +2236,21 @@ const ModuleTestOptions = struct {
22352236 skip_libc: bool,
22362237 max_rss: usize = 0,
22372238 no_builtin: bool = false,
2238 build_options: ?*std.Build.Step.Options = null,
2239 build_options: ?*Step.Options = null,
22392240};
22402241
22412242pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
22422243 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| {
22452254 if (test_target.skip_modules.len > 0) {
22462255 for (test_target.skip_modules) |skip_mod| {
22472256 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 {
23062315 } else false;
23072316 if (!want_this_mode) continue;
23082317
2309 const libc_suffix = if (test_target.link_libc == true) "-libc" else "";
2310 const model_txt = target.cpu.model.name;
2318 addOneModuleTest(b, step, test_target, &resolved_target, triple_txt, options);
2319 }
2320 return step;
2321}
23112322
2312 // wasm32-wasi builds need more RAM, idk why
2313 const max_rss = if (target.os.tag == .wasi)
2314 options.max_rss * 2
2315 else
2316 options.max_rss;
2323fn addOneModuleTest(
2324 b: *std.Build,
2325 step: *Step,
2326 test_target: *const TestTarget,
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(.{
2319 .root_module = b.createModule(.{
2320 .root_source_file = b.path(options.root_src),
2321 .optimize = test_target.optimize_mode,
2322 .target = resolved_target,
2323 .link_libc = test_target.link_libc,
2324 .pic = test_target.pic,
2325 .strip = test_target.strip,
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"),
2392 if (target.ofmt == std.Target.ObjectFormat.c) {
2393 var altered_query = test_target.target;
2394 altered_query.ofmt = null;
2395
2396 const compile_c = b.createModule(.{
2397 .root_source_file = null,
2398 .link_libc = test_target.link_libc,
2399 .target = b.resolveTargetQuery(altered_query),
23332400 });
2334 these_tests.linkage = test_target.linkage;
2335 if (options.no_builtin) these_tests.root_module.no_builtin = false;
2336 if (options.build_options) |build_options| {
2337 these_tests.root_module.addOptions("build_options", build_options);
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,
2401 const compile_c_exe = b.addExecutable(.{
2402 .name = qualified_name,
2403 .root_module = compile_c,
2404 .zig_lib_dir = b.path("lib"),
23672405 });
23682406
2369 if (target.ofmt == std.Target.ObjectFormat.c) {
2370 var altered_query = test_target.target;
2371 altered_query.ofmt = null;
2372
2373 const compile_c = b.createModule(.{
2374 .root_source_file = null,
2375 .link_libc = test_target.link_libc,
2376 .target = b.resolveTargetQuery(altered_query),
2377 });
2378 const compile_c_exe = b.addExecutable(.{
2379 .name = qualified_name,
2380 .root_module = compile_c,
2381 .zig_lib_dir = b.path("lib"),
2382 });
2383
2384 compile_c.addCSourceFile(.{
2385 .file = these_tests.getEmittedBin(),
2386 .flags = &.{
2387 // Tracking issue for making the C backend generate C89 compatible code:
2388 // https://github.com/ziglang/zig/issues/19468
2389 "-std=c99",
2390 "-Werror",
2391
2392 "-Wall",
2393 "-Wembedded-directive",
2394 "-Wempty-translation-unit",
2395 "-Wextra",
2396 "-Wgnu",
2397 "-Winvalid-utf8",
2398 "-Wkeyword-macro",
2399 "-Woverlength-strings",
2400
2401 // Tracking issue for making the C backend generate code
2402 // that does not trigger warnings:
2403 // https://github.com/ziglang/zig/issues/19467
2404
2405 // spotted everywhere
2406 "-Wno-builtin-requires-header",
2407
2408 // spotted on linux
2409 "-Wno-braced-scalar-init",
2410 "-Wno-excess-initializers",
2411 "-Wno-incompatible-pointer-types-discards-qualifiers",
2412 "-Wno-unused",
2413 "-Wno-unused-parameter",
2414
2415 // spotted on darwin
2416 "-Wno-incompatible-pointer-types",
2417
2418 // https://github.com/llvm/llvm-project/issues/153314
2419 "-Wno-unterminated-string-initialization",
2420
2421 // In both Zig and C it is legal to return a pointer to a
2422 // local. The C backend lowers such thing directly, so the
2423 // corresponding warning in C must be disabled.
2424 "-Wno-return-stack-address",
2425 },
2426 });
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 }
2407 compile_c.addCSourceFile(.{
2408 .file = these_tests.getEmittedBin(),
2409 .flags = &.{
2410 // Tracking issue for making the C backend generate C89 compatible code:
2411 // https://github.com/ziglang/zig/issues/19468
2412 "-std=c99",
2413 "-Werror",
2414
2415 "-Wall",
2416 "-Wembedded-directive",
2417 "-Wempty-translation-unit",
2418 "-Wextra",
2419 "-Wgnu",
2420 "-Winvalid-utf8",
2421 "-Wkeyword-macro",
2422 "-Woverlength-strings",
2423
2424 // Tracking issue for making the C backend generate code
2425 // that does not trigger warnings:
2426 // https://github.com/ziglang/zig/issues/19467
2427
2428 // spotted everywhere
2429 "-Wno-builtin-requires-header",
2430
2431 // spotted on linux
2432 "-Wno-braced-scalar-init",
2433 "-Wno-excess-initializers",
2434 "-Wno-incompatible-pointer-types-discards-qualifiers",
2435 "-Wno-unused",
2436 "-Wno-unused-parameter",
2437
2438 // spotted on darwin
2439 "-Wno-incompatible-pointer-types",
2440
2441 // https://github.com/llvm/llvm-project/issues/153314
2442 "-Wno-unterminated-string-initialization",
2443
2444 // In both Zig and C it is legal to return a pointer to a
2445 // local. The C backend lowers such thing directly, so the
2446 // corresponding warning in C must be disabled.
2447 "-Wno-return-stack-address",
2448 },
2449 });
2450 compile_c.addIncludePath(b.path("lib")); // for zig.h
2451 if (target.os.tag == .windows) {
2452 if (true) {
2453 // Unfortunately this requires about 8G of RAM for clang to compile
2454 // and our Windows CI runners do not have this much.
2455 // TODO This is not an appropriate way to work around this problem.
2456 step.dependOn(&these_tests.step);
2457 return;
2458 }
2459 if (test_target.link_libc == false) {
2460 compile_c_exe.subsystem = .Console;
2461 compile_c.linkSystemLibrary("kernel32", .{});
2462 compile_c.linkSystemLibrary("ntdll", .{});
2463 }
2464 if (mem.eql(u8, options.name, "std")) {
24352465 if (test_target.link_libc == false) {
2436 compile_c_exe.subsystem = .Console;
2437 compile_c.linkSystemLibrary("kernel32", .{});
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", .{});
2466 compile_c.linkSystemLibrary("shell32", .{});
2467 compile_c.linkSystemLibrary("advapi32", .{});
24482468 }
2469 compile_c.linkSystemLibrary("crypt32", .{});
2470 compile_c.linkSystemLibrary("ws2_32", .{});
2471 compile_c.linkSystemLibrary("ole32", .{});
24492472 }
2473 }
24502474
2451 const run = b.addRunArtifact(compile_c_exe);
2452 run.skip_foreign_checks = true;
2453 run.enableTestRunnerMode();
2454 run.setName(b.fmt("run test {s}", .{qualified_name}));
2475 const run = b.addRunArtifact(compile_c_exe);
2476 run.skip_foreign_checks = true;
2477 run.enableTestRunnerMode();
2478 run.setName(b.fmt("run test {s}", .{qualified_name}));
24552479
2456 step.dependOn(&run.step);
2457 } else if (target.cpu.arch.isSpirV()) {
2458 // Don't run spirv binaries
2459 _ = these_tests.getEmittedBin();
2460 step.dependOn(&these_tests.step);
2461 } else {
2462 const run = b.addRunArtifact(these_tests);
2463 run.skip_foreign_checks = true;
2464 run.setName(b.fmt("run test {s}", .{qualified_name}));
2480 step.dependOn(&run.step);
2481 } else if (target.cpu.arch.isSpirV()) {
2482 // Don't run spirv binaries
2483 _ = these_tests.getEmittedBin();
2484 step.dependOn(&these_tests.step);
2485 } else {
2486 const run = b.addRunArtifact(these_tests);
2487 run.skip_foreign_checks = true;
2488 run.setName(b.fmt("run test {s}", .{qualified_name}));
24652489
2466 step.dependOn(&run.step);
2467 }
2490 step.dependOn(&run.step);
24682491 }
2469 return step;
24702492}
24712493
24722494pub fn wouldUseLlvm(use_llvm: ?bool, query: std.Target.Query, optimize_mode: OptimizeMode) bool {