Skip to content

Commit fa3dd12

Browse files
committed
[Explicit Module Builds] Refactor placeholder dependency resolution to use the dependency oracle
1 parent 082dc70 commit fa3dd12

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public class InterModuleDependencyOracle {
4747
/// The complete set of modules discovered so far, spanning potentially multiple targets
4848
internal var modules: ModuleInfoMap = [:]
4949

50-
/// Override the default initializer's access level for test access
51-
@_spi(Testing) public init() {}
50+
/// Allow external clients to instantiate the oracle
51+
public init() {}
5252
}
5353

5454
// This is a shim for backwards-compatibility with existing API used by SwiftPM.

Sources/SwiftDriver/Explicit Module Builds/PlaceholderDependencyResolution.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ import Foundation
4141
// which the driver will then resolve using B's full dependency graph provided by the client.
4242

4343
/// Resolve all placeholder dependencies using external dependency information provided by the client
44-
mutating func resolvePlaceholderDependencies(using externalBuildArtifacts: ExternalBuildArtifacts)
44+
mutating func resolvePlaceholderDependencies(for externalBuildArtifacts: ExternalBuildArtifacts,
45+
using dependencyOracle: InterModuleDependencyOracle)
4546
throws {
4647
let externalTargetModulePathMap = externalBuildArtifacts.0
47-
let externalModuleInfoMap = externalBuildArtifacts.1
48+
//let externalModuleInfoMap = externalBuildArtifacts.1
4849
let placeholderFilter : (ModuleDependencyId) -> Bool = {
4950
if case .swiftPlaceholder(_) = $0 {
5051
return true
@@ -61,7 +62,7 @@ import Foundation
6162
}
6263
try resolveTargetPlaceholder(placeholderId: moduleId,
6364
placeholderPath: placeholderModulePath,
64-
externalModuleInfoMap: externalModuleInfoMap)
65+
dependencyOracle: dependencyOracle)
6566
}
6667

6768
// Process remaining placeholders until there are none left
@@ -70,17 +71,17 @@ import Foundation
7071
let moduleId = placeholderModules.first!
7172
let swiftModuleId = ModuleDependencyId.swift(moduleId.moduleName)
7273

73-
guard externalModuleInfoMap[swiftModuleId] != nil else {
74+
guard dependencyOracle.getModuleInfo(of: swiftModuleId) != nil else {
7475
throw Driver.Error.missingExternalDependency(moduleId.moduleName)
7576
}
76-
let moduleInfo = externalModuleInfoMap[swiftModuleId]!
77+
let moduleInfo = dependencyOracle.getModuleInfo(of: swiftModuleId)!
7778

7879
// Insert the resolved module, replacing the placeholder.
7980
try Self.mergeModule(swiftModuleId, moduleInfo, into: &modules)
8081

8182
// Traverse and add all of this external module's dependencies to the current graph.
8283
try resolvePlaceholderModuleDependencies(moduleId: swiftModuleId,
83-
externalModuleInfoMap: externalModuleInfoMap)
84+
dependencyOracle: dependencyOracle)
8485

