@@ -22,31 +22,45 @@ import class Build.BuildPlan
22
22
import class Build. ClangTargetBuildDescription
23
23
import class Build. SwiftTargetBuildDescription
24
24
import struct PackageGraph. ResolvedTarget
25
+ import struct PackageGraph. ModulesGraph
25
26
26
27
public protocol BuildTarget {
27
28
var sources : [ URL ] { get }
28
29
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
+
29
33
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
+ }
31
44
32
- extension ClangTargetBuildDescription : BuildTarget {
33
45
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) } ) ?? [ ]
35
47
}
36
48
37
49
public func compileArguments( for fileURL: URL ) throws -> [ String ] {
38
50
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)
40
52
// First element on the command line is the compiler itself, not an argument.
41
53
return Array ( commandLine. dropFirst ( ) )
42
54
}
43
55
}
44
56
45
57
private struct WrappedSwiftTargetBuildDescription : BuildTarget {
46
58
private let description : SwiftTargetBuildDescription
59
+ let isPartOfRootPackage : Bool
47
60
48
- init ( description: SwiftTargetBuildDescription ) {
61
+ init ( description: SwiftTargetBuildDescription , isPartOfRootPackage : Bool ) {
49
62
self . description = description
63
+ self . isPartOfRootPackage = isPartOfRootPackage
50
64
}
51
65
52
66
var sources : [ URL ] {
@@ -71,17 +85,27 @@ public struct BuildDescription {
71
85
}
72
86
73
87
// FIXME: should not use `ResolvedTarget` in the public interface
74
- public func getBuildTarget( for target: ResolvedTarget ) -> BuildTarget ? {
88
+ public func getBuildTarget( for target: ResolvedTarget , in modulesGraph : ModulesGraph ) -> BuildTarget ? {
75
89
if let description = buildPlan. targetMap [ target. id] {
76
90
switch description {
77
91
case . clang( let description) :
78
- return description
92
+ return WrappedClangTargetBuildDescription (
93
+ description: description,
94
+ isPartOfRootPackage: modulesGraph. rootPackages. map ( \. id) . contains ( description. package . id)
95
+ )
79
96
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
+ )
81
101
}
82
102
} else {
83
103
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
+ )
85
109
}
86
110
return nil
87
111
}
0 commit comments