/
BuildParameters.swift
366 lines (308 loc) · 13.6 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
355
356
357
358
359
360
361
362
363
364
365
366
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020 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 class Foundation.ProcessInfo
import TSCBasic
import PackageModel
import PackageGraph
import struct TSCUtility.BuildFlags
import struct TSCUtility.Triple
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
}
/// Represents the debugging strategy.
///
/// Swift binaries requires the swiftmodule files in order for lldb to work.
/// On Darwin, linker can directly take the swiftmodule file path using the
/// -add_ast_path flag. On other platforms, we convert the swiftmodule into
/// an object file using Swift's modulewrap tool.
public enum DebuggingStrategy {
case swiftAST
case modulewrap
}
/// Represents the test discovery strategy.
public enum TestDiscoveryStrategy: Encodable {
// Rely on objective C runtime
case objectiveC
// Use a test-manifest that lists the tests
// generate: Whether test-manifest generation is forced
// This flag is or backwards compatibility, remove with --enable-test-discovery
case manifest(generate: Bool)
public enum DiscriminatorKeys: String, Codable {
case objectiveC
case manifest
}
public enum CodingKeys: CodingKey {
case _case
case generate
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .objectiveC:
try container.encode(DiscriminatorKeys.objectiveC, forKey: ._case)
case .manifest(let generate):
try container.encode(DiscriminatorKeys.manifest, forKey: ._case)
try container.encode(generate, forKey: .generate)
}
}
}
/// 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
/// Host triple.
public var hostTriple: Triple
/// Destination triple.
public var triple: Triple
/// The architectures to build for.
public var archs: [String]
/// Extra build flags.
public var flags: BuildFlags
/// How many jobs should llbuild and the Swift compiler spawn
public var jobs: UInt32
/// If should link the Swift stdlib statically.
public var shouldLinkStaticSwiftStdlib: Bool
/// Which compiler sanitizers should be enabled
public var sanitizers: EnabledSanitizers
/// If should enable llbuild manifest caching.
public var shouldEnableManifestCaching: Bool
/// The mode to use for indexing-while-building feature.
public var indexStoreMode: IndexStoreMode
/// Whether to enable code coverage.
public var enableCodeCoverage: Bool
/// Whether to enable generation of `.swiftinterface` files alongside
/// `.swiftmodule`s.
public var enableParseableModuleInterfaces: Bool
/// Emit Swift module separately from object files. This can enable more parallelism
/// since downstream targets can begin compiling without waiting for the entire
/// module to finish building.
public var emitSwiftModuleSeparately: Bool
/// Whether to use the integrated Swift driver rather than shelling out
/// to a separate process.
public var useIntegratedSwiftDriver: Bool
/// Whether to use the explicit module build flow (with the integrated driver)
public var useExplicitModuleBuild: Bool
/// Whether to output a graphviz file visualization of the combined job graph for all targets
public var printManifestGraphviz: Bool
/// Whether to create dylibs for dynamic library products.
public var shouldCreateDylibForDynamicProducts: Bool
/// Whether to enable the entry-point-function-name feature.
public var canRenameEntrypointFunctionName: 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() {
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
/// Extra arguments to pass when using xcbuild.
public var xcbuildFlags: [String]
// Whether building for testability is enabled.
public var enableTestability: Bool
// What strategy to use to discover tests
public var testDiscoveryStrategy: TestDiscoveryStrategy
/// Whether to disable dead code stripping by the linker
public var linkerDeadStrip: Bool
public var colorizedOutput: Bool
public var verboseOutput: Bool
public init(
dataPath: AbsolutePath,
configuration: BuildConfiguration,
toolchain: Toolchain,
hostTriple: Triple? = nil,
destinationTriple: Triple? = nil,
archs: [String] = [],
flags: BuildFlags,
xcbuildFlags: [String] = [],
jobs: UInt32 = UInt32(ProcessInfo.processInfo.activeProcessorCount),
shouldLinkStaticSwiftStdlib: Bool = false,
shouldEnableManifestCaching: Bool = false,
canRenameEntrypointFunctionName: Bool = false,
shouldCreateDylibForDynamicProducts: Bool = true,
sanitizers: EnabledSanitizers = EnabledSanitizers(),
enableCodeCoverage: Bool = false,
indexStoreMode: IndexStoreMode = .auto,
enableParseableModuleInterfaces: Bool = false,
emitSwiftModuleSeparately: Bool = false,
useIntegratedSwiftDriver: Bool = false,
useExplicitModuleBuild: Bool = false,
isXcodeBuildSystemEnabled: Bool = false,
printManifestGraphviz: Bool = false,
enableTestability: Bool? = nil,
forceTestDiscovery: Bool = false,
linkerDeadStrip: Bool = true,
colorizedOutput: Bool = false,
verboseOutput: Bool = false
) {
let triple = destinationTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
self.dataPath = dataPath
self.configuration = configuration
self._toolchain = _Toolchain(toolchain: toolchain)
self.hostTriple = hostTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
self.triple = triple
self.archs = archs
self.flags = flags
self.xcbuildFlags = xcbuildFlags
self.jobs = jobs
self.shouldLinkStaticSwiftStdlib = shouldLinkStaticSwiftStdlib
self.shouldEnableManifestCaching = shouldEnableManifestCaching
self.shouldCreateDylibForDynamicProducts = shouldCreateDylibForDynamicProducts
self.canRenameEntrypointFunctionName = canRenameEntrypointFunctionName
self.sanitizers = sanitizers
self.enableCodeCoverage = enableCodeCoverage
self.indexStoreMode = indexStoreMode
self.enableParseableModuleInterfaces = enableParseableModuleInterfaces
self.emitSwiftModuleSeparately = emitSwiftModuleSeparately
self.useIntegratedSwiftDriver = useIntegratedSwiftDriver
self.useExplicitModuleBuild = useExplicitModuleBuild
self.isXcodeBuildSystemEnabled = isXcodeBuildSystemEnabled
self.printManifestGraphviz = printManifestGraphviz
// decide on testability based on debug/release config
// the goals of this being based on the build configuration is
// that `swift build` followed by a `swift test` will need to do minimal rebuilding
// given that the default configuration for `swift build` is debug
// and that `swift test` normally requires building with testable enabled.
// when building and testing in release mode, one can use the '--disable-testable-imports' flag
// to disable testability in `swift test`, but that requires that the tests do not use the testable imports feature
self.enableTestability = enableTestability ?? (.debug == configuration)
// decide if to enable the use of test manifests based on platform. this is likely to change in the future
self.testDiscoveryStrategy = triple.isDarwin() ? .objectiveC : .manifest(generate: forceTestDiscovery)
self.linkerDeadStrip = linkerDeadStrip
self.colorizedOutput = colorizedOutput
self.verboseOutput = verboseOutput
}
/// 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(component: "codecov")
}
/// The path to the code coverage profdata file.
public var codeCovDataFile: AbsolutePath {
return codeCovPath.appending(component: "default.profdata")
}
public var llbuildManifest: AbsolutePath {
return dataPath.appending(components: "..", configuration.dirname + ".yaml")
}
public var pifManifest: AbsolutePath {
return dataPath.appending(components: "..", "manifest.pif")
}
public var buildDescriptionPath: AbsolutePath {
return buildPath.appending(components: "description.json")
}
/// The debugging strategy according to the current build parameters.
public var debuggingStrategy: DebuggingStrategy? {
guard configuration == .debug else {
return nil
}
if triple.isDarwin() {
return .swiftAST
}
return .modulewrap
}
/// Returns the path to the binary of a product for the current build parameters.
public func binaryPath(for product: ResolvedProduct) -> AbsolutePath {
return buildPath.appending(binaryRelativePath(for: product))
}
/// 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) -> RelativePath {
switch product.type {
case .executable, .snippet:
return RelativePath("\(product.name)\(triple.executableExtension)")
case .library(.static):
return RelativePath("lib\(product.name)\(triple.staticLibraryExtension)")
case .library(.dynamic):
return RelativePath("\(triple.dynamicLibraryPrefix)\(product.name)\(triple.dynamicLibraryExtension)")
case .library(.automatic), .plugin:
fatalError()
case .test:
guard !triple.isWASI() else {
return RelativePath("\(product.name).wasm")
}
let base = "\(product.name).xctest"
if triple.isDarwin() {
return RelativePath("\(base)/Contents/MacOS/\(product.name)")
} else {
return RelativePath(base)
}
}
}
}
/// 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.extraCCFlags, forKey: .extraCCFlags)
try container.encode(toolchain.extraCPPFlags, forKey: .extraCPPFlags)
try container.encode(toolchain.extraSwiftCFlags, 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
}
}
}