Repository navigation
build: support tvOS, macOS, and Mac Catalyst apps #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
Open
bvolpato
wants to merge
4
commits into
xtool-org:main
Choose a base branch
from
bvolpato:codex/linux-apple-platforms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
554df08
build: support tvOS and Mac Catalyst targets
bvolpato c386095
fix: reject unsupported native desktop package resources
bvolpato 272799f
fix: validate platform targets and linker options
bvolpato a444f0d
fix: normalize macOS SDK aliases
bvolpato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| 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 init(validatingTriple triple: String) throws { | ||
| guard let platform = Self(triple: triple) else { | ||
| let supported = Self.allCases.map(\.rawValue).joined(separator: ", ") | ||
| throw StringError("Unsupported target triple '\(triple)'. Supported platforms: \(supported).") | ||
| } | ||
| self = platform | ||
| } | ||
|
|
||
| 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-simulator" : "ios" | ||
| case .tvOS: return simulator ? "tvos-simulator" : "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)", | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 let index = words.firstIndex(of: "LLD"), index + 1 < words.endIndex else { return nil } | ||
| return words[index + 1].split(separator: ".").first.flatMap { Int($0) } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.