File: SwiftDriverJobDynamicTaskSpec.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (270 lines) | stat: -rw-r--r-- 12,785 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

import SWBCore
import SWBProtocol
public import SWBUtil

public struct SwiftDriverJobTaskKey: Serializable, CustomDebugStringConvertible {
    let identifier: String
    let variant: String
    let arch: String
    let driverJobKey: LibSwiftDriver.JobKey
    let driverJobSignature: ByteString
    let isUsingWholeModuleOptimization: Bool
    let compilerLocation: LibSwiftDriver.CompilerLocation
    let casOptions: CASOptions?

    init(identifier: String, variant: String, arch: String, driverJobKey: LibSwiftDriver.JobKey, driverJobSignature: ByteString, isUsingWholeModuleOptimization: Bool, compilerLocation: LibSwiftDriver.CompilerLocation, casOptions: CASOptions?) {
        self.identifier = identifier
        self.variant = variant
        self.arch = arch
        self.driverJobKey = driverJobKey
        self.driverJobSignature = driverJobSignature
        self.isUsingWholeModuleOptimization = isUsingWholeModuleOptimization
        self.compilerLocation = compilerLocation
        self.casOptions = casOptions
    }

    public func serialize<T>(to serializer: T) where T : Serializer {
        serializer.serializeAggregate(8) {
            serializer.serialize(identifier)
            serializer.serialize(variant)
            serializer.serialize(arch)
            serializer.serialize(driverJobKey)
            serializer.serialize(driverJobSignature)
            serializer.serialize(isUsingWholeModuleOptimization)
            serializer.serialize(compilerLocation)
            serializer.serialize(casOptions)
        }
    }

    public init(from deserializer: any Deserializer) throws {
        try deserializer.beginAggregate(8)
        identifier = try deserializer.deserialize()
        variant = try deserializer.deserialize()
        arch = try deserializer.deserialize()
        driverJobKey = try deserializer.deserialize()
        driverJobSignature = try deserializer.deserialize()
        isUsingWholeModuleOptimization = try deserializer.deserialize()
        compilerLocation = try deserializer.deserialize()
        casOptions = try deserializer.deserialize()
    }

    public var debugDescription: String {
        "<SwiftDriverJob identifier=\(identifier) arch=\(arch) variant=\(variant) jobKey=\(driverJobKey) jobSignature=\(driverJobSignature) isUsingWholeModuleOptimization=\(isUsingWholeModuleOptimization) compilerLocation=\(compilerLocation) casOptions=\(String(describing: casOptions))>"
    }
}

public struct SwiftDriverExplicitDependencyJobTaskKey: Serializable, CustomDebugStringConvertible {
    let arch: String
    let driverJobKey: LibSwiftDriver.JobKey
    let driverJobSignature: ByteString
    let compilerLocation: LibSwiftDriver.CompilerLocation
    let casOptions: CASOptions?

    init(arch: String, driverJobKey: LibSwiftDriver.JobKey, driverJobSignature: ByteString, compilerLocation: LibSwiftDriver.CompilerLocation, casOptions: CASOptions?) {
        self.arch = arch
        self.driverJobKey = driverJobKey
        self.driverJobSignature = driverJobSignature
        self.compilerLocation = compilerLocation
        self.casOptions = casOptions
    }

    public func serialize<T>(to serializer: T) where T : Serializer {
        serializer.serializeAggregate(5) {
            serializer.serialize(arch)
            serializer.serialize(driverJobKey)
            serializer.serialize(driverJobSignature)
            serializer.serialize(compilerLocation)
            serializer.serialize(casOptions)
        }
    }

    public init(from deserializer: any Deserializer) throws {
        try deserializer.beginAggregate(5)
        arch = try deserializer.deserialize()
        driverJobKey = try deserializer.deserialize()
        driverJobSignature = try deserializer.deserialize()
        compilerLocation = try deserializer.deserialize()
        casOptions = try deserializer.deserialize()
    }

