Skip to content

Commit 082dc70

Browse files
committed
[Explicit Module Builds] Add backwards-compatibility shim for dependency graph getter
1 parent 8a85baf commit 082dc70

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ public struct Driver {
253253
var forceEmitModuleBeforeCompile: Bool = false
254254

255255
// FIXME: We should soon be able to remove this from being in the Driver's state.
256-
// Its only remaining use outside of actual dependency-build planning is in
257-
// command-line option generation for the main module compile.
256+
// Its only remaining use outside of actual dependency build planning is in
257+
// command-line input option generation for the explicit main module compile job.
258258
/// Planner for constructing module build jobs using Explicit Module Builds.
259-
/// Constructed during the planning phase only when all modules will be prebuilt and treated
260-
/// as explicit by the various compilation jobs.
259+
/// Constructed during the planning phase only when all module dependencies will be prebuilt and treated
260+
/// as explicit inputs by the various compilation jobs.
261261
@_spi(Testing) public var explicitDependencyBuildPlanner: ExplicitDependencyBuildPlanner? = nil
262262

263263
/// An oracle for querying inter-module dependencies

Sources/SwiftDriver/Explicit Module Builds/Inter Module Dependencies/CommonDependencyOperations.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@
2020
}
2121
}
2222

23-
internal extension InterModuleDependencyGraph {
23+
// This is a backwards-compatibility shim
24+
public extension InterModuleDependencyGraph {
25+
// This is a shim for backwards-compatibility with existing API used by SwiftPM.
26+
// TODO: After SwiftPM switches to using the oracle, this should be deleted.
27+
static func mergeModules(
28+
from dependencyGraph: InterModuleDependencyGraph,
29+
into discoveredModules: inout ModuleInfoMap
30+
) throws {
31+
for (moduleId, moduleInfo) in dependencyGraph.modules {
32+
try mergeModule(moduleId, moduleInfo, into: &discoveredModules)
33+
}
34+
}
35+
}
36+
37+
38+
@_spi(Testing) public extension InterModuleDependencyGraph {
2439
/// Merge a module with a given ID and Info into a ModuleInfoMap
2540
static func mergeModule(_ moduleId: ModuleDependencyId,
2641
_ moduleInfo: ModuleInfo,

Sources/SwiftDriver/Explicit Module Builds/Inter Module Dependencies/InterModuleDependencyOracle.swift

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,69 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

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+
//
1416
// 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
1618
// 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.
1820
//
1921
// 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
2123
// a persistent-across-targets dependency scanning library.
2224
//
2325
/// An abstraction of a cache and query-engine of inter-module dependencies
2426
public class InterModuleDependencyOracle {
25-
// MARK: Public API
2627
/// 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? {
2829
return modules[moduleId]
2930
}
3031

3132
/// 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]? {
3335
return modules[moduleId]?.directDependencies
3436
}
3537

36-
// MARK: Private Implementation
37-
3838
// TODO: This will require a SwiftDriver entry-point for scanning a module given
3939
// a command invocation and a set of source-files. As-is, the driver itself is responsible
4040
// 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+
//
4146

4247
/// The complete set of modules discovered so far, spanning potentially multiple targets
4348
internal var modules: ModuleInfoMap = [:]
4449

45-
/// Override the default initializer's internal access level for test access
50+
/// Override the default initializer's access level for test access
4651
@_spi(Testing) public init() {}
4752
}
4853

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

Comments
 (0)