Commit 0c1d610015

Jakub Konka <kubkon@jakubkonka.com>
2021-11-23 15:59:49
zld: handle -current_version and -compatibility_version
and transfer them correctly to the generated dylib as part of the dylib id load command.
1 parent de8e612
src/link/MachO.zig
@@ -4038,12 +4038,16 @@ pub fn populateMissingMetadata(self: *MachO) !void {
             self.base.options.emit.?.sub_path,
         });
         defer self.base.allocator.free(install_name);
+        const current_version = self.base.options.version orelse
+            std.builtin.Version{ .major = 1, .minor = 0, .patch = 0 };
+        const compat_version = self.base.options.compatibility_version orelse
+            std.builtin.Version{ .major = 1, .minor = 0, .patch = 0 };
         var dylib_cmd = try commands.createLoadDylibCommand(
             self.base.allocator,
             install_name,
             2,
-            0x10000, // TODO forward user-provided versions
-            0x10000,
+            current_version.major << 16 | current_version.minor << 8 | current_version.patch,
+            compat_version.major << 16 | compat_version.minor << 8 | compat_version.patch,
         );
         errdefer dylib_cmd.deinit(self.base.allocator);
         dylib_cmd.inner.cmd = macho.LC_ID_DYLIB;
src/Compilation.zig
@@ -758,6 +758,7 @@ pub const InitOptions = struct {
     image_base_override: ?u64 = null,
     self_exe_path: ?[]const u8 = null,
     version: ?std.builtin.Version = null,
+    compatibility_version: ?std.builtin.Version = null,
     libc_installation: ?*const LibCInstallation = null,
     machine_code_model: std.builtin.CodeModel = .default,
     clang_preprocessor_mode: ClangPreprocessorMode = .no,
@@ -1439,6 +1440,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
             .extra_lld_args = options.lld_argv,
             .soname = options.soname,
             .version = options.version,
+            .compatibility_version = options.compatibility_version,
             .libc_installation = libc_dirs.libc_installation,
             .pic = pic,
             .pie = pie,
src/link.zig
@@ -143,6 +143,7 @@ pub const Options = struct {
     rpath_list: []const []const u8,
 
     version: ?std.builtin.Version,
+    compatibility_version: ?std.builtin.Version,
     libc_installation: ?*const LibCInstallation,
 
     /// WASI-only. Type of WASI execution model ("command" or "reactor").
src/main.zig
@@ -564,6 +564,7 @@ fn buildOutputType(
     var root_src_file: ?[]const u8 = null;
     var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 };
     var have_version = false;
+    var compatibility_version: ?std.builtin.Version = null;
     var strip = false;
     var single_threaded = false;
     var function_sections = false;
@@ -1613,12 +1614,29 @@ fn buildOutputType(
                     ) catch |err| {
                         fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
                     };
-                } else if (mem.eql(u8, arg, "-weak_framework")) {
+                } else if (mem.eql(u8, arg, "-framework") or mem.eql(u8, arg, "-weak_framework")) {
                     i += 1;
                     if (i >= linker_args.items.len) {
                         fatal("expected linker arg after '{s}'", .{arg});
                     }
                     try frameworks.append(linker_args.items[i]);
+                } else if (mem.eql(u8, arg, "-compatibility_version")) {
+                    i += 1;
+                    if (i >= linker_args.items.len) {
+                        fatal("expected linker arg after '{s}'", .{arg});
+                    }
+                    compatibility_version = std.builtin.Version.parse(linker_args.items[i]) catch |err| {
+                        fatal("unable to parse -compatibility_version '{s}': {s}", .{ linker_args.items[i], @errorName(err) });
+                    };
+                } else if (mem.eql(u8, arg, "-current_version")) {
+                    i += 1;
+                    if (i >= linker_args.items.len) {
+                        fatal("expected linker arg after '{s}'", .{arg});
+                    }
+                    version = std.builtin.Version.parse(linker_args.items[i]) catch |err| {
+                        fatal("unable to parse -current_version '{s}': {s}", .{ linker_args.items[i], @errorName(err) });
+                    };
+                    have_version = true;
                 } else {
                     warn("unsupported linker arg: {s}", .{arg});
                 }