-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBuildParameters.swift
354 lines (310 loc) · 13.5 KB
/
BuildParameters.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
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
//===----------------------------------------------------------------------===//
//
// 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
public struct BuildParameters: Encodable {
/// 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 path to the data directory.
public var dataPath: AbsolutePath
/// The build configuration.
public var configuration: BuildConfiguration
/// The toolchain.
public var toolchain: Toolchain { _toolchain.toolchain }
private let _toolchain: _Toolchain
/// 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: [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 {
return .linux
}
}
/// Whether the Xcode build system is used.
public var isXcodeBuildSystemEnabled: Bool
public var shouldSkipBuilding: Bool
/// 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(
dataPath: AbsolutePath,
configuration: BuildConfiguration,
toolchain: Toolchain,
triple: Triple? = nil,
flags: BuildFlags,
pkgConfigDirectories: [AbsolutePath] = [],
architectures: [String]? = nil,
workers: UInt32 = UInt32(ProcessInfo.processInfo.activeProcessorCount),
shouldCreateDylibForDynamicProducts: Bool = true,
sanitizers: EnabledSanitizers = EnabledSanitizers(),
indexStoreMode: IndexStoreMode = .auto,
isXcodeBuildSystemEnabled: Bool = false,
shouldSkipBuilding: Bool = false,
debuggingParameters: Debugging? = nil,
driverParameters: Driver = .init(),
linkingParameters: Linking = .init(),
outputParameters: Output = .init(),
testingParameters: Testing? = nil
) throws {
let triple = try triple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
self.debuggingParameters = debuggingParameters ?? .init(
triple: triple,
shouldEnableDebuggingEntitlement: configuration == .debug,
omitFramePointers: nil
)
self.dataPath = dataPath
self.configuration = configuration
self._toolchain = _Toolchain(toolchain: toolchain)
self.triple = triple
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.isXcodeBuildSystemEnabled = isXcodeBuildSystemEnabled
self.shouldSkipBuilding = shouldSkipBuilding
self.driverParameters = driverParameters
self.linkingParameters = linkingParameters
self.outputParameters = outputParameters
self.testingParameters = testingParameters ?? .init(configuration: configuration, targetTriple: triple)
}
@available(*, deprecated, message: "Use build parameters value separately created at a higher value instead")
public func forTriple(_ targetTriple: Triple) throws -> BuildParameters {
var hostSDK = try SwiftSDK.hostSwiftSDK()
hostSDK.targetTriple = targetTriple
return try .init(
dataPath: self.dataPath.parentDirectory.appending(components: ["plugins", "tools"]),
configuration: self.configuration,
toolchain: try UserToolchain(swiftSDK: hostSDK),
triple: nil,
flags: BuildFlags(),
pkgConfigDirectories: self.pkgConfigDirectories,
architectures: nil,
workers: self.workers,
shouldCreateDylibForDynamicProducts: self.shouldCreateDylibForDynamicProducts,
sanitizers: self.sanitizers,
indexStoreMode: self.indexStoreMode,
isXcodeBuildSystemEnabled: self.isXcodeBuildSystemEnabled,
shouldSkipBuilding: self.shouldSkipBuilding,
driverParameters: self.driverParameters,
linkingParameters: self.linkingParameters,
outputParameters: self.outputParameters,
testingParameters: self.testingParameters
)
}
/// The path to the build directory (inside the data directory).
public var buildPath: AbsolutePath {
if isXcodeBuildSystemEnabled {
return dataPath.appending(components: "Products", configuration.dirname.capitalized)
} else {
return dataPath.appending(component: configuration.dirname)
}
}
/// The path to the index store directory.
public var indexStore: AbsolutePath {
assert(indexStoreMode != .off, "index store is disabled")
return buildPath.appending(components: "index", "store")
}
/// The path to the code coverage directory.
public var codeCovPath: AbsolutePath {
return buildPath.appending("codecov")
}
/// The path to the code coverage profdata file.
public var codeCovDataFile: AbsolutePath {
return codeCovPath.appending("default.profdata")
}
public var llbuildManifest: 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: 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: 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: 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 -> AbsolutePath {
return try buildPath.appending(binaryRelativePath(for: product))
}
/// Returns the path to the dynamic library of a product for the current build parameters.
func potentialDynamicLibraryPath(for product: ResolvedProduct) throws -> RelativePath {
try RelativePath(validating: "\(self.triple.dynamicLibraryPrefix)\(product.name)\(self.triple.dynamicLibraryExtension)")
}
/// 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 -> RelativePath {
let potentialExecutablePath = try RelativePath(validating: "\(product.name)\(self.triple.executableExtension)")
switch product.type {
case .executable, .snippet:
return potentialExecutablePath
case .library(.static):
return try RelativePath(validating: "lib\(product.name)\(self.triple.staticLibraryExtension)")
case .library(.dynamic):
return try potentialDynamicLibraryPath(for: product)
case .library(.automatic), .plugin:
fatalError()
case .test:
guard !self.triple.isWASI() else {
return try RelativePath(validating: "\(product.name).wasm")
}
switch testingParameters.library {
case .xctest:
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 .swiftTesting:
return try RelativePath(validating: "\(product.name).swift-testing")
}
case .macro:
#if BUILD_MACROS_AS_DYLIBS
return try potentialDynamicLibraryPath(for: product)
#else
return potentialExecutablePath
#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 BuildParameters {
/// Whether to build Swift code with whole module optimization (WMO)
/// enabled.
public var useWholeModuleOptimization: Bool {
switch configuration {
case .debug:
return false
case .release:
return true
}
}
}
extension Triple {
public var supportsTestSummary: Bool {
return !self.isWindows()
}
}