File: SWBServiceConsoleBuildCommand.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 (507 lines) | stat: -rw-r--r-- 23,314 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
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
//===----------------------------------------------------------------------===//
//
// 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 SWBProtocol
import SWBUtil

class SWBServiceConsoleBuildCommand: SWBServiceConsoleCommand {
    static let name = "build"

    static func usage() -> String {
        return name + " [options] <container-path>"
    }

    static func validate(invocation: SWBServiceConsoleCommandInvocation) -> SWBServiceConsoleError? {
        return nil
    }

    static func perform(invocation: SWBServiceConsoleCommandInvocation) async -> SWBCommandResult {
        // Parse the arguments.
        var positionalArgs = [String]()
        var configuredTargetNames = [String]()
        var configurationName: String? = nil
        var actionName: String? = nil
        var buildRequestFile: Path? = nil
        var buildParametersFile: Path? = nil
        var derivedDataPath: Path? = nil

        var iterator = invocation.commandLine.makeIterator()
        _ = iterator.next()
        while let arg = iterator.next() {
            switch arg {
            case "--action":
                guard let name = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }

                actionName = name

            case "--target":
                guard let name = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                configuredTargetNames.append(name)

            case "--configuration":
                guard let name = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                configurationName = name

            case "--derivedDataPath":
                guard let path = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                derivedDataPath = Path(path)

            case "--buildRequestFile":
                guard let path = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                buildRequestFile = Path(path)

            case "--buildParametersFile":
                guard let path = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                buildParametersFile = Path(path)

            case _ where arg.hasPrefix("-"):
                // Skip single-dash arguments so they can be interpreted as user defaults, but only with two or more non-dash characters (to avoid conflicting with the POSIX single-argument convention for arguments like -j).
                if arg.count > 2 && !arg.hasPrefix("--") {
                    guard iterator.next() != nil else {
                        return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                    }
                    break
                }

                return .failure(.invalidCommandError(description: "error: unknown argument \(arg)"))

            default:
                positionalArgs.append(arg)
            }
        }
        if positionalArgs.count != 1 {
            return .failure(.invalidCommandError(description: "usage: " + usage() + "\n"))
        }

        let containerPath = Path(positionalArgs[0])

        return await invocation.console.service.withSession(sessionName: containerPath.str) { session, diagnostics in
            let baseDirectory: AbsolutePath
            do {
                baseDirectory = try AbsolutePath(validating: Path.currentDirectory.str)
            } catch {
                return .failure(.failedCommandError(description: error.localizedDescription))
            }

            // Load the workspace.
            do {
                try await session.loadWorkspace(containerPath: containerPath.str, baseDirectory: baseDirectory)
            } catch {
                return .failure(SWBServiceConsoleError(error))
            }

            // Construct the build parameters.
            var parameters = SWBBuildParameters()

            // If a serialized build parameters file was supplied, use that.
            // More specific command line arguments for the action or configuration
            // will override the contents of those fields within the file if given.
            if let buildParametersFile {
                do {
                    parameters = try JSONDecoder().decode(SWBBuildParameters.self, from: buildParametersFile, fs: localFS)
                } catch {
                    return .failure(.failedCommandError(description: "error: unable to load --buildParametersFile ('\(buildParametersFile.str)'): \(error)"))
                }
            }

            // Default to `build`, if no action was given.
            if let actionName {
                parameters.action = actionName
            } else if parameters.action == nil {
                parameters.action = "build"
            }

            // If we don't have a build configuration at this point try to set a meaningful default
            if parameters.configurationName == nil {
                parameters.configurationName = configurationName ?? "Debug"
            }

            // Find the targets to build.
            let configuredTargets: [SWBConfiguredTarget]
            do {
                let workspaceInfo = try await session.workspaceInfo()
                configuredTargets = try workspaceInfo.configuredTargets(targetNames: configuredTargetNames, parameters: parameters)
            } catch {
                return .failure(.failedCommandError(description: error.localizedDescription))
            }

            // Create and configure a build request.
            var request = SWBBuildRequest()
            request.parameters = parameters
            request.configuredTargets = configuredTargets
            request.useParallelTargets = true
            request.useImplicitDependencies = false
            request.useDryRun = false
            request.hideShellScriptEnvironment = true
            request.showNonLoggedProgress = true

            // If a serialized build request file was supplied, use that.
            // This is higher precedence than everything else, and entirely overrides
            // any command line options pertaining to the build parameters or the
            // targets to build.
            if let buildRequestFile {
                do {
                    request = try JSONDecoder().decode(SWBBuildRequest.self, from: buildRequestFile, fs: localFS)
                } catch {
                    return .failure(.failedCommandError(description: "error: unable to load --buildRequestFile ('\(buildRequestFile.str)'): \(error)"))
                }
            }

            // Override the arena, if requested.
            // This is the absolute highest precedence option, and even overrides the arena info
            // of the serialized build request file above, if one was given. 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 let path = derivedDataPath {
                request.setDerivedDataPath(path)
            }