    public var debugDescription: String {
        "<SwiftDriverExplicitDependencyJob arch=\(arch) jobKey=\(driverJobKey) jobSignature=\(driverJobSignature) compilerLocation=\(compilerLocation) casOptions=\(String(describing: casOptions))>"
    }
}

struct SwiftDriverJobDynamicTaskPayload: TaskPayload {
    let expectedOutputs: [Path]
    let isUsingWholeModuleOptimization: Bool
    let compilerLocation: LibSwiftDriver.CompilerLocation
    let casOptions: CASOptions?

    init(expectedOutputs: [Path], isUsingWholeModuleOptimization: Bool, compilerLocation: LibSwiftDriver.CompilerLocation, casOptions: CASOptions?) {
        self.expectedOutputs = expectedOutputs
        self.isUsingWholeModuleOptimization = isUsingWholeModuleOptimization
        self.compilerLocation = compilerLocation
        self.casOptions = casOptions
    }

    init(from deserializer: any Deserializer) throws {
        try deserializer.beginAggregate(4)
        self.expectedOutputs = try deserializer.deserialize()
        self.isUsingWholeModuleOptimization = try deserializer.deserialize()
        self.compilerLocation = try deserializer.deserialize()
        self.casOptions = try deserializer.deserialize()
    }

    func serialize<T>(to serializer: T) where T : Serializer {
        serializer.serializeAggregate(4) {
            serializer.serialize(expectedOutputs)
            serializer.serialize(isUsingWholeModuleOptimization)
            serializer.serialize(compilerLocation)
            serializer.serialize(casOptions)
        }
    }
}

final class SwiftDriverJobDynamicTaskSpec: DynamicTaskSpec {
    func buildExecutableTask(dynamicTask: DynamicTask, context: DynamicTaskOperationContext) throws -> any ExecutableTask {
        let commandLinePrefix: [ByteString] = [
            "builtin-swiftTaskExecution",
            "--"
        ]
        var commandLine: [ByteString]
        let expectedOutputs: [Path]
        let forTarget: ConfiguredTarget?
        let ruleInfo: [String]
        let descriptionForLifecycle: String
        let isUsingWholeModuleOptimization: Bool
        let compilerLocation: LibSwiftDriver.CompilerLocation
        let casOpts: CASOptions?
        switch dynamicTask.taskKey {
            case .swiftDriverJob(let key):
                guard let job = try context.swiftModuleDependencyGraph.queryPlannedBuild(for: key.identifier).plannedTargetJob(for: key.driverJobKey)?.driverJob else {
                    throw StubError.error("Failed to lookup Swift driver job \(key.driverJobKey) in build plan \(key.identifier)")
                }
                commandLine = commandLinePrefix + job.commandLine
                expectedOutputs = job.outputs
                ruleInfo = ["Swift\(job.ruleInfoType)", key.variant, key.arch, job.descriptionForLifecycle] + job.displayInputs.map(\.str)
                forTarget = dynamicTask.target
                descriptionForLifecycle = job.descriptionForLifecycle
                isUsingWholeModuleOptimization = key.isUsingWholeModuleOptimization
                compilerLocation = key.compilerLocation
                casOpts = key.casOptions
            case .swiftDriverExplicitDependencyJob(let key):
                guard let job = context.swiftModuleDependencyGraph.plannedExplicitDependencyBuildJob(for: key.driverJobKey)?.driverJob else {
                    throw StubError.error("Failed to lookup explicit modules Swift driver job \(key.driverJobKey)")
                }
                commandLine = commandLinePrefix + job.commandLine
                expectedOutputs = job.outputs
                assert(expectedOutputs.count > 0, "Explicit modules job was expected to have at least one primary output")
                ruleInfo = ["SwiftExplicitDependency\(job.ruleInfoType)", key.arch, expectedOutputs.first?.str ?? "<unknown>"]
                forTarget = nil
                descriptionForLifecycle = job.descriptionForLifecycle
                // WMO doesn't apply to explicit module builds
                isUsingWholeModuleOptimization = false
                compilerLocation = key.compilerLocation
                casOpts = key.casOptions
            default:
                fatalError("Unexpected dynamic task: \(dynamicTask)")
        }

        if !supportsParseableOutput(for: ruleInfo) {
            commandLine = commandLine.filter({ $0 != "-frontend-parseable-output" })
        }

        return Task(type: self,
                    payload:
                        SwiftDriverJobDynamicTaskPayload(
                            expectedOutputs: expectedOutputs,
                            isUsingWholeModuleOptimization: isUsingWholeModuleOptimization,
                            compilerLocation: compilerLocation,
                            casOptions: casOpts
                        ),
                    forTarget: forTarget,
                    ruleInfo: ruleInfo,
                    commandLine: commandLine.map { .literal($0) },
                    environment: dynamicTask.environment,
                    workingDirectory: dynamicTask.workingDirectory,
                    showEnvironment: dynamicTask.showEnvironment,
                    execDescription: descriptionForLifecycle,
                    preparesForIndexing: true,
                    showCommandLineInLog: false,
                    isDynamic: true
                )
    }

