Skip to content

Commit f5df590

Browse files
committed
[Explicit Module Builds] Fix topological sort of inter-module dependency graph
to account for Swift overlay dependencies
1 parent ac85d40 commit f5df590

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/InterModuleDependencies/CommonDependencyOperations.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ extension InterModuleDependencyGraph {
5757
var topologicalSorting: [ModuleDependencyId] {
5858
get throws {
5959
try topologicalSort(Array(modules.keys),
60-
successors: { try moduleInfo(of: $0).directDependencies! })
60+
successors: {
61+
var dependencies: [ModuleDependencyId] = []
62+
let moduleInfo = try moduleInfo(of: $0)
63+
dependencies.append(contentsOf: moduleInfo.directDependencies ?? [])
64+
if case .swift(let swiftModuleDetails) = moduleInfo.details {
65+
dependencies.append(contentsOf: swiftModuleDetails.swiftOverlayDependencies ?? [])
66+
}
67+
return dependencies
68+
})
6169
}
6270
}
6371

@@ -71,7 +79,7 @@ extension InterModuleDependencyGraph {
7179
/// T(v) = T(v) ∪ T(w)
7280
/// }
7381
/// }
74-
func computeTransitiveClosure() throws -> [ModuleDependencyId : Set<ModuleDependencyId>] {
82+
@_spi(Testing) public func computeTransitiveClosure() throws -> [ModuleDependencyId : Set<ModuleDependencyId>] {
7583
let topologicalIdList = try self.topologicalSorting
7684
// This structure will contain the final result
7785
var transitiveClosureMap =

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,18 @@ final class ExplicitModuleBuildTests: XCTestCase {
17911791
XCTAssertTrue(scanJobCommand.contains(swiftInputWithoutSpace))
17921792
}
17931793

1794+
func testDependencyGraphTransitiveClosure() throws {
1795+
let moduleDependencyGraph =
1796+
try JSONDecoder().decode(
1797+
InterModuleDependencyGraph.self,
1798+
from: ModuleDependenciesInputs.simpleDependencyGraphInputWithSwiftOverlayDep.data(using: .utf8)!)
1799+
let reachabilityMap = try moduleDependencyGraph.computeTransitiveClosure()
1800+
let mainModuleDependencies = try XCTUnwrap(reachabilityMap[.swift("simpleTestModule")])
1801+
let aModuleDependencies = try XCTUnwrap(reachabilityMap[.swift("A")])
1802+
XCTAssertTrue(mainModuleDependencies.contains(.swift("B")))
1803+
XCTAssertTrue(aModuleDependencies.contains(.swift("B")))
1804+
}
1805+
17941806
func testExplicitSwiftModuleMap() throws {
17951807
let jsonExample : String = """
17961808
[

Tests/SwiftDriverTests/Inputs/ExplicitModuleDependencyBuildInputs.swift

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,112 @@ enum ModuleDependenciesInputs {
515515
"""
516516
}
517517

518+
static var simpleDependencyGraphInputWithSwiftOverlayDep: String {
519+
"""
520+
{
521+
"mainModuleName": "simpleTestModule",
522+
"modules": [
523+
{
524+
"swift": "A"
525+
},
526+
{
527+
"modulePath": "A.swiftmodule",
528+
"sourceFiles": [
529+
],
530+
"directDependencies": [
531+
{
532+
"clang": "B"
533+
}
534+
],
535+
"details": {
536+
"swift": {
537+
"moduleInterfacePath": "A.swiftmodule/A.swiftinterface",
538+
"isFramework": false,
539+
"extraPcmArgs": [
540+
"-Xcc",
541+
"-fapinotes-swift-version=5"
542+
],
543+
"swiftOverlayDependencies": [
544+
{
545+
"swift": "B"
546+
}
547+
]
548+
}
549+
}
550+
},
551+
{
552+
"swift": "simpleTestModule"
553+
},
554+
{
555+
"modulePath": "simpleTestModule.swiftmodule",
556+
"sourceFiles": [
557+
"/main/simpleTestModule.swift"
558+
],
559+
"directDependencies": [
560+
{
561+
"swift": "A"
562+
},
563+
],
564+
"details": {
565+
"swift": {
566+
"isFramework": false,
567+
"extraPcmArgs": [
568+
"-Xcc",
569+
"-fapinotes-swift-version=5"
570+
]
571+
}
572+
}
573+
},
574+
{
575+
"swift" : "B"
576+
},
577+
{
578+
"modulePath" : "B.swiftmodule",
579+
"sourceFiles": [
580+
],
581+
"directDependencies" : [
582+
583+
],
584+
"details" : {
585+
"swift" : {
586+
"moduleInterfacePath": "B.swiftmodule/B.swiftinterface",
587+
"isFramework": false,
588+
"extraPcmArgs": [
589+
"-Xcc",
590+
"-fapinotes-swift-version=5"
591+
],
592+
}
593+
}
594+
},
595+
{
596+
"clang": "B"
597+
},
598+
{
599+
"modulePath": "B.pcm",
600+
"sourceFiles": [
601+
"/B/module.map",
602+
"/B/include/b.h"
603+
],
604+
"directDependencies": [
605+
],
606+
"details": {
607+
"clang": {
608+
"moduleMapPath": "/B/module.map",
609+
"contextHash": "2QEMRLNY63H2N",
610+
"commandLine": [
611+
"-remove-preceeding-explicit-module-build-incompatible-options",
612+
"-fno-implicit-modules",
613+
"-emit-module",
614+
"-fmodule-name=c_simd"
615+
]
616+
}
617+
}
618+
}
619+
]
620+
}
621+
"""
622+
}
623+
518624
static var mergeGraphInput2: String {
519625
"""
520626
{

0 commit comments

Comments
 (0)