            let absoluteDerivedDataPath: AbsolutePath?
            do {
                absoluteDerivedDataPath = try (request.parameters.arenaInfo?.derivedDataPath).map { try AbsolutePath(validating: $0) } ?? nil
            } catch {
                return .failure(.failedCommandError(description: error.localizedDescription))
            }

            return await doBuildOperation(startInfo: .init(baseDirectory: baseDirectory, derivedDataPath: absoluteDerivedDataPath), session: session, sessionCreationDiagnostics: diagnostics, request: request)
        }
    }
}

class SWBServiceConsolePrepareForIndexCommand: SWBServiceConsoleCommand {
    static let name = "prepareForIndex"

    static func usage() -> String {
        return name + " [options] <container-path>"
    }

    static func validate(invocation: SWBServiceConsoleCommandInvocation) -> SWBServiceConsoleError? {
        return nil
    }

    static func perform(invocation: SWBServiceConsoleCommandInvocation) async -> SWBCommandResult {
        // Parse the arguments.
        var positionalArgs = [String]()
        var configuredTargetNames = [String]()
        var prepareTargetNames = [String]()
        var configurationName: String? = nil
        var derivedDataPath: Path? = nil

        var iterator = invocation.commandLine.makeIterator()
        _ = iterator.next()
        while let arg = iterator.next() {
            switch arg {
            case "--target":
                guard let name = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                configuredTargetNames.append(name)

            case "--prepare":
                guard let name = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                prepareTargetNames.append(name)

            case "--configuration":
                guard let name = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                configurationName = name

            case "--derivedDataPath":
                guard let path = iterator.next() else {
                    return .failure(.invalidCommandError(description: "error: missing argument for \(arg)"))
                }
                derivedDataPath = Path(path)

            case _ where arg.hasPrefix("-"):
                return .failure(.invalidCommandError(description: "error: unknown argument \(arg)"))

            default:
                positionalArgs.append(arg)
            }
        }
        if positionalArgs.count != 1 {
            return .failure(.invalidCommandError(description: "usage: " + usage() + "\n"))
        }

        let containerPath = Path(positionalArgs[0])

        return await invocation.console.service.withSession(sessionName: containerPath.str) { session, diagnostics in
            let baseDirectory: AbsolutePath
            do {
                baseDirectory = try AbsolutePath(validating: Path.currentDirectory.str)
            } catch {
                return .failure(.failedCommandError(description: error.localizedDescription))
            }

            // Load the workspace.
            do {
                try await session.loadWorkspace(containerPath: containerPath.str, baseDirectory: baseDirectory)
            } catch {
                return .failure(SWBServiceConsoleError(error))
            }

            // Construct the build parameters.
            var parameters = SWBBuildParameters()
            parameters.action = "build"

            // If we don't have a build configuration at this point try to set a meaningful default
            if parameters.configurationName == nil {
                parameters.configurationName = configurationName ?? "Debug"
            }

            // Find the targets to build.
            let configuredTargets: [SWBConfiguredTarget]
            var prepareTargets: [String]?
            do {
                let workspaceInfo = try await session.workspaceInfo()
                configuredTargets = try workspaceInfo.configuredTargets(targetNames: configuredTargetNames, parameters: parameters)

                if !prepareTargetNames.isEmpty {
                    do {
                        prepareTargets = try workspaceInfo.configuredTargets(targetNames: configuredTargetNames, parameters: parameters).map(\.guid)
                    } catch {
                        return .failure(.failedCommandError(description: error.localizedDescription))
                    }
                }
            } catch {
                return .failure(.failedCommandError(description: error.localizedDescription))
            }

            // Create and configure a build request.
            var request = SWBBuildRequest()
            request.buildCommand = .prepareForIndexing(buildOnlyTheseTargets: prepareTargets, enableIndexBuildArena: false)
            request.parameters = parameters
            request.configuredTargets = configuredTargets
            request.useParallelTargets = true
            request.useImplicitDependencies = true
            request.useDryRun = false
            request.hideShellScriptEnvironment = true
            request.showNonLoggedProgress = false
            request.continueBuildingAfterErrors = true

            // Override the arena, if requested.
            if let path = derivedDataPath {
                request.setDerivedDataPath(path)
            }

            let absoluteDerivedDataPath: AbsolutePath?
            do {
                absoluteDerivedDataPath = try (request.parameters.arenaInfo?.derivedDataPath).map { try AbsolutePath(validating: $0) } ?? nil
            } catch {
                return .failure(.failedCommandError(description: error.localizedDescription))
            }

            return await doBuildOperation(startInfo: .init(baseDirectory: baseDirectory, derivedDataPath: absoluteDerivedDataPath), session: session, sessionCreationDiagnostics: diagnostics, request: request)
        }
    }
}

