Commit 075f5bc5ff

Jakub Konka <kubkon@jakubkonka.com>
2022-06-28 09:14:35
link-tests: test -weak-lx and -weak_framework x
1 parent 20bd722
Changed files (10)
lib
test
link
macho
needed_library
weak_framework
weak_library
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');
test/link/macho/needed_l/a.c โ†’ test/link/macho/needed_library/a.c
File renamed without changes
test/link/macho/needed_l/build.zig โ†’ test/link/macho/needed_library/build.zig
File renamed without changes
test/link/macho/needed_l/main.c โ†’ test/link/macho/needed_library/main.c
File renamed without changes
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);
+}
test/link/macho/weak_framework/main.c
@@ -0,0 +1,3 @@
+int main(int argc, char* argv[]) {
+  return 0;
+}
test/link/macho/weak_library/a.c
@@ -0,0 +1,9 @@
+#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
@@ -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);
+}
test/link/macho/weak_library/main.c
@@ -0,0 +1,9 @@
+#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.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,