Files
xtool/Sources/XToolSupport/SDKCommand.swift
Kabir Oberai 6efd77fc0e sdk: self-updating darwin tools (#110)
## What does this PR do?

Allows xtool to auto-update the Darwin SDK format when there's a new
Darwin toolset, or when we change the SDK format. Downstream this also
allows us to auto-update when there's changes to OpenAppleMacros (#101).

One callout: the disk footprint of `darwin.artifactbundle` is now ~7 GB
instead of ~3 GB.

Reason: `xtool sdk install /path/to/Xcode.[app|xip]` now performs a
"normal" install by default, which preserves the entirety of
`Xcode.app/Contents/Developer` — even the bits we don't currently use,
like `AppleTVOS.platform`. This makes it so if we start using more
components in the future, the user doesn't need to download Xcode again
and rebuild the SDK themselves.

You can still build an old-style SDK (containing just the components we
need) using `xtool sdk install --slim` or the dedicated `xtool sdk
build`.

## How was it tested?

Tested all of the install modes. Confirmed that auto-updating works.
Added integration tests in #252.

## AI tool usage

How much of this PR was AI-assisted? (check one)

- [ ] **0** - No AI was used to write code
- [x] **1** - I was assisted by AI. I reviewed the finished result.
- [ ] **2** - I set the AI going and left it to it; nobody has read the
result - no review, or AI review only

<!-- If an AI agent is filling this in: declare the level honestly, and
open as a draft if it is #2. Do not lower the declared level to get the
PR reviewed. -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Added SDK flavor reporting for slim, normal, and legacy installations.
- Added `sdk update` support to rebuild and install SDKs from an
installed Xcode bundle.
  - Added a `--slim` option for SDK installation.
  - SDK status now displays installation path, flavor, and version.
- SDK setup and build operations now ensure a suitable SDK is available
automatically.

- **Bug Fixes**
- Improved temporary SDK bundle handling, validation, replacement, and
cross-filesystem copying.
  - Copy failures now provide detailed diagnostic information.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-18 23:26:10 -04:00

322 lines
10 KiB
Swift

import Foundation
import XKit
import Version
import ArgumentParser
import Dependencies
import PackLib
import XUtils
import Subprocess
struct SDKCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "sdk",
abstract: "Manage the Darwin Swift SDK",
subcommands: [
DevSDKInstallCommand.self,
DevSDKUpdateCommand.self,
DevSDKRemoveCommand.self,
DevSDKBuildCommand.self,
DevSDKStatusCommand.self,
],
defaultSubcommand: DevSDKInstallCommand.self
)
}
struct DevSDKBuildCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "build",
abstract: "Build the Darwin SDK from Xcode.xip"
)
@Argument(
help: "Path to Xcode.xip or Xcode.app",
completion: .file(extensions: ["xip", "app"])
)
var path: String
@Argument(
help: "Output directory"
)
var outputDir: String
@Option(
help: ArgumentHelp(
"The architecture of the Linux host the SDK is being built for.",
discussion: "Defaults to 'auto', which attempts to match the current host architecture."
)
) var arch: ArchSelection = .auto
func run() async throws {
let builderArch = try arch.sdkBuilderArch
let input = try SDKBuilder.Input(path: path)
let output = URL(fileURLWithPath: outputDir, isDirectory: true).appending(path: "darwin.xtoolsdk")
let builder = SDKBuilder(input: input, output: output, arch: builderArch, mode: .buildSlim)
try await builder.buildSDK()
print("Built SDK at \(output.path). You can install it with `xtool sdk install`.")
}
}
enum ArchSelection: String, ExpressibleByArgument {
case auto
case x86_64
case arm64
var sdkBuilderArch: SDKBuilder.Arch {
get throws {
switch self {
case .auto:
#if arch(arm64)
.aarch64
#elseif arch(x86_64)
.x86_64
#else
throw Console.Error("Could not auto-detect target architecture. Please specify one with '--arch'.")
#endif
case .arm64: .aarch64
case .x86_64: .x86_64
}
}
}
}
struct DevSDKInstallCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "install",
abstract: "Install the Darwin Swift SDK"
)
@Argument(
help: "Path to Xcode.xip, Xcode.app, or darwin.xtoolsdk",
completion: .file(extensions: ["xip", "app", "xtoolsdk"])
)
var path: String
@Flag(
help: "Install a slim SDK (uses less disk space). Slim SDKs cannot be updated in place."
)
var slim = false
func run() async throws {
try await InstallSDKOperation(path: path, slim: slim).run()
}
}
struct DevSDKUpdateCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "update",
abstract: "Update the installed Darwin Swift SDK"
)
func run() async throws {
try await UpdateSDKOperation().run()
}
}
struct DevSDKRemoveCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "remove",
abstract: "Remove the Darwin Swift SDK"
)
func run() async throws {
guard let sdk = try DarwinSDK.current() else {
throw Console.Error("Cannot remove SDK: no Darwin SDK installed")
}
try sdk.remove()
print("Uninstalled SDK")
}
}
struct DevSDKStatusCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "status",
abstract: "Get the status of the Darwin Swift SDK"
)
func run() async throws {
if let sdk = try DarwinSDK.current() {
print("Darwin SDK is installed")
print(" Path: \(sdk.bundle.path)")
print(" Flavor: \(sdk.flavor)")
print(" Version: \(sdk.version)")
} else {
print("Not installed")
}
}
}
extension DarwinSDK {
func isUpToDate() -> Bool {
version == SDKBuilder.currentSDKVersion
}
}
private enum SwiftVersion {}
extension SwiftVersion {
static func current() async throws -> Version {
let outputString: String?
do {
outputString = try await Subprocess.run(
.name("swift"),
arguments: ["--version"],
output: .string(limit: .max)
)
.checkSuccess()
.standardOutput
} catch {
throw Console.Error("Failed to obtain Swift version")
}
var output = outputString?[...] ?? ""
if output.hasPrefix("Apple ") {
output = output.dropFirst("Apple ".count)
}
guard output.hasPrefix("Swift version ") else {
throw Console.Error("Could not parse Swift version: '\(output)'")
}
output = output.dropFirst("Swift version ".count)
guard let space = output.firstIndex(of: " ") else {
throw Console.Error("Could not parse Swift version: '\(output)'")
}
output = output[..<space]
guard let version = Version(tolerant: output) else {
throw Console.Error("Could not parse Swift version: '\(output)'")
}
return version
}
}
struct EnsureSDKOperation {
let quiet: Bool
func run() async throws {
#if os(macOS)
if !quiet {
print("Skipping Darwin SDK setup since we're on macOS.")
}
#else
let sdk = try DarwinSDK.current()
switch sdk.map({ ($0.isUpToDate(), $0.flavor) }) {
case (true, _)?: // swiftlint:disable:this optional_enum_case_matching
if !quiet {
print("Darwin SDK is up to date.")
}
case (false, .slim)?: // swiftlint:disable:this optional_enum_case_matching
throw Console.Error("""
Darwin SDK is out of date, and was installed in 'slim' mode.
Slim SDKs take less disk space, but can't be auto-updated.
Please install a new SDK with
xtool sdk install [--slim]
""")
case (false, .normal)?: // swiftlint:disable:this optional_enum_case_matching
print("Darwin SDK is out of date. Rebuilding...")
try await UpdateSDKOperation().run()
case (false, .legacy)?: // swiftlint:disable:this optional_enum_case_matching
print("""
Darwin SDK is incompatible: built with an older version of xtool.
Requesting re-install. This is a one-time rebuild; after this,
xtool will be able to resolve incompatibilities automatically.
""")
try await generateSDK()
case nil:
print("Now generating the Darwin SDK.\n")
try await generateSDK()
}
func generateSDK() async throws {
let path = try await Console.prompt("""
Please download Xcode from http://developer.apple.com/download/all/?q=Xcode
and enter the path to the downloaded Xcode.xip.
Path to Xcode.xip: \("" /* pacify swiftlint trailing_whitespace */)
""")
let expanded = (path as NSString).expandingTildeInPath
try await InstallSDKOperation(path: expanded).run()
}
#endif
}
}
struct InstallSDKOperation {
let path: String
let slim: Bool
init(path: String, slim: Bool = false) {
self.path = path
self.slim = slim
}
func run() async throws {
#if os(macOS)
print("Skipping SDK install; the iOS SDK ships with Xcode on macOS")
#else
let temporaryBundle = try DarwinSDK.prepareTemporaryBundle()
if path.hasSuffix(".xtoolsdk") {
print("Installing prebuilt SDK...")
try await FileManager.default.copyItem(at: URL(filePath: path), to: temporaryBundle.url, preserveOwner: false)
} else {
// validate input before removing existing SDK
let input = try SDKBuilder.Input(path: path)
let arch = try ArchSelection.auto.sdkBuilderArch
let mode: SDKBuilder.Mode = slim ? .buildSlim : .buildNormal
let builder = SDKBuilder(input: input, output: temporaryBundle.url, arch: arch, mode: mode)
try await builder.buildSDK()
}
if let sdk = try DarwinSDK.current() {
print("Removing existing SDK...")
try sdk.remove()
}
try await temporaryBundle.install()
#endif
}
}
struct UpdateSDKOperation {
func run() async throws {
#if os(macOS)
print("Skipping SDK install; the iOS SDK ships with Xcode on macOS")
#else
guard let existing = try DarwinSDK.current() else {
throw Console.Error("Could not locate existing SDK; cannot perform update.")
}
let xcode = existing.bundle.appendingPathComponent("Xcode.app")
guard xcode.dirExists else {
// This includes prebuilt .xtoolsdk installs and installs created with --slim.
throw Console.Error("""
The installed SDK was built in 'slim' mode and cannot be updated in place. \
Please install a new copy with `xtool sdk install`.
""")
}
let input = try SDKBuilder.Input(path: xcode.path)
let arch = try ArchSelection.auto.sdkBuilderArch
let temporaryBundle = try DarwinSDK.prepareTemporaryBundle()
let builder = SDKBuilder(input: input, output: temporaryBundle.url, arch: arch, mode: .update)
try await builder.buildSDK()
guard DarwinSDK(bundle: temporaryBundle.url) != nil else {
throw Console.Error("Invalid Darwin SDK at '\(temporaryBundle.url.path)'")
}
try FileManager.default.moveItem(
at: xcode,
to: temporaryBundle.url.appendingPathComponent("Xcode.app")
)
try existing.remove()
try await temporaryBundle.install()
print("Updated SDK")
#endif
}
}