extension SWBWorkspaceInfo {
    func configuredTargets(targetNames: [String], parameters: SWBBuildParameters) throws -> [SWBConfiguredTarget] {
        return try targetNames.map { targetName in
            let infos = targetInfos.filter { $0.targetName == targetName }
            switch infos.count {
            case 0:
                throw SwiftBuildError.requestError(description: "Could not find target named '\(targetName)'")
            case 1:
                return SWBConfiguredTarget(guid: infos[0].guid, parameters: parameters)
            default:
                throw SwiftBuildError.requestError(description: "Found multiple targets named '\(targetName)'")
            }
        }
    }
}

extension SWBBuildServiceSession {
    fileprivate func loadWorkspace(containerPath: String, baseDirectory: AbsolutePath) async throws {
        // Make the container path absolute, since the build service process may not have the same working directory as the command line tool.
        let absoluteContainerPath = Path(containerPath).makeAbsolute(relativeTo: Path(baseDirectory.pathString))?.normalize().str ?? containerPath

        return try await loadWorkspace(containerPath: absoluteContainerPath)
    }
}

extension SWBBuildService {
    /// Creates a session with the specified name and runs the `block`. The session is guaranteed to be closed once `block` returns.
    fileprivate func withSession(sessionName: String, _ block: (_ session: SWBBuildServiceSession, _ diagnostics: [SwiftBuildMessage.DiagnosticInfo]) async -> SWBCommandResult) async -> SWBCommandResult {
        let session: SWBBuildServiceSession
        let diagnostics: [SwiftBuildMessage.DiagnosticInfo]
        switch await createSession(name: sessionName, cachePath: nil, inferiorProductsPath: nil, environment: nil) {
        case let (.success(s), d):
            session = s
            diagnostics = d
            let result = await block(session, diagnostics)
            do {
                try await session.close()
            } catch {
                return .failure(.failedCommandError(description: "error: failed to close session: \(error)"))
            }
            return result
        case let (.failure(error), diagnostics):
            return .failure(SWBServiceConsoleError(error, diagnostics))
        }
    }
}

