@@ -22,31 +22,45 @@ import class Build.BuildPlan
2222import class Build. ClangTargetBuildDescription
2323import class Build. SwiftTargetBuildDescription
2424import struct PackageGraph. ResolvedModule
25+ import struct PackageGraph. ModulesGraph
2526
2627public protocol BuildTarget {
2728 var sources : [ URL ] { get }
2829
30+ /// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
31+ var isPartOfRootPackage : Bool { get }
32+
2933 func compileArguments( for fileURL: URL ) throws -> [ String ]
30- }
34+ }
35+
36+ private struct WrappedClangTargetBuildDescription : BuildTarget {
37+ private let description : ClangTargetBuildDescription
38+ let isPartOfRootPackage : Bool
39+
40+ init ( description: ClangTargetBuildDescription , isPartOfRootPackage: Bool ) {
41+ self . description = description
42+ self . isPartOfRootPackage = isPartOfRootPackage
43+ }
3144
32- extension ClangTargetBuildDescription : BuildTarget {
3345 public var sources : [ URL ] {
34- return ( try ? compilePaths ( ) . map { URL ( fileURLWithPath: $0. source. pathString) } ) ?? [ ]
46+ return ( try ? description . compilePaths ( ) . map { URL ( fileURLWithPath: $0. source. pathString) } ) ?? [ ]
3547 }
3648
3749 public func compileArguments( for fileURL: URL ) throws -> [ String ] {
3850 let filePath = try resolveSymlinks ( try AbsolutePath ( validating: fileURL. path) )
39- let commandLine = try self . emitCommandLine ( for: filePath)
51+ let commandLine = try description . emitCommandLine ( for: filePath)
4052 // First element on the command line is the compiler itself, not an argument.
4153 return Array ( commandLine. dropFirst ( ) )
4254 }
4355}
4456
4557private struct WrappedSwiftTargetBuildDescription : BuildTarget {
4658 private let description : SwiftTargetBuildDescription
59+ let isPartOfRootPackage : Bool
4760
48- init ( description: SwiftTargetBuildDescription ) {
61+ init ( description: SwiftTargetBuildDescription , isPartOfRootPackage : Bool ) {
4962 self . description = description
63+ self . isPartOfRootPackage = isPartOfRootPackage
5064 }
5165
5266 var sources : [ URL ] {
@@ -71,17 +85,27 @@ public struct BuildDescription {
7185 }
7286
7387 // FIXME: should not use `ResolvedTarget` in the public interface
74- public func getBuildTarget( for target: ResolvedModule ) -> BuildTarget ? {
88+ public func getBuildTarget( for target: ResolvedModule , in modulesGraph : ModulesGraph ) -> BuildTarget ? {
7589 if let description = buildPlan. targetMap [ target. id] {
7690 switch description {
7791 case . clang( let description) :
78- return description
92+ return WrappedClangTargetBuildDescription (
93+ description: description,
94+ isPartOfRootPackage: modulesGraph. rootPackages. map ( \. id) . contains ( description. package . id)
95+ )
7996 case . swift( let description) :
80- return WrappedSwiftTargetBuildDescription ( description: description)
97+ return WrappedSwiftTargetBuildDescription (
98+ description: description,
99+ isPartOfRootPackage: modulesGraph. rootPackages. map ( \. id) . contains ( description. package . id)
100+ )
81101 }
82102 } else {
83103 if target. type == . plugin, let package = self . buildPlan. graph. package ( for: target) {
84- return PluginTargetBuildDescription ( target: target, toolsVersion: package . manifest. toolsVersion)
104+ return PluginTargetBuildDescription (
105+ target: target,
106+ toolsVersion: package . manifest. toolsVersion,
107+ isPartOfRootPackage: modulesGraph. rootPackages. map ( \. id) . contains ( package . id)
108+ )
85109 }
86110 return nil
87111 }
0 commit comments