authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-13 19:02:29+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:39+01:00
log49c11e0c3404b28af8b98994bad15db138272fa7
tree9c60738b210d62a204b7a478b2acb905e2a7f2f8
parentd93a0763d4c732fcdb3ed891d5b44a10943ccfed

test/link/macho: upgrade and migrate needed_library test


5 files changed, 43 insertions(+), 71 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_library",
140 .import = @import("link/macho/needed_library/build.zig"),
141 },
142138 .{
143139 .build_root = "test/link/macho/objc",
144140 .import = @import("link/macho/objc/build.zig"),
test/link/macho.zig+43-12
......@@ -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, build_opts: BuildOptions) *Step {
4pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
55 const macho_step = b.step("test-macho", "Run MachO tests");
66
77 const default_target = b.resolveTargetQuery(.{
......@@ -13,15 +13,20 @@ pub fn testAll(b: *std.Build, build_opts: BuildOptions) *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 }));
16 // Tests requiring symlinks when tested on Windows
17 if (build_opts.has_symlinks_windows) {
18 macho_step.dependOn(testNeededLibrary(b, .{ .target = default_target }));
19
20 // Tests requiring presence of macOS SDK in system path
21 if (build_opts.has_macos_sdk) {
22 macho_step.dependOn(testNeededFramework(b, .{ .target = b.host }));
23 }
1924 }
2025
2126 return macho_step;
2227}
2328
24fn testDeadStrip(b: *std.Build, opts: Options) *Step {
29fn testDeadStrip(b: *Build, opts: Options) *Step {
2530 const test_step = addTestStep(b, "macho-dead-strip", opts);
2631
2732 const obj = addObject(b, opts, .{ .name = "a", .cpp_source_bytes =
......@@ -102,7 +107,7 @@ fn testDeadStrip(b: *std.Build, opts: Options) *Step {
102107 return test_step;
103108}
104109
105fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {
110fn testEntryPointDylib(b: *Build, opts: Options) *Step {
106111 const test_step = addTestStep(b, "macho-entry-point-dylib", opts);
107112
108113 const dylib = addSharedLibrary(b, opts, .{ .name = "a" });
......@@ -156,11 +161,11 @@ fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {
156161 return test_step;
157162}
158163
159fn testNeededFramework(b: *std.Build, opts: Options) *Step {
164fn testNeededFramework(b: *Build, opts: Options) *Step {
160165 const test_step = addTestStep(b, "macho-needed-framework", opts);
161166
162167 const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes = "int main() { return 0; }" });
163 exe.linkFrameworkNeeded("Cocoa");
168 exe.root_module.linkFramework("Cocoa", .{ .needed = true });
164169 exe.dead_strip_dylibs = true;
165170
166171 const check = exe.checkObject();
......@@ -176,7 +181,31 @@ fn testNeededFramework(b: *std.Build, opts: Options) *Step {
176181 return test_step;
177182}
178183
179fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step {
184fn testNeededLibrary(b: *Build, opts: Options) *Step {
185 const test_step = addTestStep(b, "macho-needed-library", opts);
186
187 const dylib = addSharedLibrary(b, opts, .{ .name = "a", .c_source_bytes = "int a = 42;" });
188
189 const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes = "int main() { return 0; }" });
190 exe.root_module.linkSystemLibrary("a", .{ .needed = true });
191 exe.addLibraryPath(dylib.getEmittedBinDirectory());
192 exe.addRPath(dylib.getEmittedBinDirectory());
193 exe.dead_strip_dylibs = true;
194
195 const check = exe.checkObject();
196 check.checkInHeaders();
197 check.checkExact("cmd LOAD_DYLIB");
198 check.checkContains("liba.dylib");
199 test_step.dependOn(&check.step);
200
201 const run = addRunArtifact(exe);
202 run.expectExitCode(0);
203 test_step.dependOn(&run.step);
204
205 return test_step;
206}
207
208fn testSectionBoundarySymbols(b: *Build, opts: Options) *Step {
180209 const test_step = addTestStep(b, "macho-section-boundary-symbols", opts);
181210
182211 const obj1 = addObject(b, opts, .{
......@@ -256,7 +285,7 @@ fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step {
256285 return test_step;
257286}
258287
259fn testSegmentBoundarySymbols(b: *std.Build, opts: Options) *Step {
288fn testSegmentBoundarySymbols(b: *Build, opts: Options) *Step {
260289 const test_step = addTestStep(b, "macho-segment-boundary-symbols", opts);
261290
262291 const obj1 = addObject(b, opts, .{ .name = "a", .cpp_source_bytes =
......@@ -324,7 +353,7 @@ fn testSegmentBoundarySymbols(b: *std.Build, opts: Options) *Step {
324353 return test_step;
325354}
326355
327fn addTestStep(b: *std.Build, comptime prefix: []const u8, opts: Options) *Step {
356fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
328357 return link.addTestStep(b, "macho-" ++ prefix, opts);
329358}
330359
......@@ -336,6 +365,8 @@ const addSharedLibrary = link.addSharedLibrary;
336365const expectLinkErrors = link.expectLinkErrors;
337366const link = @import("link.zig");
338367const std = @import("std");
368
369const Build = std.Build;
339370const BuildOptions = link.BuildOptions;
340371const Options = link.Options;
341const Step = std.Build.Step;
372const Step = Build.Step;
test/link/macho/needed_library/a.c deleted-1
......@@ -1 +0,0 @@
1int a = 42;
test/link/macho/needed_library/build.zig deleted-51
......@@ -1,51 +0,0 @@
1const std = @import("std");
2
3pub const requires_symlinks = true;
4
5pub fn build(b: *std.Build) void {
6 const test_step = b.step("test", "Test it");
7 b.default_step = test_step;
8
9 add(b, test_step, .Debug);
10 add(b, test_step, .ReleaseFast);
11 add(b, test_step, .ReleaseSmall);
12 add(b, test_step, .ReleaseSafe);
13}
14
15fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
16 const target = b.resolveTargetQuery(.{ .os_tag = .macos });
17
18 const dylib = b.addSharedLibrary(.{
19 .name = "a",
20 .version = .{ .major = 1, .minor = 0, .patch = 0 },
21 .optimize = optimize,
22 .target = target,
23 });
24 dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
25 dylib.linkLibC();
26
27 // -dead_strip_dylibs
28 // -needed-la
29 const exe = b.addExecutable(.{
30 .name = "test",
31 .optimize = optimize,
32 .target = target,
33 });
34 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
35 exe.linkLibC();
36 exe.root_module.linkSystemLibrary("a", .{ .needed = true });
37 exe.addLibraryPath(dylib.getEmittedBinDirectory());
38 exe.addRPath(dylib.getEmittedBinDirectory());
39 exe.dead_strip_dylibs = true;
40
41 const check = exe.checkObject();
42 check.checkInHeaders();
43 check.checkExact("cmd LOAD_DYLIB");
44 check.checkExact("name @rpath/liba.dylib");
45 test_step.dependOn(&check.step);
46
47 const run = b.addRunArtifact(exe);
48 run.skip_foreign_checks = true;
49 run.expectStdOutEqual("");
50 test_step.dependOn(&run.step);
51}
test/link/macho/needed_library/main.c deleted-3
......@@ -1,3 +0,0 @@
1int main(int argc, char* argv[]) {
2 return 0;
3}