fileprivate func doBuildOperation(startInfo: SwiftBuildMessage.BuildStartedInfo, session: SWBBuildServiceSession, sessionCreationDiagnostics: [SwiftBuildMessage.DiagnosticInfo], request: SWBBuildRequest) async -> SWBCommandResult {
    let systemInfo: SWBSystemInfo
    do {
        systemInfo = try .default()
    } catch {
        return .failure(.failedCommandError(description: error.localizedDescription))
    }

    do {
        try await session.setSystemInfo(systemInfo)
    } catch {
        return .failure(.failedCommandError(description: error.localizedDescription))
    }

    // Also initialize the user info.
    do {
        try await session.setUserInfo(.default)
    } catch {
        return .failure(.failedCommandError(description: error.localizedDescription))
    }

    // FIXME: We need to be able to abstract the console output.
    let stdoutHandle = FileHandle.standardOutput

    func emitEvent(_ message: SwiftBuildMessage) throws {
        let stream = OutputByteStream()
        try stream.write(JSONEncoder(outputFormatting: [.sortedKeys, .withoutEscapingSlashes]).encode(message))
        stream.write("\n")

        // Emit using the same encoding as the Swift compiler streaming JSON structured output.
        //
        // NOTE: The count doesn't include the trailing newline.
        let payload = stream.bytes
        try stdoutHandle.write(contentsOf: Data("\(payload.count - 1)\n".utf8))
        try stdoutHandle.write(contentsOf: Data(payload.bytes))
    }

    // Start a build operation.  We set ourself as the delegate, so we will hear about output and completion.
    do {
        let operation = try await session.createBuildOperation(request: request, delegate: PlanningOperationDelegate())
        for try await event in try await operation.start() {
            switch event {
            case .buildStarted:
                // FIXME: We override the startInfo because the lower layers fill it in with bogus info since we don't normally have the container base path available there. Eventually this should be handled there instead, though.
                try emitEvent(.buildStarted(startInfo))
            default:
                try emitEvent(event)
            }
        }

        switch operation.state {
        case .succeeded:
            return .success(SWBServiceConsoleResult(output: sessionCreationDiagnostics.map { "\($0.kind.rawValue): \($0.message)" }.joined(separator: "\n")))
        case .failed:
            return .failure(.failedCommandError(description: "error: build failed"))
        case .cancelled:
            return .failure(.failedCommandError(description: "error: build was cancelled"))
        case .requested, .running, .aborted:
            return .failure(.failedCommandError(description: "error: unexpected build state"))
        }
    } catch {
        return .failure(.failedCommandError(description: error.localizedDescription))
    }
}

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
    }
}

func registerBuildCommands() {
    for commandClass in ([
        SWBServiceConsoleBuildCommand.self,
        SWBServiceConsolePrepareForIndexCommand.self
    ] as [any SWBServiceConsoleCommand.Type]) { SWBServiceConsoleCommandRegistry.registerCommandClass(commandClass) }
}

extension SwiftBuildMessage.BuildCompletedInfo.Result {
    init(_ state: SWBBuildOperationState) {
        switch state {
        case .requested, .running:
            preconditionFailure()
        case .succeeded:
            self = .ok
        case .failed:
            self = .failed
        case .cancelled:
            self = .cancelled
        case .aborted:
            self = .aborted
        }
    }
}

extension SWBBuildRequest {
    mutating func setDerivedDataPath(_ derivedDataPath: Path) {
        let arenaInfo = SWBArenaInfo(
            derivedDataPath: derivedDataPath.str,
            buildProductsPath: derivedDataPath.join("Products").str,
            buildIntermediatesPath: derivedDataPath.join("Intermediates.noindex").str,
            pchPath: derivedDataPath.str,
            indexRegularBuildProductsPath: nil,
            indexRegularBuildIntermediatesPath: nil,
            indexPCHPath: derivedDataPath.str,
            indexDataStoreFolderPath: derivedDataPath.str,
            indexEnableDataStore: parameters.arenaInfo?.indexEnableDataStore ?? false)

        parameters.arenaInfo = arenaInfo
        configuredTargets = configuredTargets.map { configuredTarget in
            var configuredTarget = configuredTarget
            configuredTarget.parameters?.arenaInfo = arenaInfo
            return configuredTarget
        }
    }
}