| ... | @@ -9,12 +9,14 @@ const macho = std.macho; | ... | @@ -9,12 +9,14 @@ const macho = std.macho; |
| 9 | const math = std.math; | 9 | const math = std.math; |
| 10 | const mem = std.mem; | 10 | const mem = std.mem; |
| 11 | const fat = @import("fat.zig"); | 11 | const fat = @import("fat.zig"); |
| | 12 | const tapi = @import("../tapi.zig"); |
| 12 | | 13 | |
| 13 | const Allocator = mem.Allocator; | 14 | const Allocator = mem.Allocator; |
| 14 | const CrossTarget = std.zig.CrossTarget; | 15 | const CrossTarget = std.zig.CrossTarget; |
| 15 | const LibStub = @import("../tapi.zig").LibStub; | 16 | const LibStub = tapi.LibStub; |
| 16 | const LoadCommandIterator = macho.LoadCommandIterator; | 17 | const LoadCommandIterator = macho.LoadCommandIterator; |
| 17 | const MachO = @import("../MachO.zig"); | 18 | const MachO = @import("../MachO.zig"); |
| | 19 | const Tbd = tapi.Tbd; |
| 18 | | 20 | |
| 19 | id: ?Id = null, | 21 | id: ?Id = null, |
| 20 | weak: bool = false, | 22 | weak: 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); |
| 251 | | 254 | |
| 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 | } |
| 272 | | 275 | |
| 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 | } |
| 306 | | 316 | |
| 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 | }; |
| 311 | | 340 | |
| ... | @@ -348,11 +377,7 @@ pub fn parseFromStub( | ... | @@ -348,11 +377,7 @@ pub fn parseFromStub( |
| 348 | defer matcher.deinit(); | 377 | defer matcher.deinit(); |
| 349 | | 378 | |
| 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; | | |
| 356 | | 381 | |
| 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; |