|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import TSCBasic |
| 12 | +import PackageModel |
| 13 | + |
| 14 | +/// Represents a fully resolved target. All the dependencies for the target are resolved. |
| 15 | +public final class ResolvedTarget: ObjectIdentifierProtocol { |
| 16 | + |
| 17 | + /// Represents dependency of a resolved target. |
| 18 | + public enum Dependency { |
| 19 | + /// Direct dependency of the target. This target is in the same package and should be statically linked. |
| 20 | + case target(_ target: ResolvedTarget, conditions: [PackageConditionProtocol]) |
| 21 | + |
| 22 | + /// The target depends on this product. |
| 23 | + case product(_ product: ResolvedProduct, conditions: [PackageConditionProtocol]) |
| 24 | + |
| 25 | + public var target: ResolvedTarget? { |
| 26 | + switch self { |
| 27 | + case .target(let target, _): return target |
| 28 | + case .product: return nil |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public var product: ResolvedProduct? { |
| 33 | + switch self { |
| 34 | + case .target: return nil |
| 35 | + case .product(let product, _): return product |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public var conditions: [PackageConditionProtocol] { |
| 40 | + switch self { |
| 41 | + case .target(_, let conditions): return conditions |
| 42 | + case .product(_, let conditions): return conditions |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Returns the direct dependencies of the underlying dependency, accross the package graph. |
| 47 | + public var dependencies: [ResolvedTarget.Dependency] { |
| 48 | + switch self { |
| 49 | + case .target(let target, _): |
| 50 | + return target.dependencies |
| 51 | + case .product(let product, _): |
| 52 | + return product.targets.map { .target($0, conditions: []) } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Returns the direct dependencies of the underlying dependency, limited to the target's package. |
| 57 | + public var packageDependencies: [ResolvedTarget.Dependency] { |
| 58 | + switch self { |
| 59 | + case .target(let target, _): |
| 60 | + return target.dependencies |
| 61 | + case .product: |
| 62 | + return [] |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public func satisfies(_ environment: BuildEnvironment) -> Bool { |
| 67 | + conditions.allSatisfy { $0.satisfies(environment) } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// The underlying target represented in this resolved target. |
| 72 | + public let underlyingTarget: Target |
| 73 | + |
| 74 | + /// The name of this target. |
| 75 | + public var name: String { |
| 76 | + return underlyingTarget.name |
| 77 | + } |
| 78 | + |
| 79 | + /// The dependencies of this target. |
| 80 | + public let dependencies: [Dependency] |
| 81 | + |
| 82 | + /// Returns dependencies which satisfy the input build environment, based on their conditions. |
| 83 | + /// - Parameters: |
| 84 | + /// - environment: The build environment to use to filter dependencies on. |
| 85 | + public func dependencies(satisfying environment: BuildEnvironment) -> [Dependency] { |
| 86 | + return dependencies.filter { $0.satisfies(environment) } |
| 87 | + } |
| 88 | + |
| 89 | + /// Returns the recursive dependencies, accross the whole package-graph. |
| 90 | + public func recursiveDependencies() -> [Dependency] { |
| 91 | + return try! topologicalSort(self.dependencies) { $0.dependencies } |
| 92 | + } |
| 93 | + |
| 94 | + /// Returns the recursive target dependencies, accross the whole package-graph. |
| 95 | + public func recursiveTargetDependencies() -> [ResolvedTarget] { |
| 96 | + return try! topologicalSort(self.dependencies) { $0.dependencies }.compactMap { $0.target } |
| 97 | + } |
| 98 | + |
| 99 | + /// Returns the recursive dependencies, accross the whole package-graph, which satisfy the input build environment, |
| 100 | + /// based on their conditions. |
| 101 | + /// - Parameters: |
| 102 | + /// - environment: The build environment to use to filter dependencies on. |
| 103 | + public func recursiveDependencies(satisfying environment: BuildEnvironment) -> [Dependency] { |
| 104 | + return try! topologicalSort(dependencies(satisfying: environment)) { dependency in |
| 105 | + return dependency.dependencies.filter { $0.satisfies(environment) } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// The language-level target name. |
| 110 | + public var c99name: String { |
| 111 | + return underlyingTarget.c99name |
| 112 | + } |
| 113 | + |
| 114 | + /// The "type" of target. |
| 115 | + public var type: Target.Kind { |
| 116 | + return underlyingTarget.type |
| 117 | + } |
| 118 | + |
| 119 | + /// The sources for the target. |
| 120 | + public var sources: Sources { |
| 121 | + return underlyingTarget.sources |
| 122 | + } |
| 123 | + |
| 124 | + /// Create a target instance. |
| 125 | + public init(target: Target, dependencies: [Dependency]) { |
| 126 | + self.underlyingTarget = target |
| 127 | + self.dependencies = dependencies |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +extension ResolvedTarget: CustomStringConvertible { |
| 132 | + public var description: String { |
| 133 | + return "<ResolvedTarget: \(name)>" |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +extension ResolvedTarget.Dependency: Equatable { |
| 138 | + public static func == (lhs: ResolvedTarget.Dependency, rhs: ResolvedTarget.Dependency) -> Bool { |
| 139 | + switch (lhs, rhs) { |
| 140 | + case (.target(let lhsTarget, _), .target(let rhsTarget, _)): |
| 141 | + return lhsTarget == rhsTarget |
| 142 | + case (.product(let lhsProduct, _), .product(let rhsProduct, _)): |
| 143 | + return lhsProduct == rhsProduct |
| 144 | + case (.product, .target), (.target, .product): |
| 145 | + return false |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +extension ResolvedTarget.Dependency: Hashable { |
| 151 | + public func hash(into hasher: inout Hasher) { |
| 152 | + switch self { |
| 153 | + case .target(let target, _): |
| 154 | + hasher.combine(target) |
| 155 | + case .product(let product, _): |
| 156 | + hasher.combine(product) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +extension ResolvedTarget.Dependency: CustomStringConvertible { |
| 162 | + public var description: String { |
| 163 | + var str = "<ResolvedTarget.Dependency: " |
| 164 | + switch self { |
| 165 | + case .product(let p, _): |
| 166 | + str += p.description |
| 167 | + case .target(let t, _): |
| 168 | + str += t.description |
| 169 | + } |
| 170 | + str += ">" |
| 171 | + return str |
| 172 | + } |
| 173 | +} |
0 commit comments