xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XToolSupport / SDKCommand.swift
SDKCommand.swift286 lines · 8.9 KiBRaw
| 1 | import Foundation |
| 2 | import XKit |
| 3 | import ArgumentParser |
| 4 | import Dependencies |
| 5 | import PackLib |
| 6 | import XUtils |
| 7 | import Subprocess |
| 8 | |
| 9 | struct SDKCommand: AsyncParsableCommand { |
| 10 | static let configuration = CommandConfiguration( |
| 11 | commandName: "sdk", |
| 12 | abstract: "Manage the Darwin Swift SDK", |
| 13 | subcommands: [ |
| 14 | DevSDKInstallCommand.self, |
| 15 | DevSDKUpdateCommand.self, |
| 16 | DevSDKRemoveCommand.self, |
| 17 | DevSDKBuildCommand.self, |
| 18 | DevSDKStatusCommand.self, |
| 19 | ], |
| 20 | defaultSubcommand: DevSDKInstallCommand.self |
| 21 | ) |
| 22 | } |
| 23 | |
| 24 | struct DevSDKBuildCommand: AsyncParsableCommand { |
| 25 | static let configuration = CommandConfiguration( |
| 26 | commandName: "build", |
| 27 | abstract: "Build the Darwin SDK from Xcode.xip" |
| 28 | ) |
| 29 | |
| 30 | @Argument( |
| 31 | help: "Path to Xcode.xip or Xcode.app", |
| 32 | completion: .file(extensions: ["xip", "app"]) |
| 33 | ) |
| 34 | var path: String |
| 35 | |
| 36 | @Argument( |
| 37 | help: "Output directory" |
| 38 | ) |
| 39 | var outputDir: String |
| 40 | |
| 41 | @Option( |
| 42 | help: ArgumentHelp( |
| 43 | "The architecture of the Linux host the SDK is being built for.", |
| 44 | discussion: "Defaults to 'auto', which attempts to match the current host architecture." |
| 45 | ) |
| 46 | ) var arch: ArchSelection = .auto |
| 47 | |
| 48 | func run() async throws { |
| 49 | let builderArch = try arch.sdkBuilderArch |
| 50 | let input = try SDKBuilder.Input(path: path) |
| 51 | let output = URL(fileURLWithPath: outputDir, isDirectory: true).appending(path: "darwin.xtoolsdk") |
| 52 | let builder = SDKBuilder(input: input, output: output, arch: builderArch, mode: .buildSlim) |
| 53 | try await builder.buildSDK() |
| 54 | print("Built SDK at \(output.path). You can install it with `xtool sdk install`.") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | enum ArchSelection: String, ExpressibleByArgument { |
| 59 | case auto |
| 60 | case x86_64 |
| 61 | case arm64 |
| 62 | |
| 63 | var sdkBuilderArch: SDKBuilder.Arch { |
| 64 | get throws { |
| 65 | switch self { |
| 66 | case .auto: |
| 67 | #if arch(arm64) |
| 68 | .aarch64 |
| 69 | #elseif arch(x86_64) |
| 70 | .x86_64 |
| 71 | #else |
| 72 | throw Console.Error("Could not auto-detect target architecture. Please specify one with '--arch'.") |
| 73 | #endif |
| 74 | case .arm64: .aarch64 |
| 75 | case .x86_64: .x86_64 |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | struct DevSDKInstallCommand: AsyncParsableCommand { |
| 82 | static let configuration = CommandConfiguration( |
| 83 | commandName: "install", |
| 84 | abstract: "Install the Darwin Swift SDK" |
| 85 | ) |
| 86 | |
| 87 | @Argument( |
| 88 | help: "Path to Xcode.xip, Xcode.app, or darwin.xtoolsdk", |
| 89 | completion: .file(extensions: ["xip", "app", "xtoolsdk"]) |
| 90 | ) |
| 91 | var path: String |
| 92 | |
| 93 | @Flag( |
| 94 | help: "Install a slim SDK (uses less disk space). Slim SDKs cannot be updated in place." |
| 95 | ) |
| 96 | var slim = false |
| 97 | |
| 98 | func run() async throws { |
| 99 | try await InstallSDKOperation(path: path, slim: slim).run() |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | struct DevSDKUpdateCommand: AsyncParsableCommand { |
| 104 | static let configuration = CommandConfiguration( |
| 105 | commandName: "update", |
| 106 | abstract: "Update the installed Darwin Swift SDK" |
| 107 | ) |
| 108 | |
| 109 | func run() async throws { |
| 110 | try await UpdateSDKOperation().run() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | struct DevSDKRemoveCommand: AsyncParsableCommand { |
| 115 | static let configuration = CommandConfiguration( |
| 116 | commandName: "remove", |
| 117 | abstract: "Remove the Darwin Swift SDK" |
| 118 | ) |
| 119 | |
| 120 | func run() async throws { |
| 121 | guard let sdk = try DarwinSDK.current() else { |
| 122 | throw Console.Error("Cannot remove SDK: no Darwin SDK installed") |
| 123 | } |
| 124 | try sdk.remove() |
| 125 | print("Uninstalled SDK") |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | struct DevSDKStatusCommand: AsyncParsableCommand { |
| 130 | static let configuration = CommandConfiguration( |
| 131 | commandName: "status", |
| 132 | abstract: "Get the status of the Darwin Swift SDK" |
| 133 | ) |
| 134 | |
| 135 | func run() async throws { |
| 136 | if let sdk = try DarwinSDK.current() { |
| 137 | print("Darwin SDK is installed") |
| 138 | print(" Path: \(sdk.bundle.path)") |
| 139 | print(" Flavor: \(sdk.flavor)") |
| 140 | print(" Version: \(sdk.version)") |
| 141 | } else { |
| 142 | print("Not installed") |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | extension DarwinSDK { |
| 148 | func isUpToDate() -> Bool { |
| 149 | version == SDKBuilder.currentSDKVersion |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | struct EnsureSDKOperation { |
| 154 | let quiet: Bool |
| 155 | |
| 156 | func run() async throws { |
| 157 | #if os(macOS) |
| 158 | if !quiet { |
| 159 | print("Skipping Darwin SDK setup since we're on macOS.") |
| 160 | } |
| 161 | #else |
| 162 | let sdk = try DarwinSDK.current() |
| 163 | switch sdk.map({ ($0.isUpToDate(), $0.flavor) }) { |
| 164 | case (true, _)?: // swiftlint:disable:this optional_enum_case_matching |
| 165 | if !quiet { |
| 166 | print("Darwin SDK is up to date.") |
| 167 | } |
| 168 | case (false, .slim)?: // swiftlint:disable:this optional_enum_case_matching |
| 169 | throw Console.Error(""" |
| 170 | Darwin SDK is out of date, and was installed in 'slim' mode. |
| 171 | |
| 172 | Slim SDKs take less disk space, but can't be auto-updated. |
| 173 | Please install a new SDK with |
| 174 | xtool sdk install [--slim] |
| 175 | """) |
| 176 | case (false, .normal)?: // swiftlint:disable:this optional_enum_case_matching |
| 177 | print("Darwin SDK is out of date. Rebuilding...") |
| 178 | try await UpdateSDKOperation().run() |
| 179 | case (false, .legacy)?: // swiftlint:disable:this optional_enum_case_matching |
| 180 | print(""" |
| 181 | Darwin SDK is incompatible: built with an older version of xtool. |
| 182 | |
| 183 | Requesting re-install. This is a one-time rebuild; after this, |
| 184 | xtool will be able to resolve incompatibilities automatically. |
| 185 | |
| 186 | """) |
| 187 | try await generateSDK() |
| 188 | case nil: |
| 189 | print("Now generating the Darwin SDK.\n") |
| 190 | try await generateSDK() |
| 191 | } |
| 192 | |
| 193 | func generateSDK() async throws { |
| 194 | let path = try await Console.prompt(""" |
| 195 | Please download Xcode from http://developer.apple.com/download/all/?q=Xcode |
| 196 | and enter the path to the downloaded Xcode.xip. |
| 197 | |
| 198 | Path to Xcode.xip: \("" /* pacify swiftlint trailing_whitespace */) |
| 199 | """) |
| 200 | |
| 201 | let expanded = (path as NSString).expandingTildeInPath |
| 202 | |
| 203 | try await InstallSDKOperation(path: expanded).run() |
| 204 | } |
| 205 | #endif |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | struct InstallSDKOperation { |
| 210 | let path: String |
| 211 | let slim: Bool |
| 212 | |
| 213 | init(path: String, slim: Bool = false) { |
| 214 | self.path = path |
| 215 | self.slim = slim |
| 216 | } |
| 217 | |
| 218 | func run() async throws { |
| 219 | #if os(macOS) |
| 220 | print("Skipping SDK install; the iOS SDK ships with Xcode on macOS") |
| 221 | #else |
| 222 | |
| 223 | let temporaryBundle = try DarwinSDK.prepareTemporaryBundle() |
| 224 | |
| 225 | if path.hasSuffix(".xtoolsdk") { |
| 226 | print("Installing prebuilt SDK...") |
| 227 | try await FileManager.default.copyItem(at: URL(filePath: path), to: temporaryBundle.url, preserveOwner: false) |
| 228 | } else { |
| 229 | // validate input before removing existing SDK |
| 230 | let input = try SDKBuilder.Input(path: path) |
| 231 | let arch = try ArchSelection.auto.sdkBuilderArch |
| 232 | |
| 233 | let mode: SDKBuilder.Mode = slim ? .buildSlim : .buildNormal |
| 234 | let builder = SDKBuilder(input: input, output: temporaryBundle.url, arch: arch, mode: mode) |
| 235 | try await builder.buildSDK() |
| 236 | } |
| 237 | |
| 238 | if let sdk = try DarwinSDK.current() { |
| 239 | print("Removing existing SDK...") |
| 240 | try sdk.remove() |
| 241 | } |
| 242 | |
| 243 | try await temporaryBundle.install() |
| 244 | #endif |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | struct UpdateSDKOperation { |
| 249 | func run() async throws { |
| 250 | #if os(macOS) |
| 251 | print("Skipping SDK install; the iOS SDK ships with Xcode on macOS") |
| 252 | #else |
| 253 | guard let existing = try DarwinSDK.current() else { |
| 254 | throw Console.Error("Could not locate existing SDK; cannot perform update.") |
| 255 | } |
| 256 | let xcode = existing.bundle.appendingPathComponent("Xcode.app") |
| 257 | guard xcode.dirExists else { |
| 258 | // This includes prebuilt .xtoolsdk installs and installs created with --slim. |
| 259 | throw Console.Error(""" |
| 260 | The installed SDK was built in 'slim' mode and cannot be updated in place. \ |
| 261 | Please install a new copy with `xtool sdk install`. |
| 262 | """) |
| 263 | } |
| 264 | |
| 265 | let input = try SDKBuilder.Input(path: xcode.path) |
| 266 | let arch = try ArchSelection.auto.sdkBuilderArch |
| 267 | |
| 268 | let temporaryBundle = try DarwinSDK.prepareTemporaryBundle() |
| 269 | let builder = SDKBuilder(input: input, output: temporaryBundle.url, arch: arch, mode: .update) |
| 270 | try await builder.buildSDK() |
| 271 | |
| 272 | guard DarwinSDK(bundle: temporaryBundle.url) != nil else { |
| 273 | throw Console.Error("Invalid Darwin SDK at '\(temporaryBundle.url.path)'") |
| 274 | } |
| 275 | |
| 276 | try FileManager.default.moveItem( |
| 277 | at: xcode, |
| 278 | to: temporaryBundle.url.appendingPathComponent("Xcode.app") |
| 279 | ) |
| 280 | try existing.remove() |
| 281 | try await temporaryBundle.install() |
| 282 | |
| 283 | print("Updated SDK") |
| 284 | #endif |
| 285 | } |
| 286 | } |