From e639196a3c39e5da62319d1beb9bc25d593e5a99 Mon Sep 17 00:00:00 2001 From: Sanket Patel Date: Wed, 2 Dec 2026 12:49:51 +0000 Subject: [PATCH 1/3] xcode: use scheme build configurations --- Sources/XToolSupport/BuildCoordinator.swift | 3 +- Sources/XToolSupport/BuildDescription.swift | 22 ++++++ Sources/XToolSupport/XPIF+Utils.swift | 4 +- Sources/XToolSupport/XToolProject.swift | 11 +++ .../XToolSupport/XcodeBuildDescriber.swift | 7 +- Sources/XcodeProjects/XcodeScheme.swift | 23 ++++++- Tests/XToolTests/XcodeSchemeTests.swift | 68 ++++++++++++++++++- 7 files changed, 128 insertions(+), 10 deletions(-) diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift index f6f43233..52057ae4 100644 --- a/Sources/XToolSupport/BuildCoordinator.swift +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -101,12 +101,11 @@ public actor BuildCoordinator { overrides[setting] = version } + parameters.configurationName = description.configurationName switch description.configuration { case .debug: - parameters.configurationName = "Debug" parameters.action = "build" case .release: - parameters.configurationName = "Release" parameters.action = "install" overrides["DSTROOT"] = xtoolDir.appending(path: ".xtool-tmp/Install/\(description.triple)").path overrides["INSTALL_PATH"] = "/" diff --git a/Sources/XToolSupport/BuildDescription.swift b/Sources/XToolSupport/BuildDescription.swift index e4b2dc04..eac95cce 100644 --- a/Sources/XToolSupport/BuildDescription.swift +++ b/Sources/XToolSupport/BuildDescription.swift @@ -5,6 +5,7 @@ struct BuildDescription { var directory: URL var pif: XPIF.Container var configuration: BuildConfiguration + var configurationName: String var triple: String var hotReloadable = false @@ -12,9 +13,30 @@ struct BuildDescription { var application: XPIF.GUID var buildTargets: [XPIF.GUID] var buildImplicitDependencies: Bool + var launchBuildConfiguration: String? + var profileBuildConfiguration: String? + var archiveBuildConfiguration: String? } var targetSelection: TargetSelection? + + init( + directory: URL, + pif: XPIF.Container, + configuration: BuildConfiguration, + configurationName: String? = nil, + triple: String, + hotReloadable: Bool = false, + targetSelection: TargetSelection? = nil, + ) { + self.directory = directory + self.pif = pif + self.configuration = configuration + self.configurationName = configurationName ?? configuration.swiftBuildValue + self.triple = triple + self.hotReloadable = hotReloadable + self.targetSelection = targetSelection + } } enum BuildConfiguration: String, CaseIterable, Sendable { diff --git a/Sources/XToolSupport/XPIF+Utils.swift b/Sources/XToolSupport/XPIF+Utils.swift index cbf14e2c..36583156 100644 --- a/Sources/XToolSupport/XPIF+Utils.swift +++ b/Sources/XToolSupport/XPIF+Utils.swift @@ -10,10 +10,12 @@ extension XPIF.Container.Workspace { // // So if the user adds a new target, although we can't mutate the build // request, we can add the new target to the umbrella and have it covered. + let projectConfigurations = projects[projectIndex].buildConfigs.map(\.name) + let configurations = projectConfigurations.isEmpty ? ["Debug", "Release"] : projectConfigurations let umbrellaTarget = XPIF.AggregateTarget( id: Self.umbrellaTargetGUID, name: "Umbrella Target", - buildConfigs: ["Debug", "Release"].map { configuration in + buildConfigs: configurations.map { configuration in XPIF.BuildConfig( id: "\(Self.umbrellaTargetGUID):\(configuration)", name: configuration, diff --git a/Sources/XToolSupport/XToolProject.swift b/Sources/XToolSupport/XToolProject.swift index 057fe229..e6b0b192 100644 --- a/Sources/XToolSupport/XToolProject.swift +++ b/Sources/XToolSupport/XToolProject.swift @@ -79,17 +79,28 @@ enum XToolProject { let scheme = scheme ?? base.schema.scheme let pif: XPIF.Container var targetSelection: BuildDescription.TargetSelection? + var configurationName = configuration.swiftBuildValue switch self { case .swiftPM(let swiftPM): guard scheme == nil else { throw Console.Error("Scheme selection requires an Xcode project or workspace.") } pif = try await SwiftPMBuildDescriber(project: swiftPM).describe() case .xcode(let xcode): (pif, targetSelection) = try await XcodeBuildDescriber(project: xcode, scheme: scheme).describe() + if let targetSelection { + let schemeConfiguration = switch configuration { + case .debug: + targetSelection.launchBuildConfiguration + case .release: + targetSelection.archiveBuildConfiguration ?? targetSelection.profileBuildConfiguration + } + if let schemeConfiguration { configurationName = schemeConfiguration } + } } return BuildDescription( directory: base.rootDirectory, pif: pif, configuration: configuration, + configurationName: configurationName, triple: triple, targetSelection: targetSelection, ) diff --git a/Sources/XToolSupport/XcodeBuildDescriber.swift b/Sources/XToolSupport/XcodeBuildDescriber.swift index 4d470c65..898fb35e 100644 --- a/Sources/XToolSupport/XcodeBuildDescriber.swift +++ b/Sources/XToolSupport/XcodeBuildDescriber.swift @@ -54,7 +54,12 @@ struct XcodeBuildDescriber { throw Console.Error("Scheme '\(scheme)' does not build its runnable application for running.") } targetSelection = .init( - application: app.id, buildTargets: targets, buildImplicitDependencies: selection.buildImplicitDependencies + application: app.id, + buildTargets: targets, + buildImplicitDependencies: selection.buildImplicitDependencies, + launchBuildConfiguration: selection.launchBuildConfiguration, + profileBuildConfiguration: selection.profileBuildConfiguration, + archiveBuildConfiguration: selection.archiveBuildConfiguration, ) } return (pif, targetSelection) diff --git a/Sources/XcodeProjects/XcodeScheme.swift b/Sources/XcodeProjects/XcodeScheme.swift index 879af1e8..920be699 100644 --- a/Sources/XcodeProjects/XcodeScheme.swift +++ b/Sources/XcodeProjects/XcodeScheme.swift @@ -22,17 +22,26 @@ public struct XcodeScheme: Sendable { public let buildTargets: [TargetReference] public let runnable: TargetReference? public let buildImplicitDependencies: Bool + public let launchBuildConfiguration: String? + public let profileBuildConfiguration: String? + public let archiveBuildConfiguration: String? public init( name: String, buildTargets: [TargetReference], runnable: TargetReference?, buildImplicitDependencies: Bool = true, + launchBuildConfiguration: String? = nil, + profileBuildConfiguration: String? = nil, + archiveBuildConfiguration: String? = nil, ) { self.name = name self.buildTargets = buildTargets self.runnable = runnable self.buildImplicitDependencies = buildImplicitDependencies + self.launchBuildConfiguration = launchBuildConfiguration + self.profileBuildConfiguration = profileBuildConfiguration + self.archiveBuildConfiguration = archiveBuildConfiguration } /// Finds shared schemes and the current user's schemes in the supplied containers. @@ -65,6 +74,7 @@ public struct XcodeScheme: Sendable { } public init(url: URL, container: URL) throws { + let xml = try String(contentsOf: url, encoding: .utf8) let scheme = try XCScheme(pathString: url.path) name = scheme.name let base = Self.referenceBase(for: container) @@ -87,6 +97,11 @@ public struct XcodeScheme: Sendable { .filter { $0.buildFor.contains(.running) } .map { try reference($0.buildableReference) } buildImplicitDependencies = scheme.buildAction?.buildImplicitDependencies ?? true + // XcodeProj synthesizes missing actions with default configurations. Only use a + // configuration when the scheme actually declares the corresponding action. + launchBuildConfiguration = xml.contains("", with: """ + + + + """).write(to: scheme, atomically: true, encoding: .utf8) + let description = try await describe(scheme: "Secondary", root: root, configuration: .release) + expectNoDifference(description.configurationName, "Staging Release") + } + + @Test func usesSchemeProfileConfigurationForReleaseBuildsWhenArchiveIsMissing() async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try copyProjectsWithCustomConfigurationNames(to: root) + let scheme = root.appending(path: "Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme") + let xml = try String(contentsOf: scheme, encoding: .utf8) + try xml.replacingOccurrences(of: "", with: """ + + + + """).write(to: scheme, atomically: true, encoding: .utf8) + let description = try await describe(scheme: "Secondary", root: root, configuration: .release) + expectNoDifference(description.configurationName, "Staging Release") + } + @Test func rejectsUnknownScheme() async throws { await #expect(throws: XcodeScheme.SchemeError.self) { try await describe(scheme: "Missing") } } @@ -198,6 +241,7 @@ struct XcodeSchemeTests { expectNoDifference(productGUID, selection.application.value) let request = await coordinator.buildRequest expectNoDifference(request.configuredTargets.map(\.guid), selection.buildTargets.map(\.value)) + expectNoDifference(request.parameters.configurationName, "Release") #expect(!request.useImplicitDependencies) let details = try await coordinator.mainProductDetails() expectNoDifference(details.productName, "OtherApp") @@ -209,6 +253,23 @@ struct XcodeSchemeTests { } #endif + private func copyProjectsWithCustomConfigurationNames(to root: URL) throws { + try FileManager.default.copyItem(at: projects(), to: root) + for project in ["App", "OtherApp", "Features"] { + let url = root.appending(path: "\(project).xcodeproj/project.xcproj") + let contents = try String(contentsOf: url, encoding: .utf8) + try contents + .replacingOccurrences(of: "\"name\": \"Debug\"", with: "\"name\": \"Staging Debug\"") + .replacingOccurrences(of: "\"name\": \"Release\"", with: "\"name\": \"Staging Release\"") + .replacingOccurrences(of: "\"default-configuration\": \"Debug\"", with: "\"default-configuration\": \"Staging Debug\"") + .write(to: url, atomically: true, encoding: .utf8) + } + let scheme = root.appending(path: "Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme") + let xml = try String(contentsOf: scheme, encoding: .utf8) + try xml.replacingOccurrences(of: "buildConfiguration=\"Release\"", with: "buildConfiguration=\"Staging Debug\"") + .write(to: scheme, atomically: true, encoding: .utf8) + } + private func projects() -> URL { fixtureURL("Projects") } @@ -216,9 +277,10 @@ struct XcodeSchemeTests { private let triple = "arm64-apple-ios-simulator" private func describe( - scheme: String?, container: String = "Schemes.xcworkspace", root: URL? = nil + scheme: String?, container: String = "Schemes.xcworkspace", root: URL? = nil, + configuration: BuildConfiguration = .debug ) async throws -> BuildDescription { try await XToolProject.testXcode((root ?? projects()).appending(path: container)) - .describe(configuration: .debug, triple: triple, scheme: scheme) + .describe(configuration: configuration, triple: triple, scheme: scheme) } } From 6978018f2feaedf498889ae4abfdb9c157fcac74 Mon Sep 17 00:00:00 2001 From: Sanket Patel Date: Wed, 2 Dec 2026 12:50:04 +0000 Subject: [PATCH 2/3] xcode: reject missing build configurations --- Sources/XToolSupport/XToolProject.swift | 35 +++++++++++++++++++++++++ Tests/XToolTests/XcodeSchemeTests.swift | 20 ++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/Sources/XToolSupport/XToolProject.swift b/Sources/XToolSupport/XToolProject.swift index e6b0b192..bc7100d7 100644 --- a/Sources/XToolSupport/XToolProject.swift +++ b/Sources/XToolSupport/XToolProject.swift @@ -95,6 +95,7 @@ enum XToolProject { } if let schemeConfiguration { configurationName = schemeConfiguration } } + try validateConfigurationName(configurationName, in: pif, targetSelection: targetSelection) } return BuildDescription( directory: base.rootDirectory, @@ -107,6 +108,40 @@ enum XToolProject { } } +private func validateConfigurationName( + _ name: String, + in pif: XPIF.Container, + targetSelection: BuildDescription.TargetSelection? +) throws { + var targets: [(project: XPIF.Project, target: XPIF.BaseTarget)] = [] + if let targetSelection { + let selected = Set(targetSelection.buildTargets + [targetSelection.application]) + targets = pif.workspace.projects.filter { !$0.isPackage }.flatMap { project in + project.targets.filter { selected.contains($0.id) }.map { (project: project, target: $0) } + } + } else { + let apps = pif.workspace.projects.filter { !$0.isPackage }.flatMap { project in + project.targets.compactMap { target -> (project: XPIF.Project, target: XPIF.BaseTarget)? in + guard case .target(let native) = target, native.productType == .application else { return nil } + return (project: project, target: target) + } + } + guard apps.count == 1 else { return } + targets = apps + } + + for (project, target) in targets { + if case .target(let native) = target, native.isPackageTarget { continue } + let configurations = target.common.buildConfigs.map(\.name) + guard configurations.contains(name) else { + throw Console.Error(""" + Build configuration '\(name)' does not exist for Xcode target '\(target.common.name)' in project '\(project.name)'. + Available configurations: \(configurations.joined(separator: ", ")) + """) + } + } +} + extension XToolProject.Base { fileprivate func project() async throws -> XToolProject { if let path = schema.project { diff --git a/Tests/XToolTests/XcodeSchemeTests.swift b/Tests/XToolTests/XcodeSchemeTests.swift index 9e7f1c7b..b345dc2c 100644 --- a/Tests/XToolTests/XcodeSchemeTests.swift +++ b/Tests/XToolTests/XcodeSchemeTests.swift @@ -120,6 +120,26 @@ struct XcodeSchemeTests { expectNoDifference(description.configurationName, "Staging Release") } + @Test func rejectsSchemeConfigurationMissingFromProject() async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try copyProjectsWithCustomConfigurationNames(to: root) + let scheme = root.appending(path: "Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme") + let xml = try String(contentsOf: scheme, encoding: .utf8) + try xml.replacingOccurrences(of: "Staging Debug", with: "Missing Debug") + .write(to: scheme, atomically: true, encoding: .utf8) + await #expect(throws: Console.Error.self) { try await describe(scheme: "Secondary", root: root) } + } + + @Test func rejectsDefaultConfigurationMissingFromProject() async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try copyProjectsWithCustomConfigurationNames(to: root) + await #expect(throws: Console.Error.self) { + try await describe(scheme: nil, container: "App.xcodeproj", root: root) + } + } + @Test func rejectsUnknownScheme() async throws { await #expect(throws: XcodeScheme.SchemeError.self) { try await describe(scheme: "Missing") } } From 29db6454ac9ffa53f892d8f713a487dea925dcaa Mon Sep 17 00:00:00 2001 From: Sanket Patel Date: Wed, 2 Dec 2026 12:53:33 +0000 Subject: [PATCH 3/3] xcode: mirror project configurations in umbrella target --- Sources/XToolSupport/XPIF+Utils.swift | 4 +--- Sources/XToolSupport/XcodeBuildDescriber.swift | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/XToolSupport/XPIF+Utils.swift b/Sources/XToolSupport/XPIF+Utils.swift index 36583156..9d9eb572 100644 --- a/Sources/XToolSupport/XPIF+Utils.swift +++ b/Sources/XToolSupport/XPIF+Utils.swift @@ -3,15 +3,13 @@ import XPIF extension XPIF.Container.Workspace { static let umbrellaTargetGUID: XPIF.GUID = "xtool:UMBRELLA" - mutating func addUmbrellaTarget(toProjectAt projectIndex: Int) throws { + mutating func addUmbrellaTarget(toProjectAt projectIndex: Int, configurations: [String] = ["Debug", "Release"]) throws { // SwiftBuild's BSP impl allows for mutable PIF but requires an immutable // SWBBuildRequest. So we construct an SWBBuildRequest that points to this // umbrella target. // // So if the user adds a new target, although we can't mutate the build // request, we can add the new target to the umbrella and have it covered. - let projectConfigurations = projects[projectIndex].buildConfigs.map(\.name) - let configurations = projectConfigurations.isEmpty ? ["Debug", "Release"] : projectConfigurations let umbrellaTarget = XPIF.AggregateTarget( id: Self.umbrellaTargetGUID, name: "Umbrella Target", diff --git a/Sources/XToolSupport/XcodeBuildDescriber.swift b/Sources/XToolSupport/XcodeBuildDescriber.swift index 898fb35e..c75afd46 100644 --- a/Sources/XToolSupport/XcodeBuildDescriber.swift +++ b/Sources/XToolSupport/XcodeBuildDescriber.swift @@ -72,7 +72,10 @@ struct XcodeBuildDescriber { #endif let projectIndex = try pif.workspace.projects.indices.first .orThrow(Console.Error("Xcode workspace contains no projects.")) - try pif.workspace.addUmbrellaTarget(toProjectAt: projectIndex) + try pif.workspace.addUmbrellaTarget( + toProjectAt: projectIndex, + configurations: pif.workspace.projects[projectIndex].buildConfigs.map(\.name), + ) return pif } }