@@ -23,11 +23,8 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
23
23
/// build jobs for all module dependencies and providing compile command options
24
24
/// that specify said explicit module dependencies.
25
25
@_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
31
28
32
29
/// Cache Clang modules for which a build job has already been constructed with a given
33
30
/// target triple.
@@ -39,11 +36,9 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
39
36
/// The toolchain to be used for frontend job generation.
40
37
private let toolchain : Toolchain
41
38
42
- public init ( mainModuleId: ModuleDependencyId ,
43
- dependencyOracle: InterModuleDependencyOracle ,
39
+ public init ( dependencyGraph: InterModuleDependencyGraph ,
44
40
toolchain: Toolchain ) throws {
45
- self . mainModuleId = mainModuleId
46
- self . dependencyOracle = dependencyOracle
41
+ self . dependencyGraph = dependencyGraph
47
42
self . toolchain = toolchain
48
43
}
49
44
@@ -121,9 +116,9 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
121
116
/// inputs and command line flags.
122
117
mutating public func resolveMainModuleDependencies( inputs: inout [ TypedVirtualPath ] ,
123
118
commandLine: inout [ Job . ArgTemplate ] ) throws {
119
+ let mainModuleId : ModuleDependencyId = . swift( dependencyGraph. mainModuleName)
124
120
try resolveExplicitModuleDependencies ( moduleId: mainModuleId,
125
- pcmArgs:
126
- try dependencyOracle. getSwiftModulePCMArgs ( of: mainModuleId) ,
121
+ pcmArgs: try dependencyGraph. swiftModulePCMArgs ( of: mainModuleId) ,
127
122
inputs: & inputs,
128
123
commandLine: & commandLine)
129
124
}
@@ -132,23 +127,20 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
132
127
/// Resolving a module's dependencies will ensure that the dependencies' build jobs are also
133
128
/// generated.
134
129
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)
138
131
var inputs : [ TypedVirtualPath ] = [ ]
139
132
let outputs : [ TypedVirtualPath ] = [
140
133
TypedVirtualPath ( file: try VirtualPath ( path: moduleInfo. modulePath) , type: . swiftModule)
141
134
]
142
135
var commandLine : [ Job . ArgTemplate ] = [ ]
143
136
144
137
// 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)
146
139
moduleDetails. commandLine? . forEach { commandLine. appendFlags ( $0) }
147
140
148
141
// Resolve all dependency module inputs for this Swift module
149
142
try resolveExplicitModuleDependencies ( moduleId: moduleId,
150
- pcmArgs:
151
- dependencyOracle. getSwiftModulePCMArgs ( of: moduleId) ,
143
+ pcmArgs: dependencyGraph. swiftModulePCMArgs ( of: moduleId) ,
152
144
inputs: & inputs, commandLine: & commandLine)
153
145
154
146
// Build the .swiftinterfaces file using a list of command line options specified in the
@@ -191,15 +183,13 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
191
183
/// generated.
192
184
mutating private func genClangModuleBuildJob( moduleId: ModuleDependencyId ,
193
185
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)
197
187
var inputs : [ TypedVirtualPath ] = [ ]
198
188
var outputs : [ TypedVirtualPath ] = [ ]
199
189
var commandLine : [ Job . ArgTemplate ] = [ ]
200
190
201
191
// 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)
203
193
moduleDetails. commandLine. forEach { commandLine. appendFlags ( $0) }
204
194
205
195
// Add the `-target` option as inherited from the dependent Swift module's PCM args
@@ -305,7 +295,7 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
305
295
clangDependencyArtifacts: inout [ ClangModuleArtifactInfo ] ,
306
296
swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
307
297
) throws {
308
- for dependencyId in dependencyOracle . getDependencies ( of: moduleId) ! {
298
+ for dependencyId in try dependencyGraph . moduleInfo ( of: moduleId) . directDependencies ! {
309
299
guard addedDependenciesSet. insert ( dependencyId) . inserted else {
310
300
continue
311
301
}
@@ -346,9 +336,7 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
346
336
swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
347
337
) throws {
348
338
// 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)
352
340
let swiftModulePath : TypedVirtualPath
353
341
let isFramework : Bool
354
342
@@ -359,7 +347,7 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
359
347
}
360
348
swiftModulePath = . init( file: try VirtualPath ( path: dependencyInfo. modulePath) ,
361
349
type: . swiftModule)
362
- isFramework = try dependencyOracle . getSwiftModuleDetails ( of: dependencyId) . isFramework
350
+ isFramework = try dependencyGraph . swiftModuleDetails ( of: dependencyId) . isFramework
363
351
364
352
// Collect the required information about this module
365
353
// TODO: add .swiftdoc and .swiftsourceinfo for this module.
@@ -393,10 +381,8 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
393
381
}
394
382
395
383
// 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)
400
386
let clangModulePath =
401
387
try ExplicitDependencyBuildPlanner . targetEncodedClangModuleFilePath ( for: dependencyInfo,
402
388
pcmArgs: pcmArgs)
@@ -423,8 +409,9 @@ public typealias ExternalBuildArtifacts = (ExternalTargetModulePathMap, ModuleIn
423
409
swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
424
410
) throws {
425
411
// 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
428
415
let swiftModulePath : TypedVirtualPath = . init( file: try VirtualPath ( path: compiledModulePath) ,
429
416
type: . swiftModule)
430
417
@@ -479,33 +466,50 @@ extension ExplicitDependencyBuildPlanner {
479
466
480
467
/// Encapsulates some of the common queries of the ExplicitDependencyBuildPlanner with error-checking
481
468
/// 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 {
485
479
throw Driver . Error. malformedModuleDependency ( moduleId. moduleName, " no Swift `details` object " )
486
480
}
487
481
return swiftModuleDetails
488
482
}
489
483
490
- func getSwiftPrebuiltDetails ( of moduleId: ModuleDependencyId )
484
+ func swiftPrebuiltDetails ( of moduleId: ModuleDependencyId )
491
485
throws -> SwiftPrebuiltExternalModuleDetails {
492
486
guard case . swiftPrebuiltExternal( let prebuiltModuleDetails) =
493
- getModuleInfo ( of: moduleId) ? . details else {
487
+ try moduleInfo ( of: moduleId) . details else {
494
488
throw Driver . Error. malformedModuleDependency ( moduleId. moduleName,
495
489
" no SwiftPrebuiltExternal `details` object " )
496
490
}
497
491
return prebuiltModuleDetails
498
492
}
499
493
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 {
502
496
throw Driver . Error. malformedModuleDependency ( moduleId. moduleName, " no Clang `details` object " )
503
497
}
504
498
return clangModuleDetails
505
499
}
506
500
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)
509
503
return moduleDetails. extraPcmArgs
510
504
}
511
505
}
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