|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 |
| - |
| 13 | +// An inter-module dependency oracle, responsible for responding to queries about |
| 14 | +// dependencies of a given module, caching already-discovered dependencies along the way. |
| 15 | +// |
14 | 16 | // The oracle is currently implemented as a simple store of ModuleInfo nodes.
|
15 |
| -// It is the currently responsibility of the swift-driver to populate and update |
| 17 | +// It is the responsibility of the Driver to populate and update |
16 | 18 | // the store. It does so by invoking individual -scan-dependencies jobs and
|
17 |
| -// accumulating resulting dependency graphs. |
| 19 | +// accumulating resulting dependency graphs into the oracle's store. |
18 | 20 | //
|
19 | 21 | // The design of the oracle's public API is meant to abstract that away,
|
20 |
| -// allowing us to, in the future, replace the underlying implementation with |
| 22 | +// allowing us to replace the underlying implementation in the future, with |
21 | 23 | // a persistent-across-targets dependency scanning library.
|
22 | 24 | //
|
23 | 25 | /// An abstraction of a cache and query-engine of inter-module dependencies
|
24 | 26 | public class InterModuleDependencyOracle {
|
25 |
| - // MARK: Public API |
26 | 27 | /// Query the ModuleInfo of a module with a given ID
|
27 |
| - public func getModuleInfo(of moduleId: ModuleDependencyId) -> ModuleInfo? { |
| 28 | + @_spi(Testing) public func getModuleInfo(of moduleId: ModuleDependencyId) -> ModuleInfo? { |
28 | 29 | return modules[moduleId]
|
29 | 30 | }
|
30 | 31 |
|
31 | 32 | /// Query the direct dependencies of a module with a given ID
|
32 |
| - public func getDependencies(of moduleId: ModuleDependencyId) -> [ModuleDependencyId]? { |
| 33 | + @_spi(Testing) public func getDependencies(of moduleId: ModuleDependencyId) |
| 34 | + -> [ModuleDependencyId]? { |
33 | 35 | return modules[moduleId]?.directDependencies
|
34 | 36 | }
|
35 | 37 |
|
36 |
| - // MARK: Private Implementation |
37 |
| - |
38 | 38 | // TODO: This will require a SwiftDriver entry-point for scanning a module given
|
39 | 39 | // a command invocation and a set of source-files. As-is, the driver itself is responsible
|
40 | 40 | // for executing individual module dependency-scanning actions and updating oracle state.
|
| 41 | + // (Implemented with InterModuleDependencyOracle::mergeModules extension) |
| 42 | + // |
| 43 | + // func getFullDependencies(inputs: [TypedVirtualPath], |
| 44 | + // commandLine: [Job.ArgTemplate]) -> InterModuleDependencyGraph {} |
| 45 | + // |
41 | 46 |
|
42 | 47 | /// The complete set of modules discovered so far, spanning potentially multiple targets
|
43 | 48 | internal var modules: ModuleInfoMap = [:]
|
44 | 49 |
|
45 |
| - /// Override the default initializer's internal access level for test access |
| 50 | + /// Override the default initializer's access level for test access |
46 | 51 | @_spi(Testing) public init() {}
|
47 | 52 | }
|
48 | 53 |
|
| 54 | +// This is a shim for backwards-compatibility with existing API used by SwiftPM. |
| 55 | +// TODO: After SwiftPM switches to using the oracle, this should be deleted. |
| 56 | +extension Driver { |
| 57 | + public var interModuleDependencyGraph: InterModuleDependencyGraph? { |
| 58 | + let mainModuleId : ModuleDependencyId = .swift(moduleOutputInfo.name) |
| 59 | + var mainModuleDependencyGraph = |
| 60 | + InterModuleDependencyGraph(mainModuleName: moduleOutputInfo.name) |
| 61 | + |
| 62 | + addModule(moduleId: mainModuleId, |
| 63 | + moduleInfo: interModuleDependencyOracle.getModuleInfo(of: mainModuleId)!, |
| 64 | + into: &mainModuleDependencyGraph) |
| 65 | + return mainModuleDependencyGraph |
| 66 | + } |
| 67 | + |
| 68 | + private func addModule(moduleId: ModuleDependencyId, |
| 69 | + moduleInfo: ModuleInfo, |
| 70 | + into dependencyGraph: inout InterModuleDependencyGraph) { |
| 71 | + dependencyGraph.modules[moduleId] = moduleInfo |
| 72 | + moduleInfo.directDependencies?.forEach { dependencyId in |
| 73 | + addModule(moduleId: dependencyId, |
| 74 | + moduleInfo: interModuleDependencyOracle.getModuleInfo(of: dependencyId)!, |
| 75 | + into: &dependencyGraph) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments