@@ -25,7 +25,7 @@ public enum DependencyScanningError: Error, DiagnosticData {
25
25
case missingRequiredSymbol( String )
26
26
case dependencyScanFailed
27
27
case failedToInstantiateScanner
28
- case failedToInstantiateCAS
28
+ case casError ( String )
29
29
case missingField( String )
30
30
case moduleNameDecodeFailure( String )
31
31
case unsupportedDependencyDetailsKind( Int )
@@ -42,8 +42,8 @@ public enum DependencyScanningError: Error, DiagnosticData {
42
42
return " libSwiftScan dependency scan query failed "
43
43
case . failedToInstantiateScanner:
44
44
return " libSwiftScan failed to create scanner instance "
45
- case . failedToInstantiateCAS :
46
- return " libSwiftScan failed to create CAS "
45
+ case . casError ( let reason ) :
46
+ return " libSwiftScan CAS error: \( reason ) "
47
47
case . missingField( let fieldName) :
48
48
return " libSwiftScan scan result missing required field: ` \( fieldName) ` "
49
49
case . moduleNameDecodeFailure( let encodedName) :
@@ -285,7 +285,12 @@ internal extension swiftscan_diagnostic_severity_t {
285
285
// Caching is currently not supported on Windows hosts.
286
286
return false
287
287
#else
288
- return api. swiftscan_cas_create != nil &&
288
+ return api. swiftscan_cas_options_create != nil &&
289
+ api. swiftscan_cas_options_dispose != nil &&
290
+ api. swiftscan_cas_options_set_ondisk_path != nil &&
291
+ api. swiftscan_cas_options_set_plugin_path != nil &&
292
+ api. swiftscan_cas_options_set_option != nil &&
293
+ api. swiftscan_cas_create_from_options != nil &&
289
294
api. swiftscan_cas_dispose != nil &&
290
295
api. swiftscan_compute_cache_key != nil &&
291
296
api. swiftscan_cas_store != nil &&
@@ -387,20 +392,48 @@ internal extension swiftscan_diagnostic_severity_t {
387
392
}
388
393
}
389
394
390
- func createCAS( casPath: String ) throws {
391
- self . cas = api. swiftscan_cas_create ( casPath. cString ( using: String . Encoding. utf8) )
392
- guard self . cas != nil else {
393
- throw DependencyScanningError . failedToInstantiateCAS
395
+ private func handleCASError( _ closure: ( inout swiftscan_string_ref_t ) -> Bool ) throws {
396
+ var err_msg : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
397
+ guard !closure( & err_msg) else {
398
+ let err_str = try toSwiftString ( err_msg)
399
+ api. swiftscan_string_dispose ( err_msg)
400
+ throw DependencyScanningError . casError ( err_str)
401
+ }
402
+ }
403
+
404
+ func createCAS( pluginPath: String ? , onDiskPath: String ? , pluginOptions: [ ( String , String ) ] ) throws {
405
+ let casOpts = api. swiftscan_cas_options_create ( )
406
+ defer {
407
+ api. swiftscan_cas_options_dispose ( casOpts)
408
+ }
409
+ if let path = pluginPath {
410
+ api. swiftscan_cas_options_set_plugin_path ( casOpts, path)
411
+ }
412
+ if let path = onDiskPath {
413
+ api. swiftscan_cas_options_set_ondisk_path ( casOpts, path)
414
+ }
415
+ for (name, value) in pluginOptions {
416
+ try handleCASError { err_msg in
417
+ return api. swiftscan_cas_options_set_option ( casOpts, name, value, & err_msg)
418
+ }
419
+ }
420
+ try handleCASError { err_msg in
421
+ self . cas = api. swiftscan_cas_create_from_options ( casOpts, & err_msg)
422
+ return self . cas == nil
394
423
}
395
424
}
396
425
397
426
func store( data: Data ) throws -> String {
398
427
guard let scan_cas = self . cas else {
399
- throw DependencyScanningError . failedToInstantiateCAS
428
+ throw DependencyScanningError . casError ( " cannot store into CAS because CAS is not yet created " )
400
429
}
401
430
let bytes = UnsafeMutablePointer< UInt8> . allocate( capacity: data. count)
402
431
data. copyBytes ( to: bytes, count: data. count)
403
- let casid = api. swiftscan_cas_store ( scan_cas, bytes, UInt32 ( data. count) )
432
+ var casid : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
433
+ try handleCASError { err_msg in
434
+ casid = api. swiftscan_cas_store ( scan_cas, bytes, UInt32 ( data. count) , & err_msg)
435
+ return casid. data == nil
436
+ }
404
437
return try toSwiftString ( casid)
405
438
}
406
439
@@ -425,15 +458,19 @@ internal extension swiftscan_diagnostic_severity_t {
425
458
426
459
func computeCacheKeyForOutput( kind: FileType , commandLine: [ String ] , input: String ) throws -> String {
427
460
guard let scan_cas = self . cas else {
428
- throw DependencyScanningError . failedToInstantiateCAS
461
+ throw DependencyScanningError . casError ( " cannot compute CacheKey for compilation because CAS is not yet created " )
429
462
}
430
- var casid : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
431
- withArrayOfCStrings ( commandLine) { commandArray in
432
- casid = api. swiftscan_compute_cache_key ( scan_cas,
433
- Int32 ( commandLine. count) ,
434
- commandArray,
435
- input. cString ( using: String . Encoding. utf8) ,
436
- getSwiftScanOutputKind ( kind: kind) )
463
+ var casid : swiftscan_string_ref_t = swiftscan_string_ref_t ( )
464
+ try handleCASError { err_msg in
465
+ withArrayOfCStrings ( commandLine) { commandArray in
466
+ casid = api. swiftscan_compute_cache_key ( scan_cas,
467
+ Int32 ( commandLine. count) ,
468
+ commandArray,
469
+ input. cString ( using: String . Encoding. utf8) ,
470
+ getSwiftScanOutputKind ( kind: kind) ,
471
+ & err_msg)
472
+ }
473
+ return casid. data == nil
437
474
}
438
475
return try toSwiftString ( casid)
439
476
}
@@ -518,7 +555,12 @@ private extension swiftscan_functions_t {
518
555
self . swiftscan_clang_detail_get_module_cache_key =
519
556
try loadOptional ( " swiftscan_clang_detail_get_module_cache_key " )
520
557
521
- self . swiftscan_cas_create = try loadOptional ( " swiftscan_cas_create " )
558
+ self . swiftscan_cas_options_create = try loadOptional ( " swiftscan_cas_options_create " )
559
+ self . swiftscan_cas_options_set_plugin_path = try loadOptional ( " swiftscan_cas_options_set_plugin_path " )
560
+ self . swiftscan_cas_options_set_ondisk_path = try loadOptional ( " swiftscan_cas_options_set_ondisk_path " )
561
+ self . swiftscan_cas_options_set_option = try loadOptional ( " swiftscan_cas_options_set_option " )
562
+ self . swiftscan_cas_options_dispose = try loadOptional ( " swiftscan_cas_options_dispose " )
563
+ self . swiftscan_cas_create_from_options = try loadOptional ( " swiftscan_cas_create_from_options " )
522
564
self . swiftscan_cas_dispose = try loadOptional ( " swiftscan_cas_dispose " )
523
565
self . swiftscan_compute_cache_key =
524
566
try loadOptional ( " swiftscan_compute_cache_key " )
0 commit comments