Skip to content

Commit 7f5a8d2

Browse files
[Caching] Add an async download API from CASID
Add an API to download CASObject using CASID. This allows better task control from build system.
1 parent 3a73847 commit 7f5a8d2

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Sources/CSwiftScan/include/swiftscan_header.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ typedef struct {
332332
void (*swiftscan_cache_cancellation_token_dispose)(
333333
swiftscan_cache_cancellation_token_t);
334334

335+
void (*swiftscan_cache_download_cas_object_async)(
336+
swiftscan_cas_t, const char *id, void *ctx,
337+
void (*callback)(void *ctx, bool success, swiftscan_string_ref_t error),
338+
swiftscan_cache_cancellation_token_t *);
339+
335340
swiftscan_cache_replay_instance_t (*swiftscan_cache_replay_instance_create)(
336341
int argc, const char **argv, swiftscan_string_ref_t *error);
337342
void (*swiftscan_cache_replay_instance_dispose)(

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ private extension swiftscan_functions_t {
528528
self.swiftscan_cache_action_cancel = try loadOptional("swiftscan_cache_action_cancel")
529529
self.swiftscan_cache_cancellation_token_dispose = try loadOptional("swiftscan_cache_cancellation_token_dispose")
530530

531+
self.swiftscan_cache_download_cas_object_async = try loadOptional("swiftscan_cache_download_cas_object_async")
532+
531533
self.swiftscan_cache_replay_instance_create = try loadOptional("swiftscan_cache_replay_instance_create")
532534
self.swiftscan_cache_replay_instance_dispose = try loadOptional("swiftscan_cache_replay_instance_dispose")
533535
self.swiftscan_cache_replay_compilation = try loadOptional("swiftscan_cache_replay_compilation")

Sources/SwiftDriver/SwiftScan/SwiftScanCAS.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,37 @@ extension SwiftScanCAS {
334334
scanner.api.swiftscan_cache_query_async(cas, key.cString(using: .utf8), globally, context.retain(), callbackFunc, nil)
335335
}
336336
}
337+
338+
public func download(with id: String) async throws -> Bool {
339+
class CallbackContext {
340+
func retain() -> UnsafeMutableRawPointer {
341+
return Unmanaged.passRetained(self).toOpaque()
342+
}
343+
344+
let continuation: CheckedContinuation<Bool, Swift.Error>
345+
let cas: SwiftScanCAS
346+
init(_ continuation: CheckedContinuation<Bool, Swift.Error>, cas: SwiftScanCAS) {
347+
self.continuation = continuation
348+
self.cas = cas
349+
}
350+
}
351+
352+
func callbackFunc(_ context: UnsafeMutableRawPointer?, _ success: Bool, _ error: swiftscan_string_ref_t) {
353+
let obj = Unmanaged<CallbackContext>.fromOpaque(context!).takeRetainedValue()
354+
if error.length != 0 {
355+
if let err = try? obj.cas.scanner.toSwiftString(error) {
356+
obj.continuation.resume(throwing: DependencyScanningError.casError(err))
357+
} else {
358+
obj.continuation.resume(throwing: DependencyScanningError.casError("unknown output loading error"))
359+
}
360+
}
361+
obj.continuation.resume(returning: success)
362+
}
363+
364+
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Swift.Error>) in
365+
let context = CallbackContext(continuation, cas: self)
366+
scanner.api.swiftscan_cache_download_cas_object_async(cas, id.cString(using: .utf8), context.retain(), callbackFunc, nil)
367+
}
368+
}
337369
}
338370
#endif

0 commit comments

Comments
 (0)