authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-13 18:49:27+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:39+01:00
logd93a0763d4c732fcdb3ed891d5b44a10943ccfed
tree47dfd634d9ed9e4f3dc777ca39221fed11da3850
parent041f7d69f0656254cfa62a59af2fa65ac1b1c433

test/link/link: pass build options to elf and macho tests


7 files changed, 76 insertions(+), 63 deletions(-)

test/link.zig-4
......@@ -135,10 +135,6 @@ pub const cases = [_]Case{
135135 .build_root = "test/link/macho/linksection",
136136 .import = @import("link/macho/linksection/build.zig"),
137137 },
138 .{
139 .build_root = "test/link/macho/needed_framework",
140 .import = @import("link/macho/needed_framework/build.zig"),
141 },
142138 .{
143139 .build_root = "test/link/macho/needed_library",
144140 .import = @import("link/macho/needed_library/build.zig"),
test/link/elf.zig+3-1
......@@ -2,7 +2,8 @@
22//! Currently, we support linking x86_64 Linux, but in the future we
33//! will progressively relax those to exercise more combinations.
44
5pub fn testAll(b: *Build) *Step {
5pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
6 _ = build_opts;
67 const elf_step = b.step("test-elf", "Run ELF tests");
78
89 const default_target = b.resolveTargetQuery(.{
......@@ -3901,6 +3902,7 @@ const link = @import("link.zig");
39013902const std = @import("std");
39023903
39033904const Build = std.Build;
3905const BuildOptions = link.BuildOptions;
39043906const Options = link.Options;
39053907const Step = Build.Step;
39063908const WriteFile = Step.WriteFile;
test/link/link.zig+18-2
......@@ -2,10 +2,26 @@ pub fn build(b: *Build) void {
22 const test_step = b.step("test-link", "Run link tests");
33 b.default_step = test_step;
44
5 test_step.dependOn(@import("elf.zig").testAll(b));
6 test_step.dependOn(@import("macho.zig").testAll(b));
5 const has_macos_sdk = b.option(bool, "has_macos_sdk", "whether the host provides a macOS SDK in system path");
6 const has_ios_sdk = b.option(bool, "has_ios_sdk", "whether the host provides a iOS SDK in system path");
7 const has_symlinks_windows = b.option(bool, "has_symlinks_windows", "whether the host is windows and has symlinks enabled");
8
9 const build_opts: BuildOptions = .{
10 .has_macos_sdk = has_macos_sdk orelse false,
11 .has_ios_sdk = has_ios_sdk orelse false,
12 .has_symlinks_windows = has_symlinks_windows orelse false,
13 };
14
15 test_step.dependOn(@import("elf.zig").testAll(b, build_opts));
16 test_step.dependOn(@import("macho.zig").testAll(b, build_opts));
717}
818
19pub const BuildOptions = struct {
20 has_macos_sdk: bool,
21 has_ios_sdk: bool,
22 has_symlinks_windows: bool,
23};
24
925pub const Options = struct {
1026 target: std.Build.ResolvedTarget,
1127 optimize: std.builtin.OptimizeMode = .Debug,
test/link/macho.zig+27-1
......@@ -1,7 +1,7 @@
11//! Here we test our MachO linker for correctness and functionality.
22//! TODO migrate standalone tests from test/link/macho/* to here.
33
4pub fn testAll(b: *std.Build) *Step {
4pub fn testAll(b: *std.Build, build_opts: BuildOptions) *Step {
55 const macho_step = b.step("test-macho", "Run MachO tests");
66
77 const default_target = b.resolveTargetQuery(.{
......@@ -13,6 +13,11 @@ pub fn testAll(b: *std.Build) *Step {
1313 macho_step.dependOn(testSectionBoundarySymbols(b, .{ .target = default_target }));
1414 macho_step.dependOn(testSegmentBoundarySymbols(b, .{ .target = default_target }));
1515
16 // Tests requiring presence of macOS SDK in system path
17 if (build_opts.has_macos_sdk and build_opts.has_symlinks_windows) {
18 macho_step.dependOn(testNeededFramework(b, .{ .target = b.host }));
19 }
20
1621 return macho_step;
1722}
1823
......@@ -151,6 +156,26 @@ fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {
151156 return test_step;
152157}
153158
159fn testNeededFramework(b: *std.Build, opts: Options) *Step {
160 const test_step = addTestStep(b, "macho-needed-framework", opts);
161
162 const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes = "int main() { return 0; }" });
163 exe.linkFrameworkNeeded("Cocoa");
164 exe.dead_strip_dylibs = true;
165
166 const check = exe.checkObject();
167 check.checkInHeaders();
168 check.checkExact("cmd LOAD_DYLIB");
169 check.checkContains("Cocoa");
170 test_step.dependOn(&check.step);
171
172 const run = addRunArtifact(exe);
173 run.expectExitCode(0);
174 test_step.dependOn(&run.step);
175
176 return test_step;
177}
178
154179fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step {
155180 const test_step = addTestStep(b, "macho-section-boundary-symbols", opts);
156181
......@@ -311,5 +336,6 @@ const addSharedLibrary = link.addSharedLibrary;
311336const expectLinkErrors = link.expectLinkErrors;
312337const link = @import("link.zig");
313338const std = @import("std");
339const BuildOptions = link.BuildOptions;
314340const Options = link.Options;
315341const Step = std.Build.Step;
test/link/macho/needed_framework/build.zig deleted-37
......@@ -1,37 +0,0 @@
1const std = @import("std");
2
3pub const requires_symlinks = true;
4pub const requires_macos_sdk = true;
5
6pub fn build(b: *std.Build) void {
7 const test_step = b.step("test", "Test it");
8 b.default_step = test_step;
9
10 add(b, test_step, .Debug);
11 add(b, test_step, .ReleaseFast);
12 add(b, test_step, .ReleaseSmall);
13 add(b, test_step, .ReleaseSafe);
14}
15
16fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
17 // -dead_strip_dylibs
18 // -needed_framework Cocoa
19 const exe = b.addExecutable(.{
20 .name = "test",
21 .optimize = optimize,
22 .target = b.host,
23 });
24 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
25 exe.linkLibC();
26 exe.linkFrameworkNeeded("Cocoa");
27 exe.dead_strip_dylibs = true;
28
29 const check = exe.checkObject();
30 check.checkInHeaders();
31 check.checkExact("cmd LOAD_DYLIB");
32 check.checkContains("Cocoa");
33 test_step.dependOn(&check.step);
34
35 const run_cmd = b.addRunArtifact(exe);
36 test_step.dependOn(&run_cmd.step);
37}
test/link/macho/needed_framework/main.c deleted-3
......@@ -1,3 +0,0 @@
1int main(int argc, char* argv[]) {
2 return 0;
3}
test/tests.zig+28-15
......@@ -750,26 +750,39 @@ pub fn addLinkTests(
750750 const omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows;
751751
752752 inline for (link.cases) |case| {
753 const requires_stage2 = @hasDecl(case.import, "requires_stage2") and
754 case.import.requires_stage2;
755 const requires_symlinks = @hasDecl(case.import, "requires_symlinks") and
756 case.import.requires_symlinks;
757 const requires_macos_sdk = @hasDecl(case.import, "requires_macos_sdk") and
758 case.import.requires_macos_sdk;
759 const requires_ios_sdk = @hasDecl(case.import, "requires_ios_sdk") and
760 case.import.requires_ios_sdk;
761 const bad =
762 (requires_stage2 and omit_stage2) or
763 (requires_symlinks and omit_symlinks) or
764 (requires_macos_sdk and !enable_macos_sdk) or
765 (requires_ios_sdk and !enable_ios_sdk);
766 if (!bad) {
767 const dep = b.anonymousDependency(case.build_root, case.import, .{});
753 if (mem.eql(u8, @typeName(case.import), "test.link.link")) {
754 const dep = b.anonymousDependency(case.build_root, case.import, .{
755 .has_macos_sdk = enable_macos_sdk,
756 .has_ios_sdk = enable_ios_sdk,
757 .has_symlinks_windows = !omit_symlinks,
758 });
768759 const dep_step = dep.builder.default_step;
769760 assert(mem.startsWith(u8, dep.builder.dep_prefix, "test."));
770761 const dep_prefix_adjusted = dep.builder.dep_prefix["test.".len..];
771762 dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
772763 step.dependOn(dep_step);
764 } else {
765 const requires_stage2 = @hasDecl(case.import, "requires_stage2") and
766 case.import.requires_stage2;
767 const requires_symlinks = @hasDecl(case.import, "requires_symlinks") and
768 case.import.requires_symlinks;
769 const requires_macos_sdk = @hasDecl(case.import, "requires_macos_sdk") and
770 case.import.requires_macos_sdk;
771 const requires_ios_sdk = @hasDecl(case.import, "requires_ios_sdk") and
772 case.import.requires_ios_sdk;
773 const bad =
774 (requires_stage2 and omit_stage2) or
775 (requires_symlinks and omit_symlinks) or
776 (requires_macos_sdk and !enable_macos_sdk) or
777 (requires_ios_sdk and !enable_ios_sdk);
778 if (!bad) {
779 const dep = b.anonymousDependency(case.build_root, case.import, .{});
780 const dep_step = dep.builder.default_step;
781 assert(mem.startsWith(u8, dep.builder.dep_prefix, "test."));
782 const dep_prefix_adjusted = dep.builder.dep_prefix["test.".len..];
783 dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
784 step.dependOn(dep_step);
785 }
773786 }
774787 }
775788