From b2b3845e40936420fb142c21cf108b531839fa0e Mon Sep 17 00:00:00 2001 From: guitaripod Date: Sun, 15 Nov 2026 15:11:16 +0200 Subject: [PATCH 1/2] packer: don't embed universal binaries of static archives Some xcframeworks wrap static archives in a universal (fat) Mach-O, such as the static Sentry framework and the MPVKit frameworks. Only the outer magic was checked, so these were taken for dynamic frameworks and embedded, and installing the app then failed with "contained no image slices". Treat a binary as static when every slice of a universal binary is an archive. Refs #130 --- Sources/PackLib/Packer+BinaryTarget.swift | 50 +++++++++++++++++++ Sources/PackLib/Packer.swift | 9 +--- Tests/XToolTests/PackerTests.swift | 60 +++++++++++++++++++++++ 3 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 Sources/PackLib/Packer+BinaryTarget.swift diff --git a/Sources/PackLib/Packer+BinaryTarget.swift b/Sources/PackLib/Packer+BinaryTarget.swift new file mode 100644 index 00000000..fc563633 --- /dev/null +++ b/Sources/PackLib/Packer+BinaryTarget.swift @@ -0,0 +1,50 @@ +import Foundation + +extension Packer { + private static let archiveMagics = [Data("!\n".utf8), Data("!\n".utf8)] + private static let maxFatSlices = 64 + + /// Whether the binary at `url` is a static archive, either bare or as every slice of a universal + /// binary. Static frameworks are already linked into the executable, so they must not be embedded. + /// + /// The archive magics are listed in + /// https://github.com/apple/llvm-project/blob/e716ff14c46490d2da6b240806c04e2beef01f40/llvm/include/llvm/Object/Archive.h#L33 + static func isStaticArchive(at url: URL) throws -> Bool { + let handle = try FileHandle(forReadingFrom: url) + defer { try? handle.close() } + + func read(_ count: Int, at offset: UInt64) throws -> Data { + try handle.seek(toOffset: offset) + return try handle.read(upToCount: count) ?? Data() + } + + let header = try read(8, at: 0) + if archiveMagics.contains(header) { return true } + guard header.count == 8 else { return false } + + let archSize: Int + switch header.bigEndian(UInt32.self, at: 0) { + case 0xCAFEBABE: archSize = 20 + case 0xCAFEBABF: archSize = 32 + default: return false + } + let sliceCount = Int(header.bigEndian(UInt32.self, at: 4)) + guard (1...maxFatSlices).contains(sliceCount) else { return false } + + let archs = try read(sliceCount * archSize, at: 8) + guard archs.count == sliceCount * archSize else { return false } + return try (0..(_: T.Type, at offset: Int) -> T { + withUnsafeBytes { T(bigEndian: $0.loadUnaligned(fromByteOffset: offset, as: T.self)) } + } +} diff --git a/Sources/PackLib/Packer.swift b/Sources/PackLib/Packer.swift index 6506fe7d..bf882879 100644 --- a/Sources/PackLib/Packer.swift +++ b/Sources/PackLib/Packer.swift @@ -166,17 +166,12 @@ public struct Packer: Sendable { try await packFile(srcName: "\(package)_\(target).bundle") case .binaryTarget(let name): let src = URL(fileURLWithPath: "\(name).framework/\(name)", relativeTo: binDir) - let magic = Data("!\n".utf8) - let thinMagic = Data("!\n".utf8) - guard let bytes = try? FileHandle(forReadingFrom: src).read(upToCount: magic.count) else { + guard FileManager.default.fileExists(atPath: src.path) else { // if we can't find the binary, it might be a static framework that SwiftPM // did not copy into the .build directory. we don't need to pack it anyway. break } - // if the magic matches one of these it's a static archive; don't embed it. - // https://github.com/apple/llvm-project/blob/e716ff14c46490d2da6b240806c04e2beef01f40/llvm/include/llvm/Object/Archive.h#L33 - // swiftlint:disable:previous line_length - if bytes != magic && bytes != thinMagic { + if try !Self.isStaticArchive(at: src) { try await packFile(srcName: "\(name).framework", dstName: "Frameworks/\(name).framework", sign: true) } case .library(let name): diff --git a/Tests/XToolTests/PackerTests.swift b/Tests/XToolTests/PackerTests.swift index 5aabda83..39f19224 100644 --- a/Tests/XToolTests/PackerTests.swift +++ b/Tests/XToolTests/PackerTests.swift @@ -1,3 +1,4 @@ +import Foundation import Testing @testable import PackLib @@ -13,3 +14,62 @@ import Testing #expect(Packer.cfBundleSupportedPlatforms(for: "unknown-triple") == ["iPhoneOS"]) } + +@Test func staticArchivesAreDetectedBareAndInsideUniversalBinaries() throws { + let archive = Data("!\n".utf8) + Data(repeating: 0, count: 64) + let thinArchive = Data("!\n".utf8) + Data(repeating: 0, count: 64) + let machO = Data([0xCF, 0xFA, 0xED, 0xFE]) + Data(repeating: 0, count: 64) + + #expect(try isStaticArchive(archive)) + #expect(try isStaticArchive(thinArchive)) + #expect(try isStaticArchive(universalBinary([archive]))) + #expect(try isStaticArchive(universalBinary([archive, thinArchive]))) + #expect(try isStaticArchive(universalBinary([archive, archive], is64: true))) + + #expect(try !isStaticArchive(machO)) + #expect(try !isStaticArchive(universalBinary([machO, machO]))) + #expect(try !isStaticArchive(universalBinary([archive, machO]))) + #expect(try !isStaticArchive(universalBinary([machO, archive], is64: true))) + #expect(try !isStaticArchive(Data())) + #expect(try !isStaticArchive(Data([0xCA, 0xFE, 0xBA, 0xBE, 0, 0, 0, 2]))) + #expect(try !isStaticArchive(Data([0xCA, 0xFE, 0xBA, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF]))) + #expect(try !isStaticArchive(universalBinary([archive]).prefix(20))) + #expect(try !isStaticArchive(universalBinary([archive]).prefix(30))) +} + +private func universalBinary(_ slices: [Data], is64: Bool = false) -> Data { + let archSize = is64 ? 32 : 20 + var data = Data() + data.appendBigEndian(UInt32(is64 ? 0xCAFEBABF : 0xCAFEBABE)) + data.appendBigEndian(UInt32(slices.count)) + var offset = 8 + archSize * slices.count + for slice in slices { + data.appendBigEndian(UInt32(0x0100_000C)) + data.appendBigEndian(UInt32(0)) + if is64 { + data.appendBigEndian(UInt64(offset)) + data.appendBigEndian(UInt64(slice.count)) + data.appendBigEndian(UInt32(3)) + data.appendBigEndian(UInt32(0)) + } else { + data.appendBigEndian(UInt32(offset)) + data.appendBigEndian(UInt32(slice.count)) + data.appendBigEndian(UInt32(3)) + } + offset += slice.count + } + return slices.reduce(into: data) { $0.append($1) } +} + +private func isStaticArchive(_ contents: Data) throws -> Bool { + let url = FileManager.default.temporaryDirectory.appendingPathComponent("xtool-packer-\(UUID().uuidString)") + try contents.write(to: url) + defer { try? FileManager.default.removeItem(at: url) } + return try Packer.isStaticArchive(at: url) +} + +extension Data { + fileprivate mutating func appendBigEndian(_ value: T) { + Swift.withUnsafeBytes(of: value.bigEndian) { append(contentsOf: $0) } + } +} From f3e164ee7bf689b7f9315b26f9219ff5d1deccaf Mon Sep 17 00:00:00 2001 From: guitaripod Date: Sun, 15 Nov 2026 15:12:44 +0200 Subject: [PATCH 2/2] packer: find binary target frameworks through their xcframework A binary target can ship a framework named differently from the target: Sentry-Dynamic.xcframework contains Sentry.framework, and SentryObjC-Dynamic.xcframework contains SentryObjC.framework. Looking for .framework missed them, so they were never embedded and the app crashed at launch with "Library not loaded". Read the LibraryPath that the xcframework's Info.plist lists for the build triple, matched by platform, variant and architecture the way SwiftPM picks the library it links. The xcframework comes from the package for local binary targets and from .build/artifacts for remote or zipped ones. When none is found, the .framework convention still applies. Refs #130 --- Sources/PackLib/Packer+BinaryTarget.swift | 65 +++++++++++++++++++++++ Sources/PackLib/Packer.swift | 45 ++++++++++++++-- Sources/PackLib/Planner.swift | 13 ++++- Tests/XToolTests/PackerTests.swift | 53 ++++++++++++++++++ 4 files changed, 171 insertions(+), 5 deletions(-) diff --git a/Sources/PackLib/Packer+BinaryTarget.swift b/Sources/PackLib/Packer+BinaryTarget.swift index fc563633..e3a6010e 100644 --- a/Sources/PackLib/Packer+BinaryTarget.swift +++ b/Sources/PackLib/Packer+BinaryTarget.swift @@ -4,6 +4,19 @@ extension Packer { private static let archiveMagics = [Data("!\n".utf8), Data("!\n".utf8)] private static let maxFatSlices = 64 + /// The `LibraryPath` that an xcframework's `Info.plist` lists for `triple`, chosen the way SwiftPM + /// chooses the library to link: same platform, same variant (simulator, Mac Catalyst or none) and + /// an architecture list that includes the triple's. + static func xcframeworkLibraryPath(infoPlist: Data, triple: String) throws -> String? { + guard let slice = XCFrameworkSlice(triple: triple) else { return nil } + let info = try PropertyListDecoder().decode(XCFrameworkInfo.self, from: infoPlist) + return info.availableLibraries.first { + $0.supportedPlatform == slice.platform + && $0.supportedPlatformVariant == slice.variant + && $0.supportedArchitectures.contains(slice.architecture) + }?.libraryPath + } + /// Whether the binary at `url` is a static archive, either bare or as every slice of a universal /// binary. Static frameworks are already linked into the executable, so they must not be embedded. /// @@ -43,6 +56,58 @@ extension Packer { } } +/// The platform names that an xcframework's `Info.plist` uses for a target triple +/// such as `arm64-apple-ios17.0-simulator`. +private struct XCFrameworkSlice { + let architecture: String + let platform: String + let variant: String? + + init?(triple: String) { + let components = triple.split(separator: "-").map(String.init) + guard let appleIndex = components.firstIndex(of: "apple"), + appleIndex > 0, + components.indices.contains(appleIndex + 1) else { + return nil + } + + architecture = components[appleIndex - 1] + let os = String(components[appleIndex + 1].prefix { !$0.isNumber }) + platform = switch os { + case "macos", "macosx": "macos" + case "visionos": "xros" + default: os + } + variant = switch components.dropFirst(appleIndex + 2).first { + case "simulator": "simulator" + case "macabi": "maccatalyst" + default: nil + } + } +} + +private struct XCFrameworkInfo: Decodable { + struct Library: Decodable { + let libraryPath: String + let supportedPlatform: String + let supportedPlatformVariant: String? + let supportedArchitectures: [String] + + private enum CodingKeys: String, CodingKey { + case libraryPath = "LibraryPath" + case supportedPlatform = "SupportedPlatform" + case supportedPlatformVariant = "SupportedPlatformVariant" + case supportedArchitectures = "SupportedArchitectures" + } + } + + let availableLibraries: [Library] + + private enum CodingKeys: String, CodingKey { + case availableLibraries = "AvailableLibraries" + } +} + extension Data { fileprivate func bigEndian(_: T.Type, at offset: Int) -> T { withUnsafeBytes { T(bigEndian: $0.loadUnaligned(fromByteOffset: offset, as: T.self)) } diff --git a/Sources/PackLib/Packer.swift b/Sources/PackLib/Packer.swift index bf882879..170261cb 100644 --- a/Sources/PackLib/Packer.swift +++ b/Sources/PackLib/Packer.swift @@ -164,15 +164,22 @@ public struct Packer: Sendable { switch command { case .bundle(let package, let target): try await packFile(srcName: "\(package)_\(target).bundle") - case .binaryTarget(let name): - let src = URL(fileURLWithPath: "\(name).framework/\(name)", relativeTo: binDir) + case .binaryTarget(let name, let xcframeworkPath): + guard let framework = try await frameworkName(forBinaryTarget: name, xcframeworkPath: xcframeworkPath) else { + break + } + let src = URL(fileURLWithPath: "\(framework).framework/\(framework)", relativeTo: binDir) guard FileManager.default.fileExists(atPath: src.path) else { // if we can't find the binary, it might be a static framework that SwiftPM // did not copy into the .build directory. we don't need to pack it anyway. break } if try !Self.isStaticArchive(at: src) { - try await packFile(srcName: "\(name).framework", dstName: "Frameworks/\(name).framework", sign: true) + try await packFile( + srcName: "\(framework).framework", + dstName: "Frameworks/\(framework).framework", + sign: true + ) } case .library(let name): try await packFile(srcName: "lib\(name).dylib", dstName: "Frameworks/lib\(name).dylib", sign: true) @@ -213,6 +220,38 @@ public struct Packer: Sendable { } } + /// The framework that SwiftPM copies into the products directory for a binary target, named by the + /// `LibraryPath` that its xcframework lists for the build triple. That name can differ from the target's: + /// `Sentry-Dynamic.xcframework` ships `Sentry.framework`. Returns `nil` when the library is not a + /// framework (a static `.a`, say), and keeps the `.framework` convention when no xcframework is found. + private func frameworkName(forBinaryTarget name: String, xcframeworkPath: String?) async throws -> String? { + guard let xcframework = xcframeworkURL(forBinaryTarget: name, xcframeworkPath: xcframeworkPath) else { + return name + } + let infoPlist = try await Data(reading: xcframework.appendingPathComponent("Info.plist")) + guard let libraryPath = try Self.xcframeworkLibraryPath(infoPlist: infoPlist, triple: buildSettings.triple) else { + return nil + } + let library = URL(fileURLWithPath: libraryPath) + guard library.pathExtension == "framework" else { return nil } + return library.deletingPathExtension().lastPathComponent + } + + /// The xcframework that a binary target links: the one its package ships in place, or the one that + /// SwiftPM downloaded or unzipped into `.build/artifacts///`. + private func xcframeworkURL(forBinaryTarget name: String, xcframeworkPath: String?) -> URL? { + let fileManager = FileManager.default + if let xcframeworkPath, URL(fileURLWithPath: xcframeworkPath).dirExists { + return URL(fileURLWithPath: xcframeworkPath, isDirectory: true) + } + let artifacts = URL(fileURLWithPath: ".build/artifacts", isDirectory: true) + let packages = (try? fileManager.contentsOfDirectory(at: artifacts, includingPropertiesForKeys: nil)) ?? [] + return packages.lazy + .compactMap { try? fileManager.contentsOfDirectory(at: $0.appendingPathComponent(name), includingPropertiesForKeys: nil) } + .compactMap { $0.first { $0.pathExtension == "xcframework" } } + .first + } + static func cfBundleSupportedPlatforms(for triple: String) -> [String] { let components = triple.split(separator: "-").map(String.init) guard let appleIndex = components.firstIndex(of: "apple"), diff --git a/Sources/PackLib/Planner.swift b/Sources/PackLib/Planner.swift index df2f0125..15d6c08c 100644 --- a/Sources/PackLib/Planner.swift +++ b/Sources/PackLib/Planner.swift @@ -142,7 +142,7 @@ public struct Planner: Sendable { // host-only dependencies (e.g. SwiftSyntaxMacros). if target.type == "macro" { continue } if target.moduleType == "BinaryTarget" { - resources.append(.binaryTarget(name: targetName)) + resources.append(.binaryTarget(name: targetName, xcframeworkPath: targetPackage.xcframeworkPath(of: target))) } if target.resources?.isEmpty == false { resources.append(.bundle(package: targetPackage.name, target: targetName)) @@ -305,7 +305,7 @@ public struct Plan: Sendable { public enum Resource: Codable, Sendable, Hashable { case bundle(package: String, target: String) - case binaryTarget(name: String) + case binaryTarget(name: String, xcframeworkPath: String?) case library(name: String) case root(source: String) } @@ -411,6 +411,7 @@ private struct PackageDump: Decodable { struct Target: Decodable { let name: String + let path: String? let moduleType: String let type: String? let productDependencies: [String]? @@ -428,9 +429,17 @@ private struct PackageDump: Decodable { } let name: String + let path: String? let products: [Product]? let targets: [Target]? let platforms: [Platform]? + + /// The absolute path of a binary target's xcframework when the package ships it unzipped. + /// Remote and zipped artifacts are unpacked under `.build/artifacts` instead. + func xcframeworkPath(of target: Target) -> String? { + guard let path, let targetPath = target.path, targetPath.hasSuffix(".xcframework") else { return nil } + return URL(fileURLWithPath: targetPath, relativeTo: URL(fileURLWithPath: path, isDirectory: true)).path + } } private struct PackageGraph { diff --git a/Tests/XToolTests/PackerTests.swift b/Tests/XToolTests/PackerTests.swift index 39f19224..bec16289 100644 --- a/Tests/XToolTests/PackerTests.swift +++ b/Tests/XToolTests/PackerTests.swift @@ -15,6 +15,38 @@ import Testing #expect(Packer.cfBundleSupportedPlatforms(for: "unknown-triple") == ["iPhoneOS"]) } +@Test func xcframeworkLibraryPathFollowsBuildTriple() throws { + let infoPlist = try xcframeworkInfoPlist([ + ("ios-arm64", "Device.framework", "ios", nil, ["arm64"]), + ("ios-arm64_x86_64-simulator", "Simulator.framework", "ios", "simulator", ["arm64", "x86_64"]), + ("ios-arm64_x86_64-maccatalyst", "Catalyst.framework", "ios", "maccatalyst", ["arm64", "x86_64"]), + ("macos-arm64_x86_64", "Mac.framework", "macos", nil, ["arm64", "x86_64"]), + ("tvos-arm64", "TV.framework", "tvos", nil, ["arm64"]), + ("xros-arm64", "Vision.framework", "xros", nil, ["arm64"]), + ]) + + func libraryPath(_ triple: String) throws -> String? { + try Packer.xcframeworkLibraryPath(infoPlist: infoPlist, triple: triple) + } + + #expect(try libraryPath("arm64-apple-ios") == "Device.framework") + #expect(try libraryPath("arm64-apple-ios17.0") == "Device.framework") + #expect(try libraryPath("arm64-apple-ios-simulator") == "Simulator.framework") + #expect(try libraryPath("x86_64-apple-ios17.0-simulator") == "Simulator.framework") + #expect(try libraryPath("arm64-apple-ios-macabi") == "Catalyst.framework") + #expect(try libraryPath("arm64-apple-macosx") == "Mac.framework") + #expect(try libraryPath("arm64-apple-tvos") == "TV.framework") + #expect(try libraryPath("arm64-apple-xros2.0") == "Vision.framework") + + #expect(try libraryPath("arm64e-apple-ios") == nil) + #expect(try libraryPath("arm64-apple-watchos") == nil) + #expect(try libraryPath("unknown-triple") == nil) + + #expect(throws: (any Error).self) { + try Packer.xcframeworkLibraryPath(infoPlist: Data("not a plist".utf8), triple: "arm64-apple-ios") + } +} + @Test func staticArchivesAreDetectedBareAndInsideUniversalBinaries() throws { let archive = Data("!\n".utf8) + Data(repeating: 0, count: 64) let thinArchive = Data("!\n".utf8) + Data(repeating: 0, count: 64) @@ -37,6 +69,27 @@ import Testing #expect(try !isStaticArchive(universalBinary([archive]).prefix(30))) } +private func xcframeworkInfoPlist( + _ libraries: [(identifier: String, path: String, platform: String, variant: String?, architectures: [String])] +) throws -> Data { + let availableLibraries = libraries.map { library in + var entry: [String: Any] = [ + "LibraryIdentifier": library.identifier, + "LibraryPath": library.path, + "SupportedPlatform": library.platform, + "SupportedArchitectures": library.architectures, + ] + entry["SupportedPlatformVariant"] = library.variant + return entry + } + let info: [String: Any] = [ + "AvailableLibraries": availableLibraries, + "CFBundlePackageType": "XFWK", + "XCFrameworkFormatVersion": "1.0", + ] + return try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0) +} + private func universalBinary(_ slices: [Data], is64: Bool = false) -> Data { let archSize = is64 ? 32 : 20 var data = Data()