Skip to content

Commit 49ce2e5

Browse files
committed
[Build/PackageGraph] Switch BuildSubset.{product, target} and ModulesGraph.{product, target}(...) to use optional destination
This is mostly an NFC change. It makes more sense to default `destination` to "undefined" when applicable because it's not always possible to know intended destination based on user input.
1 parent 160018b commit 49ce2e5

File tree

6 files changed

+68
-38
lines changed

6 files changed

+68
-38
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,15 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
526526
// FIXME: This is super unfortunate that we might need to load the package graph.
527527
let graph = try getPackageGraph()
528528

529+
let buildTriple: BuildTriple? = if let destination {
530+
destination == .host ? .tools : .destination
531+
} else {
532+
nil
533+
}
534+
529535
let product = graph.product(
530536
for: productName,
531-
destination: destination == .host ? .tools : .destination
537+
destination: buildTriple
532538
)
533539

534540
guard let product else {
@@ -556,9 +562,15 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
556562
// FIXME: This is super unfortunate that we might need to load the package graph.
557563
let graph = try getPackageGraph()
558564

565+
let buildTriple: BuildTriple? = if let destination {
566+
destination == .host ? .tools : .destination
567+
} else {
568+
nil
569+
}
570+
559571
let target = graph.target(
560572
for: targetName,
561-
destination: destination == .host ? .tools : .destination
573+
destination: buildTriple
562574
)
563575

564576
guard let target else {
@@ -669,7 +681,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
669681
// Emit warnings about any unhandled files in authored packages. We do this after applying build tool plugins, once we know what files they handled.
670682
// rdar://113256834 This fix works for the plugins that do not have PreBuildCommands.
671683
let targetsToConsider: [ResolvedModule]
672-
if let subset = subset, let recursiveDependencies = try
684+
if let subset = subset, let recursiveDependencies = try
673685
subset.recursiveDependencies(for: graph, observabilityScope: observabilityScope) {
674686
targetsToConsider = recursiveDependencies
675687
} else {
@@ -939,18 +951,30 @@ extension BuildSubset {
939951
case .allExcludingTests:
940952
return graph.reachableTargets.filter { $0.type != .test }
941953
case .product(let productName, let destination):
954+
let buildTriple: BuildTriple? = if let destination {
955+
destination == .host ? .tools : .destination
956+
} else {
957+
nil
958+
}
959+
942960
guard let product = graph.product(
943961
for: productName,
944-
destination: destination == .host ? .tools : .destination
962+
destination: buildTriple
945963
) else {
946964
observabilityScope.emit(error: "no product named '\(productName)'")
947965
return nil
948966
}
949967
return try product.recursiveTargetDependencies()
950968
case .target(let targetName, let destination):
969+
let buildTriple: BuildTriple? = if let destination {
970+
destination == .host ? .tools : .destination
971+
} else {
972+
nil
973+
}
974+
951975
guard let target = graph.target(
952976
for: targetName,
953-
destination: destination == .host ? .tools : .destination
977+
destination: buildTriple
954978
) else {
955979
observabilityScope.emit(error: "no target named '\(targetName)'")
956980
return nil

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ final class PluginDelegate: PluginInvocationDelegate {
385385

386386
// Find the target in the build operation's package graph; it's an error if we don't find it.
387387
let packageGraph = try buildSystem.getPackageGraph()
388-
guard let target = packageGraph.target(for: targetName, destination: .destination) else {
388+
guard let target = packageGraph.target(for: targetName) else {
389389
throw StringError("could not find a target named “\(targetName)")
390390
}
391391

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,55 +166,59 @@ public struct ModulesGraph {
166166
package.dependencies.compactMap { self.package(for: $0) }
167167
}
168168

169-
public func product(for name: String, destination: BuildTriple) -> ResolvedProduct? {
169+
/// Find a product given a name and an optional destination. If a destination is not specified
170+
/// this method uses `.destination` and falls back to `.tools` for macros, plugins, and tests.
171+
public func product(for name: String, destination: BuildTriple? = .none) -> ResolvedProduct? {
170172
func findProduct(name: String, destination: BuildTriple) -> ResolvedProduct? {
171173
self.allProducts.first { $0.name == name && $0.buildTriple == destination }
172174
}
173175

174-
if let product = findProduct(name: name, destination: destination) {
175-
return product
176+
if let destination {
177+
return findProduct(name: name, destination: destination)
176178
}
177179

178-
// FIXME: This is a temporary workaround and needs to be handled by the callers.
180+
if let product = findProduct(name: name, destination: .destination) {
181+
return product
182+
}
179183

180184
// It's possible to request a build of a macro, a plugin, or a test via `swift build`
181185
// which won't have the right destination set because it's impossible to indicate it.
182186
//
183187
// Same happens with `--test-product` - if one of the test targets directly references
184188
// a macro then all if its targets and the product itself become `host`.
185-
if destination == .destination {
186-
if let toolsProduct = findProduct(name: name, destination: .tools),
187-
toolsProduct.type == .macro || toolsProduct.type == .plugin || toolsProduct.type == .test
188-
{
189-
return toolsProduct
190-
}
189+
if let toolsProduct = findProduct(name: name, destination: .tools),
190+
toolsProduct.type == .macro || toolsProduct.type == .plugin || toolsProduct.type == .test
191+
{
192+
return toolsProduct
191193
}
192194

193195
return nil
194196
}
195197

196-
public func target(for name: String, destination: BuildTriple) -> ResolvedModule? {
198+
/// Find a target given a name and an optional destination. If a destination is not specified
199+
/// this method uses `.destination` and falls back to `.tools` for macros, plugins, and tests.
200+
public func target(for name: String, destination: BuildTriple? = .none) -> ResolvedModule? {
197201
func findModule(name: String, destination: BuildTriple) -> ResolvedModule? {
198202
self.allTargets.first { $0.name == name && $0.buildTriple == destination }
199203
}
200204

201-
if let module = findModule(name: name, destination: destination) {
202-
return module
205+
if let destination {
206+
return findModule(name: name, destination: destination)
203207
}
204208

205-
// FIXME: This is a temporary workaround and needs to be handled by the callers.
209+
if let module = findModule(name: name, destination: .destination) {
210+
return module
211+
}
206212

207213
// It's possible to request a build of a macro, a plugin or a test via `swift build`
208214
// which won't have the right destination set because it's impossible to indicate it.
209215
//
210216
// Same happens with `--test-product` - if one of the test targets directly references
211217
// a macro then all if its targets and the product itself become `host`.
212-
if destination == .destination {
213-
if let toolsModule = findModule(name: name, destination: .tools),
214-
toolsModule.type == .macro || toolsModule.type == .plugin || toolsModule.type == .test
215-
{
216-
return toolsModule
217-
}
218+
if let toolsModule = findModule(name: name, destination: .tools),
219+
toolsModule.type == .macro || toolsModule.type == .plugin || toolsModule.type == .test
220+
{
221+
return toolsModule
218222
}
219223

220224
return nil

Sources/SPMBuildCore/BuildSystem/BuildSystem.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ public enum BuildSubset {
2424
/// Represents the subset of all products and targets.
2525
case allIncludingTests
2626

27-
/// Represents a specific product.
28-
case product(String, for: BuildParameters.Destination = .target)
27+
/// Represents a specific product. Allows to set a specific
28+
/// destination if it's known.
29+
case product(String, for: BuildParameters.Destination? = .none)
2930

30-
/// Represents a specific target.
31-
case target(String, for: BuildParameters.Destination = .target)
31+
/// Represents a specific target. Allows to set a specific
32+
/// destination if it's known.
33+
case target(String, for: BuildParameters.Destination? = .none)
3234
}
3335

3436
/// A protocol that represents a build system used by SwiftPM for all build operations. This allows factoring out the

Sources/SPMTestSupport/PackageGraphTester.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public final class PackageGraphResult {
8888

8989
public func checkTarget(
9090
_ name: String,
91-
destination: BuildTriple = .destination,
91+
destination: BuildTriple? = .none,
9292
file: StaticString = #file,
9393
line: UInt = #line,
9494
body: (ResolvedTargetResult) -> Void
@@ -113,7 +113,7 @@ public final class PackageGraphResult {
113113

114114
public func checkProduct(
115115
_ name: String,
116-
destination: BuildTriple = .destination,
116+
destination: BuildTriple? = .none,
117117
file: StaticString = #file,
118118
line: UInt = #line,
119119
body: (ResolvedProductResult) -> Void

Tests/PackageGraphTests/CrossCompilationPackageGraphTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
8383
}
8484
}
8585

86-
result.checkProduct("MMIOMacros", destination: .destination) { result in
86+
result.checkProduct("MMIOMacros") { result in
8787
result.check(buildTriple: .tools)
8888
}
8989

@@ -152,11 +152,11 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
152152
}
153153
}
154154

155-
result.checkTarget("MMIOMacros", destination: .destination) { result in
155+
result.checkTarget("MMIOMacros") { result in
156156
result.check(buildTriple: .tools)
157157
}
158158

159-
result.checkTarget("MMIOMacrosTests", destination: .destination) { result in
159+
result.checkTarget("MMIOMacrosTests") { result in
160160
result.check(buildTriple: .tools)
161161
}
162162

@@ -190,15 +190,15 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
190190
result.check(buildTriple: .tools)
191191
}
192192

193-
result.checkProduct("MMIOPlugin", destination: .destination) { result in
193+
result.checkProduct("MMIOPlugin") { result in
194194
result.check(buildTriple: .tools)
195195
}
196196

197197
result.checkTarget("MMIOPlugin", destination: .tools) { result in
198198
result.check(buildTriple: .tools)
199199
}
200200

201-
result.checkTarget("MMIOPlugin", destination: .destination) { result in
201+
result.checkTarget("MMIOPlugin") { result in
202202
result.check(buildTriple: .tools)
203203
}
204204

@@ -207,7 +207,7 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
207207
result.check(dependencies: "MMIOPlugin", "MMIOMacros")
208208
}
209209

210-
result.checkTarget("MMIOMacro+PluginTests", destination: .destination) { result in
210+
result.checkTarget("MMIOMacro+PluginTests") { result in
211211
result.check(buildTriple: .tools)
212212
result.check(dependencies: "MMIOPlugin", "MMIOMacros")
213213
}

0 commit comments

Comments
 (0)