authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-14 10:20:22+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:40+01:00
loge96f8b817a889abecc86a71961ba9a95c13c315e
treeb0357bd6f7fe9661dcd6c5c5f741f9aab035f0d6
parent1e0eb3c8097a3bd45cd5e6c7b67f6f7d4f974c6f

test/link/macho: upgrade weak library test


5 files changed, 44 insertions(+), 77 deletions(-)

test/link.zig-4
......@@ -171,10 +171,6 @@ pub const cases = [_]Case{
171171 .build_root = "test/link/macho/unwind_info",
172172 .import = @import("link/macho/unwind_info/build.zig"),
173173 },
174 .{
175 .build_root = "test/link/macho/weak_library",
176 .import = @import("link/macho/weak_library/build.zig"),
177 },
178174 .{
179175 .build_root = "test/link/macho/weak_framework",
180176 .import = @import("link/macho/weak_framework/build.zig"),
test/link/macho.zig+44
......@@ -26,6 +26,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
2626 // Tests requiring symlinks when tested on Windows
2727 if (build_opts.has_symlinks_windows) {
2828 macho_step.dependOn(testNeededLibrary(b, .{ .target = default_target }));
29 macho_step.dependOn(testWeakLibrary(b, .{ .target = default_target }));
2930
3031 // Tests requiring presence of macOS SDK in system path
3132 if (build_opts.has_macos_sdk) {
......@@ -766,6 +767,49 @@ fn testWeakBind(b: *Build, opts: Options) *Step {
766767 return test_step;
767768}
768769
770fn testWeakLibrary(b: *Build, opts: Options) *Step {
771 const test_step = addTestStep(b, "macho-weak-library", opts);
772
773 const dylib = addSharedLibrary(b, opts, .{ .name = "a", .c_source_bytes =
774 \\#include<stdio.h>
775 \\int a = 42;
776 \\const char* asStr() {
777 \\ static char str[3];
778 \\ sprintf(str, "%d", 42);
779 \\ return str;
780 \\}
781 });
782
783 const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes =
784 \\#include<stdio.h>
785 \\extern int a;
786 \\extern const char* asStr();
787 \\int main() {
788 \\ printf("%d %s", a, asStr());
789 \\ return 0;
790 \\}
791 });
792 exe.root_module.linkSystemLibrary("a", .{ .weak = true });
793 exe.addLibraryPath(dylib.getEmittedBinDirectory());
794 exe.addRPath(dylib.getEmittedBinDirectory());
795
796 const check = exe.checkObject();
797 check.checkInHeaders();
798 check.checkExact("cmd LOAD_WEAK_DYLIB");
799 check.checkContains("liba.dylib");
800 check.checkInSymtab();
801 check.checkExact("(undefined) weakref external _a (from liba)");
802 check.checkInSymtab();
803 check.checkExact("(undefined) weakref external _asStr (from liba)");
804 test_step.dependOn(&check.step);
805
806 const run = addRunArtifact(exe);
807 run.expectStdOutEqual("42 42");
808 test_step.dependOn(&run.step);
809
810 return test_step;
811}
812
769813fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
770814 return link.addTestStep(b, "macho-" ++ prefix, opts);
771815}
test/link/macho/weak_library/a.c deleted-9
......@@ -1,9 +0,0 @@
1#include <stdio.h>
2
3int a = 42;
4
5const char* asStr() {
6 static char str[3];
7 sprintf(str, "%d", 42);
8 return str;
9}
test/link/macho/weak_library/build.zig deleted-55
......@@ -1,55 +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 .target = target,
22 .optimize = optimize,
23 });
24 dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
25 dylib.linkLibC();
26 b.installArtifact(dylib);
27
28 const exe = b.addExecutable(.{
29 .name = "test",
30 .target = target,
31 .optimize = optimize,
32 });
33 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
34 exe.linkLibC();
35 exe.root_module.linkSystemLibrary("a", .{ .weak = true });
36 exe.addLibraryPath(dylib.getEmittedBinDirectory());
37 exe.addRPath(dylib.getEmittedBinDirectory());
38
39 const check = exe.checkObject();
40 check.checkInHeaders();
41 check.checkExact("cmd LOAD_WEAK_DYLIB");
42 check.checkExact("name @rpath/liba.dylib");
43
44 check.checkInSymtab();
45 check.checkExact("(undefined) weakref external _a (from liba)");
46
47 check.checkInSymtab();
48 check.checkExact("(undefined) weakref external _asStr (from liba)");
49 test_step.dependOn(&check.step);
50
51 const run = b.addRunArtifact(exe);
52 run.skip_foreign_checks = true;
53 run.expectStdOutEqual("42 42");
54 test_step.dependOn(&run.step);
55}
test/link/macho/weak_library/main.c deleted-9
......@@ -1,9 +0,0 @@
1#include <stdio.h>
2
3extern int a;
4extern const char* asStr();
5
6int main(int argc, char* argv[]) {
7 printf("%d %s", a, asStr());
8 return 0;
9}