sdk: prune Xcode when installing from xip (#280)
- Commit
f9706d900b48112483e662ac016eb2ba88739fe1- Author
- Kabir Oberai <[email protected]>
- When
- 2026-09-23 05:14 UTC (2 months ago)
- Parents
a07b730
## What does this PR do?
We only need `Xcode.app/Contents/{Info.plist,version.plist,Developer}`
for the SDK (and expect that to continue being the case). We already
delete other directories when using `xtool sdk install Xcode.app` but we
weren't doing this on `xtool sdk install Xcode.xip`. Prune on the xip
path as well.
Also prune on update (and bump the SDK epoch) so that existing users
benefit from this.
## How was it tested?
- [x] Install SDK from xip, confirm `Xcode.app/Contents` only contains
those three things
- [x] Install SDK from app, confirm `Xcode.app/Contents` only contains
those three things + we don't modify the original app
- [x] Build SDK from app (`xtool sdk build Xcode.app`), confirm we don't
clobber the input Xcode app
- [x] Update SDK, confirm that extra contents are removed
- [x] SDK auto-updates after updating xtool binary
## AI tool usage
How much of this PR was AI-assisted? (check one)
- [x] **0** - No AI was used to write code
- [ ] **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
* **Bug Fixes**
* SDK builds from Xcode app and XIP inputs now check for required Xcode
components before installation. Missing components produce an error.
* Extra direct items in Xcode’s `Contents` folder are removed during XIP
builds and app updates. Slim builds remain unchanged.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->Diff
Sources/XToolSupport/SDKBuilder.swift
diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swiftindex 1826fc1..09af2dd 100644--- a/Sources/XToolSupport/SDKBuilder.swift+++ b/Sources/XToolSupport/SDKBuilder.swift@@ -76,7 +76,7 @@ struct SDKBuilder { let mode: Mode // bump this when the sdk builder logic changes- static let sdkEpoch = 2+ static let sdkEpoch = 3 // tag from https://github.com/xtool-org/darwin-tools-linux-llvm static let darwinToolsVersion = "1.1.0"@@ -425,6 +425,8 @@ struct SDKBuilder { print() } + private static let expectedXcodeChildren = ["Info.plist", "version.plist", "Developer"]+ // swiftlint:disable:next cyclomatic_complexity function_body_length private func installDeveloper(in output: URL) async throws -> URL { let dev = output.appendingPathComponent("Developer")@@ -458,19 +460,25 @@ struct SDKBuilder { wanted = try await extractXIP(inputPath: inputPath, outDir: output.path) appDir = expectedAppDir cleanupStageDir = nil+ try cleanUpXcode(appDir) case (.xip, .update): throw Console.Error("Can't update with xip input")- case (.app(let appPath), .buildSlim), (.app(let appPath), .update):+ case (.app(let appPath), .buildSlim): appDir = URL(fileURLWithPath: appPath) wanted = nil cleanupStageDir = nil+ case (.app(let appPath), .update):+ appDir = URL(fileURLWithPath: appPath)+ wanted = nil+ cleanupStageDir = nil+ try cleanUpXcode(appDir) case (.app(let appPath), .buildNormal): let source = URL(fileURLWithPath: appPath) let sourceContentsDir = source.appendingPathComponent("Contents") let expectedContentsDir = expectedAppDir.appendingPathComponent("Contents") try FileManager.default.createDirectory(at: expectedContentsDir, withIntermediateDirectories: true) print("[Copying Xcode.app] This might take a minute...")- for child in ["Info.plist", "version.plist", "Developer"] {+ for child in Self.expectedXcodeChildren { let expectedFile = expectedContentsDir.appendingPathComponent(child) let sourceFile = sourceContentsDir.appendingPathComponent(child) guard FileManager.default.fileExists(atPath: sourceFile.path) else {@@ -630,6 +638,30 @@ struct SDKBuilder { return dev } + /// Removes unneeded files from a copy of Xcode.app that's owned by xtool.+ /// + /// This is conservative: we keep anything that we expect _might_ be necessary+ /// even in a future update of xtool, so that SDK self-updates work. We only+ /// delete files that we're relatively confident won't be needed.+ /// + /// - Important: ONLY call this if we own appDir, lest we delete files from the user's Xcode.+ private func cleanUpXcode(_ appDir: URL) throws {+ let contentsDir = appDir.appendingPathComponent("Contents")+ let contents = try FileManager.default.contentsOfDirectory(+ at: contentsDir,+ includingPropertiesForKeys: nil+ )+ let expectedChildren = Set(Self.expectedXcodeChildren)+ let missingChildren = expectedChildren.subtracting(contents.map(\.lastPathComponent))+ guard missingChildren.isEmpty else {+ let missing = missingChildren.sorted().joined(separator: ", ")+ throw Console.Error("Unrecognized xip layout: missing \(missing)")+ }+ for child in contents where !expectedChildren.contains(child.lastPathComponent) {+ try FileManager.default.removeItem(at: child)+ }+ }+ private func extractXIP(inputPath: String, outDir: String) async throws -> Int { // unxip doesn't like cooperative cancellation atm so shield it. // if the user does a ^C during unxip, we'll just wait until extraction