@@ -526,13 +526,33 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
526
526
// FIXME: This is super unfortunate that we might need to load the package graph.
527
527
let graph = try getPackageGraph ( )
528
528
529
- guard let product = graph. product (
529
+ var product = graph. product (
530
530
for: productName,
531
531
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 {
533
552
observabilityScope. emit ( error: " no product named ' \( productName) ' " )
534
553
throw Diagnostics . fatalError
535
554
}
555
+
536
556
// If the product is automatic, we build the main target because automatic products
537
557
// do not produce a binary right now.
538
558
if product. type == . library( . automatic) {
@@ -542,28 +562,39 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
542
562
)
543
563
return LLBuildManifestBuilder . TargetKind. main. targetName
544
564
}
545
- return try product. getLLBuildTargetName (
546
- buildParameters: destination == . host
547
- ? self . toolsBuildParameters
548
- : self . productsBuildParameters
549
- )
565
+ return try product. getLLBuildTargetName ( buildParameters: buildParameters)
550
566
case . target( let targetName, let destination) :
551
567
// FIXME: This is super unfortunate that we might need to load the package graph.
552
568
let graph = try getPackageGraph ( )
553
569
554
- guard let target = graph. target (
570
+ var target = graph. target (
555
571
for: targetName,
556
572
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 {
558
593
observabilityScope. emit ( error: " no target named ' \( targetName) ' " )
559
594
throw Diagnostics . fatalError
560
595
}
561
596
562
- return target. getLLBuildTargetName (
563
- buildParameters: destination == . host
564
- ? self . toolsBuildParameters
565
- : self . productsBuildParameters
566
- )
597
+ return target. getLLBuildTargetName ( buildParameters: buildParameters)
567
598
}
568
599
}
569
600
0 commit comments