13
13
import Basics
14
14
import CoreCommands
15
15
import Foundation
16
+ import PackageGraph
16
17
import PackageModel
17
18
import SPMBuildCore
18
19
@@ -383,14 +384,38 @@ final class PluginDelegate: PluginInvocationDelegate {
383
384
// Create a build system for building the target., skipping the the cache because we need the build plan.
384
385
let buildSystem = try swiftCommandState. createBuildSystem ( explicitBuildSystem: . native, cacheBuildManifest: false )
385
386
386
- // Find the target in the build operation's package graph; it's an error if we don't find it.
387
+ // Find the target in the build operation's package graph
388
+ // 1. First look for a matching destination module
389
+ // 2. If not found, search for a matching tools module, only allowing
390
+ // modules that should be built for the host (macro, plugin, test).
391
+ // 3. Error if no matching targets are found.
392
+ //
393
+ // FIXME: dynamic graph
394
+ // This should work by requesting a build for a target w/ destination:
395
+ // .destination and generating the graph needed on demand instead of
396
+ // looking for a preexisting matching target.
397
+ let target : ResolvedModule
398
+ let destination : BuildParameters . Destination
387
399
let packageGraph = try buildSystem. getPackageGraph ( )
388
- guard let target = packageGraph. target ( for: targetName, destination: . destination) else {
389
- throw StringError ( " could not find a target named “ \( targetName) ” " )
400
+ if let _target = packageGraph. target ( for: targetName, destination: . destination) {
401
+ target = _target
402
+ destination = . target
403
+ } else if let _target = packageGraph. target ( for: targetName, destination: . tools) ,
404
+ ( _target. type == . macro || _target. type == . plugin || _target. type == . test) {
405
+ target = _target
406
+ destination = . host
407
+ } else {
408
+ throw StringError ( " Could not find a target named “ \( targetName) ” " )
390
409
}
391
410
392
411
// Build the target, if needed.
393
- try buildSystem. build ( subset: . target( target. name) )
412
+ try buildSystem. build ( subset: . target( target. name, for: destination) )
413
+
414
+ let buildParameters : BuildParameters = try if destination == . target {
415
+ buildSystem. buildPlan. destinationBuildParameters
416
+ } else {
417
+ buildSystem. buildPlan. toolsBuildParameters
418
+ }
394
419
395
420
// Configure the symbol graph extractor.
396
421
var symbolGraphExtractor = try SymbolGraphExtract (
@@ -419,7 +444,7 @@ final class PluginDelegate: PluginInvocationDelegate {
419
444
guard let package = packageGraph. package ( for: target) else {
420
445
throw StringError ( " could not determine the package for target “ \( target. name) ” " )
421
446
}
422
- let outputDir = try buildSystem . buildPlan . toolsBuildParameters . dataPath. appending (
447
+ let outputDir = buildParameters . dataPath. appending (
423
448
components: " extracted-symbols " ,
424
449
package . identity. description,
425
450
target. name
@@ -430,7 +455,7 @@ final class PluginDelegate: PluginInvocationDelegate {
430
455
let result = try symbolGraphExtractor. extractSymbolGraph (
431
456
module: target,
432
457
buildPlan: try buildSystem. buildPlan,
433
- buildParameters: buildSystem . buildPlan . destinationBuildParameters ,
458
+ buildParameters: buildParameters ,
434
459
outputRedirection: . collect,
435
460
outputDirectory: outputDir,
436
461
verboseOutput: self . swiftCommandState. logLevel <= . info
0 commit comments