Skip to content

Commit 64da735

Browse files
committed
Add request to get preferred run destination for a platform
This can be useful when implementing a system that needs to configure the active run destination of a build request but only knows the platform that the user is requesting a build for, but no architecture etc.
1 parent 7adc227 commit 64da735

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Sources/SWBBuildService/Messages.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,25 @@ private struct ClearAllCaches: MessageHandler {
15281528
}
15291529
}
15301530

1531+
private struct GetPlatformPreferredRunDestinationMsg: MessageHandler {
1532+
struct PlatformNotFoundError: Error, CustomStringConvertible {
1533+
let platform: String
1534+
var description: String { "Unknown platform '\(platform)'" }
1535+
}
1536+
1537+
func handle(request: Request, message: GetPlatformPreferredRunDestinationRequest) async throws -> GetPlatformPreferredRunDestinationResponse {
1538+
let session = try request.session(for: message)
1539+
guard let platform = session.core.platformRegistry.lookup(name: message.platform) else {
1540+
throw PlatformNotFoundError(platform: message.platform)
1541+
}
1542+
// All relevant platforms define a preferredArch, so the undefined_arch fallback case should never happen
1543+
// in practice, and indicates a serious issue occurred during plugin loading.
1544+
let targetArchitecture = platform.preferredArch ?? "undefined_arch"
1545+
let runDestination = RunDestinationInfo(platform: platform.name, sdk: platform.name, sdkVariant: nil, targetArchitecture: targetArchitecture, supportedArchitectures: [targetArchitecture], disableOnlyActiveArch: false, hostTargetedPlatform: nil)
1546+
return GetPlatformPreferredRunDestinationResponse(info: runDestination)
1547+
}
1548+
}
1549+
15311550
// MARK: ServiceExtension Support
15321551

15331552
public struct ServiceSessionMessageHandlers: ServiceExtension {
@@ -1593,6 +1612,7 @@ package struct ServiceMessageHandlers: ServiceExtension {
15931612
service.registerMessageHandler(GetStatisticsDumpMsg.self)
15941613
service.registerMessageHandler(GetBuildSettingsDescriptionDumpMsg.self)
15951614
service.registerMessageHandler(ExecuteCommandLineToolMsg.self)
1615+
service.registerMessageHandler(GetPlatformPreferredRunDestinationMsg.self)
15961616

15971617
service.registerMessageHandler(CreateXCFrameworkHandler.self)
15981618

Sources/SWBProtocol/Message.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public struct PingRequest: RequestMessage, Equatable {
6565

6666
public struct SetConfigItemRequest: RequestMessage, Equatable {
6767
public typealias ResponseMessage = VoidResponse
68-
68+
6969
public static let name = "SET_CONFIG_ITEM"
7070

7171
public let key: String
@@ -286,6 +286,33 @@ public struct GetBuildSettingsDescriptionRequest: SessionMessage, RequestMessage
286286
}
287287
}
288288

289+
/// Get the preferred run destination for a given platform
290+
public struct GetPlatformPreferredRunDestinationRequest: SessionMessage, RequestMessage, Equatable, SerializableCodable {
291+
public typealias ResponseMessage = GetPlatformPreferredRunDestinationResponse
292+
293+
public static let name = "GET_PLATFORM_PREFERRED_RUN_DESTINATION_REQUEST"
294+
295+
public let sessionHandle: String
296+
297+
/// The name of the platform to get the run destination for, eg. `macosx`, `iphoneos`, `iphonesimulator`
298+
public let platform: String
299+
300+
public init(sessionHandle: String, platform: String) {
301+
self.sessionHandle = sessionHandle
302+
self.platform = platform
303+
}
304+
}
305+
306+
public struct GetPlatformPreferredRunDestinationResponse: Message, Equatable, SerializableCodable {
307+
public static let name = "GET_PLATFORM_PREFERRED_RUN_DESTINATION_RESPONSE"
308+
309+
public let info: RunDestinationInfo
310+
311+
public init(info: RunDestinationInfo) {
312+
self.info = info
313+
}
314+
}
315+
289316
public struct CreateXCFrameworkRequest: RequestMessage, Equatable, SerializableCodable {
290317
public typealias ResponseMessage = StringResponse
291318

@@ -1171,6 +1198,9 @@ public struct IPCMessage: Serializable, Sendable {
11711198
GetBuildSettingsDescriptionRequest.self,
11721199
ExecuteCommandLineToolRequest.self,
11731200

1201+
GetPlatformPreferredRunDestinationRequest.self,
1202+
GetPlatformPreferredRunDestinationResponse.self,
1203+
11741204
CreateSessionRequest.self,
11751205
CreateSessionResponse.self,
11761206
SetSessionSystemInfoRequest.self,

Sources/SwiftBuild/SWBBuildServiceSession.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ public final class SWBBuildServiceSession: Sendable {
582582
try await service.send(request: DeveloperPathRequest(sessionHandle: uid)).value
583583
}
584584

585+
public func preferredRunDestination(forPlatform platformName: String) async throws -> RunDestinationInfo? {
586+
return try await service.send(request: GetPlatformPreferredRunDestinationRequest(sessionHandle: uid, platform: platformName)).info
587+
}
588+
585589
/// Set the session system information.
586590
public func setSystemInfo(_ systemInfo: SWBSystemInfo) async throws {
587591
_ = try await service.send(request: SetSessionSystemInfoRequest(sessionHandle: uid, operatingSystemVersion: Version(systemInfo.operatingSystemVersion), productBuildVersion: systemInfo.productBuildVersion, nativeArchitecture: systemInfo.nativeArchitecture))

0 commit comments

Comments
 (0)