Skip to content

Commit feeb6f7

Browse files
authored
Add generateDependencyInfo to generate build dependency info (#869)
This allows to request build dependency info via the `swbuild` CLI.
1 parent 52250a8 commit feeb6f7

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Sources/SwiftBuild/ConsoleCommands/SWBServiceConsoleBuildCommand.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,35 @@ class SWBServiceConsolePrepareForIndexCommand: SWBServiceConsoleCommand {
345345
}
346346
}
347347

348+
class SWBServiceConsoleDependencyInfoCommand: SWBServiceConsoleCommand {
349+
public static let name = "generateDependencyInfo"
350+
351+
static func usage() -> String {
352+
return name + " [options] <container-path>"
353+
}
354+
355+
static func validate(invocation: SWBServiceConsoleCommandInvocation) -> SWBServiceConsoleError? {
356+
return nil
357+
}
358+
359+
static func perform(invocation: SWBServiceConsoleCommandInvocation) async -> SWBCommandResult {
360+
return await SWBServiceConsoleBuildCommand.perform(invocation: invocation, operationFunc: generateDependencyInfo)
361+
}
362+
}
363+
364+
fileprivate func generateDependencyInfo(startInfo: SwiftBuildMessage.BuildStartedInfo, session: SWBBuildServiceSession, sessionCreationDiagnostics: [SwiftBuildMessage.DiagnosticInfo], request: SWBBuildRequest) async -> SWBCommandResult {
365+
do {
366+
let output = try await withTemporaryDirectory(removeTreeOnDeinit: true) {
367+
let outputPath = $0.join("dump.json")
368+
try await session.dumpBuildDependencyInfo(for: request, to: outputPath.str)
369+
return try String(contentsOf: URL(fileURLWithPath: outputPath.str))
370+
}
371+
return .success(.init(output: output))
372+
} catch {
373+
return .failure(.failedCommandError(description: "\(error)"))
374+
}
375+
}
376+
348377
extension SWBWorkspaceInfo {
349378
func configuredTargets(targetNames: [String], parameters: SWBBuildParameters) throws -> [SWBConfiguredTarget] {
350379
return try targetNames.map { targetName in
@@ -499,6 +528,7 @@ func registerBuildCommands() {
499528
SWBServiceConsoleBuildCommand.self,
500529
SWBServiceConsolePrepareForIndexCommand.self,
501530
SWBServiceConsoleCreateBuildDescriptionCommand.self,
531+
SWBServiceConsoleDependencyInfoCommand.self,
502532
] as [any SWBServiceConsoleCommand.Type]) { SWBServiceConsoleCommandRegistry.registerCommandClass(commandClass) }
503533
}
504534

Sources/SwiftBuild/SWBBuildService.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,18 @@ public final class SWBBuildService: Sendable {
301301
}
302302
}
303303

304+
public func dumpBuildDependencyInfo(session: SWBBuildServiceSession, request: SWBBuildRequest, outputPath: String) async {
305+
let (channel, _): (UInt64, Bool) = await withCheckedContinuation { continuation in
306+
Task {
307+
let channel = self.openChannel { channel, message in
308+
}
309+
310+
_ = await send(DumpBuildDependencyInfoRequest(sessionHandle: session.name, responseChannel: channel, request: request.messagePayloadRepresentation, outputPath: outputPath))
311+
}
312+
}
313+
self.connection.close(channel: channel)
314+
}
315+
304316
@available(*, deprecated, message: "Do not use.")
305317
public func appleSystemFrameworkNames(developerPath: String?) async throws -> Set<String> {
306318
try await Set(send(request: AppleSystemFrameworkNamesRequest(developerPath: developerPath.map(Path.init))).value)

Tests/SwiftBuildTests/ConsoleCommands/GeneralCommandsTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fileprivate struct GeneralCommandsTests {
7878
dumpMsgPack\r
7979
dumpPID\r
8080
exit\r
81+
generateDependencyInfo\r
8182
headermap\r
8283
help\r
8384
isAlive\r
@@ -141,6 +142,7 @@ fileprivate struct GeneralCommandsTests {
141142
"dumpMsgPack",
142143
"dumpPID",
143144
"exit",
145+
"generateDependencyInfo",
144146
"headermap",
145147
"help",
146148
"isAlive",

0 commit comments

Comments
 (0)