authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-15 11:43:36+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:40+01:00
logb70fedee7e61d0243211ef36635681fcb998ca54
treeb9bfd890f95530d50ec75d69394012d08bc481f5
parent933231868ab37c67af53ab67aaf1355b5f50413a

test/link/macho: upgrade search strategy test


5 files changed, 70 insertions(+), 105 deletions(-)

test/link.zig-4
......@@ -119,10 +119,6 @@ pub const cases = [_]Case{
119119 .build_root = "test/link/macho/reexports",
120120 .import = @import("link/macho/reexports/build.zig"),
121121 },
122 .{
123 .build_root = "test/link/macho/search_strategy",
124 .import = @import("link/macho/search_strategy/build.zig"),
125 },
126122 .{
127123 .build_root = "test/link/macho/stack_size",
128124 .import = @import("link/macho/stack_size/build.zig"),
test/link/macho.zig+70-1
......@@ -45,9 +45,10 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
4545 macho_step.dependOn(testEntryPointDylib(b, .{ .target = default_target }));
4646 macho_step.dependOn(testDylib(b, .{ .target = default_target }));
4747 macho_step.dependOn(testNeededLibrary(b, .{ .target = default_target }));
48 macho_step.dependOn(testWeakLibrary(b, .{ .target = default_target }));
48 macho_step.dependOn(testSearchStrategy(b, .{ .target = b.host }));
4949 macho_step.dependOn(testTls(b, .{ .target = default_target }));
5050 macho_step.dependOn(testTwoLevelNamespace(b, .{ .target = default_target }));
51 macho_step.dependOn(testWeakLibrary(b, .{ .target = default_target }));
5152
5253 // Tests requiring presence of macOS SDK in system path
5354 if (build_opts.has_macos_sdk) {
......@@ -1046,6 +1047,74 @@ fn testRelocatableZig(b: *Build, opts: Options) *Step {
10461047 return test_step;
10471048}
10481049
1050fn testSearchStrategy(b: *Build, opts: Options) *Step {
1051 const test_step = addTestStep(b, "macho-search-strategy", opts);
1052
1053 const obj = addObject(b, opts, .{ .name = "a", .c_source_bytes =
1054 \\#include<stdio.h>
1055 \\char world[] = "world";
1056 \\char* hello() {
1057 \\ return "Hello";
1058 \\}
1059 });
1060
1061 const liba = addStaticLibrary(b, opts, .{ .name = "a" });
1062 liba.addObject(obj);
1063
1064 const dylib = addSharedLibrary(b, opts, .{ .name = "a" });
1065 dylib.addObject(obj);
1066
1067 const main_o = addObject(b, opts, .{ .name = "main", .c_source_bytes =
1068 \\#include<stdio.h>
1069 \\char* hello();
1070 \\extern char world[];
1071 \\int main() {
1072 \\ printf("%s %s", hello(), world);
1073 \\ return 0;
1074 \\}
1075 });
1076
1077 {
1078 const exe = addExecutable(b, opts, .{ .name = "main" });
1079 exe.addObject(main_o);
1080 exe.root_module.linkSystemLibrary("a", .{ .use_pkg_config = .no, .search_strategy = .mode_first });
1081 exe.root_module.addLibraryPath(liba.getEmittedBinDirectory());
1082 exe.root_module.addLibraryPath(dylib.getEmittedBinDirectory());
1083 exe.root_module.addRPath(dylib.getEmittedBinDirectory());
1084
1085 const run = addRunArtifact(exe);
1086 run.expectStdOutEqual("Hello world");
1087 test_step.dependOn(&run.step);
1088
1089 const check = exe.checkObject();
1090 check.checkInHeaders();
1091 check.checkExact("cmd LOAD_DYLIB");
1092 check.checkContains("liba.dylib");
1093 test_step.dependOn(&check.step);
1094 }
1095
1096 {
1097 const exe = addExecutable(b, opts, .{ .name = "main" });
1098 exe.addObject(main_o);
1099 exe.root_module.linkSystemLibrary("a", .{ .use_pkg_config = .no, .search_strategy = .paths_first });
1100 exe.root_module.addLibraryPath(liba.getEmittedBinDirectory());
1101 exe.root_module.addLibraryPath(dylib.getEmittedBinDirectory());
1102 exe.root_module.addRPath(dylib.getEmittedBinDirectory());
1103
1104 const run = addRunArtifact(exe);
1105 run.expectStdOutEqual("Hello world");
1106 test_step.dependOn(&run.step);
1107
1108 const check = exe.checkObject();
1109 check.checkInHeaders();
1110 check.checkExact("cmd LOAD_DYLIB");
1111 check.checkNotPresent("liba.dylib");
1112 test_step.dependOn(&check.step);
1113 }
1114
1115 return test_step;
1116}
1117
10491118fn testSectionBoundarySymbols(b: *Build, opts: Options) *Step {
10501119 const test_step = addTestStep(b, "macho-section-boundary-symbols", opts);
10511120
test/link/macho/search_strategy/a.c deleted-7
......@@ -1,7 +0,0 @@
1#include <stdio.h>
2
3char world[] = "world";
4
5char* hello() {
6 return "Hello";
7}
test/link/macho/search_strategy/build.zig deleted-84
......@@ -1,84 +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 {
19 // -search_dylibs_first
20 const exe = createScenario(b, optimize, target, "search_dylibs_first", .mode_first);
21
22 const check = exe.checkObject();
23 check.checkInHeaders();
24 check.checkExact("cmd LOAD_DYLIB");
25 check.checkExact("name @rpath/libsearch_dylibs_first.dylib");
26 test_step.dependOn(&check.step);
27
28 const run = b.addRunArtifact(exe);
29 run.skip_foreign_checks = true;
30 run.expectStdOutEqual("Hello world");
31 test_step.dependOn(&run.step);
32 }
33
34 {
35 // -search_paths_first
36 const exe = createScenario(b, optimize, target, "search_paths_first", .paths_first);
37
38 const run = b.addRunArtifact(exe);
39 run.skip_foreign_checks = true;
40 run.expectStdOutEqual("Hello world");
41 test_step.dependOn(&run.step);
42 }
43}
44
45fn createScenario(
46 b: *std.Build,
47 optimize: std.builtin.OptimizeMode,
48 target: std.Build.ResolvedTarget,
49 name: []const u8,
50 search_strategy: std.Build.Module.SystemLib.SearchStrategy,
51) *std.Build.Step.Compile {
52 const static = b.addStaticLibrary(.{
53 .name = name,
54 .optimize = optimize,
55 .target = target,
56 });
57 static.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
58 static.linkLibC();
59
60 const dylib = b.addSharedLibrary(.{
61 .name = name,
62 .version = .{ .major = 1, .minor = 0, .patch = 0 },
63 .optimize = optimize,
64 .target = target,
65 });
66 dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
67 dylib.linkLibC();
68
69 const exe = b.addExecutable(.{
70 .name = name,
71 .optimize = optimize,
72 .target = target,
73 });
74 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
75 exe.linkSystemLibrary2(name, .{
76 .use_pkg_config = .no,
77 .search_strategy = search_strategy,
78 });
79 exe.linkLibC();
80 exe.addLibraryPath(static.getEmittedBinDirectory());
81 exe.addLibraryPath(dylib.getEmittedBinDirectory());
82 exe.addRPath(dylib.getEmittedBinDirectory());
83 return exe;
84}
test/link/macho/search_strategy/main.c deleted-9
......@@ -1,9 +0,0 @@
1#include <stdio.h>
2
3char* hello();
4extern char world[];
5
6int main(int argc, char* argv[]) {
7 printf("%s %s", hello(), world);
8 return 0;
9}