@@ -248,7 +248,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
248248 // Prohibit the frontend from implicitly building textual modules into binary modules.
249249 var swiftDependencyArtifacts : [ SwiftModuleArtifactInfo ] = [ ]
250250 var clangDependencyArtifacts : [ ClangModuleArtifactInfo ] = [ ]
251- try addModuleDependencies ( moduleId : moduleId, pcmArgs: pcmArgs,
251+ try addModuleDependencies ( of : moduleId, pcmArgs: pcmArgs,
252252 clangDependencyArtifacts: & clangDependencyArtifacts,
253253 swiftDependencyArtifacts: & swiftDependencyArtifacts)
254254
@@ -285,54 +285,64 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
285285 }
286286 }
287287
288+ private mutating func addModuleDependency( of moduleId: ModuleDependencyId ,
289+ dependencyId: ModuleDependencyId , pcmArgs: [ String ] ,
290+ clangDependencyArtifacts: inout [ ClangModuleArtifactInfo ] ,
291+ swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
292+ ) throws {
293+ switch dependencyId {
294+ case . swift:
295+ let dependencyInfo = try dependencyGraph. moduleInfo ( of: dependencyId)
296+ let swiftModulePath : TypedVirtualPath
297+ let isFramework : Bool
298+ swiftModulePath = . init( file: dependencyInfo. modulePath. path,
299+ type: . swiftModule)
300+ isFramework = try dependencyGraph. swiftModuleDetails ( of: dependencyId) . isFramework ?? false
301+ // Accumulate the required information about this dependency
302+ // TODO: add .swiftdoc and .swiftsourceinfo for this module.
303+ swiftDependencyArtifacts. append (
304+ SwiftModuleArtifactInfo ( name: dependencyId. moduleName,
305+ modulePath: TextualVirtualPath ( path: swiftModulePath. fileHandle) ,
306+ isFramework: isFramework) )
307+ case . clang:
308+ let dependencyInfo = try dependencyGraph. moduleInfo ( of: dependencyId)
309+ let dependencyClangModuleDetails =
310+ try dependencyGraph. clangModuleDetails ( of: dependencyId)
311+ // Accumulate the required information about this dependency
312+ clangDependencyArtifacts. append (
313+ ClangModuleArtifactInfo ( name: dependencyId. moduleName,
314+ modulePath: TextualVirtualPath ( path: dependencyInfo. modulePath. path) ,
315+ moduleMapPath: dependencyClangModuleDetails. moduleMapPath) )
316+ case . swiftPrebuiltExternal:
317+ let prebuiltModuleDetails = try dependencyGraph. swiftPrebuiltDetails ( of: dependencyId)
318+ let compiledModulePath = prebuiltModuleDetails. compiledModulePath
319+ let isFramework = prebuiltModuleDetails. isFramework ?? false
320+ let swiftModulePath : TypedVirtualPath =
321+ . init( file: compiledModulePath. path, type: . swiftModule)
322+ // Accumulate the required information about this dependency
323+ // TODO: add .swiftdoc and .swiftsourceinfo for this module.
324+ swiftDependencyArtifacts. append (
325+ SwiftModuleArtifactInfo ( name: dependencyId. moduleName,
326+ modulePath: TextualVirtualPath ( path: swiftModulePath. fileHandle) ,
327+ isFramework: isFramework) )
328+ case . swiftPlaceholder:
329+ fatalError ( " Unresolved placeholder dependencies at planning stage: \( dependencyId) of \( moduleId) " )
330+ }
331+ }
332+
288333 /// Add a specific module dependency as an input and a corresponding command
289334 /// line flag.
290- private mutating func addModuleDependencies( moduleId: ModuleDependencyId , pcmArgs: [ String ] ,
335+ private mutating func addModuleDependencies( of moduleId: ModuleDependencyId , pcmArgs: [ String ] ,
291336 clangDependencyArtifacts: inout [ ClangModuleArtifactInfo ] ,
292337 swiftDependencyArtifacts: inout [ SwiftModuleArtifactInfo ]
293338 ) throws {
294339 guard let moduleDependencies = reachabilityMap [ moduleId] else {
295340 fatalError ( " Expected reachability information for the module: \( moduleId. moduleName) . " )
296341 }
297342 for dependencyId in moduleDependencies {
298- switch dependencyId {
299- case . swift:
300- let dependencyInfo = try dependencyGraph. moduleInfo ( of: dependencyId)
301- let swiftModulePath : TypedVirtualPath
302- let isFramework : Bool
303- swiftModulePath = . init( file: dependencyInfo. modulePath. path,
304- type: . swiftModule)
305- isFramework = try dependencyGraph. swiftModuleDetails ( of: dependencyId) . isFramework ?? false
306- // Accumulate the required information about this dependency
307- // TODO: add .swiftdoc and .swiftsourceinfo for this module.
308- swiftDependencyArtifacts. append (
309- SwiftModuleArtifactInfo ( name: dependencyId. moduleName,
310- modulePath: TextualVirtualPath ( path: swiftModulePath. fileHandle) ,
311- isFramework: isFramework) )
312- case . clang:
313- let dependencyInfo = try dependencyGraph. moduleInfo ( of: dependencyId)
314- let dependencyClangModuleDetails =
315- try dependencyGraph. clangModuleDetails ( of: dependencyId)
316- // Accumulate the required information about this dependency
317- clangDependencyArtifacts. append (
318- ClangModuleArtifactInfo ( name: dependencyId. moduleName,
319- modulePath: TextualVirtualPath ( path: dependencyInfo. modulePath. path) ,
320- moduleMapPath: dependencyClangModuleDetails. moduleMapPath) )
321- case . swiftPrebuiltExternal:
322- let prebuiltModuleDetails = try dependencyGraph. swiftPrebuiltDetails ( of: dependencyId)
323- let compiledModulePath = prebuiltModuleDetails. compiledModulePath
324- let isFramework = prebuiltModuleDetails. isFramework ?? false
325- let swiftModulePath : TypedVirtualPath =
326- . init( file: compiledModulePath. path, type: . swiftModule)
327- // Accumulate the required information about this dependency
328- // TODO: add .swiftdoc and .swiftsourceinfo for this module.
329- swiftDependencyArtifacts. append (
330- SwiftModuleArtifactInfo ( name: dependencyId. moduleName,
331- modulePath: TextualVirtualPath ( path: swiftModulePath. fileHandle) ,
332- isFramework: isFramework) )
333- case . swiftPlaceholder:
334- fatalError ( " Unresolved placeholder dependencies at planning stage: \( dependencyId) of \( moduleId) " )
335- }
343+ try addModuleDependency ( of: moduleId, dependencyId: dependencyId, pcmArgs: pcmArgs,
344+ clangDependencyArtifacts: & clangDependencyArtifacts,
345+ swiftDependencyArtifacts: & swiftDependencyArtifacts)
336346 }
337347 }
338348
@@ -370,6 +380,63 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
370380 commandLine: & commandLine)
371381 }
372382
383+ /// Resolve all module dependencies of the main module and add them to the lists of
384+ /// inputs and command line flags.
385+ public mutating func resolveBridgingHeaderDependencies( inputs: inout [ TypedVirtualPath ] ,
386+ commandLine: inout [ Job . ArgTemplate ] ) throws {
387+ let mainModuleId : ModuleDependencyId = . swift( dependencyGraph. mainModuleName)
388+ // Prohibit the frontend from implicitly building textual modules into binary modules.
389+ commandLine. appendFlags ( " -disable-implicit-swift-modules " ,
390+ " -Xcc " , " -fno-implicit-modules " ,
391+ " -Xcc " , " -fno-implicit-module-maps " )
392+
393+ var swiftDependencyArtifacts : [ SwiftModuleArtifactInfo ] = [ ]
394+ var clangDependencyArtifacts : [ ClangModuleArtifactInfo ] = [ ]
395+ let mainModuleDetails = try dependencyGraph. swiftModuleDetails ( of: mainModuleId)
396+
397+ var addedDependencies : Set < ModuleDependencyId > = [ ]
398+ var dependenciesWorklist = mainModuleDetails. bridgingHeaderDependencies ?? [ ]
399+
400+ while !dependenciesWorklist. isEmpty {
401+ guard let bridgingHeaderDepID = dependenciesWorklist. popLast ( ) else {
402+ break
403+ }
404+ guard !addedDependencies. contains ( bridgingHeaderDepID) else {
405+ continue
406+ }
407+ addedDependencies. insert ( bridgingHeaderDepID)
408+ try addModuleDependency ( of: mainModuleId, dependencyId: bridgingHeaderDepID, pcmArgs: [ ] ,
409+ clangDependencyArtifacts: & clangDependencyArtifacts,
410+ swiftDependencyArtifacts: & swiftDependencyArtifacts)
411+ try addModuleDependencies ( of: bridgingHeaderDepID, pcmArgs: [ ] ,
412+ clangDependencyArtifacts: & clangDependencyArtifacts,
413+ swiftDependencyArtifacts: & swiftDependencyArtifacts)
414+ let depInfo = try dependencyGraph. moduleInfo ( of: bridgingHeaderDepID)
415+ dependenciesWorklist. append ( contentsOf: depInfo. directDependencies ?? [ ] )
416+ }
417+
418+ // Clang module dependencies are specified on the command line explicitly
419+ for moduleArtifactInfo in clangDependencyArtifacts {
420+ let clangModulePath =
421+ TypedVirtualPath ( file: moduleArtifactInfo. clangModulePath. path,
422+ type: . pcm)
423+ let clangModuleMapPath =
424+ TypedVirtualPath ( file: moduleArtifactInfo. clangModuleMapPath. path,
425+ type: . clangModuleMap)
426+ inputs. append ( clangModulePath)
427+ inputs. append ( clangModuleMapPath)
428+ }
429+
430+ let dependencyFile =
431+ try serializeModuleDependencies ( for: mainModuleId,
432+ swiftDependencyArtifacts: swiftDependencyArtifacts,
433+ clangDependencyArtifacts: clangDependencyArtifacts)
434+ commandLine. appendFlag ( " -explicit-swift-module-map-file " )
435+ commandLine. appendPath ( dependencyFile)
436+ inputs. append ( TypedVirtualPath ( file: dependencyFile. intern ( ) ,
437+ type: . jsonSwiftArtifacts) )
438+ }
439+
373440 /// Store the output file artifacts for a given module in a JSON file, return the file's path.
374441 private func serializeModuleDependencies( for moduleId: ModuleDependencyId ,
375442 swiftDependencyArtifacts: [ SwiftModuleArtifactInfo ] ,
0 commit comments