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;
99const math = std.math;
1010const mem = std.mem;
1111const fat = @import("fat.zig");
12const tapi = @import("../tapi.zig");
1213
1314const Allocator = mem.Allocator;
1415const CrossTarget = std.zig.CrossTarget;
15const LibStub = @import("../tapi.zig").LibStub;
16const LibStub = tapi.LibStub;
1617const LoadCommandIterator = macho.LoadCommandIterator;
1718const MachO = @import("../MachO.zig");
19const Tbd = tapi.Tbd;
1820
1921id: ?Id = null,
2022weak: bool = false,
......@@ -247,7 +249,8 @@ const TargetMatcher = struct {
247249 .allocator = allocator,
248250 .target = target,
249251 };
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
252255 const abi = target.abi orelse .none;
253256 if (abi == .simulator) {
......@@ -270,22 +273,29 @@ const TargetMatcher = struct {
270273 self.target_strings.deinit(self.allocator);
271274 }
272275
273 fn targetToAppleString(allocator: Allocator, target: CrossTarget) ![]const u8 {
274 const cpu_arch = switch (target.cpu_arch.?) {
276 inline fn cpuArchToAppleString(cpu_arch: std.Target.Cpu.Arch) []const u8 {
277 return switch (cpu_arch) {
275278 .aarch64 => "arm64",
276279 .x86_64 => "x86_64",
277280 else => unreachable,
278281 };
279 const os_tag = @tagName(target.os_tag.?);
280 const target_abi = target.abi orelse .none;
281 const abi: ?[]const u8 = switch (target_abi) {
282 }
283
284 inline fn abiToAppleString(abi: std.Target.Abi) ?[]const u8 {
285 return switch (abi) {
282286 .none => null,
283287 .simulator => "simulator",
284288 .macabi => "maccatalyst",
285289 else => unreachable,
286290 };
287 if (abi) |x| {
288 return std.fmt.allocPrint(allocator, "{s}-{s}-{s}", .{ cpu_arch, os_tag, x });
291 }
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 });
289299 }
290300 return std.fmt.allocPrint(allocator, "{s}-{s}", .{ cpu_arch, os_tag });
291301 }
......@@ -305,7 +315,26 @@ const TargetMatcher = struct {
305315 }
306316
307317 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);
309338 }
310339};
311340
......@@ -348,11 +377,7 @@ pub fn parseFromStub(
348377 defer matcher.deinit();
349378
350379 for (lib_stub.inner, 0..) |elem, stub_index| {
351 const is_match = switch (elem) {
352 .v3 => |stub| matcher.matchesArch(stub.archs),
353 .v4 => |stub| matcher.matchesTarget(stub.targets),
354 };
355 if (!is_match) continue;
380 if (!(try matcher.matchesTargetTbd(elem))) continue;
356381
357382 if (stub_index > 0) {
358383 // TODO I thought that we could switch on presence of `parent-umbrella` map;