forked from swiftlang/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildDescription.swift
240 lines (197 loc) · 8.07 KB
/
BuildDescription.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import struct Foundation.URL
private import struct Basics.AbsolutePath
private import func Basics.resolveSymlinks
internal import SPMBuildCore
// FIXME: should import these module with `private` or `internal` access control
import class Build.BuildPlan
import class Build.ClangModuleBuildDescription
import class Build.SwiftModuleBuildDescription
import struct PackageGraph.ResolvedModule
import struct PackageGraph.ModulesGraph
internal import class PackageModel.UserToolchain
public enum BuildDestination {
case host
case target
}
public protocol BuildTarget {
/// Source files in the target
var sources: [URL] { get }
/// Header files in the target
var headers: [URL] { get }
/// The resource files in the target.
var resources: [URL] { get }
/// Files in the target that were marked as ignored.
var ignored: [URL] { get }
/// Other kinds of files in the target.
var others: [URL] { get }
/// The name of the target. It should be possible to build a target by passing this name to `swift build --target`
var name: String { get }
var destination: BuildDestination { get }
/// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
var isPartOfRootPackage: Bool { get }
var isTestTarget: Bool { get }
func compileArguments(for fileURL: URL) throws -> [String]
}
private struct WrappedClangTargetBuildDescription: BuildTarget {
private let description: ClangModuleBuildDescription
let isPartOfRootPackage: Bool
let isTestTarget: Bool
init(description: ClangModuleBuildDescription, isPartOfRootPackage: Bool) {
self.description = description
self.isPartOfRootPackage = isPartOfRootPackage
self.isTestTarget = description.isTestTarget
}
public var sources: [URL] {
guard let compilePaths = try? description.compilePaths() else {
return []
}
return compilePaths.map(\.source.asURL)
}
public var headers: [URL] {
return description.clangTarget.headers.map(\.asURL)
}
var resources: [URL] {
return description.resources.map { URL(fileURLWithPath: $0.path.pathString) }
}
var ignored: [URL] {
return description.ignored.map { URL(fileURLWithPath: $0.pathString) }
}
var others: [URL] {
return description.others.map { URL(fileURLWithPath: $0.pathString) }
}
public var name: String {
return description.clangTarget.name
}
public var destination: BuildDestination {
return description.destination == .host ? .host : .target
}
public func compileArguments(for fileURL: URL) throws -> [String] {
let filePath = try resolveSymlinks(try AbsolutePath(validating: fileURL.path))
let commandLine = try description.emitCommandLine(for: filePath)
// First element on the command line is the compiler itself, not an argument.
return Array(commandLine.dropFirst())
}
}
private struct WrappedSwiftTargetBuildDescription: BuildTarget {
private let description: SwiftModuleBuildDescription
let isPartOfRootPackage: Bool
let isTestTarget: Bool
init(description: SwiftModuleBuildDescription, isPartOfRootPackage: Bool) {
self.description = description
self.isPartOfRootPackage = isPartOfRootPackage
self.isTestTarget = description.isTestTarget
}
public var name: String {
return description.target.name
}
public var destination: BuildDestination {
return description.destination == .host ? .host : .target
}
var sources: [URL] {
return description.sources.map { URL(fileURLWithPath: $0.pathString) }
}
var headers: [URL] { [] }
var resources: [URL] {
return description.resources.map { URL(fileURLWithPath: $0.path.pathString) }
}
var ignored: [URL] {
return description.ignored.map { URL(fileURLWithPath: $0.pathString) }
}
var others: [URL] {
return description.others.map { URL(fileURLWithPath: $0.pathString) }
}
func compileArguments(for fileURL: URL) throws -> [String] {
// Note: we ignore the `fileURL` here as the expectation is that we get a command line for the entire target
// in case of Swift.
let commandLine = try description.emitCommandLine(scanInvocation: false, writeOutputFileMap: false)
// First element on the command line is the compiler itself, not an argument.
return Array(commandLine.dropFirst())
}
}
public struct BuildDescription {
private let buildPlan: Build.BuildPlan
/// The inputs of the build plan so we don't need to re-compute them on every call to
/// `fileAffectsSwiftOrClangBuildSettings`.
private let inputs: [BuildPlan.Input]
// FIXME: should not use `BuildPlan` in the public interface
public init(buildPlan: Build.BuildPlan) {
self.buildPlan = buildPlan
self.inputs = buildPlan.inputs
}
func getBuildTarget(
for module: ResolvedModule,
destination: BuildParameters.Destination
) -> BuildTarget? {
if let description = self.buildPlan.description(for: module, context: destination) {
let modulesGraph = self.buildPlan.graph
switch description {
case .clang(let description):
return WrappedClangTargetBuildDescription(
description: description,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(description.package.id)
)
case .swift(let description):
return WrappedSwiftTargetBuildDescription(
description: description,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(description.package.id)
)
}
} else {
if module.type == .plugin, let package = self.buildPlan.graph.package(for: module) {
let modulesGraph = self.buildPlan.graph
return PluginTargetBuildDescription(
target: module,
toolsVersion: package.manifest.toolsVersion,
toolchain: buildPlan.toolsBuildParameters.toolchain,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(package.id)
)
}
return nil
}
}
public func traverseModules(
callback: (any BuildTarget, _ parent: (any BuildTarget)?) -> Void
) {
self.buildPlan.traverseModules { module, parent in
let parentDescription: (any BuildTarget)? = if let parent {
getBuildTarget(for: parent.0, destination: parent.1)
} else {
nil
}
if let description = getBuildTarget(for: module.0, destination: module.1) {
callback(description, parentDescription)
}
}
}
/// Returns `true` if the file at the given path might influence build settings for a `swiftc` or `clang` invocation
/// generated by SwiftPM.
public func fileAffectsSwiftOrClangBuildSettings(_ url: URL) -> Bool {
guard let filePath = try? AbsolutePath(validating: url.path) else {
return false
}
for input in self.inputs {
switch input {
case .directoryStructure(let path):
if path.isAncestor(of: filePath) {
return true
}
case .file(let path):
if filePath == path {
return true
}
}
}
return false
}
}