Commit e96f8b817a

Jakub Konka <kubkon@jakubkonka.com>
2024-01-14 10:20:22
test/link/macho: upgrade weak library test
1 parent 1e0eb3c
Changed files (5)
test
link
macho
weak_library
test/link/macho/weak_library/a.c
@@ -1,9 +0,0 @@
-#include <stdio.h>
-
-int a = 42;
-
-const char* asStr() {
-  static char str[3];
-  sprintf(str, "%d", 42);
-  return str;
-}
test/link/macho/weak_library/build.zig
@@ -1,55 +0,0 @@
-const std = @import("std");
-
-pub const requires_symlinks = true;
-
-pub fn build(b: *std.Build) void {
-    const test_step = b.step("test", "Test it");
-    b.default_step = test_step;
-
-    add(b, test_step, .Debug);
-    add(b, test_step, .ReleaseFast);
-    add(b, test_step, .ReleaseSmall);
-    add(b, test_step, .ReleaseSafe);
-}
-
-fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
-    const target = b.resolveTargetQuery(.{ .os_tag = .macos });
-
-    const dylib = b.addSharedLibrary(.{
-        .name = "a",
-        .version = .{ .major = 1, .minor = 0, .patch = 0 },
-        .target = target,
-        .optimize = optimize,
-    });
-    dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
-    dylib.linkLibC();
-    b.installArtifact(dylib);
-
-    const exe = b.addExecutable(.{
-        .name = "test",
-        .target = target,
-        .optimize = optimize,
-    });
-    exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
-    exe.linkLibC();
-    exe.root_module.linkSystemLibrary("a", .{ .weak = true });
-    exe.addLibraryPath(dylib.getEmittedBinDirectory());
-    exe.addRPath(dylib.getEmittedBinDirectory());
-
-    const check = exe.checkObject();
-    check.checkInHeaders();
-    check.checkExact("cmd LOAD_WEAK_DYLIB");
-    check.checkExact("name @rpath/liba.dylib");
-
-    check.checkInSymtab();
-    check.checkExact("(undefined) weakref external _a (from liba)");
-
-    check.checkInSymtab();
-    check.checkExact("(undefined) weakref external _asStr (from liba)");
-    test_step.dependOn(&check.step);
-
-    const run = b.addRunArtifact(exe);
-    run.skip_foreign_checks = true;
-    run.expectStdOutEqual("42 42");
-    test_step.dependOn(&run.step);
-}
test/link/macho/weak_library/main.c
@@ -1,9 +0,0 @@
-#include <stdio.h>
-
-extern int a;
-extern const char* asStr();
-
-int main(int argc, char* argv[]) {
-  printf("%d %s", a, asStr());
-  return 0;
-}
test/link/macho.zig
@@ -26,6 +26,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
     // Tests requiring symlinks when tested on Windows
     if (build_opts.has_symlinks_windows) {
         macho_step.dependOn(testNeededLibrary(b, .{ .target = default_target }));
+        macho_step.dependOn(testWeakLibrary(b, .{ .target = default_target }));
 
         // Tests requiring presence of macOS SDK in system path
         if (build_opts.has_macos_sdk) {
@@ -766,6 +767,49 @@ fn testWeakBind(b: *Build, opts: Options) *Step {
     return test_step;
 }
 
+fn testWeakLibrary(b: *Build, opts: Options) *Step {
+    const test_step = addTestStep(b, "macho-weak-library", opts);
+
+    const dylib = addSharedLibrary(b, opts, .{ .name = "a", .c_source_bytes = 
+    \\#include<stdio.h>
+    \\int a = 42;
+    \\const char* asStr() {
+    \\  static char str[3];
+    \\  sprintf(str, "%d", 42);
+    \\  return str;
+    \\}
+    });
+
+    const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes = 
+    \\#include<stdio.h>
+    \\extern int a;
+    \\extern const char* asStr();
+    \\int main() {
+    \\  printf("%d %s", a, asStr());
+    \\  return 0;
+    \\}
+    });
+    exe.root_module.linkSystemLibrary("a", .{ .weak = true });
+    exe.addLibraryPath(dylib.getEmittedBinDirectory());
+    exe.addRPath(dylib.getEmittedBinDirectory());
+
+    const check = exe.checkObject();
+    check.checkInHeaders();
+    check.checkExact("cmd LOAD_WEAK_DYLIB");
+    check.checkContains("liba.dylib");
+    check.checkInSymtab();
+    check.checkExact("(undefined) weakref external _a (from liba)");
+    check.checkInSymtab();
+    check.checkExact("(undefined) weakref external _asStr (from liba)");
+    test_step.dependOn(&check.step);
+
+    const run = addRunArtifact(exe);
+    run.expectStdOutEqual("42 42");
+    test_step.dependOn(&run.step);
+
+    return test_step;
+}
+
 fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
     return link.addTestStep(b, "macho-" ++ prefix, opts);
 }
test/link.zig
@@ -171,10 +171,6 @@ pub const cases = [_]Case{
         .build_root = "test/link/macho/unwind_info",
         .import = @import("link/macho/unwind_info/build.zig"),
     },
-    .{
-        .build_root = "test/link/macho/weak_library",
-        .import = @import("link/macho/weak_library/build.zig"),
-    },
     .{
         .build_root = "test/link/macho/weak_framework",
         .import = @import("link/macho/weak_framework/build.zig"),