Commit b70fedee7e

Jakub Konka <kubkon@jakubkonka.com>
2024-01-15 11:43:36
test/link/macho: upgrade search strategy test
1 parent 9332318
Changed files (5)
test
link
macho
search_strategy
test/link/macho/search_strategy/a.c
@@ -1,7 +0,0 @@
-#include <stdio.h>
-
-char world[] = "world";
-
-char* hello() {
-  return "Hello";
-}
test/link/macho/search_strategy/build.zig
@@ -1,84 +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 });
-
-    {
-        // -search_dylibs_first
-        const exe = createScenario(b, optimize, target, "search_dylibs_first", .mode_first);
-
-        const check = exe.checkObject();
-        check.checkInHeaders();
-        check.checkExact("cmd LOAD_DYLIB");
-        check.checkExact("name @rpath/libsearch_dylibs_first.dylib");
-        test_step.dependOn(&check.step);
-
-        const run = b.addRunArtifact(exe);
-        run.skip_foreign_checks = true;
-        run.expectStdOutEqual("Hello world");
-        test_step.dependOn(&run.step);
-    }
-
-    {
-        // -search_paths_first
-        const exe = createScenario(b, optimize, target, "search_paths_first", .paths_first);
-
-        const run = b.addRunArtifact(exe);
-        run.skip_foreign_checks = true;
-        run.expectStdOutEqual("Hello world");
-        test_step.dependOn(&run.step);
-    }
-}
-
-fn createScenario(
-    b: *std.Build,
-    optimize: std.builtin.OptimizeMode,
-    target: std.Build.ResolvedTarget,
-    name: []const u8,
-    search_strategy: std.Build.Module.SystemLib.SearchStrategy,
-) *std.Build.Step.Compile {
-    const static = b.addStaticLibrary(.{
-        .name = name,
-        .optimize = optimize,
-        .target = target,
-    });
-    static.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
-    static.linkLibC();
-
-    const dylib = b.addSharedLibrary(.{
-        .name = name,
-        .version = .{ .major = 1, .minor = 0, .patch = 0 },
-        .optimize = optimize,
-        .target = target,
-    });
-    dylib.addCSourceFile(.{ .file = .{ .path = "a.c" }, .flags = &.{} });
-    dylib.linkLibC();
-
-    const exe = b.addExecutable(.{
-        .name = name,
-        .optimize = optimize,
-        .target = target,
-    });
-    exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
-    exe.linkSystemLibrary2(name, .{
-        .use_pkg_config = .no,
-        .search_strategy = search_strategy,
-    });
-    exe.linkLibC();
-    exe.addLibraryPath(static.getEmittedBinDirectory());
-    exe.addLibraryPath(dylib.getEmittedBinDirectory());
-    exe.addRPath(dylib.getEmittedBinDirectory());
-    return exe;
-}
test/link/macho/search_strategy/main.c
@@ -1,9 +0,0 @@
-#include <stdio.h>
-
-char* hello();
-extern char world[];
-
-int main(int argc, char* argv[]) {
-  printf("%s %s", hello(), world);
-  return 0;
-}
test/link/macho.zig
@@ -45,9 +45,10 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
         macho_step.dependOn(testEntryPointDylib(b, .{ .target = default_target }));
         macho_step.dependOn(testDylib(b, .{ .target = default_target }));
         macho_step.dependOn(testNeededLibrary(b, .{ .target = default_target }));
-        macho_step.dependOn(testWeakLibrary(b, .{ .target = default_target }));
+        macho_step.dependOn(testSearchStrategy(b, .{ .target = b.host }));
         macho_step.dependOn(testTls(b, .{ .target = default_target }));
         macho_step.dependOn(testTwoLevelNamespace(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) {
@@ -1046,6 +1047,74 @@ fn testRelocatableZig(b: *Build, opts: Options) *Step {
     return test_step;
 }
 
+fn testSearchStrategy(b: *Build, opts: Options) *Step {
+    const test_step = addTestStep(b, "macho-search-strategy", opts);
+
+    const obj = addObject(b, opts, .{ .name = "a", .c_source_bytes = 
+    \\#include<stdio.h>
+    \\char world[] = "world";
+    \\char* hello() {
+    \\  return "Hello";
+    \\}
+    });
+
+    const liba = addStaticLibrary(b, opts, .{ .name = "a" });
+    liba.addObject(obj);
+
+    const dylib = addSharedLibrary(b, opts, .{ .name = "a" });
+    dylib.addObject(obj);
+
+    const main_o = addObject(b, opts, .{ .name = "main", .c_source_bytes = 
+    \\#include<stdio.h>
+    \\char* hello();
+    \\extern char world[];
+    \\int main() {
+    \\  printf("%s %s", hello(), world);
+    \\  return 0;
+    \\}
+    });
+
+    {
+        const exe = addExecutable(b, opts, .{ .name = "main" });
+        exe.addObject(main_o);
+        exe.root_module.linkSystemLibrary("a", .{ .use_pkg_config = .no, .search_strategy = .mode_first });
+        exe.root_module.addLibraryPath(liba.getEmittedBinDirectory());
+        exe.root_module.addLibraryPath(dylib.getEmittedBinDirectory());
+        exe.root_module.addRPath(dylib.getEmittedBinDirectory());
+
+        const run = addRunArtifact(exe);
+        run.expectStdOutEqual("Hello world");
+        test_step.dependOn(&run.step);
+
+        const check = exe.checkObject();
+        check.checkInHeaders();
+        check.checkExact("cmd LOAD_DYLIB");
+        check.checkContains("liba.dylib");
+        test_step.dependOn(&check.step);
+    }
+
+    {
+        const exe = addExecutable(b, opts, .{ .name = "main" });
+        exe.addObject(main_o);
+        exe.root_module.linkSystemLibrary("a", .{ .use_pkg_config = .no, .search_strategy = .paths_first });
+        exe.root_module.addLibraryPath(liba.getEmittedBinDirectory());
+        exe.root_module.addLibraryPath(dylib.getEmittedBinDirectory());
+        exe.root_module.addRPath(dylib.getEmittedBinDirectory());
+
+        const run = addRunArtifact(exe);
+        run.expectStdOutEqual("Hello world");
+        test_step.dependOn(&run.step);
+
+        const check = exe.checkObject();
+        check.checkInHeaders();
+        check.checkExact("cmd LOAD_DYLIB");
+        check.checkNotPresent("liba.dylib");
+        test_step.dependOn(&check.step);
+    }
+
+    return test_step;
+}
+
 fn testSectionBoundarySymbols(b: *Build, opts: Options) *Step {
     const test_step = addTestStep(b, "macho-section-boundary-symbols", opts);
 
test/link.zig
@@ -119,10 +119,6 @@ pub const cases = [_]Case{
         .build_root = "test/link/macho/reexports",
         .import = @import("link/macho/reexports/build.zig"),
     },
-    .{
-        .build_root = "test/link/macho/search_strategy",
-        .import = @import("link/macho/search_strategy/build.zig"),
-    },
     .{
         .build_root = "test/link/macho/stack_size",
         .import = @import("link/macho/stack_size/build.zig"),