authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2024-02-17 17:47:13+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-07 16:05:54-07:00
log5d5e89aa8d5a454bce2c93555d3bc7c7ae1aa162
treed8a9766a12b90ac7ab9c80b562c2c8c46d29740b
parent33b8fdb6bc68fc6eb2c401070d062d153d780a6e

Promote linker test cases to packages


6 files changed, 144 insertions(+), 142 deletions(-)

build.zig+1-1
......@@ -524,7 +524,7 @@ pub fn build(b: *std.Build) !void {
524524 enable_symlinks_windows,
525525 ));
526526 test_step.dependOn(tests.addCAbiTests(b, skip_non_native, skip_release));
527 test_step.dependOn(tests.addLinkTests(b, enable_macos_sdk, enable_ios_sdk, false, enable_symlinks_windows));
527 test_step.dependOn(tests.addLinkTests(b, enable_macos_sdk, enable_ios_sdk, enable_symlinks_windows));
528528 test_step.dependOn(tests.addStackTraceTests(b, test_filters, optimization_modes));
529529 test_step.dependOn(tests.addCliTests(b));
530530 test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filters, optimization_modes));
build.zig.zon+3
......@@ -7,6 +7,9 @@
77 .standalone_test_cases = .{
88 .path = "test/standalone",
99 },
10 .link_test_cases = .{
11 .path = "test/link",
12 },
1013 },
1114 .paths = .{""},
1215}
test/link.zig deleted-94
......@@ -1,94 +0,0 @@
1pub const Case = struct {
2 build_root: []const u8,
3 import: type,
4};
5
6pub const cases = [_]Case{
7 .{
8 .build_root = "test/link",
9 .import = @import("link/link.zig"),
10 },
11
12 .{
13 .build_root = "test/link/bss",
14 .import = @import("link/bss/build.zig"),
15 },
16 .{
17 .build_root = "test/link/common_symbols",
18 .import = @import("link/common_symbols/build.zig"),
19 },
20 .{
21 .build_root = "test/link/common_symbols_alignment",
22 .import = @import("link/common_symbols_alignment/build.zig"),
23 },
24 .{
25 .build_root = "test/link/interdependent_static_c_libs",
26 .import = @import("link/interdependent_static_c_libs/build.zig"),
27 },
28 .{
29 .build_root = "test/link/static_libs_from_object_files",
30 .import = @import("link/static_libs_from_object_files/build.zig"),
31 },
32 .{
33 .build_root = "test/link/glibc_compat",
34 .import = @import("link/glibc_compat/build.zig"),
35 },
36
37 // WASM Cases
38 .{
39 .build_root = "test/link/wasm/archive",
40 .import = @import("link/wasm/archive/build.zig"),
41 },
42 .{
43 .build_root = "test/link/wasm/basic-features",
44 .import = @import("link/wasm/basic-features/build.zig"),
45 },
46 .{
47 .build_root = "test/link/wasm/bss",
48 .import = @import("link/wasm/bss/build.zig"),
49 },
50 .{
51 .build_root = "test/link/wasm/export",
52 .import = @import("link/wasm/export/build.zig"),
53 },
54 .{
55 .build_root = "test/link/wasm/export-data",
56 .import = @import("link/wasm/export-data/build.zig"),
57 },
58 .{
59 .build_root = "test/link/wasm/extern",
60 .import = @import("link/wasm/extern/build.zig"),
61 },
62 .{
63 .build_root = "test/link/wasm/extern-mangle",
64 .import = @import("link/wasm/extern-mangle/build.zig"),
65 },
66 .{
67 .build_root = "test/link/wasm/function-table",
68 .import = @import("link/wasm/function-table/build.zig"),
69 },
70 .{
71 .build_root = "test/link/wasm/infer-features",
72 .import = @import("link/wasm/infer-features/build.zig"),
73 },
74 .{
75 .build_root = "test/link/wasm/producers",
76 .import = @import("link/wasm/producers/build.zig"),
77 },
78 .{
79 .build_root = "test/link/wasm/segments",
80 .import = @import("link/wasm/segments/build.zig"),
81 },
82 .{
83 .build_root = "test/link/wasm/shared-memory",
84 .import = @import("link/wasm/shared-memory/build.zig"),
85 },
86 .{
87 .build_root = "test/link/wasm/stack_pointer",
88 .import = @import("link/wasm/stack_pointer/build.zig"),
89 },
90 .{
91 .build_root = "test/link/wasm/type",
92 .import = @import("link/wasm/type/build.zig"),
93 },
94};
test/link/build.zig created+54
......@@ -0,0 +1,54 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const link = @import("link.zig");
4
5pub fn build(b: *std.Build) void {
6 const step = b.step("test", "Run link test cases");
7 b.default_step = step;
8
9 const enable_ios_sdk = b.option(bool, "enable_ios_sdk", "Run tests requiring presence of iOS SDK and frameworks") orelse false;
10 const enable_macos_sdk = b.option(bool, "enable_macos_sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse enable_ios_sdk;
11 const enable_symlinks_windows = b.option(bool, "enable_symlinks_windows", "Run tests requiring presence of symlinks on Windows") orelse false;
12 const omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows;
13
14 const build_opts: link.BuildOptions = .{
15 .has_ios_sdk = enable_ios_sdk,
16 .has_macos_sdk = enable_macos_sdk,
17 .has_symlinks_windows = omit_symlinks,
18 };
19 step.dependOn(@import("elf.zig").testAll(b, build_opts));
20 step.dependOn(@import("macho.zig").testAll(b, build_opts));
21
22 add_dep_steps: for (b.available_deps) |available_dep| {
23 const dep_name, const dep_hash = available_dep;
24
25 const all_pkgs = @import("root").dependencies.packages;
26 inline for (@typeInfo(all_pkgs).Struct.decls) |decl| {
27 const pkg_hash = decl.name;
28 if (std.mem.eql(u8, dep_hash, pkg_hash)) {
29 const pkg = @field(all_pkgs, pkg_hash);
30 if (!@hasDecl(pkg, "build_zig")) {
31 std.debug.panic("link test case '{s}' is missing a 'build.zig' file", .{dep_name});
32 }
33 const requires_ios_sdk = @hasDecl(pkg.build_zig, "requires_ios_sdk") and
34 pkg.build_zig.requires_ios_sdk;
35 const requires_macos_sdk = @hasDecl(pkg.build_zig, "requires_macos_sdk") and
36 pkg.build_zig.requires_macos_sdk;
37 const requires_symlinks = @hasDecl(pkg.build_zig, "requires_symlinks") and
38 pkg.build_zig.requires_symlinks;
39 if ((requires_symlinks and omit_symlinks) or
40 (requires_macos_sdk and !enable_macos_sdk) or
41 (requires_ios_sdk and !enable_ios_sdk))
42 {
43 continue :add_dep_steps;
44 }
45 break;
46 }
47 } else unreachable;
48
49 const dep = b.dependency(dep_name, .{});
50 const dep_step = dep.builder.default_step;
51 dep_step.name = b.fmt("link_test_cases.{s}", .{dep_name});
52 step.dependOn(dep_step);
53 }
54}
test/link/build.zig.zon created+69
......@@ -0,0 +1,69 @@
1.{
2 .name = "link_test_cases",
3 .version = "0.0.0",
4 .dependencies = .{
5 .bss = .{
6 .path = "bss",
7 },
8 .common_symbols_alignment = .{
9 .path = "common_symbols_alignment",
10 },
11 .interdependent_static_c_libs = .{
12 .path = "interdependent_static_c_libs",
13 },
14 .static_libs_from_object_files = .{
15 .path = "static_libs_from_object_files",
16 },
17 .glibc_compat = .{
18 .path = "glibc_compat",
19 },
20 // WASM Cases
21 .wasm_archive = .{
22 .path = "wasm/archive",
23 },
24 .wasm_basic_features = .{
25 .path = "wasm/basic-features",
26 },
27 .wasm_bss = .{
28 .path = "wasm/bss",
29 },
30 .wasm_export = .{
31 .path = "wasm/export",
32 },
33 .wasm_export_data = .{
34 .path = "wasm/export-data",
35 },
36 .wasm_extern = .{
37 .path = "wasm/extern",
38 },
39 .wasm_extern_mangle = .{
40 .path = "wasm/extern-mangle",
41 },
42 .wasm_function_table = .{
43 .path = "wasm/function-table",
44 },
45 .wasm_infer_features = .{
46 .path = "wasm/infer-features",
47 },
48 .wasm_producers = .{
49 .path = "wasm/producers",
50 },
51 .wasm_segments = .{
52 .path = "wasm/segments",
53 },
54 .wasm_shared_memory = .{
55 .path = "wasm/shared-memory",
56 },
57 .wasm_stack_pointer = .{
58 .path = "wasm/stack_pointer",
59 },
60 .wasm_type = .{
61 .path = "wasm/type",
62 },
63 },
64 .paths = .{
65 "build.zig",
66 "build.zig.zon",
67 "link.zig",
68 },
69}
test/tests.zig+17-47
......@@ -11,7 +11,6 @@ const stack_traces = @import("stack_traces.zig");
1111const assemble_and_link = @import("assemble_and_link.zig");
1212const translate_c = @import("translate_c.zig");
1313const run_translated_c = @import("run_translated_c.zig");
14const link = @import("link.zig");
1514
1615// Implementations
1716pub const TranslateCContext = @import("src/TranslateC.zig");
......@@ -662,6 +661,12 @@ pub fn addStackTraceTests(
662661 return cases.step;
663662}
664663
664fn compilerHasPackageManager(b: *std.Build) bool {
665 // We can only use dependencies if the compiler was built with support for package management.
666 // (zig2 doesn't support it, but we still need to construct a build graph to build stage3.)
667 return b.available_deps.len != 0;
668}
669
665670pub fn addStandaloneTests(
666671 b: *std.Build,
667672 optimize_modes: []const OptimizeMode,
......@@ -670,12 +675,7 @@ pub fn addStandaloneTests(
670675 enable_symlinks_windows: bool,
671676) *Step {
672677 const step = b.step("test-standalone", "Run the standalone tests");
673
674 // We can only use dependencies if the compiler was built with support for package management.
675 // (zig2 doesn't support it, but we still need to construct a build graph to build stage3.)
676 const package_management_available = b.available_deps.len != 0;
677
678 if (package_management_available) {
678 if (compilerHasPackageManager(b)) {
679679 const test_cases_dep_name = "standalone_test_cases";
680680 const test_cases_dep = b.dependency(test_cases_dep_name, .{
681681 .enable_ios_sdk = enable_ios_sdk,
......@@ -690,7 +690,6 @@ pub fn addStandaloneTests(
690690 test_cases_dep_step.name = b.dupe(test_cases_dep_name);
691691 step.dependOn(test_cases_dep.builder.default_step);
692692 }
693
694693 return step;
695694}
696695
......@@ -698,49 +697,20 @@ pub fn addLinkTests(
698697 b: *std.Build,
699698 enable_macos_sdk: bool,
700699 enable_ios_sdk: bool,
701 omit_stage2: bool,
702700 enable_symlinks_windows: bool,
703701) *Step {
704702 const step = b.step("test-link", "Run the linker tests");
705 const omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows;
706
707 inline for (link.cases) |case| {
708 if (mem.eql(u8, @typeName(case.import), "test.link.link")) {
709 const dep = b.anonymousDependency(case.build_root, case.import, .{
710 .has_macos_sdk = enable_macos_sdk,
711 .has_ios_sdk = enable_ios_sdk,
712 .has_symlinks_windows = !omit_symlinks,
713 });
714 const dep_step = dep.builder.default_step;
715 assert(mem.startsWith(u8, dep.builder.dep_prefix, "test."));
716 const dep_prefix_adjusted = dep.builder.dep_prefix["test.".len..];
717 dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
718 step.dependOn(dep_step);
719 } else {
720 const requires_stage2 = @hasDecl(case.import, "requires_stage2") and
721 case.import.requires_stage2;
722 const requires_symlinks = @hasDecl(case.import, "requires_symlinks") and
723 case.import.requires_symlinks;
724 const requires_macos_sdk = @hasDecl(case.import, "requires_macos_sdk") and
725 case.import.requires_macos_sdk;
726 const requires_ios_sdk = @hasDecl(case.import, "requires_ios_sdk") and
727 case.import.requires_ios_sdk;
728 const bad =
729 (requires_stage2 and omit_stage2) or
730 (requires_symlinks and omit_symlinks) or
731 (requires_macos_sdk and !enable_macos_sdk) or
732 (requires_ios_sdk and !enable_ios_sdk);
733 if (!bad) {
734 const dep = b.anonymousDependency(case.build_root, case.import, .{});
735 const dep_step = dep.builder.default_step;
736 assert(mem.startsWith(u8, dep.builder.dep_prefix, "test."));
737 const dep_prefix_adjusted = dep.builder.dep_prefix["test.".len..];
738 dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
739 step.dependOn(dep_step);
740 }
741 }
703 if (compilerHasPackageManager(b)) {
704 const test_cases_dep_name = "link_test_cases";
705 const test_cases_dep = b.dependency(test_cases_dep_name, .{
706 .enable_ios_sdk = enable_ios_sdk,
707 .enable_macos_sdk = enable_macos_sdk,
708 .enable_symlinks_windows = enable_symlinks_windows,
709 });
710 const test_cases_dep_step = test_cases_dep.builder.default_step;
711 test_cases_dep_step.name = b.dupe(test_cases_dep_name);
712 step.dependOn(test_cases_dep.builder.default_step);
742713 }
743
744714 return step;
745715}
746716