From 075f5bc5ffdbe0a458a733d596a1fc4a74b8e1ab Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 28 Jun 2022 09:14:35 +0200 Subject: [PATCH] link-tests: test -weak-lx and -weak_framework x --- lib/std/build/CheckObjectStep.zig | 2 ++ test/link.zig | 11 ++++++- .../macho/{needed_l => needed_library}/a.c | 0 .../{needed_l => needed_library}/build.zig | 0 .../macho/{needed_l => needed_library}/main.c | 0 test/link/macho/weak_framework/build.zig | 24 ++++++++++++++ test/link/macho/weak_framework/main.c | 3 ++ test/link/macho/weak_library/a.c | 9 +++++ test/link/macho/weak_library/build.zig | 33 +++++++++++++++++++ test/link/macho/weak_library/main.c | 9 +++++ 10 files changed, 90 insertions(+), 1 deletion(-) rename test/link/macho/{needed_l => needed_library}/a.c (100%) rename test/link/macho/{needed_l => needed_library}/build.zig (100%) rename test/link/macho/{needed_l => needed_library}/main.c (100%) create mode 100644 test/link/macho/weak_framework/build.zig create mode 100644 test/link/macho/weak_framework/main.c create mode 100644 test/link/macho/weak_library/a.c create mode 100644 test/link/macho/weak_library/build.zig create mode 100644 test/link/macho/weak_library/main.c diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index 375e183231..c7e91bb7cb 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -408,6 +408,8 @@ const MachODumper = struct { .ID_DYLIB, .LOAD_DYLIB, + .LOAD_WEAK_DYLIB, + .REEXPORT_DYLIB, => { const dylib = lc.dylib.inner.dylib; try writer.writeByte('\n'); diff --git a/test/link.zig b/test/link.zig index 6878881a66..c578638ec3 100644 --- a/test/link.zig +++ b/test/link.zig @@ -45,7 +45,11 @@ pub fn addCases(cases: *tests.StandaloneContext) void { .requires_macos_sdk = true, }); - cases.addBuildFile("test/link/macho/needed_l/build.zig", .{ + cases.addBuildFile("test/link/macho/needed_library/build.zig", .{ + .build_modes = true, + }); + + cases.addBuildFile("test/link/macho/weak_library/build.zig", .{ .build_modes = true, }); @@ -54,6 +58,11 @@ pub fn addCases(cases: *tests.StandaloneContext) void { .requires_macos_sdk = true, }); + cases.addBuildFile("test/link/macho/weak_framework/build.zig", .{ + .build_modes = true, + .requires_macos_sdk = true, + }); + // Try to build and run an Objective-C executable. cases.addBuildFile("test/link/macho/objc/build.zig", .{ .build_modes = true, diff --git a/test/link/macho/needed_l/a.c b/test/link/macho/needed_library/a.c similarity index 100% rename from test/link/macho/needed_l/a.c rename to test/link/macho/needed_library/a.c diff --git a/test/link/macho/needed_l/build.zig b/test/link/macho/needed_library/build.zig similarity index 100% rename from test/link/macho/needed_l/build.zig rename to test/link/macho/needed_library/build.zig diff --git a/test/link/macho/needed_l/main.c b/test/link/macho/needed_library/main.c similarity index 100% rename from test/link/macho/needed_l/main.c rename to test/link/macho/needed_library/main.c diff --git a/test/link/macho/weak_framework/build.zig b/test/link/macho/weak_framework/build.zig new file mode 100644 index 0000000000..44675a15f8 --- /dev/null +++ b/test/link/macho/weak_framework/build.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const Builder = std.build.Builder; +const LibExeObjectStep = std.build.LibExeObjStep; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(b.getInstallStep()); + + const exe = b.addExecutable("test", null); + exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.setBuildMode(mode); + exe.linkLibC(); + exe.linkFrameworkWeak("Cocoa"); + + const check = exe.checkObject(.macho); + check.checkStart("cmd LOAD_WEAK_DYLIB"); + check.checkNext("name {*}Cocoa"); + test_step.dependOn(&check.step); + + const run_cmd = exe.run(); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/link/macho/weak_framework/main.c b/test/link/macho/weak_framework/main.c new file mode 100644 index 0000000000..ca68d24cc7 --- /dev/null +++ b/test/link/macho/weak_framework/main.c @@ -0,0 +1,3 @@ +int main(int argc, char* argv[]) { + return 0; +} diff --git a/test/link/macho/weak_library/a.c b/test/link/macho/weak_library/a.c new file mode 100644 index 0000000000..9f49802ce6 --- /dev/null +++ b/test/link/macho/weak_library/a.c @@ -0,0 +1,9 @@ +#include + +int a = 42; + +const char* asStr() { + static char str[3]; + sprintf(str, "%d", 42); + return str; +} diff --git a/test/link/macho/weak_library/build.zig b/test/link/macho/weak_library/build.zig new file mode 100644 index 0000000000..5a1f7b4ce5 --- /dev/null +++ b/test/link/macho/weak_library/build.zig @@ -0,0 +1,33 @@ +const std = @import("std"); +const Builder = std.build.Builder; +const LibExeObjectStep = std.build.LibExeObjStep; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(b.getInstallStep()); + + const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); + dylib.setBuildMode(mode); + dylib.addCSourceFile("a.c", &.{}); + dylib.linkLibC(); + dylib.install(); + + const exe = b.addExecutable("test", null); + exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.setBuildMode(mode); + exe.linkLibC(); + exe.linkSystemLibraryWeak("a"); + exe.addLibraryPath(b.pathFromRoot("zig-out/lib")); + exe.addRPath(b.pathFromRoot("zig-out/lib")); + + const check = exe.checkObject(.macho); + check.checkStart("cmd LOAD_WEAK_DYLIB"); + check.checkNext("name @rpath/liba.dylib"); + test_step.dependOn(&check.step); + + const run_cmd = exe.run(); + run_cmd.expectStdOutEqual("42 42"); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/link/macho/weak_library/main.c b/test/link/macho/weak_library/main.c new file mode 100644 index 0000000000..ee5367fef7 --- /dev/null +++ b/test/link/macho/weak_library/main.c @@ -0,0 +1,9 @@ +#include + +extern int a; +extern const char* asStr(); + +int main(int argc, char* argv[]) { + printf("%d %s", a, asStr()); + return 0; +}