From 50f0fc0bccfe2d40a04584efd3b5aef426bcaf1a Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sat, 5 Dec 2026 17:15:43 +0000 Subject: [PATCH 1/5] feat(sdk): support iOS-only slim SDK import on Linux without requiring MacOSX.sdk --- Sources/XToolSupport/SDKBuilder.swift | 268 ++++++++++++++++--------- Tests/XToolTests/SDKBuilderTests.swift | 156 ++++++++++++++ 2 files changed, 324 insertions(+), 100 deletions(-) create mode 100644 Tests/XToolTests/SDKBuilderTests.swift diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swift index f9f7b92d..a94feccb 100644 --- a/Sources/XToolSupport/SDKBuilder.swift +++ b/Sources/XToolSupport/SDKBuilder.swift @@ -107,40 +107,7 @@ struct SDKBuilder { try await installToolset(in: output) - let dev = try await installDeveloper(in: output) - - func sdk(platform: String, prefix: String) throws -> String { - let regex = try NSRegularExpression(pattern: #"^\#(prefix)\d+\.\d+\.sdk$"#) - let dir = dev.appendingPathComponent("Platforms/\(platform).platform/Developer/SDKs") - let names = try dir.contents().map(\.lastPathComponent) - guard let name = names.first(where: { - regex.firstMatch(in: $0, range: NSRange($0.startIndex..., in: $0)) != nil - }) else { - throw Console.Error("Could not find SDK for \(platform)/\(prefix)") - } - return name - } - - func triple(platform: String, sdk: String) -> SDKDefinition.Triple { - SDKDefinition.Triple( - sdkRootPath: "Developer/Platforms/\(platform).platform/Developer/SDKs/\(sdk)", - includeSearchPaths: ["Developer/Platforms/\(platform).platform/Developer/usr/lib"], - librarySearchPaths: ["Developer/Platforms/\(platform).platform/Developer/usr/lib"], - swiftResourcesPath: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift", - swiftStaticResourcesPath: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static", - toolsetPaths: ["toolset.json"] - ) - } - - let iPhoneOSSDK = try sdk(platform: "iPhoneOS", prefix: "iPhoneOS") - let iPhoneSimSDK = try sdk(platform: "iPhoneSimulator", prefix: "iPhoneSimulator") - let macOSSDK = try sdk(platform: "MacOSX", prefix: "MacOSX") - - d.out.print(""" - - \(iPhoneOSSDK) - - \(iPhoneSimSDK) - - \(macOSSDK) - """) + let sdkDefinition = try await installDeveloper(in: output) d.out.print("[Writing metadata]") try """ @@ -213,17 +180,6 @@ struct SDKBuilder { encoding: .utf8 ) - let sdkDefinition = SDKDefinition( - schemaVersion: "4.0", - targetTriples: [ - "arm64-apple-ios": triple(platform: "iPhoneOS", sdk: iPhoneOSSDK), - "arm64-apple-ios-simulator": triple(platform: "iPhoneSimulator", sdk: iPhoneSimSDK), - "x86_64-apple-ios-simulator": triple(platform: "iPhoneSimulator", sdk: iPhoneSimSDK), - "arm64-apple-macosx": triple(platform: "MacOSX", sdk: macOSSDK), - "x86_64-apple-macosx": triple(platform: "MacOSX", sdk: macOSSDK), - ] - ) - let encoder = JSONEncoder() try encoder .encode(sdkDefinition) @@ -476,7 +432,7 @@ struct SDKBuilder { 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 { + func installDeveloper(in output: URL) async throws -> SDKDefinition { let dev = output.appendingPathComponent("Developer") let expectedAppDir = output.appendingPathComponent("Xcode.app") @@ -596,77 +552,189 @@ struct SDKBuilder { try FileManager.default.removeItem(at: cleanupStageDir) } - d.out.print("[Finalizing SDKs]") + return try finalizeDeveloper(in: dev) + } - for platform in ["iPhoneOS", "MacOSX", "iPhoneSimulator"] { - let lib = "../../../../../Library" - let platformDir = dev.appending(path: "Platforms/\(platform).platform") + private enum Platform: String, CaseIterable { + case iPhoneOS + case iPhoneSimulator + case macOS = "MacOSX" - try patchPlatformManifest(path: platformDir.appending(path: "Info.plist")) + var triples: [String] { + switch self { + case .iPhoneOS: ["arm64-apple-ios"] + case .iPhoneSimulator: ["arm64-apple-ios-simulator", "x86_64-apple-ios-simulator"] + case .macOS: ["arm64-apple-macosx", "x86_64-apple-macosx"] + } + } - /* - XCTest and Testing.framework are located in *.platform/Developer/{Library/Frameworks,usr/lib} rather than inside - the SDK. These search paths are explicitly included when building tests, which presumably ensures that normal - applications don't accidentally link against them in production. + func directory(in developer: URL) -> URL { + developer.appending(path: "Platforms/\(rawValue).platform") + } + } - SwiftPM makes no such affordances outside of macOS, so we add the usr/lib path as include/library search paths - in the SDK config, and symlink the frameworks into the SDKs (since there's no frameworkSearchPaths option). - While this drops a safeguard it's better than not having the testing libs at all. - */ + private var allowsIOSOnly: Bool { + if case (.app, .buildSlim) = (input, mode) { return true } + return false + } - let fwk = platformDir.appending(path: "Developer/SDKs/\(platform).sdk/System/Library/Frameworks").path + private func findSDK(for platform: Platform, in developer: URL, required: Bool) throws -> URL? { + let dir = platform.directory(in: developer).appending(path: "Developer/SDKs") + let entries: [URL] + do { + entries = try FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil) + } catch let error as CocoaError where !required && + (error.code == .fileReadNoSuchFile || error.code == .fileNoSuchFile) { + return nil + } - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/Testing.framework", - withDestinationPath: "\(lib)/Frameworks/Testing.framework" - ) + let name = platform.rawValue + let regex = try NSRegularExpression(pattern: #"^\#(name)\d+\.\d+\.sdk$"#) + let root = entries.first { entry in + let candidate = entry.lastPathComponent + return regex.firstMatch(in: candidate, range: NSRange(candidate.startIndex..., in: candidate)) != nil + } + if !required && root == nil && !entries.contains(where: { $0.pathExtension == "sdk" }) { + return nil + } + guard let root, root.dirExists else { + throw Console.Error("Could not find a valid SDK for \(name) in '\(dir.path)'") + } + return root + } - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/XCTest.framework", - withDestinationPath: "\(lib)/Frameworks/XCTest.framework" - ) + private func validateStagedSDK(_ root: URL, for platform: Platform) throws { + struct Settings: Decodable { + let canonicalName: String - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/XCUIAutomation.framework", - withDestinationPath: "\(lib)/Frameworks/XCUIAutomation.framework" - ) + enum CodingKeys: String, CodingKey { + case canonicalName = "CanonicalName" + } + } - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/XCTestCore.framework", - withDestinationPath: "\(lib)/PrivateFrameworks/XCTestCore.framework" - ) + let manifest = root.appending(path: "SDKSettings.json") + let expected = platform.rawValue.lowercased() + let versioned = root.deletingPathExtension().lastPathComponent.lowercased() + guard let data = try? Data(contentsOf: manifest), + let settings = try? JSONDecoder().decode(Settings.self, from: data), + [expected, versioned].contains(settings.canonicalName.lowercased()) else { + throw Console.Error("Invalid SDK settings for \(platform.rawValue) at '\(manifest.path)'") + } + } - /* - Apply patches to load OpenAppleMacros as the swift-plugin-server - */ - - let bin = platformDir.appendingPathComponent("Developer/usr/bin") - let pluginServer = bin.appendingPathComponent("swift-plugin-server") - try? FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true) - try? FileManager.default.removeItem(at: pluginServer) - try FileManager.default.createSymbolicLink( - atPath: pluginServer.path, - withDestinationPath: "../../../../../../OpenAppleMacrosServer" - ) + private func finalizeDeveloper(in dev: URL) throws -> SDKDefinition { + d.out.print("[Finalizing SDKs]") + + var roots: [Platform: URL] = [:] + for platform in Platform.allCases { + roots[platform] = try findSDK(for: platform, in: dev, required: platform != .macOS || !allowsIOSOnly) + } - let hostDir = platformDir.appendingPathComponent("Developer/usr/lib/swift/host") - let pluginsDir = hostDir.appendingPathComponent("plugins") + let isIOSOnly = roots[.macOS] == nil + let swiftResources = "Toolchains/XcodeDefault.xctoolchain/usr/lib/swift" + guard dev.appending(path: swiftResources).dirExists else { + throw Console.Error("Could not find Swift resources in XcodeDefault.xctoolchain") + } - try FileManager.default.createDirectory(at: hostDir, withIntermediateDirectories: true) - try? FileManager.default.removeItem(at: pluginsDir) - try FileManager.default.createSymbolicLink( - atPath: pluginsDir.path, - withDestinationPath: "../../../../../../../../OpenAppleMacrosPlugins" + var triples: [String: SDKDefinition.Triple] = [:] + for platform in Platform.allCases { + guard let root = roots[platform] else { continue } + let platformDir = platform.directory(in: dev) + if isIOSOnly { + try validateStagedSDK(root, for: platform) + } + let alias = platformDir.appending(path: "Developer/SDKs/\(platform.rawValue).sdk") + try finalizePlatform(at: platformDir, sdkRoot: isIOSOnly ? root : alias, allowMissingManifest: isIOSOnly) + + let platformPath = "Developer/Platforms/\(platform.rawValue).platform/Developer" + let definition = SDKDefinition.Triple( + sdkRootPath: "\(platformPath)/SDKs/\(root.lastPathComponent)", + includeSearchPaths: ["\(platformPath)/usr/lib"], + librarySearchPaths: ["\(platformPath)/usr/lib"], + swiftResourcesPath: "Developer/\(swiftResources)", + swiftStaticResourcesPath: "Developer/\(swiftResources)_static", + toolsetPaths: ["toolset.json"] ) + for triple in platform.triples { + triples[triple] = definition + } + } + + // Install replaces the Xcode clang headers with those from the host Swift toolchain. + try FileManager.default.removeItem(at: dev.appending(path: "\(swiftResources)/clang/include")) + + for platform in Platform.allCases { + if let root = roots[platform] { + d.out.print("- \(root.lastPathComponent)") + } } + return SDKDefinition(schemaVersion: "4.0", targetTriples: triples) + } - // we copy over the host's clang resorurce dir during install, because intrinsics can differ - // by LLVM version - try FileManager.default.removeItem( - at: dev.appending(path: "Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include") + private func finalizePlatform(at platformDir: URL, sdkRoot: URL, allowMissingManifest: Bool) throws { + // SDK-only archives can omit platform manifests. Patch any that are supplied. + let hasManifest = try !allowMissingManifest || platformDir.contents().contains { + $0.lastPathComponent == "Info.plist" + } + if hasManifest { + try patchPlatformManifest(path: platformDir.appending(path: "Info.plist")) + } + + /* + XCTest and Testing.framework are located in *.platform/Developer/{Library/Frameworks,usr/lib} rather than inside + the SDK. These search paths are explicitly included when building tests, which presumably ensures that normal + applications don't accidentally link against them in production. + + SwiftPM makes no such affordances outside of macOS, so we add the usr/lib path as include/library search paths + in the SDK config, and symlink the frameworks into the SDKs (since there's no frameworkSearchPaths option). + While this drops a safeguard it's better than not having the testing libs at all. + */ + + let lib = "../../../../../Library" + let fwk = sdkRoot.appending(path: "System/Library/Frameworks").path + + try FileManager.default.createSymbolicLink( + atPath: "\(fwk)/Testing.framework", + withDestinationPath: "\(lib)/Frameworks/Testing.framework" + ) + + try FileManager.default.createSymbolicLink( + atPath: "\(fwk)/XCTest.framework", + withDestinationPath: "\(lib)/Frameworks/XCTest.framework" ) - return dev + try FileManager.default.createSymbolicLink( + atPath: "\(fwk)/XCUIAutomation.framework", + withDestinationPath: "\(lib)/Frameworks/XCUIAutomation.framework" + ) + + try FileManager.default.createSymbolicLink( + atPath: "\(fwk)/XCTestCore.framework", + withDestinationPath: "\(lib)/PrivateFrameworks/XCTestCore.framework" + ) + + /* + Apply patches to load OpenAppleMacros as the swift-plugin-server + */ + + let bin = platformDir.appendingPathComponent("Developer/usr/bin") + let pluginServer = bin.appendingPathComponent("swift-plugin-server") + try? FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true) + try? FileManager.default.removeItem(at: pluginServer) + try FileManager.default.createSymbolicLink( + atPath: pluginServer.path, + withDestinationPath: "../../../../../../OpenAppleMacrosServer" + ) + + let hostDir = platformDir.appendingPathComponent("Developer/usr/lib/swift/host") + let pluginsDir = hostDir.appendingPathComponent("plugins") + + try FileManager.default.createDirectory(at: hostDir, withIntermediateDirectories: true) + try? FileManager.default.removeItem(at: pluginsDir) + try FileManager.default.createSymbolicLink( + atPath: pluginsDir.path, + withDestinationPath: "../../../../../../../../OpenAppleMacrosPlugins" + ) } /// Removes unneeded files from a copy of Xcode.app that's owned by xtool. diff --git a/Tests/XToolTests/SDKBuilderTests.swift b/Tests/XToolTests/SDKBuilderTests.swift new file mode 100644 index 00000000..eb167003 --- /dev/null +++ b/Tests/XToolTests/SDKBuilderTests.swift @@ -0,0 +1,156 @@ +import Dependencies +import Foundation +import Testing +import XCLIUtils +import XUtils +@testable import XToolSupport + +struct SDKBuilderTests { + @Test(arguments: [false, true]) + func slimImportAcceptsStagedIOSSdks(withAliases: Bool) async throws { + let fixture = try Fixture() + defer { fixture.remove() } + try fixture.addSDK("iPhoneOS", withAlias: withAliases) + try fixture.addSDK("iPhoneSimulator", withAlias: withAliases) + + let definition = try await fixture.importDeveloper(mode: .buildSlim) + + #expect(Set(definition.targetTriples.keys) == [ + "arm64-apple-ios", "arm64-apple-ios-simulator", "x86_64-apple-ios-simulator", + ]) + #expect(definition.targetTriples["arm64-apple-ios"]?.sdkRootPath == fixture.sdkPath("iPhoneOS")) + #expect(definition.targetTriples["arm64-apple-ios-simulator"]?.sdkRootPath == fixture.sdkPath("iPhoneSimulator")) + #expect(definition.targetTriples["x86_64-apple-ios-simulator"]?.sdkRootPath == fixture.sdkPath("iPhoneSimulator")) + #expect(!FileManager.default.fileExists(atPath: fixture.output.appending(path: fixture.macPlatformPath).path)) + + let frameworks = fixture.output.appending(path: "\(fixture.sdkPath("iPhoneOS"))/System/Library/Frameworks") + let link = frameworks.appending(path: "XCTest.framework") + let destination = try FileManager.default.destinationOfSymbolicLink(atPath: link.path) + #expect(destination == "../../../../../Library/Frameworks/XCTest.framework") + } + + @Test(arguments: [SDKBuilder.Mode.buildSlim, .buildNormal]) + func fullImportKeepsMacOSTriples(mode: SDKBuilder.Mode) async throws { + let fixture = try Fixture() + defer { fixture.remove() } + for platform in ["iPhoneOS", "iPhoneSimulator", "MacOSX"] { + try fixture.addSDK(platform, withAlias: true, withManifest: true) + } + + let definition = try await fixture.importDeveloper(mode: mode) + + #expect(Set(definition.targetTriples.keys) == [ + "arm64-apple-ios", "arm64-apple-ios-simulator", "x86_64-apple-ios-simulator", + "arm64-apple-macosx", "x86_64-apple-macosx", + ]) + for triple in ["arm64-apple-macosx", "x86_64-apple-macosx"] { + #expect(definition.targetTriples[triple]?.sdkRootPath == fixture.sdkPath("MacOSX")) + } + let manifest = fixture.output.appending(path: "\(fixture.macPlatformPath)/Info.plist") + let contents = try String(contentsOf: manifest, encoding: .utf8) + #expect(contents.contains("SWIFT_RESOURCE_DIR")) + } + + @Test func normalImportRequiresMacOSSDK() async throws { + let fixture = try Fixture() + defer { fixture.remove() } + try fixture.addSDK("iPhoneOS", withAlias: true, withManifest: true) + try fixture.addSDK("iPhoneSimulator", withAlias: true, withManifest: true) + + await #expect(throws: CocoaError.self) { + try await fixture.importDeveloper(mode: .buildNormal) + } + } + + @Test(arguments: ["iPhoneOS", "iPhoneSimulator", "XcodeDefault.xctoolchain"]) + func stagedImportRequiresBothIOSSdksAndToolchain(missing: String) async throws { + let fixture = try Fixture() + defer { fixture.remove() } + for platform in ["iPhoneOS", "iPhoneSimulator"] { + try fixture.addSDK(platform) + } + let path = missing == "XcodeDefault.xctoolchain" + ? "Contents/Developer/Toolchains/\(missing)" + : "Contents/\(fixture.sdkPath(missing))" + try FileManager.default.removeItem(at: fixture.app.appending(path: path)) + + await #expect(throws: Console.Error.self) { + try await fixture.importDeveloper(mode: .buildSlim) + } + } + + @Test(arguments: ["not JSON", #"{"CanonicalName":"iphoneos27.2"}"#]) + func stagedImportRejectsInvalidSimulatorSettings(settings: String) async throws { + let fixture = try Fixture() + defer { fixture.remove() } + try fixture.addSDK("iPhoneOS") + try fixture.addSDK("iPhoneSimulator") + let manifest = fixture.app.appending(path: "Contents/\(fixture.sdkPath("iPhoneSimulator"))/SDKSettings.json") + try Data(settings.utf8).write(to: manifest) + + await #expect(throws: Console.Error.self) { + try await fixture.importDeveloper(mode: .buildSlim) + } + } + + private struct Fixture { + let root: URL + var app: URL { root.appending(path: "Xcode.app") } + var output: URL { root.appending(path: "darwin.xtoolsdk") } + var macPlatformPath: String { "Developer/Platforms/MacOSX.platform" } + + init() throws { + root = FileManager.default.temporaryDirectory.appending(path: "SDKBuilderTests-\(UUID().uuidString)") + let toolchain = "Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift" + try FileManager.default.createDirectory( + at: app.appending(path: "\(toolchain)/clang/include"), + withIntermediateDirectories: true + ) + try FileManager.default.createDirectory(at: output, withIntermediateDirectories: false) + for name in ["Info.plist", "version.plist"] { + try Data().write(to: app.appending(path: "Contents/\(name)")) + } + } + + func remove() { + try? FileManager.default.removeItem(at: root) + } + + func sdkPath(_ platform: String) -> String { + "Developer/Platforms/\(platform).platform/Developer/SDKs/\(platform)27.2.sdk" + } + + func addSDK(_ platform: String, withAlias: Bool = false, withManifest: Bool = false) throws { + let sdk = app.appending(path: "Contents/\(sdkPath(platform))") + try FileManager.default.createDirectory( + at: sdk.appending(path: "System/Library/Frameworks"), + withIntermediateDirectories: true + ) + let sdkSettings = #"{"CanonicalName":"\#(platform.lowercased())27.2"}"# + try Data(sdkSettings.utf8).write(to: sdk.appending(path: "SDKSettings.json")) + + let sdks = sdk.deletingLastPathComponent() + if withAlias { + try FileManager.default.createSymbolicLink( + atPath: sdks.appending(path: "\(platform).sdk").path, + withDestinationPath: sdk.lastPathComponent + ) + } + if withManifest { + let manifest = sdks.deletingLastPathComponent().deletingLastPathComponent().appending(path: "Info.plist") + let plist = try PropertyListSerialization.data(fromPropertyList: [String: String](), format: .xml, options: 0) + try plist.write(to: manifest) + } + } + + func importDeveloper(mode: SDKBuilder.Mode) async throws -> SDKDefinition { + let input = try SDKBuilder.Input(path: app.path) + let builder = SDKBuilder(input: input, output: output, arch: .x86_64, mode: mode) + return try await withDependencies { + $0.out.write = { _, _, _ in } + } operation: { + try await builder.installDeveloper(in: output) + } + } + } +} From 3d43984deae4f7a6008757197be5291260b0443b Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sat, 5 Dec 2026 17:18:41 +0000 Subject: [PATCH 2/5] fix(pr4): validate SDKSettings.plist and require essential usr/lib/libSystem.tbd payload --- Sources/XToolSupport/SDKBuilder.swift | 15 +++++++++ Tests/XToolTests/SDKBuilderTests.swift | 42 +++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swift index a94feccb..2504a7bb 100644 --- a/Sources/XToolSupport/SDKBuilder.swift +++ b/Sources/XToolSupport/SDKBuilder.swift @@ -620,6 +620,21 @@ struct SDKBuilder { [expected, versioned].contains(settings.canonicalName.lowercased()) else { throw Console.Error("Invalid SDK settings for \(platform.rawValue) at '\(manifest.path)'") } + + let plistURL = root.appending(path: "SDKSettings.plist") + if FileManager.default.fileExists(atPath: plistURL.path) { + guard let plistData = try? Data(contentsOf: plistURL), + let raw = try? PropertyListSerialization.propertyList(from: plistData, format: nil) as? [String: Any], + let plistName = raw["CanonicalName"] as? String, + [expected, versioned].contains(plistName.lowercased()) else { + throw Console.Error("Invalid SDK settings plist for \(platform.rawValue) at '\(plistURL.path)'") + } + } + + let libSystem = root.appending(path: "usr/lib/libSystem.tbd") + guard FileManager.default.fileExists(atPath: libSystem.path) else { + throw Console.Error("Missing essential SDK payload for \(platform.rawValue) at '\(libSystem.path)'") + } } private func finalizeDeveloper(in dev: URL) throws -> SDKDefinition { diff --git a/Tests/XToolTests/SDKBuilderTests.swift b/Tests/XToolTests/SDKBuilderTests.swift index eb167003..92205755 100644 --- a/Tests/XToolTests/SDKBuilderTests.swift +++ b/Tests/XToolTests/SDKBuilderTests.swift @@ -93,6 +93,34 @@ struct SDKBuilderTests { } } + @Test(arguments: ["iPhoneOS", "iPhoneSimulator"]) + func stagedImportRejectsMalformedSettingsPlist(platform: String) async throws { + let fixture = try Fixture() + defer { fixture.remove() } + try fixture.addSDK("iPhoneOS") + try fixture.addSDK("iPhoneSimulator") + let plist = fixture.app.appending(path: "Contents/\(fixture.sdkPath(platform))/SDKSettings.plist") + try Data("not a valid plist".utf8).write(to: plist) + + await #expect(throws: Console.Error.self) { + try await fixture.importDeveloper(mode: .buildSlim) + } + } + + @Test(arguments: ["iPhoneOS", "iPhoneSimulator"]) + func stagedImportRejectsEmptySDKPayload(platform: String) async throws { + let fixture = try Fixture() + defer { fixture.remove() } + try fixture.addSDK("iPhoneOS") + try fixture.addSDK("iPhoneSimulator") + let libSystem = fixture.app.appending(path: "Contents/\(fixture.sdkPath(platform))/usr/lib/libSystem.tbd") + try FileManager.default.removeItem(at: libSystem) + + await #expect(throws: Console.Error.self) { + try await fixture.importDeveloper(mode: .buildSlim) + } + } + private struct Fixture { let root: URL var app: URL { root.appending(path: "Xcode.app") } @@ -126,8 +154,20 @@ struct SDKBuilderTests { at: sdk.appending(path: "System/Library/Frameworks"), withIntermediateDirectories: true ) - let sdkSettings = #"{"CanonicalName":"\#(platform.lowercased())27.2"}"# + try FileManager.default.createDirectory( + at: sdk.appending(path: "usr/lib"), + withIntermediateDirectories: true + ) + try Data("--- !tapi-tbd\n".utf8).write(to: sdk.appending(path: "usr/lib/libSystem.tbd")) + let canonical = "\(platform.lowercased())27.2" + let sdkSettings = #"{"CanonicalName":"\#(canonical)"}"# try Data(sdkSettings.utf8).write(to: sdk.appending(path: "SDKSettings.json")) + let plistData = try PropertyListSerialization.data( + fromPropertyList: ["CanonicalName": canonical], + format: .xml, + options: 0 + ) + try plistData.write(to: sdk.appending(path: "SDKSettings.plist")) let sdks = sdk.deletingLastPathComponent() if withAlias { From b8fc64f66138dd948b68a3b1d39112c9eba7bab7 Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sat, 5 Dec 2026 17:21:01 +0000 Subject: [PATCH 3/5] fix(pr4): require regular non-empty libSystem.tbd file and remove superfluous SwiftLint disable --- Sources/XToolSupport/SDKBuilder.swift | 10 ++++++++-- Tests/XToolTests/SDKBuilderTests.swift | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swift index 2504a7bb..a44f0e82 100644 --- a/Sources/XToolSupport/SDKBuilder.swift +++ b/Sources/XToolSupport/SDKBuilder.swift @@ -431,7 +431,7 @@ struct SDKBuilder { private static let expectedXcodeChildren = ["Info.plist", "version.plist", "Developer"] - // swiftlint:disable:next cyclomatic_complexity function_body_length + // swiftlint:disable:next cyclomatic_complexity func installDeveloper(in output: URL) async throws -> SDKDefinition { let dev = output.appendingPathComponent("Developer") @@ -632,7 +632,13 @@ struct SDKBuilder { } let libSystem = root.appending(path: "usr/lib/libSystem.tbd") - guard FileManager.default.fileExists(atPath: libSystem.path) else { + let resolvedLibSystem = libSystem.resolvingSymlinksInPath() + var isDir: ObjCBool = false + guard FileManager.default.fileExists(atPath: resolvedLibSystem.path, isDirectory: &isDir), + !isDir.boolValue, + (try? resolvedLibSystem.resourceValues(forKeys: [.isRegularFileKey]))?.isRegularFile == true, + let payload = try? Data(contentsOf: resolvedLibSystem), + !payload.isEmpty else { throw Console.Error("Missing essential SDK payload for \(platform.rawValue) at '\(libSystem.path)'") } } diff --git a/Tests/XToolTests/SDKBuilderTests.swift b/Tests/XToolTests/SDKBuilderTests.swift index 92205755..aa3d1fd0 100644 --- a/Tests/XToolTests/SDKBuilderTests.swift +++ b/Tests/XToolTests/SDKBuilderTests.swift @@ -119,6 +119,12 @@ struct SDKBuilderTests { await #expect(throws: Console.Error.self) { try await fixture.importDeveloper(mode: .buildSlim) } + + // A directory named libSystem.tbd must also be rejected. + try FileManager.default.createDirectory(at: libSystem, withIntermediateDirectories: false) + await #expect(throws: Console.Error.self) { + try await fixture.importDeveloper(mode: .buildSlim) + } } private struct Fixture { From 07f094fca8e5eade5946f7dc6d205f504af69343 Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sat, 5 Dec 2026 17:23:47 +0000 Subject: [PATCH 4/5] fix(pr4): move function_body_length SwiftLint disable from buildSDK to installDeveloper --- Sources/XToolSupport/SDKBuilder.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swift index a44f0e82..144ae6a4 100644 --- a/Sources/XToolSupport/SDKBuilder.swift +++ b/Sources/XToolSupport/SDKBuilder.swift @@ -90,7 +90,6 @@ struct SDKBuilder { """ } - // swiftlint:disable:next function_body_length func buildSDK() async throws { let sdkVersion = Self.currentSDKVersion @@ -431,7 +430,7 @@ struct SDKBuilder { private static let expectedXcodeChildren = ["Info.plist", "version.plist", "Developer"] - // swiftlint:disable:next cyclomatic_complexity + // swiftlint:disable:next cyclomatic_complexity function_body_length func installDeveloper(in output: URL) async throws -> SDKDefinition { let dev = output.appendingPathComponent("Developer") From 19f38150bc959caacbf71411ad5c46972b352170 Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sat, 5 Dec 2026 17:36:30 +0000 Subject: [PATCH 5/5] test(pr4): isolate per-case output directories in SDKBuilderTests and cover zero-byte libSystem.tbd --- Tests/XToolTests/SDKBuilderTests.swift | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Tests/XToolTests/SDKBuilderTests.swift b/Tests/XToolTests/SDKBuilderTests.swift index aa3d1fd0..89672e84 100644 --- a/Tests/XToolTests/SDKBuilderTests.swift +++ b/Tests/XToolTests/SDKBuilderTests.swift @@ -107,21 +107,27 @@ struct SDKBuilderTests { } } - @Test(arguments: ["iPhoneOS", "iPhoneSimulator"]) - func stagedImportRejectsEmptySDKPayload(platform: String) async throws { + enum InvalidPayload: CaseIterable { + case missing, directory, emptyFile + } + + @Test(arguments: ["iPhoneOS", "iPhoneSimulator"], InvalidPayload.allCases) + func stagedImportRejectsInvalidSDKPayload(platform: String, payload: InvalidPayload) async throws { let fixture = try Fixture() defer { fixture.remove() } try fixture.addSDK("iPhoneOS") try fixture.addSDK("iPhoneSimulator") let libSystem = fixture.app.appending(path: "Contents/\(fixture.sdkPath(platform))/usr/lib/libSystem.tbd") try FileManager.default.removeItem(at: libSystem) - - await #expect(throws: Console.Error.self) { - try await fixture.importDeveloper(mode: .buildSlim) + switch payload { + case .missing: + break + case .directory: + try FileManager.default.createDirectory(at: libSystem, withIntermediateDirectories: false) + case .emptyFile: + try Data().write(to: libSystem) } - // A directory named libSystem.tbd must also be rejected. - try FileManager.default.createDirectory(at: libSystem, withIntermediateDirectories: false) await #expect(throws: Console.Error.self) { try await fixture.importDeveloper(mode: .buildSlim) }