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
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
public import SWBUtil
public import SWBCore
public import SWBLLBuild
import Foundation
/// Used only when remote caching is enabled, to manage tasks for remote key
/// querying and compilation output downloading as dependency tasks.
/// The task on its own doesn't perform any work.
///
/// After this task is finished, the dependent compilation tasks only need to
/// query the local CAS for accessing the data related to a cache key.
public final class ClangCachingMaterializeKeyTaskAction: TaskAction {
public override class var toolIdentifier: String {
return "clang-caching-materialize-key"
}
private let taskKey: ClangCachingTaskCacheKey
package init(key: ClangCachingTaskCacheKey) {
self.taskKey = key
super.init()
}
/// It doesn't perform any work, no need to be scheduled for an execution lane.
override public var shouldExecuteDetached: Bool {
return true
}
private enum State {
/// The action is in its initial state, and has not yet performed any work.
case initial
/// Waiting for the caching key query to finish.
case waitingForKeyQuery(jobTaskIDBase: UInt, casDBs: ClangCASDatabases)
/// Waiting for the outputs to finish downloading.
case waitingForOutputDownloads
/// Not waiting for any other dependency
case done
/// The action failed internally.
case executionError(any Error)
mutating func reset() {
self = .initial
}
}
private var state = State.initial
public override func taskSetup(
_ task: any ExecutableTask,
executionDelegate: any TaskExecutionDelegate,
dynamicExecutionDelegate: any DynamicTaskExecutionDelegate
) {
state.reset()
let clangModuleDependencyGraph = dynamicExecutionDelegate.operationContext.clangModuleDependencyGraph
do {
guard let casDBs = try clangModuleDependencyGraph.getCASDatabases(
libclangPath: taskKey.libclangPath,
casOptions: taskKey.casOptions
) else {
throw StubError.error("unable to use CAS databases")
}
if let cachedComp = try casDBs.getLocalCachedCompilation(cacheKey: taskKey.cacheKey) {
let numOutputsToDownload = requestCompilationOutputs(cachedComp, dynamicExecutionDelegate: dynamicExecutionDelegate, jobTaskIDBase: 0)
if numOutputsToDownload == 0 {
state = .done
} else {
state = .waitingForOutputDownloads
}
} else {
let dependencyID: UInt = 0
dynamicExecutionDelegate.requestDynamicTask(
toolIdentifier: ClangCachingKeyQueryTaskAction.toolIdentifier,
taskKey: .clangCachingKeyQuery(taskKey),
taskID: dependencyID,
singleUse: true,
workingDirectory: Path(""),
environment: .init(),
forTarget: nil,
priority: .network,
showEnvironment: false,
reason: .wasCompilationCachingQuery
)
state = .waitingForKeyQuery(jobTaskIDBase: dependencyID + 1, casDBs: casDBs)
}
} catch {
state = .executionError(error)
return
}
}
public override func taskDependencyReady(
_ task: any ExecutableTask,
_ dependencyID: UInt,
_ buildValueKind: BuildValueKind?,
dynamicExecutionDelegate: any DynamicTaskExecutionDelegate,
executionDelegate: any TaskExecutionDelegate
) {
switch state {
case .initial:
state = .executionError(StubError.error("taskDependencyReady unexpectedly called in initial state"))
case .waitingForKeyQuery(jobTaskIDBase: let jobTaskIDBase, casDBs: let casDBs):
do {
guard let cachedComp = try casDBs.getLocalCachedCompilation(cacheKey: taskKey.cacheKey) else {
state = .done
return // compilation key not found.
}
let numOutputsToDownload = requestCompilationOutputs(cachedComp, dynamicExecutionDelegate: dynamicExecutionDelegate, jobTaskIDBase: jobTaskIDBase)
if numOutputsToDownload == 0 {
state = .done
} else {
state = .waitingForOutputDownloads
}
} catch {
state = .executionError(error)
return
}
case .waitingForOutputDownloads:
break
case .done:
state = .executionError(StubError.error("taskDependencyReady unexpectedly called while not waiting on a dependency"))
case .executionError(_):
break
}
}
private func requestCompilationOutputs(
_ cachedComp: ClangCASCachedCompilation,
dynamicExecutionDelegate: any DynamicTaskExecutionDelegate,
jobTaskIDBase: UInt
) -> Int {
var numOutputsToDownload = 0
for output in cachedComp.getOutputs() {
guard !cachedComp.isOutputMaterialized(output) else {
continue
}
let outputMaterializeKey = ClangCachingOutputMaterializerTaskKey(
libclangPath: taskKey.libclangPath,
casOptions: taskKey.casOptions,
casID: output.casID,
outputName: output.name
)
dynamicExecutionDelegate.requestDynamicTask(
toolIdentifier: ClangCachingOutputMaterializerTaskAction.toolIdentifier,
taskKey: .clangCachingOutputMaterializer(outputMaterializeKey),
taskID: jobTaskIDBase + UInt(numOutputsToDownload),
singleUse: true,
workingDirectory: Path(""),
environment: .init(),
forTarget: nil,
priority: .network,
showEnvironment: false,
reason: .wasCompilationCachingQuery
)
numOutputsToDownload += 1
}
return numOutputsToDownload
}
public override func performTaskAction(
_ task: any ExecutableTask,
dynamicExecutionDelegate: any DynamicTaskExecutionDelegate,
executionDelegate: any TaskExecutionDelegate,
clientDelegate: any TaskExecutionClientDelegate,
outputDelegate: any TaskOutputDelegate
) async -> CommandResult {
defer {
state.reset()
}
if case .executionError(let error) = state {
outputDelegate.error(error.localizedDescription)
return .failed
}
return .succeeded
}
public override func serialize<T: Serializer>(to serializer: T) {
serializer.beginAggregate(2)
serializer.serialize(taskKey)
super.serialize(to: serializer)
}
public required init(from deserializer: any Deserializer) throws {
try deserializer.beginAggregate(2)
self.taskKey = try deserializer.deserialize()
try super.init(from: deserializer)
}
}
|