File: ClangModuleVerifierInputGeneratorTaskAction.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 (184 lines) | stat: -rw-r--r-- 7,274 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
//===----------------------------------------------------------------------===//
//
// 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 SWBCore
import SWBUtil

public final class ClangModuleVerifierInputGeneratorTaskAction: TaskAction {

    public override class var toolIdentifier: String {
        return "modules-verifier-task"
    }

    private struct Options {
        var language: ModuleVerifierLanguage
        var inputFramework: Path
        var mainOutput: Path
        var headerOutput: Path
        var moduleMapOutput: Path

        init?(commandLine: some Sequence<String>, outputDelegate: any TaskOutputDelegate) {
            var iterator = commandLine.makeIterator()
            _ = iterator.next() // Skip argv[0]
            guard let inputFramework = iterator.next().map(Path.init) else {
                outputDelegate.emitError("no input framework specified")
                return nil
            }

            var language: ModuleVerifierLanguage? = nil
            var mainOutput: Path? = nil
            var headerOutput: Path? = nil
            var moduleMapOutput: Path? = nil

            while let arg = iterator.next() {
                switch arg {
                case "--language":
                    guard let languageName = iterator.next() else {
                        outputDelegate.emitError("missing argument to \(arg)")
                        return nil
                    }
                    language = ModuleVerifierLanguage(rawValue: languageName)
                    if language == nil {
                        outputDelegate.emitError("unrecognized language '\(languageName)'")
                        return nil
                    }
                case "--main-output":
                    guard let path = iterator.next() else {
                        outputDelegate.emitError("missing argument to \(arg)")
                        return nil
                    }
                    mainOutput = Path(path)
                case "--header-output":
                    guard let path = iterator.next() else {
                        outputDelegate.emitError("missing argument to \(arg)")
                        return nil
                    }
                    headerOutput = Path(path)
                case "--module-map-output":
                    guard let path = iterator.next() else {
                        outputDelegate.emitError("missing argument to \(arg)")
                        return nil
                    }
                    moduleMapOutput = Path(path)
                default:
                    outputDelegate.emitError("unknown argument '\(arg)'")
                    return nil
                }
            }

            if language == nil {
                outputDelegate.emitError("missing required argument --language")
                return nil
            }
            if mainOutput == nil {
                outputDelegate.emitError("missing required argument --main-output")
                return nil
            }
            if headerOutput == nil {
                outputDelegate.emitError("missing required argument --header-output")
                return nil
            }
            if moduleMapOutput == nil {
                outputDelegate.emitError("missing required argument --module-map-output")
                return nil
            }

            self.language = language!
            self.inputFramework = inputFramework
            self.mainOutput = mainOutput!
            self.headerOutput = headerOutput!
            self.moduleMapOutput = moduleMapOutput!
        }
    }

    public override func performTaskAction(_ task: any ExecutableTask, dynamicExecutionDelegate: any DynamicTaskExecutionDelegate, executionDelegate: any TaskExecutionDelegate, clientDelegate: any TaskExecutionClientDelegate, outputDelegate: any TaskOutputDelegate) async -> CommandResult {
        guard let options = Options(commandLine: task.commandLineAsStrings, outputDelegate: outputDelegate) else {
            return .failed
        }

        let specLookup = SpecLookupCtxt(specRegistry: executionDelegate.specRegistry, platform: nil)
        let fs = executionDelegate.fs

        let framework: ModuleVerifierFramework
        do {
            framework = try ModuleVerifierFramework(directory: options.inputFramework, fs: fs, inSDK: false, specLookupContext: specLookup)
        } catch {
            outputDelegate.emitError("failed to read framework '\(options.inputFramework.str)': \(error)")
            return .failed
        }

        let (verifyPublic, verifyPrivate, diagnostics) = ModuleVerifierModuleMapFileVerifier.verify(framework: framework)
        var hadError = false
        for diagnostic in diagnostics {
            outputDelegate.emit(diagnostic)
            if diagnostic.behavior == .error {
                hadError = true
            }
        }
        if hadError {
            return .failed
        }

        do {
            try fs.createDirectory(options.mainOutput.dirname, recursive: true)
            try fs.createDirectory(options.headerOutput.dirname, recursive: true)
            try fs.createDirectory(options.moduleMapOutput.dirname, recursive: true)
        } catch {
            outputDelegate.emitError("failed to create directory structure: \(error)")
            return .failed
        }

        do {
            try fs.write(options.mainOutput, contents: ByteString(encodingAsUTF8: """
            \(options.language.includeStatement) <Test/Test.h>
            """))
        } catch {
            outputDelegate.emitError("failed to write \(options.mainOutput): \(error)")
            return .failed
        }
        do {
            var output: ByteString = ""
            if verifyPublic {
                output += ByteString(encodingAsUTF8: framework.allModularHeaderIncludes(language: options.language))
            }
            if verifyPrivate && framework.hasPrivateHeaders {
                if !output.isEmpty {
                    output += "\n";
                }
                output += ByteString(encodingAsUTF8: """
                // Private
                \(framework.allModularPrivateHeaderIncludes(language: options.language))
                """)
            }

            try fs.write(options.headerOutput, contents: output)
        } catch {
            outputDelegate.emitError("failed to write \(options.headerOutput): \(error)")
            return .failed
        }
        do {
            try fs.write(options.moduleMapOutput, contents: """
            framework module Test {
                umbrella header "Test.h"

                export *
                module * { export * }
            }
            """)
        } catch {
            outputDelegate.emitError("failed to write \(options.moduleMapOutput): \(error)")
            return .failed
        }

        return .succeeded
    }
}