Skip to content

Commit 0a1e082

Browse files
[Caching] Only implement async APIs when concurrency is available
SwiftPM is built with `-disable-implicit-concurrency-module-import` to avoid warnings about concurrency module on older SDKs. Explicitly importing concurrency and only vend async APIs when concurrency is available.
1 parent 1c5937f commit 0a1e082

File tree

1 file changed

+108
-92
lines changed

1 file changed

+108
-92
lines changed

Sources/SwiftDriver/SwiftScan/SwiftScanCAS.swift

Lines changed: 108 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
@_implementationOnly import CSwiftScan
1414
import struct Foundation.Data
1515

16+
// Swift Package Manager is building with `-disable-implicit-concurrency-module-import`
17+
// to avoid warnings on old SDKs. Explicity importing concurrency if available
18+
// and only adds async APIs when concurrency is available.
19+
#if canImport(_Concurrency)
20+
import _Concurrency
21+
#endif
22+
1623
public final class CachedCompilation {
1724
let ptr: swiftscan_cached_compilation_t
1825
private let lib: SwiftScan
@@ -30,38 +37,6 @@ public final class CachedCompilation {
3037
lib.api.swiftscan_cached_compilation_is_uncacheable(ptr)
3138
}
3239

33-
public func makeGlobal() async throws -> Bool {
34-
class CallbackContext {
35-
func retain() -> UnsafeMutableRawPointer {
36-
return Unmanaged.passRetained(self).toOpaque()
37-
}
38-
39-
let continuation: CheckedContinuation<Bool, Swift.Error>
40-
let comp: CachedCompilation
41-
init(_ continuation: CheckedContinuation<Bool, Swift.Error>, compilation: CachedCompilation) {
42-
self.continuation = continuation
43-
self.comp = compilation
44-
}
45-
}
46-
47-
func callbackFunc(_ context: UnsafeMutableRawPointer?, _ error: swiftscan_string_ref_t) {
48-
let obj = Unmanaged<CallbackContext>.fromOpaque(context!).takeRetainedValue()
49-
if error.length != 0 {
50-
if let err = try? obj.comp.lib.toSwiftString(error) {
51-
obj.continuation.resume(throwing: DependencyScanningError.casError(err))
52-
} else {
53-
obj.continuation.resume(throwing: DependencyScanningError.casError("unknown makeGlobal error"))
54-
}
55-
}
56-
obj.continuation.resume(returning: true)
57-
}
58-
59-
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Swift.Error>) in
60-
let context = CallbackContext(continuation, compilation: self)
61-
lib.api.swiftscan_cached_compilation_make_global_async(ptr, context.retain(), callbackFunc, nil)
62-
}
63-
}
64-
6540
deinit {
6641
lib.api.swiftscan_cached_compilation_dispose(ptr)
6742
}
@@ -103,38 +78,6 @@ public final class CachedOutput {
10378
}
10479
}
10580

106-
public func load() async throws -> Bool {
107-
class CallbackContext {
108-
func retain() -> UnsafeMutableRawPointer {
109-
return Unmanaged.passRetained(self).toOpaque()
110-
}
111-
112-
let continuation: CheckedContinuation<Bool, Swift.Error>
113-
let output: CachedOutput
114-
init(_ continuation: CheckedContinuation<Bool, Swift.Error>, output: CachedOutput) {
115-
self.continuation = continuation
116-
self.output = output
117-
}
118-
}
119-
120-
func callbackFunc(_ context: UnsafeMutableRawPointer?, _ success: Bool, _ error: swiftscan_string_ref_t) {
121-
let obj = Unmanaged<CallbackContext>.fromOpaque(context!).takeRetainedValue()
122-
if error.length != 0 {
123-
if let err = try? obj.output.lib.toSwiftString(error) {
124-
obj.continuation.resume(throwing: DependencyScanningError.casError(err))
125-
} else {
126-
obj.continuation.resume(throwing: DependencyScanningError.casError("unknown output loading error"))
127-
}
128-
}
129-
obj.continuation.resume(returning: success)
130-
}
131-
132-
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Swift.Error>) in
133-
let context = CallbackContext(continuation, output: self)
134-
lib.api.swiftscan_cached_output_load_async(ptr, context.retain(), callbackFunc, nil)
135-
}
136-
}
137-
13881
public var isMaterialized: Bool {
13982
lib.api.swiftscan_cached_output_is_materialized(ptr)
14083
}
@@ -260,6 +203,106 @@ public final class SwiftScanCAS {
260203
return convert(compilation: result)
261204
}
262205