8586
// Update the set of remaining placeholders to resolve
8687
placeholderModules = modules.keys.filter(placeholderFilter)
@@ -92,7 +93,7 @@ fileprivate extension InterModuleDependencyGraph {
9293
/// Resolve a placeholder dependency that is an external target.
9394
mutating func resolveTargetPlaceholder(placeholderId: ModuleDependencyId,
9495
placeholderPath: AbsolutePath,
95-
externalModuleInfoMap: ModuleInfoMap)
96+
dependencyOracle: InterModuleDependencyOracle)
9697
throws {
9798
// For this placeholder dependency, generate a new module info containing only the pre-compiled
9899
// module path, and insert it into the current module's dependency graph,
@@ -119,15 +120,15 @@ fileprivate extension InterModuleDependencyGraph {
119120
let swiftPrebuiltModuleId = ModuleDependencyId.swiftPrebuiltExternal(placeholderId.moduleName)
120121

121122
let externalModuleId: ModuleDependencyId
122-
if externalModuleInfoMap[swiftModuleId] != nil {
123+
if dependencyOracle.getModuleInfo(of: swiftModuleId) != nil {
123124
externalModuleId = swiftModuleId
124-
} else if externalModuleInfoMap[swiftPrebuiltModuleId] != nil {
125+
} else if dependencyOracle.getModuleInfo(of: swiftPrebuiltModuleId) != nil {
125126
externalModuleId = swiftPrebuiltModuleId
126127
} else {
127128
throw Driver.Error.missingExternalDependency(placeholderId.moduleName)
128129
}
129130

130-
let externalModuleInfo = externalModuleInfoMap[externalModuleId]!
131+
let externalModuleInfo = dependencyOracle.getModuleInfo(of: externalModuleId)!
131132
let newExternalModuleDetails =
132133
SwiftPrebuiltExternalModuleDetails(compiledModulePath: placeholderPath.description)
133134
let newInfo = ModuleInfo(modulePath: placeholderPath.description,
@@ -140,13 +141,14 @@ fileprivate extension InterModuleDependencyGraph {
140141

141142
// Traverse and add all of this external target's dependencies to the current graph.
142143
try resolvePlaceholderModuleDependencies(moduleId: externalModuleId,
143-
externalModuleInfoMap: externalModuleInfoMap)
144+
dependencyOracle: dependencyOracle)
144145
}
145146

146147
/// Resolve all dependencies of a placeholder module (direct and transitive), but merging them into the current graph.
147148
mutating func resolvePlaceholderModuleDependencies(moduleId: ModuleDependencyId,
148-
externalModuleInfoMap: ModuleInfoMap) throws {
149-
guard let resolvingModuleInfo = externalModuleInfoMap[moduleId] else {
149+
dependencyOracle: InterModuleDependencyOracle)
150+
throws {
151+
guard let resolvingModuleInfo = dependencyOracle.getModuleInfo(of: moduleId) else {
150152
throw Driver.Error.missingExternalDependency(moduleId.moduleName)
151153
}
152154

@@ -157,7 +159,7 @@ fileprivate extension InterModuleDependencyGraph {
157159
while let currentId = toVisit[currentIndex...].first {
158160
currentIndex += 1
159161
visited.insert(currentId)
160-
guard let currentInfo = externalModuleInfoMap[currentId] else {
162+
guard let currentInfo = dependencyOracle.getModuleInfo(of: currentId) else {
161163
throw Driver.Error.missingExternalDependency(currentId.moduleName)
162164
}
163165

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ extension Driver {
395395

396396
// Resolve placeholder dependencies in the dependency graph, if any.
397397
if externalBuildArtifacts != nil {
398-
try dependencyGraph.resolvePlaceholderDependencies(using: externalBuildArtifacts!)
398+
try dependencyGraph.resolvePlaceholderDependencies(for: externalBuildArtifacts!,
399+
using: dependencyOracle)
399400
}
400401

401402
// Re-scan Clang modules at all the targets they will be built against.

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ final class ExplicitModuleBuildTests: XCTestCase {
204204
try JSONDecoder().decode(
205205
InterModuleDependencyGraph.self,
206206
from: ModuleDependenciesInputs.bPlaceHolderInput.data(using: .utf8)!)
207-
208207
let targetModulePathMap: ExternalTargetModulePathMap =
209208
[ModuleDependencyId.swiftPlaceholder("B"):AbsolutePath("/Somewhere/B.swiftmodule")]
210-
let externalModuleInfoMap: ModuleInfoMap = inputDependencyGraph.modules
209+
let dependencyOracle = InterModuleDependencyOracle()
210+
try dependencyOracle.mergeModules(from: inputDependencyGraph)
211211

212212
// Construct a module dependency graph that will contain .swiftPlaceholder("B"),
213213
// .swiftPlaceholder("Swift"), .swiftPlaceholder("SwiftOnoneSupport")
@@ -224,12 +224,13 @@ final class ExplicitModuleBuildTests: XCTestCase {
224224
fileSystem: localFileSystem,
225225
env: ProcessEnv.vars)
226226
var driver = try Driver(args: commandLine, executor: executor,
227-
externalBuildArtifacts: (targetModulePathMap, externalModuleInfoMap))
227+
externalBuildArtifacts: (targetModulePathMap, [:]),
228+
interModuleDependencyOracle: dependencyOracle)
228229

229230

230231
// Plan explicit dependency jobs, after resolving placeholders to actual dependencies.
231-
try moduleDependencyGraph.resolvePlaceholderDependencies(
232-
using: (targetModulePathMap, externalModuleInfoMap))
232+
try moduleDependencyGraph.resolvePlaceholderDependencies(for: (targetModulePathMap, [:]),
233+
using: dependencyOracle)
233234

234235
// Ensure the graph no longer contains any placeholders
235236
XCTAssertFalse(moduleDependencyGraph.modules.keys.contains {
@@ -239,7 +240,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
239240
return false
240241
})
241242

242-
let dependencyOracle = InterModuleDependencyOracle()
243+
// Merge the resolved version of the graph into the oracle
243244
try dependencyOracle.mergeModules(from: moduleDependencyGraph)
244245
driver.explicitDependencyBuildPlanner =
245246
try ExplicitDependencyBuildPlanner(mainModuleId: .swift("A"),

0 commit comments

Comments
 (0)