@@ -174,6 +174,17 @@ public enum TargetBuildDescription {
174
174
}
175
175
}
176
176
177
+ /// The resources in this target.
178
+ var resources : [ Resource ] {
179
+ switch self {
180
+ case . swift( let target) :
181
+ return target. resources
182
+ case . clang( let target) :
183
+ // TODO: Clang targets should support generated resources in the future.
184
+ return target. target. underlyingTarget. resources
185
+ }
186
+ }
187
+
177
188
/// Path to the bundle generated for this module (if any).
178
189
var bundlePath : AbsolutePath ? {
179
190
switch self {
@@ -239,7 +250,7 @@ public final class ClangTargetBuildDescription {
239
250
240
251
/// Path to the bundle generated for this module (if any).
241
252
var bundlePath : AbsolutePath ? {
242
- buildParameters. bundlePath ( for : target )
253
+ target . underlyingTarget . bundleName . map ( buildParameters. bundlePath ( named : ) )
243
254
}
244
255
245
256
/// The modulemap file for this target, if any.
@@ -542,16 +553,28 @@ public final class SwiftTargetBuildDescription {
542
553
/// These are the source files derived from plugins.
543
554
private var pluginDerivedSources : Sources
544
555
556
+ /// These are the resource files derived from plugins.
557
+ private var pluginDerivedResources : [ Resource ]
558
+
545
559
/// Path to the bundle generated for this module (if any).
546
560
var bundlePath : AbsolutePath ? {
547
- buildParameters. bundlePath ( for: target)
561
+ if let bundleName = target. underlyingTarget. potentialBundleName, !resources. isEmpty {
562
+ return buildParameters. bundlePath ( named: bundleName)
563
+ } else {
564
+ return nil
565
+ }
548
566
}
549
567
550
568
/// The list of all source files in the target, including the derived ones.
551
569
public var sources : [ AbsolutePath ] {
552
570
target. sources. paths + derivedSources. paths + pluginDerivedSources. paths
553
571
}
554
572
573
+ /// The list of all resource files in the target, including the derived ones.
574
+ public var resources : [ Resource ] {
575
+ target. underlyingTarget. resources + pluginDerivedResources
576
+ }
577
+
555
578
/// The objects in this target.
556
579
public var objects : [ AbsolutePath ] {
557
580
let relativePaths = target. sources. relativePaths + derivedSources. relativePaths + pluginDerivedSources. relativePaths
@@ -633,16 +656,21 @@ public final class SwiftTargetBuildDescription {
633
656
/// The results of running any prebuild commands for this target.
634
657
public let prebuildCommandResults : [ PrebuildCommandResult ]
635
658
659
+ /// ObservabilityScope with which to emit diagnostics
660
+ private let observabilityScope : ObservabilityScope
661
+
636
662
/// Create a new target description with target and build parameters.
637
663
init (
638
664
target: ResolvedTarget ,
639
665
toolsVersion: ToolsVersion ,
666
+ additionalFileRules: [ FileRuleDescription ] = [ ] ,
640
667
buildParameters: BuildParameters ,
641
668
buildToolPluginInvocationResults: [ BuildToolPluginInvocationResult ] = [ ] ,
642
669
prebuildCommandResults: [ PrebuildCommandResult ] = [ ] ,
643
670
isTestTarget: Bool ? = nil ,
644
671
isTestDiscoveryTarget: Bool = false ,
645
- fileSystem: FileSystem
672
+ fileSystem: FileSystem ,
673
+ observabilityScope: ObservabilityScope
646
674
) throws {
647
675
guard target. underlyingTarget is SwiftTarget else {
648
676
throw InternalError ( " underlying target type mismatch \( target) " )
@@ -657,24 +685,60 @@ public final class SwiftTargetBuildDescription {
657
685
self . tempsPath = buildParameters. buildPath. appending ( component: target. c99name + " .build " )
658
686
self . derivedSources = Sources ( paths: [ ] , root: tempsPath. appending ( component: " DerivedSources " ) )
659
687
self . pluginDerivedSources = Sources ( paths: [ ] , root: buildParameters. dataPath)
688
+ self . pluginDerivedResources = [ ]
660
689
self . buildToolPluginInvocationResults = buildToolPluginInvocationResults
661
690
self . prebuildCommandResults = prebuildCommandResults
691
+ self . observabilityScope = observabilityScope
692
+
693
+ func handlePluginDerivedFile( path absPath: AbsolutePath ) {
694
+ // 5.6 handled treated all generated files as sources.
695
+ if toolsVersion <= . v5_6 {
696
+ let relPath = absPath. relative ( to: self . pluginDerivedSources. root)
697
+ self . pluginDerivedSources. relativePaths. append ( relPath)
698
+ return
699
+ }
700
+
701
+ var rule = TargetSourcesBuilder . computeRule ( for: absPath, toolsVersion: toolsVersion, additionalFileRules: additionalFileRules, observabilityScope: observabilityScope)
702
+
703
+ // If we did not find a rule for a generated file, we treat it as to be processed for now. Eventually, we should handle generated files the same as other files and require explicit handling in the manifest for unknown types.
704
+ if rule == . none {
705
+ rule = . processResource( localization: . none)
706
+ }
707
+
708
+ switch rule {
709
+ case . compile:
710
+ if absPath. extension == " swift " {
711
+ let relPath = absPath. relative ( to: self . pluginDerivedSources. root)
712
+ self . pluginDerivedSources. relativePaths. append ( relPath)
713
+ } else {
714
+ observabilityScope. emit ( error: " Generated plugin source files can only use Swift: \( absPath) " )
715
+ }
716
+ case . copy, . processResource:
717
+ if let resource = TargetSourcesBuilder . resource ( for: absPath, with: rule, defaultLocalization: target. defaultLocalization, targetName: target. name, targetPath: target. underlyingTarget. path, observabilityScope: observabilityScope) {
718
+ self . pluginDerivedResources. append ( resource)
719
+ } else {
720
+ // If this is reached, `TargetSourcesBuilder` already emitted a diagnostic, so we can ignore this case here.
721
+ }
722
+ case . header:
723
+ observabilityScope. emit ( error: " Headers generated by plugins are not supported at this time: \( absPath) " )
724
+ case . modulemap:
725
+ observabilityScope. emit ( error: " Module maps generated by plugins are not supported at this time: \( absPath) " )
726
+ case . ignored, . none:
727
+ break
728
+ }
729
+ }
662
730
663
731
// Add any derived source files that were declared for any commands from plugin invocations.
664
732
for command in buildToolPluginInvocationResults. reduce ( [ ] , { $0 + $1. buildCommands } ) {
665
- // TODO: What should we do if we find non-Swift sources here?
666
733
for absPath in command. outputFiles {
667
- let relPath = absPath. relative ( to: self . pluginDerivedSources. root)
668
- self . pluginDerivedSources. relativePaths. append ( relPath)
734
+ handlePluginDerivedFile ( path: absPath)
669
735
}
670
736
}
671
737
672
738
// Add any derived source files that were discovered from output directories of prebuild commands.
673
739
for result in self . prebuildCommandResults {
674
- // TODO: What should we do if we find non-Swift sources here?
675
- for path in result. derivedSourceFiles {
676
- let relPath = path. relative ( to: self . pluginDerivedSources. root)
677
- self . pluginDerivedSources. relativePaths. append ( relPath)
740
+ for path in result. derivedFiles {
741
+ handlePluginDerivedFile ( path: path)
678
742
}
679
743
}
680
744
@@ -1585,7 +1649,8 @@ public class BuildPlan {
1585
1649
toolsVersion: toolsVersion,
1586
1650
buildParameters: buildParameters,
1587
1651
isTestTarget: true ,
1588
- fileSystem: fileSystem
1652
+ fileSystem: fileSystem,
1653
+ observabilityScope: observabilityScope
1589
1654
)
1590
1655
1591
1656
result. append ( ( testProduct, desc) )
@@ -1621,7 +1686,8 @@ public class BuildPlan {
1621
1686
buildParameters: buildParameters,
1622
1687
isTestTarget: true ,
1623
1688
isTestDiscoveryTarget: true ,
1624
- fileSystem: fileSystem
1689
+ fileSystem: fileSystem,
1690
+ observabilityScope: observabilityScope
1625
1691
)
1626
1692
1627
1693
result. append ( ( testProduct, target) )
@@ -1635,28 +1701,11 @@ public class BuildPlan {
1635
1701
return result
1636
1702
}
1637
1703
1638
- @available ( * , deprecated, message: " use observability system instead " )
1639
- public convenience init (
1640
- buildParameters: BuildParameters ,
1641
- graph: PackageGraph ,
1642
- buildToolPluginInvocationResults: [ ResolvedTarget : [ BuildToolPluginInvocationResult ] ] = [ : ] ,
1643
- prebuildCommandResults: [ ResolvedTarget : [ PrebuildCommandResult ] ] = [ : ] ,
1644
- diagnostics: DiagnosticsEngine ,
1645
- fileSystem: FileSystem
1646
- ) throws {
1647
- let observabilitySystem = ObservabilitySystem ( diagnosticEngine: diagnostics)
1648
- try self . init (
1649
- buildParameters: buildParameters,
1650
- graph: graph,
1651
- fileSystem: fileSystem,
1652
- observabilityScope: observabilitySystem. topScope
1653
- )
1654
- }
1655
-
1656
1704
/// Create a build plan with build parameters and a package graph.
1657
1705
public init (
1658
1706
buildParameters: BuildParameters ,
1659
1707
graph: PackageGraph ,
1708
+ additionalFileRules: [ FileRuleDescription ] = [ ] ,
1660
1709
buildToolPluginInvocationResults: [ ResolvedTarget : [ BuildToolPluginInvocationResult ] ] = [ : ] ,
1661
1710
prebuildCommandResults: [ ResolvedTarget : [ PrebuildCommandResult ] ] = [ : ] ,
1662
1711
fileSystem: FileSystem ,
@@ -1700,10 +1749,12 @@ public class BuildPlan {
1700
1749
targetMap [ target] = try . swift( SwiftTargetBuildDescription (
1701
1750
target: target,
1702
1751
toolsVersion: toolsVersion,
1752
+ additionalFileRules: additionalFileRules,
1703
1753
buildParameters: buildParameters,
1704
1754
buildToolPluginInvocationResults: buildToolPluginInvocationResults [ target] ?? [ ] ,
1705
1755
prebuildCommandResults: prebuildCommandResults [ target] ?? [ ] ,
1706
- fileSystem: fileSystem)
1756
+ fileSystem: fileSystem,
1757
+ observabilityScope: observabilityScope)
1707
1758
)
1708
1759
case is ClangTarget :
1709
1760
targetMap [ target] = try . clang( ClangTargetBuildDescription (
@@ -2236,11 +2287,9 @@ private extension Basics.Diagnostic {
2236
2287
}
2237
2288
2238
2289
extension BuildParameters {
2239
- /// Returns a target's bundle path inside the build directory.
2240
- fileprivate func bundlePath( for target: ResolvedTarget ) -> AbsolutePath ? {
2241
- target. underlyingTarget. bundleName
2242
- . map { $0 + triple. nsbundleExtension }
2243
- . map ( buildPath. appending ( component: ) )
2290
+ /// Returns a named bundle's path inside the build directory.
2291
+ fileprivate func bundlePath( named name: String ) -> AbsolutePath {
2292
+ return buildPath. appending ( component: name + triple. nsbundleExtension)
2244
2293
}
2245
2294
}
2246
2295
0 commit comments