authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-07-25 22:20:31+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-07-26 08:10:52+02:00
log27f471860a86e8a6c3a54be8aa704d7feadf6123
treea2cd2cf800e4c25f4134003501ba75f8e42a845b
parent6cee98eb3074fcb99297f23f30e3a230a14e8db7

macho: fix matching target triples for TBDv3


1 files changed, 40 insertions(+), 15 deletions(-)

src/link/MachO/Dylib.zig+40-15
...@@ -9,12 +9,14 @@ const macho = std.macho;...@@ -9,12 +9,14 @@ const macho = std.macho;
9const math = std.math;9const math = std.math;
10const mem = std.mem;10const mem = std.mem;
11const fat = @import("fat.zig");11const fat = @import("fat.zig");
12const tapi = @import("../tapi.zig");
1213
13const Allocator = mem.Allocator;14const Allocator = mem.Allocator;
14const CrossTarget = std.zig.CrossTarget;15const CrossTarget = std.zig.CrossTarget;
15const LibStub = @import("../tapi.zig").LibStub;16const LibStub = tapi.LibStub;
16const LoadCommandIterator = macho.LoadCommandIterator;17const LoadCommandIterator = macho.LoadCommandIterator;
17const MachO = @import("../MachO.zig");18const MachO = @import("../MachO.zig");
19const Tbd = tapi.Tbd;
1820
19id: ?Id = null,21id: ?Id = null,
20weak: bool = false,22weak: bool = false,
...@@ -247,7 +249,8 @@ const TargetMatcher = struct {...@@ -247,7 +249,8 @@ const TargetMatcher = struct {
247 .allocator = allocator,249 .allocator = allocator,
248 .target = target,250 .target = target,
249 };251 };
250 try self.target_strings.append(allocator, try targetToAppleString(allocator, target));252 const apple_string = try targetToAppleString(allocator, target);
253 try self.target_strings.append(allocator, apple_string);
251254
252 const abi = target.abi orelse .none;255 const abi = target.abi orelse .none;
253 if (abi == .simulator) {256 if (abi == .simulator) {
...@@ -270,22 +273,29 @@ const TargetMatcher = struct {...@@ -270,22 +273,29 @@ const TargetMatcher = struct {
270 self.target_strings.deinit(self.allocator);273 self.target_strings.deinit(self.allocator);
271 }274 }
272275
273 fn targetToAppleString(allocator: Allocator, target: CrossTarget) ![]const u8 {276 inline fn cpuArchToAppleString(cpu_arch: std.Target.Cpu.Arch) []const u8 {
274 const cpu_arch = switch (target.cpu_arch.?) {277 return switch (cpu_arch) {
275 .aarch64 => "arm64",278 .aarch64 => "arm64",
276 .x86_64 => "x86_64",279 .x86_64 => "x86_64",
277 else => unreachable,280 else => unreachable,
278 };281 };
279 const os_tag = @tagName(target.os_tag.?);282 }
280 const target_abi = target.abi orelse .none;283
281 const abi: ?[]const u8 = switch (target_abi) {284 inline fn abiToAppleString(abi: std.Target.Abi) ?[]const u8 {
285 return switch (abi) {
282 .none => null,286 .none => null,
283 .simulator => "simulator",287 .simulator => "simulator",
284 .macabi => "maccatalyst",288 .macabi => "maccatalyst",
285 else => unreachable,289 else => unreachable,
286 };290 };
287 if (abi) |x| {291 }
288 return std.fmt.allocPrint(allocator, "{s}-{s}-{s}", .{ cpu_arch, os_tag, x });292
293 fn targetToAppleString(allocator: Allocator, target: CrossTarget) ![]const u8 {
294 const cpu_arch = cpuArchToAppleString(target.cpu_arch.?);
295 const os_tag = @tagName(target.os_tag.?);
296 const target_abi = abiToAppleString(target.abi orelse .none);
297 if (target_abi) |abi| {
298 return std.fmt.allocPrint(allocator, "{s}-{s}-{s}", .{ cpu_arch, os_tag, abi });
289 }299 }
290 return std.fmt.allocPrint(allocator, "{s}-{s}", .{ cpu_arch, os_tag });300 return std.fmt.allocPrint(allocator, "{s}-{s}", .{ cpu_arch, os_tag });
291 }301 }
...@@ -305,7 +315,26 @@ const TargetMatcher = struct {...@@ -305,7 +315,26 @@ const TargetMatcher = struct {
305 }315 }
306316
307 fn matchesArch(self: TargetMatcher, archs: []const []const u8) bool {317 fn matchesArch(self: TargetMatcher, archs: []const []const u8) bool {
308 return hasValue(archs, @tagName(self.target.cpu_arch.?));318 return hasValue(archs, cpuArchToAppleString(self.target.cpu_arch.?));
319 }
320
321 fn matchesTargetTbd(self: TargetMatcher, tbd: Tbd) !bool {
322 var arena = std.heap.ArenaAllocator.init(self.allocator);
323 defer arena.deinit();
324
325 const targets = switch (tbd) {
326 .v3 => |v3| blk: {
327 var targets = std.ArrayList([]const u8).init(arena.allocator());
328 for (v3.archs) |arch| {
329 const target = try std.fmt.allocPrint(arena.allocator(), "{s}-{s}", .{ arch, v3.platform });
330 try targets.append(target);
331 }
332 break :blk targets.items;
333 },
334 .v4 => |v4| v4.targets,
335 };
336
337 return self.matchesTarget(targets);
309 }338 }
310};339};
311340
...@@ -348,11 +377,7 @@ pub fn parseFromStub(...@@ -348,11 +377,7 @@ pub fn parseFromStub(
348 defer matcher.deinit();377 defer matcher.deinit();
349378
350 for (lib_stub.inner, 0..) |elem, stub_index| {379 for (lib_stub.inner, 0..) |elem, stub_index| {
351 const is_match = switch (elem) {380 if (!(try matcher.matchesTargetTbd(elem))) continue;
352 .v3 => |stub| matcher.matchesArch(stub.archs),
353 .v4 => |stub| matcher.matchesTarget(stub.targets),
354 };
355 if (!is_match) continue;
356381
357 if (stub_index > 0) {382 if (stub_index > 0) {
358 // TODO I thought that we could switch on presence of `parent-umbrella` map;383 // TODO I thought that we could switch on presence of `parent-umbrella` map;