Skip to content

Commit 5d5e89a

Browse files
castholmandrewrk
authored andcommitted
Promote linker test cases to packages
1 parent 33b8fdb commit 5d5e89a

File tree

6 files changed

+144
-142
lines changed

6 files changed

+144
-142
lines changed

build.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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

-94
This file was deleted.

test/link/build.zig

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const link = @import("link.zig");
4+
5+
pub 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

+69
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const stack_traces = @import("stack_traces.zig");
1111
const assemble_and_link = @import("assemble_and_link.zig");
1212
const translate_c = @import("translate_c.zig");
1313
const run_translated_c = @import("run_translated_c.zig");
14-
const link = @import("link.zig");
1514

1615
// Implementations
1716
pub const TranslateCContext = @import("src/TranslateC.zig");
@@ -662,6 +661,12 @@ pub fn addStackTraceTests(
662661
return cases.step;
663662
}
664663

664+
fn 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+
665670
pub 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,57 +690,27 @@ 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

697696
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

0 commit comments

Comments
 (0)