Skip to content

Commit 09df7e5

Browse files
committed
[Build] Make sure swift build --target/--product works for macros and plugins
`Buildset` provided by `SwiftBuildCommand` cannot have right destination because it doesn't know what kind of product/target it's attempting to build so we need to fix that up in `computeLLBuildTargetName` until there is a way to properly express that via command line options.
1 parent d561153 commit 09df7e5

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,33 @@ 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-
guard let product = graph.product(
529+
var product = graph.product(
530530
for: productName,
531531
destination: destination == .host ? .tools : .destination
532-
) else {
532+
)
533+
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+
if product == nil && destination == .target {
543+
if let toolsProduct = graph.product(for: productName, destination: .tools),
544+
toolsProduct.type == .macro || toolsProduct.type == .plugin
545+
{
546+
product = toolsProduct
547+
buildParameters = self.toolsBuildParameters
548+
}
549+
}
550+
551+
guard let product else {
533552
observabilityScope.emit(error: "no product named '\(productName)'")
534553
throw Diagnostics.fatalError
535554
}
555+
536556
// If the product is automatic, we build the main target because automatic products
537557
// do not produce a binary right now.
538558
if product.type == .library(.automatic) {
@@ -542,28 +562,39 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
542562
)
543563
return LLBuildManifestBuilder.TargetKind.main.targetName
544564
}
545-
return try product.getLLBuildTargetName(
546-
buildParameters: destination == .host
547-
? self.toolsBuildParameters
548-
: self.productsBuildParameters
549-
)
565+
return try product.getLLBuildTargetName(buildParameters: buildParameters)
550566
case .target(let targetName, let destination):
551567
// FIXME: This is super unfortunate that we might need to load the package graph.
552568
let graph = try getPackageGraph()
553569

554-
guard let target = graph.target(
570+
var target = graph.target(
555571
for: targetName,
556572
destination: destination == .host ? .tools : .destination
557-
) else {
573+
)
574+
575+
var buildParameters = if destination == .host {
576+
self.toolsBuildParameters
577+
} else {
578+
self.productsBuildParameters
579+
}
580+
581+
// It's possible to request a build of a macro or a plugin via `swift build`
582+
// which won't have the right destination because it's impossible to indicate it.
583+
if target == nil && destination == .target {
584+
if let toolsTarget = graph.target(for: targetName, destination: .tools),
585+
toolsTarget.type == .macro || toolsTarget.type == .plugin
586+
{
587+
target = toolsTarget
588+
buildParameters = self.toolsBuildParameters
589+
}
590+
}
591+
592+
guard let target else {
558593
observabilityScope.emit(error: "no target named '\(targetName)'")
559594
throw Diagnostics.fatalError
560595
}
561596

562-
return target.getLLBuildTargetName(
563-
buildParameters: destination == .host
564-
? self.toolsBuildParameters
565-
: self.productsBuildParameters
566-
)
597+
return target.getLLBuildTargetName(buildParameters: buildParameters)
567598
}
568599
}
569600

0 commit comments

Comments
 (0)