206+
public func replayCompilation(instance: CacheReplayInstance, compilation: CachedCompilation) throws -> CacheReplayResult {
207+
let result = try scanner.handleCASError { err_msg in
208+
scanner.api.swiftscan_cache_replay_compilation(instance.ptr, compilation.ptr, &err_msg)
209+
}
210+
guard let res = convert(result: result) else {
211+
throw DependencyScanningError.casError("unexpected nil for cache_replay_result")
212+
}
213+
return res
214+
}
215+
}
216+
217+
extension swiftscan_cached_compilation_t {
218+
func convert(_ lib: SwiftScan) -> CachedCompilation {
219+
return CachedCompilation(self, lib: lib)
220+
}
221+
}
222+
223+
extension swiftscan_cache_replay_instance_t {
224+
func convert(_ lib: SwiftScan) -> CacheReplayInstance {
225+
return CacheReplayInstance(self, lib: lib)
226+
}
227+
}
228+
229+
extension swiftscan_cache_replay_result_t {
230+
func convert(_ lib: SwiftScan) -> CacheReplayResult {
231+
return CacheReplayResult(self, lib: lib)
232+
}
233+
}
234+
235+
#if canImport(_Concurrency)
236+
// Async API Vendor
237+
extension CachedCompilation {
238+
public func makeGlobal() async throws -> Bool {
239+
class CallbackContext {
240+
func retain() -> UnsafeMutableRawPointer {
241+
return Unmanaged.passRetained(self).toOpaque()
242+
}
243+
244+
let continuation: CheckedContinuation<Bool, Swift.Error>
245+
let comp: CachedCompilation
246+
init(_ continuation: CheckedContinuation<Bool, Swift.Error>, compilation: CachedCompilation) {
247+
self.continuation = continuation
248+
self.comp = compilation
249+
}
250+
}
251+
252+
func callbackFunc(_ context: UnsafeMutableRawPointer?, _ error: swiftscan_string_ref_t) {
253+
let obj = Unmanaged<CallbackContext>.fromOpaque(context!).takeRetainedValue()
254+
if error.length != 0 {
255+
if let err = try? obj.comp.lib.toSwiftString(error) {
256+
obj.continuation.resume(throwing: DependencyScanningError.casError(err))
257+
} else {
258+
obj.continuation.resume(throwing: DependencyScanningError.casError("unknown makeGlobal error"))
259+
}
260+
}
261+
obj.continuation.resume(returning: true)
262+
}
263+
264+
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Swift.Error>) in
265+
let context = CallbackContext(continuation, compilation: self)
266+
lib.api.swiftscan_cached_compilation_make_global_async(ptr, context.retain(), callbackFunc, nil)
267+
}
268+
}
269+
}
270+
271+
extension CachedOutput {
272+
public func load() async throws -> Bool {
273+
class CallbackContext {
274+
func retain() -> UnsafeMutableRawPointer {
275+
return Unmanaged.passRetained(self).toOpaque()
276+
}
277+
278+
let continuation: CheckedContinuation<Bool, Swift.Error>
279+
let output: CachedOutput
280+
init(_ continuation: CheckedContinuation<Bool, Swift.Error>, output: CachedOutput) {
281+
self.continuation = continuation
282+
self.output = output
283+
}
284+
}
285+
286+
func callbackFunc(_ context: UnsafeMutableRawPointer?, _ success: Bool, _ error: swiftscan_string_ref_t) {
287+
let obj = Unmanaged<CallbackContext>.fromOpaque(context!).takeRetainedValue()
288+
if error.length != 0 {
289+
if let err = try? obj.output.lib.toSwiftString(error) {
290+
obj.continuation.resume(throwing: DependencyScanningError.casError(err))
291+
} else {
292+
obj.continuation.resume(throwing: DependencyScanningError.casError("unknown output loading error"))
293+
}
294+
}
295+
obj.continuation.resume(returning: success)
296+
}
297+
298+
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Swift.Error>) in
299+
let context = CallbackContext(continuation, output: self)
300+
lib.api.swiftscan_cached_output_load_async(ptr, context.retain(), callbackFunc, nil)
301+
}
302+
}
303+
}
304+
305+
extension SwiftScanCAS {
263306
public func queryCacheKey(_ key: String, globally: Bool) async throws -> CachedCompilation? {
264307
class CallbackContext {
265308
func retain() -> UnsafeMutableRawPointer {
@@ -291,32 +334,5 @@ public final class SwiftScanCAS {
291334
scanner.api.swiftscan_cache_query_async(cas, key.cString(using: .utf8), globally, context.retain(), callbackFunc, nil)
292335
}
293336
}
294-
295-
public func replayCompilation(instance: CacheReplayInstance, compilation: CachedCompilation) throws -> CacheReplayResult {
296-
let result = try scanner.handleCASError { err_msg in
297-
scanner.api.swiftscan_cache_replay_compilation(instance.ptr, compilation.ptr, &err_msg)
298-
}
299-
guard let res = convert(result: result) else {
300-
throw DependencyScanningError.casError("unexpected nil for cache_replay_result")
301-
}
302-
return res
303-
}
304-
}
305-
306-
extension swiftscan_cached_compilation_t {
307-
func convert(_ lib: SwiftScan) -> CachedCompilation {
308-
return CachedCompilation(self, lib: lib)
309-
}
310-
}
311-
312-
extension swiftscan_cache_replay_instance_t {
313-
func convert(_ lib: SwiftScan) -> CacheReplayInstance {
314-
return CacheReplayInstance(self, lib: lib)
315-
}
316-
}
317-
318-
extension swiftscan_cache_replay_result_t {
319-
func convert(_ lib: SwiftScan) -> CacheReplayResult {
320-
return CacheReplayResult(self, lib: lib)
321-
}
322337
}
338+
#endif

0 commit comments

Comments
 (0)