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 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
|
//===----------------------------------------------------------------------===//
//
// 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
import PackageLoading
@_spi(SwiftPMInternal)
import SPMBuildCore
import class Basics.AsyncProcess
import func TSCBasic.memoize
import protocol TSCBasic.OutputByteStream
import func TSCBasic.withTemporaryFile
import enum TSCUtility.Diagnostics
import Foundation
import SWBBuildService
import 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()
}
public func createSession(
service: SWBBuildService,
name: String,
toolchainPath: Basics.AbsolutePath,
packageManagerResourcesDirectory: Basics.AbsolutePath?
) async throws-> (SWBBuildServiceSession, [SwiftBuildMessage.DiagnosticInfo]) {
// SWIFT_EXEC and SWIFT_EXEC_MANIFEST may need to be overridden in debug scenarios in order to pick up Open Source toolchains
let sessionResult = if toolchainPath.components.contains(where: { $0.hasSuffix(".xctoolchain") }) {
await service.createSession(name: name, developerPath: nil, resourceSearchPaths: packageManagerResourcesDirectory.map { [$0.pathString] } ?? [], cachePath: nil, inferiorProductsPath: nil, environment: nil)
} else {
await service.createSession(name: name, swiftToolchainPath: toolchainPath.pathString, resourceSearchPaths: packageManagerResourcesDirectory.map { [$0.pathString] } ?? [], cachePath: nil, inferiorProductsPath: nil, environment: nil)
}
switch sessionResult {
case (.success(let session), let diagnostics):
return (session, diagnostics)
case (.failure(let error), let diagnostics):
throw SessionFailedError(error: error, diagnostics: diagnostics)
}
}
func withSession(
service: SWBBuildService,
name: String,
toolchainPath: Basics.AbsolutePath,
packageManagerResourcesDirectory: Basics.AbsolutePath?,
body: @escaping (
_ session: SWBBuildServiceSession,
_ diagnostics: [SwiftBuild.SwiftBuildMessage.DiagnosticInfo]
) async throws -> Void
) async throws {
let (session, diagnostics) = try await createSession(service: service, name: name, toolchainPath: toolchainPath, packageManagerResourcesDirectory: packageManagerResourcesDirectory)
do {
try await body(session, diagnostics)
} catch let bodyError {
do {
try await session.close()
} catch _ {
// Assumption is that the first error is the most important one
throw bodyError
}
throw bodyError
}
do {
try await session.close()
} catch {
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
}
}
public struct PluginConfiguration {
/// Entity responsible for compiling and running plugin scripts.
let scriptRunner: PluginScriptRunner
/// Directory where plugin intermediate files are stored.
let workDirectory: Basics.AbsolutePath
/// Whether to sandbox commands from build tool plugins.
let disableSandbox: Bool
public init(
scriptRunner: PluginScriptRunner,
workDirectory: Basics.AbsolutePath,
disableSandbox: Bool
) {
self.scriptRunner = scriptRunner
self.workDirectory = workDirectory
self.disableSandbox = disableSandbox
}
}
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?
/// Configuration for building and invoking plugins.
private let pluginConfiguration: PluginConfiguration
/// Additional rules for different file types generated from plugins.
private let additionalFileRules: [FileRuleDescription]
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?,
additionalFileRules: [FileRuleDescription],
outputStream: OutputByteStream,
logLevel: Basics.Diagnostic.Severity,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
pluginConfiguration: PluginConfiguration
) throws {
self.buildParameters = buildParameters
self.packageGraphLoader = packageGraphLoader
self.packageManagerResourcesDirectory = packageManagerResourcesDirectory
self.additionalFileRules = additionalFileRules
self.outputStream = outputStream
self.logLevel = logLevel
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope.makeChildScope(description: "Swift Build System")
self.pluginConfiguration = pluginConfiguration
}
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 {
guard !buildParameters.shouldSkipBuilding else {
return
}
try await writePIF(buildParameters: buildParameters)
try await startSWBuildOperation(pifTargetName: subset.pifTargetName)
}
private func startSWBuildOperation(pifTargetName: String) async throws {
let buildStartTime = ContinuousClock.Instant.now
try await withService(connectionMode: .inProcessStatic(swiftbuildServiceEntryPoint)) { service in
let derivedDataPath = self.buildParameters.dataPath
let progressAnimation = ProgressAnimation.ninja(
stream: self.outputStream,
verbose: self.logLevel.isVerbose
)
do {
try await withSession(service: service, name: self.buildParameters.pifManifest.pathString, toolchainPath: self.buildParameters.toolchain.toolchainDir, 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: [SWBTargetGUID]
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 SWBTargetGUID(rawValue: infos[0].guid)
default:
self.observabilityScope.emit(error: "Found multiple targets named '\(targetName)'")
throw Diagnostics.fatalError
}
}
} catch {
self.observabilityScope.emit(error: error.localizedDescription)
throw error
}
let request = try self.makeBuildRequest(configuredTargets: configuredTargets, derivedDataPath: derivedDataPath)
struct BuildState {
private var activeTasks: [Int: SwiftBuild.SwiftBuildMessage.TaskStartedInfo] = [:]
mutating func started(task: SwiftBuild.SwiftBuildMessage.TaskStartedInfo) throws {
if activeTasks[task.taskID] != nil {
throw Diagnostics.fatalError
}
activeTasks[task.taskID] = task
}
mutating func completed(task: SwiftBuild.SwiftBuildMessage.TaskCompleteInfo) throws -> SwiftBuild.SwiftBuildMessage.TaskStartedInfo {
guard let task = activeTasks[task.taskID] else {
throw Diagnostics.fatalError
}
return task
}
}
func emitEvent(_ message: SwiftBuild.SwiftBuildMessage, buildState: inout BuildState) throws {
guard !self.logLevel.isQuiet else { return }
switch message {
case .buildCompleted(let info):
progressAnimation.complete(success: info.result == .ok)
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):
func emitInfoAsDiagnostic(info: SwiftBuildMessage.DiagnosticInfo) {
let fixItsDescription = if info.fixIts.hasContent {
": " + info.fixIts.map { String(describing: $0) }.joined(separator: ", ")
} else {
""
}
let message = if let locationDescription = info.location.userDescription {
"\(locationDescription) \(info.message)\(fixItsDescription)"
} else {
"\(info.message)\(fixItsDescription)"
}
let severity: Diagnostic.Severity = switch info.kind {
case .error: .error
case .warning: .warning
case .note: .info
case .remark: .debug
}
self.observabilityScope.emit(severity: severity, message: message)
for childDiagnostic in info.childDiagnostics {
emitInfoAsDiagnostic(info: childDiagnostic)
}
}
emitInfoAsDiagnostic(info: info)
case .output(let info):
self.observabilityScope.emit(info: "\(String(decoding: info.data, as: UTF8.self))")
case .taskStarted(let info):
try buildState.started(task: info)
if let commandLineDisplay = info.commandLineDisplayString {
self.observabilityScope.emit(info: "\(info.executionDescription)\n\(commandLineDisplay)")
} else {
self.observabilityScope.emit(info: "\(info.executionDescription)")
}
case .taskComplete(let info):
let startedInfo = try buildState.completed(task: info)
if info.result != .success {
self.observabilityScope.emit(severity: .error, message: "\(startedInfo.ruleInfo) failed with a nonzero exit code")
}
case .planningOperationStarted, .planningOperationCompleted, .reportBuildDescription, .reportPathMap, .preparedForIndex, .backtraceFrame, .buildStarted, .preparationComplete, .targetUpToDate, .targetStarted, .targetComplete, .taskUpToDate:
break
case .buildDiagnostic, .targetDiagnostic, .taskDiagnostic:
break // deprecated
case .buildOutput, .targetOutput, .taskOutput:
break // deprecated
@unknown default:
break
}
}
let operation = try await session.createBuildOperation(
request: request,
delegate: PlanningOperationDelegate()
)
var buildState = BuildState()
for try await event in try await operation.start() {
try emitEvent(event, buildState: &buildState)
}
await operation.waitForCompletion()
switch operation.state {
case .succeeded:
guard !self.logLevel.isQuiet else { return }
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
}
}
}
private func makeRunDestination() -> SwiftBuild.SWBRunDestinationInfo {
let platformName: String
let sdkName: String
if self.buildParameters.triple.isAndroid() {
// Android triples are identified by the environment part of the triple
platformName = "android"
sdkName = platformName
} else if self.buildParameters.triple.isWasm {
// Swift Build uses webassembly instead of wasi as the platform name
platformName = "webassembly"
sdkName = platformName
} else {
platformName = self.buildParameters.triple.darwinPlatform?.platformName ?? self.buildParameters.triple.osNameUnversioned
sdkName = platformName
}
let sdkVariant: String?
if self.buildParameters.triple.environment == .macabi {
sdkVariant = "iosmac"
} else {
sdkVariant = nil
}
return SwiftBuild.SWBRunDestinationInfo(
platform: platformName,
sdk: sdkName,
sdkVariant: sdkVariant,
targetArchitecture: buildParameters.triple.archName,
supportedArchitectures: [],
disableOnlyActiveArch: false
)
}
private func makeBuildParameters() throws -> SwiftBuild.SWBBuildParameters {
// Generate the run destination parameters.
let runDestination = makeRunDestination()
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"
let normalizedTriple = Triple(buildParameters.triple.triple, normalizing: true)
if let deploymentTargetSettingName = normalizedTriple.deploymentTargetSettingName {
let value = normalizedTriple.deploymentTargetVersion
// Only override the deployment target if a version is explicitly specified;
// for Apple platforms this normally comes from the package manifest and may
// not be set to the same value for all packages in the package graph.
if value != .zero {
settings[deploymentTargetSettingName] = value.description
}
}
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: " ")
}
func reportConflict(_ a: String, _ b: String) throws -> String {
throw StringError("Build parameters constructed conflicting settings overrides '\(a)' and '\(b)'")
}
try settings.merge(Self.constructDebuggingSettingsOverrides(from: buildParameters.debuggingParameters), uniquingKeysWith: reportConflict)
try settings.merge(Self.constructDriverSettingsOverrides(from: buildParameters.driverParameters), uniquingKeysWith: reportConflict)
try settings.merge(Self.constructLinkerSettingsOverrides(from: buildParameters.linkingParameters), uniquingKeysWith: reportConflict)
try settings.merge(Self.constructTestingSettingsOverrides(from: buildParameters.testingParameters), uniquingKeysWith: reportConflict)
// 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
}
public func makeBuildRequest(configuredTargets: [SWBTargetGUID], derivedDataPath: Basics.AbsolutePath) throws -> SWBBuildRequest {
var request = SWBBuildRequest()
request.parameters = try makeBuildParameters()
request.configuredTargets = configuredTargets.map { SWBConfiguredTarget(guid: $0.rawValue, parameters: request.parameters) }
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.pathString.replacingOccurrences(of: "\\", with: "/")
#else
let ddPathPrefix = derivedDataPath.pathString
#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
}
return request
}
private static func constructDebuggingSettingsOverrides(from parameters: BuildParameters.Debugging) -> [String: String] {
var settings: [String: String] = [:]
// TODO: debugInfoFormat: https://github.com/swiftlang/swift-build/issues/560
// TODO: shouldEnableDebuggingEntitlement: Enable/Disable get-task-allow
// TODO: omitFramePointer: https://github.com/swiftlang/swift-build/issues/561
return settings
}
private static func constructDriverSettingsOverrides(from parameters: BuildParameters.Driver) -> [String: String] {
var settings: [String: String] = [:]
switch parameters.explicitTargetDependencyImportCheckingMode {
case .none:
break
case .warn:
settings["DIAGNOSE_MISSING_TARGET_DEPENDENCIES"] = "YES"
case .error:
settings["DIAGNOSE_MISSING_TARGET_DEPENDENCIES"] = "YES_ERROR"
}
if parameters.enableParseableModuleInterfaces {
settings["SWIFT_EMIT_MODULE_INTERFACE"] = "YES"
}
return settings
}
private static func constructLinkerSettingsOverrides(from parameters: BuildParameters.Linking) -> [String: String] {
var settings: [String: String] = [:]
if parameters.linkerDeadStrip {
settings["DEAD_CODE_STRIPPING"] = "YES"
}
switch parameters.linkTimeOptimizationMode {
case .full:
settings["LLVM_LTO"] = "YES"
settings["SWIFT_LTO"] = "YES"
case .thin:
settings["LLVM_LTO"] = "YES_THIN"
settings["SWIFT_LTO"] = "YES_THIN"
case nil:
break
}
// TODO: shouldDisableLocalRpath
// TODO: shouldLinkStaticSwiftStdlib
return settings
}
private static func constructTestingSettingsOverrides(from parameters: BuildParameters.Testing) -> [String: String] {
var settings: [String: String] = [:]
// TODO: enableCodeCoverage
// explicitlyEnabledTestability
switch parameters.explicitlyEnabledTestability {
case true:
settings["ENABLE_TESTABILITY"] = "YES"
case false:
settings["ENABLE_TESTABILITY"] = "NO"
default:
break
}
// TODO: experimentalTestOutput
// TODO: explicitlyEnabledDiscovery
// TODO: explicitlySpecifiedPath
return settings
}
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(),
pluginScriptRunner: self.pluginConfiguration.scriptRunner,
disableSandbox: self.pluginConfiguration.disableSandbox,
pluginWorkingDirectory: self.pluginConfiguration.workDirectory,
additionalFileRules: additionalFileRules
),
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
return pifBuilder
}
}
public func writePIF(buildParameters: BuildParameters) async throws {
let pifBuilder = try await getPIFBuilder()
let pif = try await pifBuilder.generatePIF(
printPIFManifestGraphviz: buildParameters.printPIFManifestGraphviz,
buildParameters: buildParameters,
)
try self.fileSystem.writeIfChanged(path: buildParameters.pifManifest, string: pif)
}
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()
}
}
}
// MARK: - Helpers
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
}
}
fileprivate extension SwiftBuild.SwiftBuildMessage.DiagnosticInfo.Location {
var userDescription: String? {
switch self {
case .path(let path, let fileLocation):
switch fileLocation {
case .textual(let line, let column):
var description = "\(path):\(line)"
if let column { description += ":\(column)" }
return description
case .object(let identifier):
return "\(path):\(identifier)"
case .none:
return path
}
case .buildSettings(let names):
return names.joined(separator: ", ")
case .buildFiles(let buildFiles, let targetGUID):
return "\(targetGUID): " + buildFiles.map { String(describing: $0) }.joined(separator: ", ")
case .unknown:
return nil
}
}
}
fileprivate extension Triple {
var deploymentTargetSettingName: String? {
switch (self.os, self.environment) {
case (.macosx, _):
return "MACOSX_DEPLOYMENT_TARGET"
case (.ios, _):
return "IPHONEOS_DEPLOYMENT_TARGET"
case (.tvos, _):
return "TVOS_DEPLOYMENT_TARGET"
case (.watchos, _):
return "WATCHOS_DEPLOYMENT_TARGET"
case (_, .android):
return "ANDROID_DEPLOYMENT_TARGET"
default:
return nil
}
}
var deploymentTargetVersion: Version {
if isAndroid() {
// Android triples store the version in the environment
var environmentName = self.environmentName[...]
if environment != nil {
let prefixes = ["androideabi", "android"]
for prefix in prefixes {
if environmentName.hasPrefix(prefix) {
environmentName = environmentName.dropFirst(prefix.count)
break
}
}
}
return Version(parse: environmentName)
}
return osVersion
}
}
|