@@ -28,46 +28,61 @@ import Foundation
28
28
/// An abstraction of a cache and query-engine of inter-module dependencies
29
29
public class InterModuleDependencyOracle {
30
30
/// Allow external clients to instantiate the oracle
31
- public init ( fileSystem: FileSystem ,
32
- toolchainPath: AbsolutePath ) throws {
33
- guard fileSystem. exists ( toolchainPath) else {
34
- fatalError ( " Path to specified toolchain does not exist: \( toolchainPath. description) " )
35
- }
36
-
37
- let swiftScanLibPath = toolchainPath. appending ( component: " lib " )
38
- . appending ( component: " lib_InternalSwiftScan.dylib " )
39
- guard fileSystem. exists ( toolchainPath) else {
40
- fatalError ( " Could not find libSwiftScan at: \( swiftScanLibPath. description) " )
41
- }
42
-
43
- swiftScanLibInstance = try SwiftScan ( dylib: swiftScanLibPath)
44
- }
31
+ public init ( ) { }
45
32
46
33
@_spi ( Testing) public func getDependencies( workingDirectory: AbsolutePath ,
47
34
commandLine: [ String ] )
48
35
throws -> InterModuleDependencyGraph {
49
- try queue. sync {
50
- return try swiftScanLibInstance. scanDependencies ( workingDirectory: workingDirectory,
36
+ precondition ( hasScannerInstance)
37
+ return try queue. sync {
38
+ return try swiftScanLibInstance!. scanDependencies ( workingDirectory: workingDirectory,
51
39
invocationCommand: commandLine)
52
40
}
53
41
}
54
42
55
- func getBatchDependencies( workingDirectory: AbsolutePath ,
56
- commandLine: [ String ] ,
57
- batchInfos: [ BatchScanModuleInfo ] )
43
+ @ _spi ( Testing ) public func getBatchDependencies( workingDirectory: AbsolutePath ,
44
+ commandLine: [ String ] ,
45
+ batchInfos: [ BatchScanModuleInfo ] )
58
46
throws -> [ ModuleDependencyId : [ InterModuleDependencyGraph ] ] {
59
- try queue. sync {
60
- return try swiftScanLibInstance. batchScanDependencies ( workingDirectory: workingDirectory,
47
+ precondition ( hasScannerInstance)
48
+ return try queue. sync {
49
+ return try swiftScanLibInstance!. batchScanDependencies ( workingDirectory: workingDirectory,
61
50
invocationCommand: commandLine,
62
51
batchInfos: batchInfos)
63
52
}
64
53
}
65
54
55
+ /// Given a specified toolchain path, locate and instantiate an instance of the SwiftScan library
56
+ @_spi ( Testing) public func verifyOrCreateScannerInstance( fileSystem: FileSystem ,
57
+ toolchainPath: AbsolutePath ) throws {
58
+ try queue. sync {
59
+ if swiftScanLibInstance == nil {
60
+ guard fileSystem. exists ( toolchainPath) else {
61
+ fatalError ( " Path to specified toolchain does not exist: \( toolchainPath. description) " )
62
+ }
63
+
64
+ let swiftScanLibPath = toolchainPath. appending ( component: " lib " )
65
+ . appending ( component: " lib_InternalSwiftScan.dylib " )
66
+ guard fileSystem. exists ( toolchainPath) else {
67
+ fatalError ( " Could not find libSwiftScan at: \( swiftScanLibPath. description) " )
68
+ }
69
+
70
+ swiftScanLibInstance = try SwiftScan ( dylib: swiftScanLibPath)
71
+ } else {
72
+ let swiftScanLibPath = toolchainPath. appending ( component: " lib " )
73
+ . appending ( component: " lib_InternalSwiftScan.dylib " )
74
+ assert ( swiftScanLibInstance!. path == swiftScanLibPath)
75
+ }
76
+ }
77
+ }
78
+
79
+ private var hasScannerInstance : Bool { self . swiftScanLibInstance != nil }
80
+
66
81
/// Queue to sunchronize accesses to the scanner
67
82
internal let queue = DispatchQueue ( label: " org.swift.swift-driver.swift-scan " )
68
83
69
84
/// A reference to an instance of the compiler's libSwiftScan shared library
70
- private let swiftScanLibInstance : SwiftScan
85
+ private var swiftScanLibInstance : SwiftScan ? = nil
71
86
72
87
// The below API is a legacy implementation of the oracle that is in-place to allow clients to
73
88
// transition to the new API. It is to be removed once that transition is complete.
0 commit comments