    var payloadType: (any TaskPayload.Type)? {
        SwiftDriverJobDynamicTaskPayload.self
    }

    private func supportsParseableOutput(for ruleInfo: [String]) -> Bool {
        return !(ruleInfo.first?.hasPrefix("SwiftExplicitDependency") ?? false) && !(ruleInfo.first?.hasPrefix("SwiftVerifyEmittedModuleInterface") ?? false)
    }

    func customOutputParserType(for task: any ExecutableTask) -> (any TaskOutputParser.Type)? {
        if supportsParseableOutput(for: task.ruleInfo) {
            return SwiftCommandOutputParser.self
        } else {
            return serializedDiagnosticsPaths(task).isEmpty ?
                        GenericOutputParser.self :
                        SerializedDiagnosticsOutputParser.self
        }
    }


    func serializedDiagnosticsPaths(_ task: any ExecutableTask) -> [Path] {
        if supportsParseableOutput(for: task.ruleInfo) {
            return []
        }

        guard let payload = task.payload as? SwiftDriverJobDynamicTaskPayload else {
            return []
        }

        return payload.expectedOutputs.filter({ $0.fileExtension == "dia" })
    }

    func serializedDiagnosticsPaths(_ task: any ExecutableTask, _ fs: any FSProxy) -> [Path] {
        let expectedDiagnostics = serializedDiagnosticsPaths(task)

        // rdar://91295617 (Swift produces empty serialized diagnostics if there are none which is not parseable by clang_loadDiagnostics)
        return expectedDiagnostics.filter { filePath in
            do {
                let shouldAdd = try fs.exists(filePath) && (try fs.getFileSize(filePath)) > .zero
                return shouldAdd
            } catch {
                return false
            }
        }
    }

    func buildTaskAction(dynamicTaskKey: DynamicTaskKey, context: DynamicTaskOperationContext) throws -> TaskAction {
        switch dynamicTaskKey {
            case .swiftDriverJob(let key):
            guard let job = try context.swiftModuleDependencyGraph.queryPlannedBuild(for: key.identifier).plannedTargetJob(for: key.driverJobKey) else {
                throw StubError.error("Failed to lookup Swift driver job \(key.driverJobKey) in build plan \(key.identifier)")
            }
            return SwiftDriverJobTaskAction(job, variant: key.variant, arch: key.arch, identifier: .targetCompile(key.identifier), isUsingWholeModuleOptimization: key.isUsingWholeModuleOptimization)
            case .swiftDriverExplicitDependencyJob(let key):
                // WMO doesn't apply to explicit module builds
                guard let job = context.swiftModuleDependencyGraph.plannedExplicitDependencyBuildJob(for: key.driverJobKey) else {
                    throw StubError.error("Failed to lookup explicit module Swift driver job \(key.driverJobKey)")
                }
            return SwiftDriverJobTaskAction(job, variant: nil, arch: key.arch, identifier: .explicitDependency, isUsingWholeModuleOptimization: false)
            default:
                fatalError("Unexpected dynamic task key: \(dynamicTaskKey)")
        }
    }
}