File: PrecompileClangModuleDynamicTaskSpec.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 (177 lines) | stat: -rw-r--r-- 7,588 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
//===----------------------------------------------------------------------===//
//
// 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 Foundation
import SWBCore
import SWBProtocol
public import SWBUtil

public struct PrecompileClangModuleTaskKey: Serializable, CustomDebugStringConvertible {
    let dependencyInfoPath: Path
    let usesSerializedDiagnostics: Bool
    let libclangPath: Path
    let casOptions: CASOptions?
    let verifyingModule: String?
    let fileNameMapPath: Path?

    init(
        dependencyInfoPath: Path,
        usesSerializedDiagnostics: Bool,
        libclangPath: Path,
        casOptions: CASOptions?,
        verifyingModule: String?,
        fileNameMapPath: Path?
    ) {
        self.dependencyInfoPath = dependencyInfoPath
        self.usesSerializedDiagnostics = usesSerializedDiagnostics
        self.libclangPath = libclangPath
        self.casOptions = casOptions
        self.verifyingModule = verifyingModule
        self.fileNameMapPath = fileNameMapPath
    }

    public func serialize<T: Serializer>(to serializer: T) {
        serializer.serializeAggregate(6) {
            serializer.serialize(dependencyInfoPath)
            serializer.serialize(usesSerializedDiagnostics)
            serializer.serialize(libclangPath)
            serializer.serialize(casOptions)
            serializer.serialize(verifyingModule)
            serializer.serialize(fileNameMapPath)
        }
    }

    public init(from deserializer: any Deserializer) throws {
        try deserializer.beginAggregate(6)
        self.dependencyInfoPath = try deserializer.deserialize()
        self.usesSerializedDiagnostics = try deserializer.deserialize()
        self.libclangPath = try deserializer.deserialize()
        self.casOptions = try deserializer.deserialize()
        self.verifyingModule = try deserializer.deserialize()
        self.fileNameMapPath = try deserializer.deserialize()
    }

    public var debugDescription: String {
        var result = "<PrecompileClangModule pcmPath=\(dependencyInfoPath) usesDia=\(usesSerializedDiagnostics)"
        if let verifyingModule {
            result += " verifyingModule=\(verifyingModule)"
        }
        if let fileNameMapPath {
            result += " fileNameMap=\(fileNameMapPath)"
        }
        result += ">"
        return result
    }
}

final class PrecompileClangModuleDynamicTaskSpec: DynamicTaskSpec {
    private struct Payload: ClangModuleVerifierPayloadType {
        let serializedDiagnosticsPath: Path?
        let fileNameMapPath: Path?

        init(serializedDiagnosticsPath: Path?, fileNameMapPath: Path?) {
            self.serializedDiagnosticsPath = serializedDiagnosticsPath
            self.fileNameMapPath = fileNameMapPath
        }

        func serialize<T>(to serializer: T) where T : SWBUtil.Serializer {
            serializer.serializeAggregate(3) {
                serializer.serialize(serializedDiagnosticsPath)
                serializer.serialize(fileNameMapPath)
            }

        }

        init(from deserializer: any SWBUtil.Deserializer) throws {
            try deserializer.beginAggregate(2)
            self.serializedDiagnosticsPath = try deserializer.deserialize()
            self.fileNameMapPath = try deserializer.deserialize()
        }
    }

    package var payloadType: (any TaskPayload.Type)? {
        return Payload.self
    }

    package func buildExecutableTask(dynamicTask: DynamicTask, context: DynamicTaskOperationContext) -> any ExecutableTask {
        guard case let .precompileClangModule(PrecompileClangModuleTaskKey) = dynamicTask.taskKey else {
            fatalError("Unexpected dynamic task key \(dynamicTask.taskKey)")
        }

        precondition(dynamicTask.target == nil, "Precompiling modules should not be target specific.")
        // FIXME: Parsing the module identifier to reconstruct the module name is unfortunate.
        var moduleID = PrecompileClangModuleTaskKey.dependencyInfoPath.basenameWithoutSuffix.components(separatedBy: ":")[0]
        let isVerify = moduleID.hasSuffix("-verify")
        if isVerify {
            moduleID.removeLast("-verify".count)
        }
        assert(!isVerify || PrecompileClangModuleTaskKey.verifyingModule != nil)
        let moduleName = moduleID.prefix(upTo: moduleID.lastIndex(of: "-") ?? moduleID.endIndex)

        let ruleName: String, execDescription: String
        if isVerify, let verifyingModule = PrecompileClangModuleTaskKey.verifyingModule {
            ruleName = "VerifyPrecompileModule"
            if moduleName == "Test" {
                execDescription = "Verifying import of Clang module \(verifyingModule)"
            } else {
                assert(moduleName == verifyingModule || moduleName == "\(verifyingModule)_Private")
                execDescription = "Verifying compile of Clang module \(moduleName)"
            }
        } else {
            ruleName = "PrecompileModule"
            execDescription = "Compiling Clang module \(moduleName)"
        }

        return Task(
            type: self,
            dependencyData: .makefile(Path(PrecompileClangModuleTaskKey.dependencyInfoPath.withoutSuffix + ".d")),
            payload: Payload(serializedDiagnosticsPath: PrecompileClangModuleTaskKey.usesSerializedDiagnostics ? Path(PrecompileClangModuleTaskKey.dependencyInfoPath.withoutSuffix + ".dia") : nil, fileNameMapPath: PrecompileClangModuleTaskKey.fileNameMapPath),
            forTarget: dynamicTask.target,
            ruleInfo: [ruleName, PrecompileClangModuleTaskKey.dependencyInfoPath.str],
            commandLine: ["builtin-precompileModule", .literal(ByteString(encodingAsUTF8: PrecompileClangModuleTaskKey.dependencyInfoPath.str))],
            environment: dynamicTask.environment,
            workingDirectory: dynamicTask.workingDirectory,
            showEnvironment: dynamicTask.showEnvironment,
            execDescription: execDescription,
            isDynamic: true
        )
    }

    package func buildTaskAction(dynamicTaskKey: DynamicTaskKey, context: DynamicTaskOperationContext) -> TaskAction {
        guard case let .precompileClangModule(PrecompileClangModuleTaskKey) = dynamicTaskKey else {
            fatalError("Unexpected dynamic task key \(dynamicTaskKey)")
        }
        return PrecompileClangModuleTaskAction(key: PrecompileClangModuleTaskKey)
    }

    func customOutputParserType(for task: any ExecutableTask) -> (any TaskOutputParser.Type)? {
        guard (task.payload as? Payload)?.serializedDiagnosticsPath != nil else {
            return GenericOutputParser.self
        }
        if task.ruleInfo.first == "VerifyPrecompileModule" {
            return ClangModuleVerifierOutputParser.self
        }
        return SerializedDiagnosticsOutputParser.self
    }

    package func serializedDiagnosticsPaths(_ task: any ExecutableTask, _ fs: any FSProxy) -> [Path] {
        guard let path = (task.payload as? Payload)?.serializedDiagnosticsPath else {
            return []
        }
        return [path]
    }

    package func shouldStart(_ task: any ExecutableTask, buildCommand: BuildCommand) -> Bool {
        // A precompile task should always start if requested.
        return true
    }
}