-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSwiftBuildSystem.swift
561 lines (500 loc) · 23.7 KB
/
SwiftBuildSystem.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
@_spi(SwiftPMInternal)
import Basics
import Dispatch
import class Foundation.FileManager
import class Foundation.JSONEncoder
import class Foundation.NSArray
import class Foundation.NSDictionary
import PackageGraph
import PackageModel
@_spi(SwiftPMInternal)
import SPMBuildCore
import class Basics.AsyncProcess
import func TSCBasic.memoize
import protocol TSCBasic.OutputByteStream
import func TSCBasic.withTemporaryFile
import enum TSCUtility.Diagnostics
#if canImport(SwiftBuild)
import Foundation
import SWBBuildService
import SwiftBuild
#endif
#if canImport(SwiftBuild)
struct SessionFailedError: Error {
var error: Error
var diagnostics: [SwiftBuild.SwiftBuildMessage.DiagnosticInfo]
}
func withService(
connectionMode: SWBBuildServiceConnectionMode = .default,
variant: SWBBuildServiceVariant = .default,
serviceBundleURL: URL? = nil,
body: @escaping (_ service: SWBBuildService) async throws -> Void
) async throws {
let service = try await SWBBuildService(connectionMode: connectionMode, variant: variant, serviceBundleURL: serviceBundleURL)
do {
try await body(service)
} catch {
await service.close()
throw error
}
await service.close()
}
func withSession(
service: SWBBuildService,
name: String,
toolchainPath: String,
packageManagerResourcesDirectory: Basics.AbsolutePath?,
body: @escaping (
_ session: SWBBuildServiceSession,
_ diagnostics: [SwiftBuild.SwiftBuildMessage.DiagnosticInfo]
) async throws -> Void
) async throws {
switch await service.createSession(
name: name,
swiftToolchainPath: toolchainPath,
resourceSearchPaths: packageManagerResourcesDirectory.map {
[$0.pathString]
} ?? [],
cachePath: nil, inferiorProductsPath: nil,
environment: nil
) {
case (.success(let session), let diagnostics):
do {
try await body(session, diagnostics)
} catch {
do {
try await session.close()
} catch _ {
// Assumption is that the first error is the most important one
throw SessionFailedError(error: error, diagnostics: diagnostics)
}
throw SessionFailedError(error: error, diagnostics: diagnostics)
}
do {
try await session.close()
} catch {
throw SessionFailedError(error: error, diagnostics: diagnostics)
}
case (.failure(let error), let diagnostics):
throw SessionFailedError(error: error, diagnostics: diagnostics)
}
}
private final class PlanningOperationDelegate: SWBPlanningOperationDelegate, Sendable {
public func provisioningTaskInputs(
targetGUID: String,
provisioningSourceData: SWBProvisioningTaskInputsSourceData
) async -> SWBProvisioningTaskInputs {
let identity = provisioningSourceData.signingCertificateIdentifier
if identity == "-" {
let signedEntitlements = provisioningSourceData.entitlementsDestination == "Signature"
? provisioningSourceData.productTypeEntitlements.merging(
["application-identifier": .plString(provisioningSourceData.bundleIdentifier)],
uniquingKeysWith: { _, new in new }
).merging(provisioningSourceData.projectEntitlements ?? [:], uniquingKeysWith: { _, new in new })
: [:]
let simulatedEntitlements = provisioningSourceData.entitlementsDestination == "__entitlements"
? provisioningSourceData.productTypeEntitlements.merging(
["application-identifier": .plString(provisioningSourceData.bundleIdentifier)],
uniquingKeysWith: { _, new in new }
).merging(provisioningSourceData.projectEntitlements ?? [:], uniquingKeysWith: { _, new in new })
: [:]
return SWBProvisioningTaskInputs(
identityHash: "-",
identityName: "-",
profileName: nil,
profileUUID: nil,
profilePath: nil,
designatedRequirements: nil,
signedEntitlements: signedEntitlements.merging(
provisioningSourceData.sdkRoot.contains("simulator") ? ["get-task-allow": .plBool(true)] : [:],
uniquingKeysWith: { _, new in new }
),
simulatedEntitlements: simulatedEntitlements,
appIdentifierPrefix: nil,
teamIdentifierPrefix: nil,
isEnterpriseTeam: nil,
keychainPath: nil,
errors: [],
warnings: []
)
} else if identity.isEmpty {
return SWBProvisioningTaskInputs()
} else {
return SWBProvisioningTaskInputs(
identityHash: "-",
errors: [
[
"description": "unable to supply accurate provisioning inputs for CODE_SIGN_IDENTITY=\(identity)\"",
],
]
)
}
}
public func executeExternalTool(
commandLine: [String],
workingDirectory: String?,
environment: [String: String]
) async throws -> SWBExternalToolResult {
.deferred
}
}
#endif
public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
private let buildParameters: BuildParameters
private let packageGraphLoader: () async throws -> ModulesGraph
private let packageManagerResourcesDirectory: Basics.AbsolutePath?
private let logLevel: Basics.Diagnostic.Severity
private var packageGraph: AsyncThrowingValueMemoizer<ModulesGraph> = .init()
private var pifBuilder: AsyncThrowingValueMemoizer<PIFBuilder> = .init()
private let fileSystem: FileSystem
private let observabilityScope: ObservabilityScope
/// The output stream for the build delegate.
private let outputStream: OutputByteStream
/// The delegate used by the build system.
public weak var delegate: SPMBuildCore.BuildSystemDelegate?
public var builtTestProducts: [BuiltTestProduct] {
get async {
do {
let graph = try await getPackageGraph()
var builtProducts: [BuiltTestProduct] = []
for package in graph.rootPackages {
for product in package.products where product.type == .test {
let binaryPath = try buildParameters.binaryPath(for: product)
builtProducts.append(
BuiltTestProduct(
productName: product.name,
binaryPath: binaryPath,
packagePath: package.path,
testEntryPointPath: product.underlying.testEntryPointPath
)
)
}
}
return builtProducts
} catch {
self.observabilityScope.emit(error)
return []
}
}
}
public var buildPlan: SPMBuildCore.BuildPlan {
get throws {
throw StringError("Swift Build does not provide a build plan")
}
}
public init(
buildParameters: BuildParameters,
packageGraphLoader: @escaping () async throws -> ModulesGraph,
packageManagerResourcesDirectory: Basics.AbsolutePath?,
outputStream: OutputByteStream,
logLevel: Basics.Diagnostic.Severity,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) throws {
self.buildParameters = buildParameters
self.packageGraphLoader = packageGraphLoader
self.packageManagerResourcesDirectory = packageManagerResourcesDirectory
self.outputStream = outputStream
self.logLevel = logLevel
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope.makeChildScope(description: "Swift Build System")
}
private func supportedSwiftVersions() throws -> [SwiftLanguageVersion] {
// Swift Build should support any of the supported language versions of SwiftPM and the rest of the toolchain
SwiftLanguageVersion.supportedSwiftLanguageVersions
}
public func build(subset: BuildSubset) async throws {
#if canImport(SwiftBuild)
guard !buildParameters.shouldSkipBuilding else {
return
}
let pifBuilder = try await getPIFBuilder()
let pif = try pifBuilder.generatePIF()
try self.fileSystem.writeIfChanged(path: buildParameters.pifManifest, string: pif)
try await startSWBuildOperation(pifTargetName: subset.pifTargetName)
#else
fatalError("Swift Build support is not linked in.")
#endif
}
#if canImport(SwiftBuild)
private func startSWBuildOperation(pifTargetName: String) async throws {
let buildStartTime = ContinuousClock.Instant.now
try await withService(connectionMode: .inProcessStatic(swiftbuildServiceEntryPoint)) { service in
let parameters = try self.makeBuildParameters()
let derivedDataPath = self.buildParameters.dataPath.pathString
let progressAnimation = ProgressAnimation.percent(
stream: self.outputStream,
verbose: self.logLevel.isVerbose,
header: "",
isColorized: self.buildParameters.outputParameters.isColorized
)
do {
let toolchainPath = self.buildParameters.toolchain.swiftCompilerPath
.parentDirectory // remove swift
.parentDirectory // remove bin
.parentDirectory // remove usr
try await withSession(
service: service,
name: self.buildParameters.pifManifest.pathString,
toolchainPath: toolchainPath.pathString,
packageManagerResourcesDirectory: self.packageManagerResourcesDirectory
) { session, _ in
self.outputStream.send("Building for \(self.buildParameters.configuration == .debug ? "debugging" : "production")...\n")
// Load the workspace, and set the system information to the default
do {
try await session.loadWorkspace(containerPath: self.buildParameters.pifManifest.pathString)
try await session.setSystemInfo(.default())
} catch {
self.observabilityScope.emit(error: error.localizedDescription)
throw error
}
// Find the targets to build.
let configuredTargets: [SWBConfiguredTarget]
do {
let workspaceInfo = try await session.workspaceInfo()
configuredTargets = try [pifTargetName].map { targetName in
let infos = workspaceInfo.targetInfos.filter { $0.targetName == targetName }
switch infos.count {
case 0:
self.observabilityScope.emit(error: "Could not find target named '\(targetName)'")
throw Diagnostics.fatalError
case 1:
return SWBConfiguredTarget(guid: infos[0].guid, parameters: parameters)
default:
self.observabilityScope.emit(error: "Found multiple targets named '\(targetName)'")
throw Diagnostics.fatalError
}
}
} catch {
self.observabilityScope.emit(error: error.localizedDescription)
throw error
}
var request = SWBBuildRequest()
request.parameters = parameters
request.configuredTargets = configuredTargets
request.useParallelTargets = true
request.useImplicitDependencies = false
request.useDryRun = false
request.hideShellScriptEnvironment = true
request.showNonLoggedProgress = true
// Override the arena. We need to apply the arena info to both the request-global build
// parameters as well as the target-specific build parameters, since they may have been
// deserialized from the build request file above overwriting the build parameters we set
// up earlier in this method.
#if os(Windows)
let ddPathPrefix = derivedDataPath.replacingOccurrences(of: "\\", with: "/")
#else
let ddPathPrefix = derivedDataPath
#endif
let arenaInfo = SWBArenaInfo(
derivedDataPath: ddPathPrefix,
buildProductsPath: ddPathPrefix + "/Products",
buildIntermediatesPath: ddPathPrefix + "/Intermediates.noindex",
pchPath: ddPathPrefix + "/PCH",
indexRegularBuildProductsPath: nil,
indexRegularBuildIntermediatesPath: nil,
indexPCHPath: ddPathPrefix,
indexDataStoreFolderPath: ddPathPrefix,
indexEnableDataStore: request.parameters.arenaInfo?.indexEnableDataStore ?? false
)
request.parameters.arenaInfo = arenaInfo
request.configuredTargets = request.configuredTargets.map { configuredTarget in
var configuredTarget = configuredTarget
configuredTarget.parameters?.arenaInfo = arenaInfo
return configuredTarget
}
func emitEvent(_ message: SwiftBuild.SwiftBuildMessage) throws {
switch message {
case .buildCompleted:
progressAnimation.complete(success: true)
case .didUpdateProgress(let progressInfo):
var step = Int(progressInfo.percentComplete)
if step < 0 { step = 0 }
let message = if let targetName = progressInfo.targetName {
"\(targetName) \(progressInfo.message)"
} else {
"\(progressInfo.message)"
}
progressAnimation.update(step: step, total: 100, text: message)
case .diagnostic(let info):
if info.kind == .error {
self.observabilityScope.emit(error: "\(info.location) \(info.message) \(info.fixIts)")
} else if info.kind == .warning {
self.observabilityScope.emit(warning: "\(info.location) \(info.message) \(info.fixIts)")
} else if info.kind == .note {
self.observabilityScope.emit(info: "\(info.location) \(info.message) \(info.fixIts)")
} else if info.kind == .remark {
self.observabilityScope.emit(debug: "\(info.location) \(info.message) \(info.fixIts)")
}
case .taskOutput(let info):
self.observabilityScope.emit(info: "\(info.data)")
case .taskStarted(let info):
if let commandLineDisplay = info.commandLineDisplayString {
self.observabilityScope.emit(info: "\(info.executionDescription)\n\(commandLineDisplay)")
} else {
self.observabilityScope.emit(info: "\(info.executionDescription)")
}
default:
break
}
}
let operation = try await session.createBuildOperation(
request: request,
delegate: PlanningOperationDelegate()
)
for try await event in try await operation.start() {
try emitEvent(event)
}
await operation.waitForCompletion()
switch operation.state {
case .succeeded:
progressAnimation.update(step: 100, total: 100, text: "")
progressAnimation.complete(success: true)
let duration = ContinuousClock.Instant.now - buildStartTime
self.outputStream.send("Build complete! (\(duration))\n")
self.outputStream.flush()
case .failed:
self.observabilityScope.emit(error: "Build failed")
throw Diagnostics.fatalError
case .cancelled:
self.observabilityScope.emit(error: "Build was cancelled")
throw Diagnostics.fatalError
case .requested, .running, .aborted:
self.observabilityScope.emit(error: "Unexpected build state")
throw Diagnostics.fatalError
}
}
} catch let sessError as SessionFailedError {
for diagnostic in sessError.diagnostics {
self.observabilityScope.emit(error: diagnostic.message)
}
throw sessError.error
} catch {
throw error
}
}
}
func makeBuildParameters() throws -> SwiftBuild.SWBBuildParameters {
// Generate the run destination parameters.
let runDestination = SwiftBuild.SWBRunDestinationInfo(
platform: self.buildParameters.triple.osNameUnversioned,
sdk: self.buildParameters.triple.osNameUnversioned,
sdkVariant: nil,
targetArchitecture: buildParameters.triple.archName,
supportedArchitectures: [],
disableOnlyActiveArch: false
)
var verboseFlag: [String] = []
if self.logLevel == .debug {
verboseFlag = ["-v"] // Clang's verbose flag
}
// Generate a table of any overriding build settings.
var settings: [String: String] = [:]
// An error with determining the override should not be fatal here.
settings["CC"] = try? buildParameters.toolchain.getClangCompiler().pathString
// Always specify the path of the effective Swift compiler, which was determined in the same way as for the
// native build system.
settings["SWIFT_EXEC"] = buildParameters.toolchain.swiftCompilerPath.pathString
// FIXME: workaround for old Xcode installations such as what is in CI
settings["LM_SKIP_METADATA_EXTRACTION"] = "YES"
settings["LIBRARY_SEARCH_PATHS"] = try "$(inherited) \(buildParameters.toolchain.toolchainLibDir.pathString)"
settings["OTHER_CFLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.cCompilerFlags.map { $0.shellEscaped() }
+ buildParameters.flags.cCompilerFlags.map { $0.shellEscaped() }
).joined(separator: " ")
settings["OTHER_CPLUSPLUSFLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.cxxCompilerFlags.map { $0.shellEscaped() }
+ buildParameters.flags.cxxCompilerFlags.map { $0.shellEscaped() }
).joined(separator: " ")
settings["OTHER_SWIFT_FLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.swiftCompilerFlags.map { $0.shellEscaped() }
+ buildParameters.flags.swiftCompilerFlags.map { $0.shellEscaped() }
).joined(separator: " ")
settings["OTHER_LDFLAGS"] = (
verboseFlag + // clang will be invoked to link so the verbose flag is valid for it
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.linkerFlags.map { $0.shellEscaped() }
+ buildParameters.flags.linkerFlags.map { $0.shellEscaped() }
).joined(separator: " ")
// Optionally also set the list of architectures to build for.
if let architectures = buildParameters.architectures, !architectures.isEmpty {
settings["ARCHS"] = architectures.joined(separator: " ")
}
// Generate the build parameters.
var params = SwiftBuild.SWBBuildParameters()
params.configurationName = buildParameters.configuration.swiftbuildName
var overridesSynthesized = SwiftBuild.SWBSettingsTable()
for (key, value) in settings {
overridesSynthesized.set(value: value, for: key)
}
params.overrides.synthesized = overridesSynthesized
params.activeRunDestination = runDestination
return params
}
private func getPIFBuilder() async throws -> PIFBuilder {
try await pifBuilder.memoize {
let graph = try await getPackageGraph()
let pifBuilder = try PIFBuilder(
graph: graph,
parameters: .init(buildParameters, supportedSwiftVersions: supportedSwiftVersions()),
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
return pifBuilder
}
}
#endif
public func cancel(deadline: DispatchTime) throws {}
/// Returns the package graph using the graph loader closure.
///
/// First access will cache the graph.
public func getPackageGraph() async throws -> ModulesGraph {
try await packageGraph.memoize {
try await packageGraphLoader()
}
}
}
extension String {
/// Escape the usual shell related things, such as quoting, but also handle Windows
/// back-slashes.
fileprivate func shellEscaped() -> String {
#if os(Windows)
return self.spm_shellEscaped().replacingOccurrences(of: "\\", with: "/")
#else
return self.spm_shellEscaped()
#endif
}
}
extension PIFBuilderParameters {
public init(_ buildParameters: BuildParameters, supportedSwiftVersions: [SwiftLanguageVersion]) {
self.init(
triple: buildParameters.triple,
isPackageAccessModifierSupported: buildParameters.driverParameters.isPackageAccessModifierSupported,
enableTestability: buildParameters.enableTestability,
shouldCreateDylibForDynamicProducts: buildParameters.shouldCreateDylibForDynamicProducts,
toolchainLibDir: (try? buildParameters.toolchain.toolchainLibDir) ?? .root,
pkgConfigDirectories: buildParameters.pkgConfigDirectories,
sdkRootPath: buildParameters.toolchain.sdkRootPath,
supportedSwiftVersions: supportedSwiftVersions
)
}
}
extension Basics.Diagnostic.Severity {
var isVerbose: Bool {
self <= .info
}
}