diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2022-06-28 07:22:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-28 07:22:19 +0200 |
| commit | d4623a8a07bf8fe3bed3b4ee50339960d623958e (patch) | |
| tree | 949f6078a42f12415508667377b40ea0e84eac0b /test | |
| parent | a76775b50a65fd0ea0fd17d6ef3c42058df13997 (diff) | |
| parent | 0dd28920daa5127ffe5a3691343fa519f7547cfd (diff) | |
| download | zig-d4623a8a07bf8fe3bed3b4ee50339960d623958e.tar.gz zig-d4623a8a07bf8fe3bed3b4ee50339960d623958e.zip | |
Merge pull request #11941 from ziglang/macho-stripping-dylibs
macho: handle `-dead_strip_dylibs`, `-needed-lx` and `-needed_framework x` flags
Diffstat (limited to 'test')
| -rw-r--r-- | test/link.zig | 11 | ||||
| -rw-r--r-- | test/link/macho/dead_strip_dylibs/build.zig | 46 | ||||
| -rw-r--r-- | test/link/macho/dead_strip_dylibs/main.c | 11 | ||||
| -rw-r--r-- | test/link/macho/dylib/main.c | 2 | ||||
| -rw-r--r-- | test/link/macho/frameworks/main.c | 7 | ||||
| -rw-r--r-- | test/link/macho/needed_framework/build.zig (renamed from test/link/macho/frameworks/build.zig) | 17 | ||||
| -rw-r--r-- | test/link/macho/needed_framework/main.c | 3 | ||||
| -rw-r--r-- | test/link/macho/needed_l/a.c | 1 | ||||
| -rw-r--r-- | test/link/macho/needed_l/build.zig | 35 | ||||
| -rw-r--r-- | test/link/macho/needed_l/main.c | 3 | ||||
| -rw-r--r-- | test/link/macho/search_strategy/main.c | 2 |
11 files changed, 117 insertions, 21 deletions
diff --git a/test/link.zig b/test/link.zig index 0c301d6bcb..6878881a66 100644 --- a/test/link.zig +++ b/test/link.zig @@ -40,7 +40,16 @@ pub fn addCases(cases: *tests.StandaloneContext) void { .build_modes = true, }); - cases.addBuildFile("test/link/macho/frameworks/build.zig", .{ + cases.addBuildFile("test/link/macho/dead_strip_dylibs/build.zig", .{ + .build_modes = true, + .requires_macos_sdk = true, + }); + + cases.addBuildFile("test/link/macho/needed_l/build.zig", .{ + .build_modes = true, + }); + + cases.addBuildFile("test/link/macho/needed_framework/build.zig", .{ .build_modes = true, .requires_macos_sdk = true, }); diff --git a/test/link/macho/dead_strip_dylibs/build.zig b/test/link/macho/dead_strip_dylibs/build.zig new file mode 100644 index 0000000000..efdaf191bd --- /dev/null +++ b/test/link/macho/dead_strip_dylibs/build.zig @@ -0,0 +1,46 @@ +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()); + + { + // Without -dead_strip_dylibs we expect `-la` to include liba.dylib in the final executable + const exe = createScenario(b, mode); + + const check = exe.checkObject(.macho); + check.checkStart("cmd LOAD_DYLIB"); + check.checkNext("name {*}Cocoa"); + + check.checkStart("cmd LOAD_DYLIB"); + check.checkNext("name {*}libobjc{*}.dylib"); + + test_step.dependOn(&check.step); + + const run_cmd = exe.run(); + test_step.dependOn(&run_cmd.step); + } + + { + // With -dead_strip_dylibs, we should include liba.dylib as it's unreachable + const exe = createScenario(b, mode); + exe.dead_strip_dylibs = true; + + const run_cmd = exe.run(); + run_cmd.expected_exit_code = @bitCast(u8, @as(i8, -2)); // should fail + test_step.dependOn(&run_cmd.step); + } +} + +fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep { + const exe = b.addExecutable("test", null); + exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.setBuildMode(mode); + exe.linkLibC(); + exe.linkFramework("Cocoa"); + return exe; +} diff --git a/test/link/macho/dead_strip_dylibs/main.c b/test/link/macho/dead_strip_dylibs/main.c new file mode 100644 index 0000000000..06668f5522 --- /dev/null +++ b/test/link/macho/dead_strip_dylibs/main.c @@ -0,0 +1,11 @@ +#include <objc/runtime.h> + +int main(int argc, char* argv[]) { + if (objc_getClass("NSObject") == 0) { + return -1; + } + if (objc_getClass("NSApplication") == 0) { + return -2; + } + return 0; +} diff --git a/test/link/macho/dylib/main.c b/test/link/macho/dylib/main.c index be1647ddad..941903f219 100644 --- a/test/link/macho/dylib/main.c +++ b/test/link/macho/dylib/main.c @@ -3,7 +3,7 @@ char* hello(); extern char world[]; -int main() { +int main(int argc, char* argv[]) { printf("%s %s", hello(), world); return 0; } diff --git a/test/link/macho/frameworks/main.c b/test/link/macho/frameworks/main.c deleted file mode 100644 index b9dab990b2..0000000000 --- a/test/link/macho/frameworks/main.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <assert.h> -#include <objc/runtime.h> - -int main() { - assert(objc_getClass("NSObject") > 0); - assert(objc_getClass("NSApplication") > 0); -} diff --git a/test/link/macho/frameworks/build.zig b/test/link/macho/needed_framework/build.zig index 7086606f30..4315935941 100644 --- a/test/link/macho/frameworks/build.zig +++ b/test/link/macho/needed_framework/build.zig @@ -1,30 +1,25 @@ 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()); + // -dead_strip_dylibs + // -needed_framework Cocoa const exe = b.addExecutable("test", null); - b.default_step.dependOn(&exe.step); exe.addCSourceFile("main.c", &[0][]const u8{}); exe.setBuildMode(mode); exe.linkLibC(); - exe.linkFramework("Cocoa"); + exe.linkFrameworkNeeded("Cocoa"); + exe.dead_strip_dylibs = true; const check = exe.checkObject(.macho); check.checkStart("cmd LOAD_DYLIB"); check.checkNext("name {*}Cocoa"); - - switch (mode) { - .Debug, .ReleaseSafe => { - check.checkStart("cmd LOAD_DYLIB"); - check.checkNext("name {*}libobjc{*}.dylib"); - }, - else => {}, - } - test_step.dependOn(&check.step); const run_cmd = exe.run(); diff --git a/test/link/macho/needed_framework/main.c b/test/link/macho/needed_framework/main.c new file mode 100644 index 0000000000..ca68d24cc7 --- /dev/null +++ b/test/link/macho/needed_framework/main.c @@ -0,0 +1,3 @@ +int main(int argc, char* argv[]) { + return 0; +} diff --git a/test/link/macho/needed_l/a.c b/test/link/macho/needed_l/a.c new file mode 100644 index 0000000000..4bcf8c9786 --- /dev/null +++ b/test/link/macho/needed_l/a.c @@ -0,0 +1 @@ +int a = 42; diff --git a/test/link/macho/needed_l/build.zig b/test/link/macho/needed_l/build.zig new file mode 100644 index 0000000000..708a09dc32 --- /dev/null +++ b/test/link/macho/needed_l/build.zig @@ -0,0 +1,35 @@ +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(); + + // -dead_strip_dylibs + // -needed-la + const exe = b.addExecutable("test", null); + exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.setBuildMode(mode); + exe.linkLibC(); + exe.linkSystemLibraryNeeded("a"); + exe.addLibraryPath(b.pathFromRoot("zig-out/lib")); + exe.addRPath(b.pathFromRoot("zig-out/lib")); + exe.dead_strip_dylibs = true; + + const check = exe.checkObject(.macho); + check.checkStart("cmd LOAD_DYLIB"); + check.checkNext("name @rpath/liba.dylib"); + test_step.dependOn(&check.step); + + const run_cmd = exe.run(); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/link/macho/needed_l/main.c b/test/link/macho/needed_l/main.c new file mode 100644 index 0000000000..ca68d24cc7 --- /dev/null +++ b/test/link/macho/needed_l/main.c @@ -0,0 +1,3 @@ +int main(int argc, char* argv[]) { + return 0; +} diff --git a/test/link/macho/search_strategy/main.c b/test/link/macho/search_strategy/main.c index be1647ddad..941903f219 100644 --- a/test/link/macho/search_strategy/main.c +++ b/test/link/macho/search_strategy/main.c @@ -3,7 +3,7 @@ char* hello(); extern char world[]; -int main() { +int main(int argc, char* argv[]) { printf("%s %s", hello(), world); return 0; } |
