@@ -23,11 +23,8 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
2323/// build jobs for all module dependencies and providing compile command options
2424/// that specify said explicit module dependencies.
2525@_spi ( Testing) public struct ExplicitDependencyBuildPlanner {
26- /// The module ID of the main module for the current target
27- public let mainModuleId : ModuleDependencyId
28-
29- /// The module dependency oracle.
30- public let dependencyOracle : InterModuleDependencyOracle
26+ /// The module dependency graph.
27+ public var dependencyGraph : InterModuleDependencyGraph
3128
3229 /// Cache Clang modules for which a build job has already been constructed with a given
3330 /// target triple.
@@ -39,11 +36,9 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
3936 /// The toolchain to be used for frontend job generation.
4037 private let toolchain : Toolchain
4138
42- public init ( mainModuleId: ModuleDependencyId ,
43- dependencyOracle: InterModuleDependencyOracle ,
39+ public init ( dependencyGraph: InterModuleDependencyGraph ,
4440 toolchain: Toolchain ) throws {
45- self . mainModuleId = mainModuleId
46- self . dependencyOracle = dependencyOracle
41+ self . dependencyGraph = dependencyGraph
4742 self . toolchain = toolchain
4843 }
4944
@@ -121,9 +116,9 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
121116 /// inputs and command line flags.
122117 mutating public func resolveMainModuleDependencies( inputs: inout [ TypedVirtualPath ] ,
123118 commandLine: inout [ Job . ArgTemplate ] ) throws {
119+ let mainModuleId : ModuleDependencyId = . swift( dependencyGraph. mainModuleName)
124120 try resolveExplicitModuleDependencies ( moduleId: mainModuleId,
125- pcmArgs:
126- try dependencyOracle. getSwiftModulePCMArgs ( of: mainModuleId) ,
121+ pcmArgs: try dependencyGraph. swiftModulePCMArgs ( of: mainModuleId) ,
127122 inputs: & inputs,
128123 commandLine: & commandLine)
129124 }
@@ -132,23 +127,20 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
132127 /// Resolving a module's dependencies will ensure that the dependencies' build jobs are also
133128 /// generated.
134129 mutating private func genSwiftModuleBuildJob( moduleId: ModuleDependencyId ) throws {
135- guard let moduleInfo = dependencyOracle. getModuleInfo ( of: moduleId) else {
136- throw Driver . Error. missingModuleDependency ( moduleId. moduleName)
137- }
130+ let moduleInfo = try dependencyGraph. moduleInfo ( of: moduleId)
138131 var inputs : [ TypedVirtualPath ] = [ ]
139132 let outputs : [ TypedVirtualPath ] = [
140133 TypedVirtualPath ( file: try VirtualPath ( path: moduleInfo. modulePath) , type: . swiftModule)
141134 ]
142135 var commandLine : [ Job . ArgTemplate ] = [ ]
143136
144137 // First, take the command line options provided in the dependency information
145- let moduleDetails = try dependencyOracle . getSwiftModuleDetails ( of: moduleId)
138+ let moduleDetails = try dependencyGraph . swiftModuleDetails ( of: moduleId)
146139 moduleDetails. commandLine? . forEach { commandLine. appendFlags ( $0) }
147140
148141 // Resolve all dependency module inputs for this Swift module
149142 try resolveExplicitModuleDependencies ( moduleId: moduleId,
150- pcmArgs:
151- dependencyOracle. getSwiftModulePCMArgs ( of: moduleId) ,
143+ pcmArgs: dependencyGraph. swiftModulePCMArgs ( of: moduleId) ,
152144 inputs: & inputs, commandLine: & commandLine)
153145
154146 // Build the .swiftinterfaces file using a list of command line options specified in the
@@ -191,15 +183,13 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
191183 /// generated.
192184 mutating private func genClangModuleBuildJob( moduleId: ModuleDependencyId ,
193185 pcmArgs: [ String ] ) throws {
194- guard let moduleInfo = dependencyOracle. getModuleInfo ( of: moduleId) else {
195- throw Driver . Error. missingModuleDependency ( moduleId. moduleName)
196- }
186+ let moduleInfo = try dependencyGraph. moduleInfo ( of: moduleId)
197187 var inputs : [ TypedVirtualPath ] = [ ]
198188 var outputs : [ TypedVirtualPath ] = [ ]
199189 var commandLine : [ Job . ArgTemplate ] = [ ]
200190
201191 // First, take the command line options provided in the dependency information
202- let moduleDetails = try dependencyOracle . getClangModuleDetails ( of: moduleId)
192+ let moduleDetails = try dependencyGraph . clangModuleDetails ( of: moduleId)
203193 moduleDetails. commandLine. forEach { commandLine. appendFlags ( $0) }
204194
205195 // Add the `-target` option as inherited from the dependent Swift module's PCM args
@@ -305,7 +295,7 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
305295 clangDependencyArtifacts: inout [ ClangModuleArtifactInfo ] ,
306296 swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
307297 ) throws {
308- for dependencyId in dependencyOracle . getDependencies ( of: moduleId) ! {
298+ for dependencyId in try dependencyGraph . moduleInfo ( of: moduleId) . directDependencies ! {
309299 guard addedDependenciesSet. insert ( dependencyId) . inserted else {
310300 continue
311301 }
@@ -346,9 +336,7 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
346336 swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
347337 ) throws {
348338 // Add it as an explicit dependency
349- guard let dependencyInfo = dependencyOracle. getModuleInfo ( of: dependencyId) else {
350- throw Driver . Error. missingModuleDependency ( moduleId. moduleName)
351- }
339+ let dependencyInfo = try dependencyGraph. moduleInfo ( of: dependencyId)
352340 let swiftModulePath : TypedVirtualPath
353341 let isFramework : Bool
354342
@@ -359,7 +347,7 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
359347 }
360348 swiftModulePath = . init( file: try VirtualPath ( path: dependencyInfo. modulePath) ,
361349 type: . swiftModule)
362- isFramework = try dependencyOracle . getSwiftModuleDetails ( of: dependencyId) . isFramework
350+ isFramework = try dependencyGraph . swiftModuleDetails ( of: dependencyId) . isFramework
363351
364352 // Collect the required information about this module
365353 // TODO: add .swiftdoc and .swiftsourceinfo for this module.
@@ -393,10 +381,8 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
393381 }
394382
395383 // Add it as an explicit dependency
396- guard let dependencyInfo = dependencyOracle. getModuleInfo ( of: dependencyId) else {
397- throw Driver . Error. missingModuleDependency ( moduleId. moduleName)
398- }
399- let dependencyClangModuleDetails = try dependencyOracle. getClangModuleDetails ( of: dependencyId)
384+ let dependencyInfo = try dependencyGraph. moduleInfo ( of: dependencyId)
385+ let dependencyClangModuleDetails = try dependencyGraph. clangModuleDetails ( of: dependencyId)
400386 let clangModulePath =
401387 try ExplicitDependencyBuildPlanner . targetEncodedClangModuleFilePath ( for: dependencyInfo,
402388 pcmArgs: pcmArgs)
@@ -423,8 +409,9 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
423409 swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
424410 ) throws {
425411 // Add it as an explicit dependency
426- let compiledModulePath =
427- try dependencyOracle. getSwiftPrebuiltDetails ( of: dependencyId) . compiledModulePath
412+ let compiledModulePath = try dependencyGraph
413+ . swiftPrebuiltDetails ( of: dependencyId)
414+ . compiledModulePath
428415 let swiftModulePath : TypedVirtualPath = . init( file: try VirtualPath ( path: compiledModulePath) ,
429416 type: . swiftModule)
430417
@@ -479,33 +466,50 @@ extension ExplicitDependencyBuildPlanner {
479466
480467/// Encapsulates some of the common queries of the ExplicitDependencyBuildPlanner with error-checking
481468/// on the dependency graph's structure.
482- internal extension InterModuleDependencyOracle {
483- func getSwiftModuleDetails( of moduleId: ModuleDependencyId ) throws -> SwiftModuleDetails {
484- guard case . swift( let swiftModuleDetails) = getModuleInfo ( of: moduleId) ? . details else {
469+ internal extension InterModuleDependencyGraph {
470+ func moduleInfo( of moduleId: ModuleDependencyId ) throws -> ModuleInfo {
471+ guard let moduleInfo = modules [ moduleId] else {
472+ throw Driver . Error. missingModuleDependency ( moduleId. moduleName)
473+ }
474+ return moduleInfo
475+ }
476+
477+ func swiftModuleDetails( of moduleId: ModuleDependencyId ) throws -> SwiftModuleDetails {
478+ guard case . swift( let swiftModuleDetails) = try moduleInfo ( of: moduleId) . details else {
485479 throw Driver . Error. malformedModuleDependency ( moduleId. moduleName, " no Swift `details` object " )
486480 }
487481 return swiftModuleDetails
488482 }
489483
490- func getSwiftPrebuiltDetails ( of moduleId: ModuleDependencyId )
484+ func swiftPrebuiltDetails ( of moduleId: ModuleDependencyId )
491485 throws -> SwiftPrebuiltExternalModuleDetails {
492486 guard case . swiftPrebuiltExternal( let prebuiltModuleDetails) =
493- getModuleInfo ( of: moduleId) ? . details else {
487+ try moduleInfo ( of: moduleId) . details else {
494488 throw Driver . Error. malformedModuleDependency ( moduleId. moduleName,
495489 " no SwiftPrebuiltExternal `details` object " )
496490 }
497491 return prebuiltModuleDetails
498492 }
499493
500- func getClangModuleDetails ( of moduleId: ModuleDependencyId ) throws -> ClangModuleDetails {
501- guard case . clang( let clangModuleDetails) = getModuleInfo ( of: moduleId) ? . details else {
494+ func clangModuleDetails ( of moduleId: ModuleDependencyId ) throws -> ClangModuleDetails {
495+ guard case . clang( let clangModuleDetails) = try moduleInfo ( of: moduleId) . details else {
502496 throw Driver . Error. malformedModuleDependency ( moduleId. moduleName, " no Clang `details` object " )
503497 }
504498 return clangModuleDetails
505499 }
506500
507- func getSwiftModulePCMArgs ( of moduleId: ModuleDependencyId ) throws -> [ String ] {
508- let moduleDetails = try getSwiftModuleDetails ( of: moduleId)
501+ func swiftModulePCMArgs ( of moduleId: ModuleDependencyId ) throws -> [ String ] {
502+ let moduleDetails = try swiftModuleDetails ( of: moduleId)
509503 return moduleDetails. extraPcmArgs
510504 }
511505}
506+
507+ // InterModuleDependencyGraph printing, useful for debugging
508+ internal extension InterModuleDependencyGraph {
509+ func prettyPrintString( ) throws -> String {
510+ let encoder = JSONEncoder ( )
511+ encoder. outputFormatting = [ . prettyPrinted]
512+ let contents = try encoder. encode ( self )
513+ return String ( data: contents, encoding: . utf8) !
514+ }
515+ }
0 commit comments