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{...@@ -135,10 +135,6 @@ pub const cases = [_]Case{
135 .build_root = "test/link/macho/linksection",135 .build_root = "test/link/macho/linksection",
136 .import = @import("link/macho/linksection/build.zig"),136 .import = @import("link/macho/linksection/build.zig"),
137 },137 },
138 .{
139 .build_root = "test/link/macho/needed_library",
140 .import = @import("link/macho/needed_library/build.zig"),
141 },
142 .{138 .{
143 .build_root = "test/link/macho/objc",139 .build_root = "test/link/macho/objc",
144 .import = @import("link/macho/objc/build.zig"),140 .import = @import("link/macho/objc/build.zig"),
test/link/macho.zig+43-12
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1//! Here we test our MachO linker for correctness and functionality.1//! Here we test our MachO linker for correctness and functionality.
2//! TODO migrate standalone tests from test/link/macho/* to here.2//! 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 {
5 const macho_step = b.step("test-macho", "Run MachO tests");5 const macho_step = b.step("test-macho", "Run MachO tests");
66
7 const default_target = b.resolveTargetQuery(.{7 const default_target = b.resolveTargetQuery(.{
...@@ -13,15 +13,20 @@ pub fn testAll(b: *std.Build, build_opts: BuildOptions) *Step {...@@ -13,15 +13,20 @@ pub fn testAll(b: *std.Build, build_opts: BuildOptions) *Step {
13 macho_step.dependOn(testSectionBoundarySymbols(b, .{ .target = default_target }));13 macho_step.dependOn(testSectionBoundarySymbols(b, .{ .target = default_target }));
14 macho_step.dependOn(testSegmentBoundarySymbols(b, .{ .target = default_target }));14 macho_step.dependOn(testSegmentBoundarySymbols(b, .{ .target = default_target }));
1515
16 // Tests requiring presence of macOS SDK in system path16 // Tests requiring symlinks when tested on Windows
17 if (build_opts.has_macos_sdk and build_opts.has_symlinks_windows) {17 if (build_opts.has_symlinks_windows) {
18 macho_step.dependOn(testNeededFramework(b, .{ .target = b.host }));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 }
19 }24 }
2025
21 return macho_step;26 return macho_step;
22}27}
2328
24fn testDeadStrip(b: *std.Build, opts: Options) *Step {29fn testDeadStrip(b: *Build, opts: Options) *Step {
25 const test_step = addTestStep(b, "macho-dead-strip", opts);30 const test_step = addTestStep(b, "macho-dead-strip", opts);
2631
27 const obj = addObject(b, opts, .{ .name = "a", .cpp_source_bytes = 32 const obj = addObject(b, opts, .{ .name = "a", .cpp_source_bytes =
...@@ -102,7 +107,7 @@ fn testDeadStrip(b: *std.Build, opts: Options) *Step {...@@ -102,7 +107,7 @@ fn testDeadStrip(b: *std.Build, opts: Options) *Step {
102 return test_step;107 return test_step;
103}108}
104109
105fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {110fn testEntryPointDylib(b: *Build, opts: Options) *Step {
106 const test_step = addTestStep(b, "macho-entry-point-dylib", opts);111 const test_step = addTestStep(b, "macho-entry-point-dylib", opts);
107112
108 const dylib = addSharedLibrary(b, opts, .{ .name = "a" });113 const dylib = addSharedLibrary(b, opts, .{ .name = "a" });
...@@ -156,11 +161,11 @@ fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {...@@ -156,11 +161,11 @@ fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {
156 return test_step;161 return test_step;
157}162}
158163
159fn testNeededFramework(b: *std.Build, opts: Options) *Step {164fn testNeededFramework(b: *Build, opts: Options) *Step {
160 const test_step = addTestStep(b, "macho-needed-framework", opts);165 const test_step = addTestStep(b, "macho-needed-framework", opts);
161166
162 const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes = "int main() { return 0; }" });167 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 });
164 exe.dead_strip_dylibs = true;169 exe.dead_strip_dylibs = true;
165170
166 const check = exe.checkObject();171 const check = exe.checkObject();
...@@ -176,7 +181,31 @@ fn testNeededFramework(b: *std.Build, opts: Options) *Step {...@@ -176,7 +181,31 @@ fn testNeededFramework(b: *std.Build, opts: Options) *Step {
176 return test_step;181 return test_step;
177}182}
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 {
180 const test_step = addTestStep(b, "macho-section-boundary-symbols", opts);209 const test_step = addTestStep(b, "macho-section-boundary-symbols", opts);
181210
182 const obj1 = addObject(b, opts, .{211 const obj1 = addObject(b, opts, .{
...@@ -256,7 +285,7 @@ fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step {...@@ -256,7 +285,7 @@ fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step {
256 return test_step;285 return test_step;
257}286}
258287
259fn testSegmentBoundarySymbols(b: *std.Build, opts: Options) *Step {288fn testSegmentBoundarySymbols(b: *Build, opts: Options) *Step {
260 const test_step = addTestStep(b, "macho-segment-boundary-symbols", opts);289 const test_step = addTestStep(b, "macho-segment-boundary-symbols", opts);
261290
262 const obj1 = addObject(b, opts, .{ .name = "a", .cpp_source_bytes = 291 const obj1 = addObject(b, opts, .{ .name = "a", .cpp_source_bytes =
...@@ -324,7 +353,7 @@ fn testSegmentBoundarySymbols(b: *std.Build, opts: Options) *Step {...@@ -324,7 +353,7 @@ fn testSegmentBoundarySymbols(b: *std.Build, opts: Options) *Step {
324 return test_step;353 return test_step;
325}354}
326355
327fn addTestStep(b: *std.Build, comptime prefix: []const u8, opts: Options) *Step {356fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
328 return link.addTestStep(b, "macho-" ++ prefix, opts);357 return link.addTestStep(b, "macho-" ++ prefix, opts);
329}358}
330359
...@@ -336,6 +365,8 @@ const addSharedLibrary = link.addSharedLibrary;...@@ -336,6 +365,8 @@ const addSharedLibrary = link.addSharedLibrary;
336const expectLinkErrors = link.expectLinkErrors;365const expectLinkErrors = link.expectLinkErrors;
337const link = @import("link.zig");366const link = @import("link.zig");
338const std = @import("std");367const std = @import("std");
368
369const Build = std.Build;
339const BuildOptions = link.BuildOptions;370const BuildOptions = link.BuildOptions;
340const Options = link.Options;371const 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}