forked from swiftlang/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildParameters.swift
More file actions
386 lines (337 loc) · 14.8 KB
/
Copy pathBuildParameters.swift
File metadata and controls
386 lines (337 loc) · 14.8 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2023 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 Basics
import class Foundation.ProcessInfo
import PackageModel
import PackageGraph
import TSCBasic
public struct BuildParameters: Encodable {
public enum PrepareForIndexingMode: Encodable {
/// Perform a normal build and don't prepare for indexing
case off
/// Prepare for indexing but don't pass `-experimental-lazy-typecheck`.
///
/// This is intended as a workaround if lazy type checking is causing compiler crashes.
case noLazy
/// Do minimal build to prepare for indexing
case on
}
/// Mode for the indexing-while-building feature.
public enum IndexStoreMode: String, Encodable {
/// Index store should be enabled.
case on
/// Index store should be disabled.
case off
/// Index store should be enabled in debug configuration.
case auto
}
/// The destination for which code should be compiled for.
public enum Destination: Hashable, Encodable {
/// The destination for which build tools are compiled.
case host
/// The destination for which end products are compiled.
case target
}
/// The destination these parameters are going to be used for.
public var destination: Destination
/// The path to the data directory.
public var dataPath: Basics.AbsolutePath
/// The build configuration.
public var configuration: BuildConfiguration
/// The toolchain.
public var toolchain: Toolchain { _toolchain.toolchain }
private let _toolchain: _Toolchain
@available(*, deprecated, renamed: "triple", message: "Use separate `BuildParameters` values for host and target.")
public var targetTriple: Triple { self.triple }
/// The triple for which the code is built using these build parameters.
public var triple: Triple
/// Extra build flags.
public var flags: BuildFlags
/// An array of paths to search for pkg-config `.pc` files.
public var pkgConfigDirectories: [Basics.AbsolutePath]
/// The architectures to build for.
// FIXME: this may be inconsistent with `targetTriple`.
public var architectures: [String]?
/// How many jobs should llbuild and the Swift compiler spawn
public var workers: UInt32
/// Which compiler sanitizers should be enabled
public var sanitizers: EnabledSanitizers
/// The mode to use for indexing-while-building feature.
public var indexStoreMode: IndexStoreMode
/// Whether to create dylibs for dynamic library products.
public var shouldCreateDylibForDynamicProducts: Bool
/// The current build environment.
public var buildEnvironment: BuildEnvironment {
BuildEnvironment(platform: currentPlatform, configuration: configuration)
}
/// The current platform we're building for.
var currentPlatform: PackageModel.Platform {
if self.triple.isDarwin() {
switch self.triple.darwinPlatform {
case .iOS(.catalyst):
return .macCatalyst
case .iOS(.device), .iOS(.simulator):
return .iOS
case .tvOS:
return .tvOS
case .watchOS:
return .watchOS
case .macOS, nil:
return .macOS
}
} else if self.triple.isAndroid() {
return .android
} else if self.triple.isWASI() {
return .wasi
} else if self.triple.isWindows() {
return .windows
} else if self.triple.isOpenBSD() {
return .openbsd
} else if self.triple.isFreeBSD() {
return .freebsd
} else {
return .linux
}
}
public var buildSystemKind: BuildSystemProvider.Kind
public var shouldSkipBuilding: Bool
public var printPIFManifestGraphviz: Bool = false
/// Do minimal build to prepare for indexing
public var prepareForIndexing: PrepareForIndexingMode
/// Build parameters related to debugging.
public var debuggingParameters: Debugging
/// Build parameters related to Swift Driver.
public var driverParameters: Driver
/// Build parameters related to linking.
public var linkingParameters: Linking
/// Build parameters related to output and logging.
public var outputParameters: Output
/// Build parameters related to testing.
public var testingParameters: Testing
public init(
destination: Destination,
dataPath: Basics.AbsolutePath,
configuration: BuildConfiguration,
toolchain: Toolchain,
triple: Triple? = nil,
flags: BuildFlags,
buildSystemKind: BuildSystemProvider.Kind = .native,
pkgConfigDirectories: [Basics.AbsolutePath] = [],
architectures: [String]? = nil,
workers: UInt32 = UInt32(ProcessInfo.processInfo.activeProcessorCount),
shouldCreateDylibForDynamicProducts: Bool = true,
sanitizers: EnabledSanitizers = EnabledSanitizers(),
indexStoreMode: IndexStoreMode = .auto,
shouldSkipBuilding: Bool = false,
prepareForIndexing: PrepareForIndexingMode = .off,
debuggingParameters: Debugging? = nil,
driverParameters: Driver = .init(),
linkingParameters: Linking = .init(),
outputParameters: Output = .init(),
testingParameters: Testing = .init()
) throws {
let triple = try triple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
self.debuggingParameters = debuggingParameters ?? .init(
triple: triple,
shouldEnableDebuggingEntitlement: configuration == .debug,
omitFramePointers: nil
)
self.destination = destination
self.dataPath = dataPath
self.configuration = configuration
self._toolchain = _Toolchain(toolchain: toolchain)
self.triple = triple
self.buildSystemKind = buildSystemKind
switch self.debuggingParameters.debugInfoFormat {
case .dwarf:
var flags = flags
// DWARF requires lld as link.exe expects CodeView debug info.
self.flags = flags.merging(triple.isWindows() ? BuildFlags(
cCompilerFlags: ["-gdwarf"],
cxxCompilerFlags: ["-gdwarf"],
swiftCompilerFlags: ["-g", "-use-ld=lld"],
linkerFlags: ["-debug:dwarf"]
) : BuildFlags(cCompilerFlags: ["-g"], cxxCompilerFlags: ["-g"], swiftCompilerFlags: ["-g"]))
case .codeview:
if !triple.isWindows() {
throw StringError("CodeView debug information is currently not supported on \(triple.osName)")
}
var flags = flags
self.flags = flags.merging(BuildFlags(
cCompilerFlags: ["-g"],
cxxCompilerFlags: ["-g"],
swiftCompilerFlags: ["-g", "-debug-info-format=codeview"],
linkerFlags: ["-debug"]
))
case .none:
var flags = flags
self.flags = flags.merging(BuildFlags(
cCompilerFlags: ["-g0"],
cxxCompilerFlags: ["-g0"],
swiftCompilerFlags: ["-gnone"]
))
}
self.pkgConfigDirectories = pkgConfigDirectories
self.architectures = architectures
self.workers = workers
self.shouldCreateDylibForDynamicProducts = shouldCreateDylibForDynamicProducts
self.sanitizers = sanitizers
self.indexStoreMode = indexStoreMode
self.shouldSkipBuilding = shouldSkipBuilding
self.prepareForIndexing = prepareForIndexing
self.driverParameters = driverParameters
self.linkingParameters = linkingParameters
self.outputParameters = outputParameters
self.testingParameters = testingParameters
}
/// The path to the build directory (inside the data directory).
public var buildPath: Basics.AbsolutePath {
// TODO: query the build system for this.
switch buildSystemKind {
case .xcode, .swiftbuild:
var configDir: String = configuration.dirname.capitalized
if self.triple.isWindows() {
configDir += "-windows"
} else if self.triple.isLinux() {
configDir += "-linux"
}
return dataPath.appending(components: "Products", configDir)
case .native:
return dataPath.appending(component: configuration.dirname)
}
}
/// The path to the index store directory.
public var indexStore: Basics.AbsolutePath {
assert(indexStoreMode != .off, "index store is disabled")
return buildPath.appending(components: "index", "store")
}
/// The path to the code coverage directory.
public var codeCovPath: Basics.AbsolutePath {
return buildPath.appending("codecov")
}
/// The path to the code coverage profdata file.
public var codeCovDataFile: Basics.AbsolutePath {
return codeCovPath.appending("default.profdata")
}
public var llbuildManifest: Basics.AbsolutePath {
// FIXME: this path isn't specific to `BuildParameters` due to its use of `..`
// FIXME: it should be calculated in a different place
return dataPath.appending(components: "..", configuration.dirname + ".yaml")
}
public var pifManifest: Basics.AbsolutePath {
// FIXME: this path isn't specific to `BuildParameters` due to its use of `..`
// FIXME: it should be calculated in a different place
return dataPath.appending(components: "..", "manifest.pif")
}
public var buildDescriptionPath: Basics.AbsolutePath {
// FIXME: this path isn't specific to `BuildParameters`, should be moved one directory level higher
return buildPath.appending(components: "description.json")
}
public var testOutputPath: Basics.AbsolutePath {
return buildPath.appending(component: "testOutput.txt")
}
/// Returns the path to the binary of a product for the current build parameters.
public func binaryPath(for product: ResolvedProduct) throws -> Basics.AbsolutePath {
return try buildPath.appending(binaryRelativePath(for: product))
}
public func macroBinaryPath(_ module: ResolvedModule) throws -> Basics.AbsolutePath {
assert(module.type == .macro)
#if BUILD_MACROS_AS_DYLIBS
return buildPath.appending(try dynamicLibraryPath(for: module.name))
#else
return buildPath.appending(try executablePath(for: module.name))
#endif
}
/// Returns the path to the dynamic library of a product for the current build parameters.
private func dynamicLibraryPath(for name: String) throws -> Basics.RelativePath {
try RelativePath(validating: "\(self.triple.dynamicLibraryPrefix)\(name)\(self.suffix)\(self.triple.dynamicLibraryExtension)")
}
/// Returns the path to the executable of a product for the current build parameters.
package func executablePath(for name: String) throws -> Basics.RelativePath {
try RelativePath(validating: "\(name)\(self.suffix)\(self.triple.executableExtension)")
}
/// Returns the path to the binary of a product for the current build parameters, relative to the build directory.
public func binaryRelativePath(for product: ResolvedProduct) throws -> Basics.RelativePath {
switch product.type {
case .executable, .snippet:
return try executablePath(for: product.name)
case .library(.static):
return try RelativePath(validating: "lib\(product.name)\(self.suffix)\(self.triple.staticLibraryExtension)")
case .library(.dynamic):
return try dynamicLibraryPath(for: product.name)
case .library(.automatic), .plugin:
fatalError()
case .test:
switch buildSystemKind {
case .native, .xcode:
let base = "\(product.name).xctest"
if self.triple.isDarwin() {
return try RelativePath(validating: "\(base)/Contents/MacOS/\(product.name)")
} else {
return try RelativePath(validating: base)
}
case .swiftbuild:
if self.triple.isDarwin() {
let base = "\(product.name).xctest"
return try RelativePath(validating: "\(base)/Contents/MacOS/\(product.name)")
} else {
var base = "\(product.name)-test-runner"
let ext = self.triple.executableExtension
if !ext.isEmpty {
base += ext
}
return try RelativePath(validating: base)
}
}
case .macro:
#if BUILD_MACROS_AS_DYLIBS
return try dynamicLibraryPath(for: product.name)
#else
return try executablePath(for: product.name)
#endif
}
}
}
/// A shim struct for toolchain so we can encode it without having to write encode(to:) for
/// entire BuildParameters by hand.
private struct _Toolchain: Encodable {
let toolchain: Toolchain
enum CodingKeys: String, CodingKey {
case swiftCompiler
case clangCompiler
case extraCCFlags
case extraSwiftCFlags
case extraCPPFlags
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
try container.encode(toolchain.getClangCompiler(), forKey: .clangCompiler)
try container.encode(toolchain.extraFlags.cCompilerFlags, forKey: .extraCCFlags)
// Maintaining `extraCPPFlags` key for compatibility with older encoding.
try container.encode(toolchain.extraFlags.cxxCompilerFlags, forKey: .extraCPPFlags)
try container.encode(toolchain.extraFlags.swiftCompilerFlags, forKey: .extraSwiftCFlags)
try container.encode(toolchain.swiftCompilerPath, forKey: .swiftCompiler)
}
}
extension Triple {
public var supportsTestSummary: Bool {
return !self.isWindows()
}
}
extension BuildParameters {
/// Suffix appended to build manifest nodes to distinguish nodes created for tools from nodes created for
/// end products, i.e. nodes for host vs target triples.
package var suffix: String {
if destination == .host { "-tool" } else { "" }
}
}