1
1
/*
2
2
This source file is part of the Swift.org open source project
3
3
4
- Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4
+ Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
5
5
Licensed under Apache License v2.0 with Runtime Library Exception
6
6
7
7
See http://swift.org/LICENSE.txt for license information
@@ -79,6 +79,18 @@ public struct PackageGraph {
79
79
return rootPackages. contains ( package )
80
80
}
81
81
82
+ /// Returns the package that contains the target, or nil if the target isn't in the graph.
83
+ public func package ( for target: ResolvedTarget ) -> ResolvedPackage ? {
84
+ return self . targetsToPackages [ target]
85
+ }
86
+ private let targetsToPackages : [ ResolvedTarget : ResolvedPackage ]
87
+
88
+ /// Returns the package that contains the product, or nil if the product isn't in the graph.
89
+ public func package ( for product: ResolvedProduct ) -> ResolvedPackage ? {
90
+ return self . productsToPackages [ product]
91
+ }
92
+ private let productsToPackages : [ ResolvedProduct : ResolvedPackage ]
93
+
82
94
/// All root and root dependency packages provided as input to the graph.
83
95
public let inputPackages : [ ResolvedPackage ]
84
96
@@ -92,6 +104,13 @@ public struct PackageGraph {
92
104
self . requiredDependencies = requiredDependencies
93
105
self . inputPackages = rootPackages + rootDependencies
94
106
self . packages = try topologicalSort ( inputPackages, successors: { $0. dependencies } )
107
+
108
+ // Create a mapping from targets to the packages that define them. Here
109
+ // we include all targets, including tests in non-root packages, since
110
+ // this is intended for lookup and not traversal.
111
+ self . targetsToPackages = packages. reduce ( into: [ : ] , { partial, package in
112
+ package . targets. forEach { partial [ $0] = package }
113
+ } )
95
114
96
115
allTargets = Set ( packages. flatMap ( { package -> [ ResolvedTarget ] in
97
116
if rootPackages. contains ( package ) {
@@ -103,6 +122,13 @@ public struct PackageGraph {
103
122
}
104
123
} ) )
105
124
125
+ // Create a mapping from products to the packages that define them. Here
126
+ // we include all products, including tests in non-root packages, since
127
+ // this is intended for lookup and not traversal.
128
+ self . productsToPackages = packages. reduce ( into: [ : ] , { partial, package in
129
+ package . products. forEach { partial [ $0] = package }
130
+ } )
131
+
106
132
allProducts = Set ( packages. flatMap ( { package -> [ ResolvedProduct ] in
107
133
if rootPackages. contains ( package ) {
108
134
return package . products
0 commit comments