Repository navigation
build: support tvOS and Mac Catalyst targets #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import Foundation | ||
|
|
||
| package enum ApplePlatform: String, CaseIterable, Sendable { | ||
| case iOS | ||
| case tvOS | ||
| case macOS | ||
| case macCatalyst | ||
|
|
||
| package init?(triple: String) { | ||
| let components = triple.split(separator: "-") | ||
| guard components.count >= 3, components[1] == "apple" else { return nil } | ||
| let os = components[2].prefix { !$0.isNumber } | ||
| switch os { | ||
| case "ios": self = components.contains("macabi") ? .macCatalyst : .iOS | ||
| case "tvos": self = .tvOS | ||
| case "macos", "macosx": self = .macOS | ||
| default: return nil | ||
| } | ||
| } | ||
|
|
||
| package var usesContentsDirectory: Bool { | ||
| self == .macOS || self == .macCatalyst | ||
| } | ||
|
|
||
| package var packagePlatformName: String { | ||
| rawValue.lowercased() | ||
| } | ||
|
|
||
| package var minimumDeploymentTarget: String { | ||
| self == .macOS ? "10.15" : "13.0" | ||
| } | ||
|
|
||
| package func effectiveDeploymentTarget( | ||
| platforms: [String: String], swiftVersion: SwiftVersion, sdkMinimum: String?, tripleMinimum: String? = nil | ||
| ) -> String { | ||
| let declared = platforms[packagePlatformName] ?? (self == .macCatalyst ? platforms["ios"] : nil) | ||
| // Match PackageModel.Platform.oldestSupportedVersion in SwiftPM 6.4. | ||
| let toolchainMinimum = swiftVersion >= SwiftVersion(6, 4) | ||
| ? (self == .macOS ? "12.0" : "15.0") : minimumDeploymentTarget | ||
| return [declared, toolchainMinimum, sdkMinimum, tripleMinimum].compactMap { $0 }.max { | ||
| $0.compare($1, options: .numeric) == .orderedAscending | ||
| } ?? toolchainMinimum | ||
| } | ||
|
|
||
| package func supportedPlatform(triple: String) -> String { | ||
| let simulator = triple.split(separator: "-").contains("simulator") | ||
| switch self { | ||
| case .iOS: return simulator ? "iPhoneSimulator" : "iPhoneOS" | ||
| case .tvOS: return simulator ? "AppleTVSimulator" : "AppleTVOS" | ||
| case .macOS, .macCatalyst: return "MacOSX" | ||
| } | ||
| } | ||
|
|
||
| package func buildProductsPlatform(triple: String) -> String { | ||
| if self == .macCatalyst { return "maccatalyst" } | ||
| return supportedPlatform(triple: triple).lowercased() | ||
| } | ||
|
|
||
| package func linkerPlatform(triple: String) -> String { | ||
| let simulator = triple.split(separator: "-").contains("simulator") | ||
| switch self { | ||
| case .iOS: return simulator ? "ios-sim" : "ios" | ||
| case .tvOS: return simulator ? "tvos-sim" : "tvos" | ||
| case .macOS: return "macos" | ||
| case .macCatalyst: return "mac-catalyst" | ||
| } | ||
| } | ||
|
|
||
| package func contentsDirectory(in bundle: URL) -> URL { | ||
| usesContentsDirectory ? bundle.appendingPathComponent("Contents", isDirectory: true) : bundle | ||
| } | ||
|
|
||
| package func resourcesDirectory(in bundle: URL) -> URL { | ||
| let contents = contentsDirectory(in: bundle) | ||
| return usesContentsDirectory ? contents.appendingPathComponent("Resources", isDirectory: true) : contents | ||
| } | ||
|
|
||
| package func executableDirectory(in bundle: URL) -> URL { | ||
| let contents = contentsDirectory(in: bundle) | ||
| return usesContentsDirectory ? contents.appendingPathComponent("MacOS", isDirectory: true) : contents | ||
| } | ||
|
|
||
| package func additionalSDKOptions(sdkRoot: URL) -> [String] { | ||
| guard self == .macCatalyst else { return [] } | ||
| let support = sdkRoot.appendingPathComponent("System/iOSSupport") | ||
| let frameworks = support.appendingPathComponent("System/Library/Frameworks").path | ||
| return [ | ||
| "-Xswiftc", "-Fsystem", "-Xswiftc", frameworks, | ||
| "-Xcc", "-isystem", "-Xcc", support.appendingPathComponent("usr/include").path, | ||
| "-Xcc", "-iframework", "-Xcc", frameworks, | ||
| "-Xlinker", "-F\(frameworks)", | ||
| "-Xlinker", "-L\(support.appendingPathComponent("usr/lib").path)", | ||
| ] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,24 @@ public struct BuildSettings: Sendable { | |
| public let buildSystem: BuildSystem | ||
| public let customOptions: [String] | ||
|
|
||
| package var platform: ApplePlatform { | ||
| ApplePlatform(triple: triple) ?? .iOS | ||
| } | ||
|
|
||
| package var sdkTriple: String { | ||
| var components = triple.split(separator: "-").map(String.init) | ||
| guard components.count >= 3 else { return triple } | ||
| components[2] = String(components[2].prefix { !$0.isNumber }) | ||
| return components.joined(separator: "-") | ||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Linux native-SwiftPM builds with a deployment-bearing newly supported triple such as AGENTS.md reference: AGENTS.md:L5-L5 Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| package var tripleDeploymentTarget: String? { | ||
| let components = triple.split(separator: "-") | ||
| guard components.count >= 3 else { return nil } | ||
| let version = components[2].drop { !$0.isNumber } | ||
| return version.isEmpty ? nil : String(version) | ||
| } | ||
|
|
||
| public var sdkOptions: [String] | ||
| public var sdkEnvironment: [Environment.Key: String?] | ||
|
|
||
|
|
@@ -56,12 +74,18 @@ public struct BuildSettings: Sendable { | |
| // https://github.com/swiftlang/swift-package-manager/pull/6828 | ||
| self.sdkOptions = ["--swift-sdk", triple] | ||
| case .swiftBuild: | ||
| self.sdkOptions = ["--triple", triple] | ||
| self.sdkOptions = [ | ||
| "--triple", triple, | ||
| // Match the native toolset: SwiftUI integration APIs such as PhotosPicker live in cross-import overlays. | ||
| "-Xswiftc", "-Xfrontend", "-Xswiftc", "-enable-cross-import-overlays", | ||
| ] | ||
| #if !os(macOS) | ||
| let darwinSDK = try DarwinSDK.current() | ||
| .orThrow(StringError("No Darwin SDK configured. Please run `xtool setup`.")) | ||
| let toolset = try LinkerConfiguration.toolsetOverride(sdk: darwinSDK) | ||
| ?? darwinSDK.bundle.appendingPathComponent("toolset-swb.json") | ||
| self.sdkOptions += [ | ||
| "--toolset", "\(darwinSDK.bundle.path)/toolset-swb.json", | ||
| "--toolset", toolset.path, | ||
| ] | ||
| self.sdkEnvironment.merge([ | ||
| "XCODE_EXTRA_PLATFORM_FOLDERS": "\(darwinSDK.bundle.path)/Developer/Platforms", | ||
|
|
@@ -70,6 +94,29 @@ public struct BuildSettings: Sendable { | |
| ]) { $1 } | ||
| #endif | ||
| } | ||
|
|
||
| if platform == .macCatalyst { | ||
| if let sdkRoot = try await sdkRootURL() { | ||
| self.sdkOptions += platform.additionalSDKOptions(sdkRoot: sdkRoot) | ||
| } | ||
| } | ||
| #if !os(macOS) | ||
| if let darwinSDK = try DarwinSDK.current() { | ||
| try await LinkerConfiguration.validate(platform: platform, sdk: darwinSDK) | ||
| if self.buildSystem == .swiftPM, let toolset = try LinkerConfiguration.toolsetOverride(sdk: darwinSDK) { | ||
| self.sdkOptions += ["--toolset", toolset.path] | ||
| } | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| package func sdkRootURL() async throws -> URL? { | ||
| #if os(macOS) | ||
| let sdkName = platform.supportedPlatform(triple: triple).lowercased() | ||
| return URL(fileURLWithPath: try await Self.xcrun(["--sdk", sdkName, "--show-sdk-path"])) | ||
| #else | ||
| return try DarwinSDK.current()?.sdkRoot(for: sdkTriple) | ||
| #endif | ||
| } | ||
|
|
||
| #if os(macOS) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||||||||||||||||||
| import Foundation | ||||||||||||||||||||||
| import Subprocess | ||||||||||||||||||||||
| import XUtils | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| package enum LinkerConfiguration { | ||||||||||||||||||||||
| package static func validate(platform: ApplePlatform, sdk: DarwinSDK) async throws { | ||||||||||||||||||||||
| let override = ProcessInfo.processInfo.environment["XTOOL_LD64_LLD"] | ||||||||||||||||||||||
| let executable: URL | ||||||||||||||||||||||
| if let override { | ||||||||||||||||||||||
| guard (override as NSString).isAbsolutePath, FileManager.default.isExecutableFile(atPath: override) else { | ||||||||||||||||||||||
| throw StringError("XTOOL_LD64_LLD must name an executable at an absolute path: '\(override)'") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| executable = URL(fileURLWithPath: override) | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| executable = sdk.bundle.appendingPathComponent("toolset/bin/ld64.lld") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if platform == .macCatalyst { | ||||||||||||||||||||||
| let result = try await Subprocess.run( | ||||||||||||||||||||||
| .path(FilePath(executable.path)), arguments: ["--version"], output: .string(limit: .max) | ||||||||||||||||||||||
| ).checkSuccess() | ||||||||||||||||||||||
| let version = result.standardOutput.trimmingCharacters(in: .whitespacesAndNewlines) | ||||||||||||||||||||||
| guard let major = majorVersion(version), major >= 23 else { | ||||||||||||||||||||||
| throw StringError(""" | ||||||||||||||||||||||
| Mac Catalyst requires LLD 23 or newer on Linux for arm64e SDK reexports. \ | ||||||||||||||||||||||
| Set XTOOL_LD64_LLD to an updated ld64.lld executable. Found: \(version) | ||||||||||||||||||||||
| """) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| package static func toolsetOverride(sdk: DarwinSDK) throws -> URL? { | ||||||||||||||||||||||
| guard let override = ProcessInfo.processInfo.environment["XTOOL_LD64_LLD"] else { return nil } | ||||||||||||||||||||||
| let key = (sdk.bundle.path + "\n" + override).utf8.reduce(UInt64(14695981039346656037)) { | ||||||||||||||||||||||
| ($0 ^ UInt64($1)) &* 1099511628211 | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| let directory = try DarwinSDK.swiftPMDirectory().appendingPathComponent("xtool-toolsets/\(String(key, radix: 16))") | ||||||||||||||||||||||
| return try writeToolset(sdk: sdk, linker: override, directory: directory) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| static func writeToolset(sdk: DarwinSDK, linker: String, directory: URL) throws -> URL { | ||||||||||||||||||||||
| try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) | ||||||||||||||||||||||
| let originalBin = sdk.bundle.appendingPathComponent("toolset/bin") | ||||||||||||||||||||||
| let wrapper = directory.appendingPathComponent("ld64.lld") | ||||||||||||||||||||||
| func shellQuote(_ value: String) -> String { "'" + value.replacingOccurrences(of: "'", with: "'\\''") + "'" } | ||||||||||||||||||||||
| let script = """ | ||||||||||||||||||||||
| #!/bin/sh | ||||||||||||||||||||||
| set -eu | ||||||||||||||||||||||
| for argument do | ||||||||||||||||||||||
| if [ "$argument" = "-r" ]; then | ||||||||||||||||||||||
| exec \(shellQuote(originalBin.appendingPathComponent("ld64.lld").path)) "$@" | ||||||||||||||||||||||
| fi | ||||||||||||||||||||||
| done | ||||||||||||||||||||||
| exec \(shellQuote(linker)) "$@" | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| try Data(script.utf8).write(to: wrapper, options: .atomic) | ||||||||||||||||||||||
| try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: wrapper.path) | ||||||||||||||||||||||
| let toolset: [String: Any] = [ | ||||||||||||||||||||||
| "schemaVersion": "1.0", "rootPath": originalBin.path, | ||||||||||||||||||||||
| "linker": ["path": wrapper.path], | ||||||||||||||||||||||
| "librarian": ["path": originalBin.appendingPathComponent("llvm-lib").path], | ||||||||||||||||||||||
| ] | ||||||||||||||||||||||
| let definition = directory.appendingPathComponent("toolset.json") | ||||||||||||||||||||||
| try JSONSerialization.data(withJSONObject: toolset, options: .sortedKeys).write(to: definition, options: .atomic) | ||||||||||||||||||||||
| return definition | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| static func majorVersion(_ output: String) -> Int? { | ||||||||||||||||||||||
| let words = output.split(whereSeparator: \.isWhitespace) | ||||||||||||||||||||||
| guard words.count >= 2, words[0] == "LLD" else { return nil } | ||||||||||||||||||||||
| return words[1].split(separator: ".").first.flatMap { Int($0) } | ||||||||||||||||||||||
|
Comment on lines
+68
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Ubuntu and other systems with vendor-prefixed LLD builds, a valid AGENTS.md reference: AGENTS.md:L5-L5 Useful? React with 👍 / 👎. |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+67
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Parse the LLD version when the output starts with a vendor prefix.
This blocks Mac Catalyst builds and 🐛 Proposed fix--- "a/Sources/PackLib/LinkerConfiguration.swift"
+++ "b/Sources/PackLib/LinkerConfiguration.swift"
@@ -64,9 +64,9 @@
return definition
}
static func majorVersion(_ output: String) -> Int? {
let words = output.split(whereSeparator: \.isWhitespace)
- guard words.count >= 2, words[0] == "LLD" else { return nil }
- return words[1].split(separator: ".").first.flatMap { Int($0) }
+ guard let index = words.firstIndex(of: "LLD"), index + 1 < words.endIndex else { return nil }
+ return words[index + 1].split(separator: ".").first.flatMap { Int($0) }
}
}Add a test case such as 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Linux iOS or tvOS simulator builds,
Packernow emits these values in-platform_versionwhenever the installed SDK supplies a version. Theld64.lld --helpsignature is-platform_version <platform> <min_version> <sdk_version>, but direct checks with LLD 21 and Ubuntu LLD 20 show thatios-simandtvos-simare rejected asmalformed platform, whileios-simulatorandtvos-simulatorpass platform parsing. The SDK's current pre-23 linker therefore aborts these builds at the final link; return the long simulator platform names here.AGENTS.md reference: AGENTS.md:L5-L5
Useful? React with 👍 / 👎.