Skip to content

Commit 09b3ed1

Browse files
committed
[Build/PackageGraph] Sink fallback logic for macro/plugin/test products and packages identification into ModulesGraph
This is a temporary fix until we can figure out a proper way to handle situations were all we get is a name of a product or a target. Resolves: rdar://129400066
1 parent ee9d0b2 commit 09b3ed1

File tree

2 files changed

+62
-41
lines changed

2 files changed

+62
-41
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -526,36 +526,22 @@ 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-
var product = graph.product(
529+
let product = graph.product(
530530
for: productName,
531531
destination: destination == .host ? .tools : .destination
532532
)
533533

534-
var buildParameters = if destination == .host {
535-
self.toolsBuildParameters
536-
} else {
537-
self.productsBuildParameters
538-
}
539-
540-
// It's possible to request a build of a macro or a plugin via `swift build`
541-
// which won't have the right destination set because it's impossible to indicate it.
542-
//
543-
// Same happens with `--test-product` - if one of the test targets directly references
544-
// a macro then all if its targets and the product itself become `host`.
545-
if product == nil && destination == .target {
546-
if let toolsProduct = graph.product(for: productName, destination: .tools),
547-
toolsProduct.type == .macro || toolsProduct.type == .plugin || toolsProduct.type == .test
548-
{
549-
product = toolsProduct
550-
buildParameters = self.toolsBuildParameters
551-
}
552-
}
553-
554534
guard let product else {
555535
observabilityScope.emit(error: "no product named '\(productName)'")
556536
throw Diagnostics.fatalError
557537
}
558538

539+
let buildParameters = if product.buildTriple == .tools {
540+
self.toolsBuildParameters
541+
} else {
542+
self.productsBuildParameters
543+
}
544+
559545
// If the product is automatic, we build the main target because automatic products
560546
// do not produce a binary right now.
561547
if product.type == .library(.automatic) {
@@ -570,33 +556,22 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
570556
// FIXME: This is super unfortunate that we might need to load the package graph.
571557
let graph = try getPackageGraph()
572558

573-
var target = graph.target(
559+
let target = graph.target(
574560
for: targetName,
575561
destination: destination == .host ? .tools : .destination
576562
)
577563

578-
var buildParameters = if destination == .host {
579-
self.toolsBuildParameters
580-
} else {
581-
self.productsBuildParameters
582-
}
583-
584-
// It's possible to request a build of a macro or a plugin via `swift build`
585-
// which won't have the right destination because it's impossible to indicate it.
586-
if target == nil && destination == .target {
587-
if let toolsTarget = graph.target(for: targetName, destination: .tools),
588-
toolsTarget.type == .macro || toolsTarget.type == .plugin
589-
{
590-
target = toolsTarget
591-
buildParameters = self.toolsBuildParameters
592-
}
593-
}
594-
595564
guard let target else {
596565
observabilityScope.emit(error: "no target named '\(targetName)'")
597566
throw Diagnostics.fatalError
598567
}
599568

569+
let buildParameters = if target.buildTriple == .tools {
570+
self.toolsBuildParameters
571+
} else {
572+
self.productsBuildParameters
573+
}
574+
600575
return target.getLLBuildTargetName(buildParameters: buildParameters)
601576
}
602577
}

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,57 @@ public struct ModulesGraph {
167167
}
168168

169169
public func product(for name: String, destination: BuildTriple) -> ResolvedProduct? {
170-
self.allProducts.first { $0.name == name && $0.buildTriple == destination }
170+
func findProduct(name: String, destination: BuildTriple) -> ResolvedProduct? {
171+
self.allProducts.first { $0.name == name && $0.buildTriple == destination }
172+
}
173+
174+
if let product = findProduct(name: name, destination: destination) {
175+
return product
176+
}
177+
178+
// FIXME: This is a temporary workaround and needs to be handled by the callers.
179+
180+
// It's possible to request a build of a macro, a plugin, or a test via `swift build`
181+
// which won't have the right destination set because it's impossible to indicate it.
182+
//
183+
// Same happens with `--test-product` - if one of the test targets directly references
184+
// 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+
}
191+
}
192+
193+
return nil
171194
}
172195

173196
public func target(for name: String, destination: BuildTriple) -> ResolvedModule? {
174-
self.allTargets.first { $0.name == name && $0.buildTriple == destination }
197+
func findModule(name: String, destination: BuildTriple) -> ResolvedModule? {
198+
self.allTargets.first { $0.name == name && $0.buildTriple == destination }
199+
}
200+
201+
if let module = findModule(name: name, destination: destination) {
202+
return module
203+
}
204+
205+
// FIXME: This is a temporary workaround and needs to be handled by the callers.
206+
207+
// It's possible to request a build of a macro, a plugin or a test via `swift build`
208+
// which won't have the right destination set because it's impossible to indicate it.
209+
//
210+
// Same happens with `--test-product` - if one of the test targets directly references
211+
// 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+
}
219+
220+
return nil
175221
}
176222

177223
/// All root and root dependency packages provided as input to the graph.

0 commit comments

Comments
 (0)