Skip to content

Commit 01de656

Browse files
authored
Merge branch 'main' into es-flag
2 parents a66129a + 99cb8b1 commit 01de656

File tree

6 files changed

+137
-5
lines changed

6 files changed

+137
-5
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,37 @@ extension Driver {
13701370
return
13711371
}
13721372

1373+
// If we're only supposed to explain a dependency on a given module, do so now.
1374+
if let explainModuleName = parsedOptions.getLastArgument(.explainModuleDependency) {
1375+
guard let dependencyPlanner = explicitDependencyBuildPlanner else {
1376+
fatalError("Cannot explain dependency without Explicit Build Planner")
1377+
}
1378+
guard let dependencyPaths = try dependencyPlanner.explainDependency(explainModuleName.asSingle) else {
1379+
diagnosticEngine.emit(.remark("No such module dependency found: '\(explainModuleName.asSingle)'"))
1380+
return
1381+
}
1382+
diagnosticEngine.emit(.remark("Module '\(moduleOutputInfo.name)' depends on '\(explainModuleName.asSingle)'"))
1383+
for path in dependencyPaths {
1384+
var pathString:String = ""
1385+
for (index, moduleId) in path.enumerated() {
1386+
switch moduleId {
1387+
case .swift(let moduleName):
1388+
pathString = pathString + "[" + moduleName + "]"
1389+
case .swiftPrebuiltExternal(let moduleName):
1390+
pathString = pathString + "[" + moduleName + "]"
1391+
case .clang(let moduleName):
1392+
pathString = pathString + "[" + moduleName + "](ObjC)"
1393+
case .swiftPlaceholder(_):
1394+
fatalError("Unexpected unresolved Placeholder module")
1395+
}
1396+
if index < path.count - 1 {
1397+
pathString = pathString + " -> "
1398+
}
1399+
}
1400+
diagnosticEngine.emit(.note(pathString))
1401+
}
1402+
}
1403+
13731404
if parsedOptions.contains(.driverPrintOutputFileMap) {
13741405
if let outputFileMap = self.outputFileMap {
13751406
stderrStream <<< outputFileMap.description

Sources/SwiftDriver/ExplicitModuleBuilds/ExplicitDependencyBuildPlanner.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
487487
}
488488
}
489489

490+
internal extension ExplicitDependencyBuildPlanner {
491+
func explainDependency(_ dependencyModuleName: String) throws -> [[ModuleDependencyId]]? {
492+
return try dependencyGraph.explainDependency(dependencyModuleName: dependencyModuleName)
493+
}
494+
}
495+
490496
// InterModuleDependencyGraph printing, useful for debugging
491497
internal extension InterModuleDependencyGraph {
492498
func prettyPrintString() throws -> String {

Sources/SwiftDriver/ExplicitModuleBuilds/InterModuleDependencies/CommonDependencyOperations.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,48 @@ extension InterModuleDependencyGraph {
235235
details: .clang(combinedModuleDetails))
236236
}
237237
}
238+
239+
internal extension InterModuleDependencyGraph {
240+
func explainDependency(dependencyModuleName: String) throws -> [[ModuleDependencyId]]? {
241+
guard modules.contains(where: { $0.key.moduleName == dependencyModuleName }) else { return nil }
242+
var results = [[ModuleDependencyId]]()
243+
try findAllPaths(source: .swift(mainModuleName),
244+
to: dependencyModuleName,
245+
pathSoFar: [.swift(mainModuleName)],
246+
results: &results)
247+
return Array(results)
248+
}
249+
250+
251+
private func findAllPaths(source: ModuleDependencyId,
252+
to moduleName: String,
253+
pathSoFar: [ModuleDependencyId],
254+
results: inout [[ModuleDependencyId]]) throws {
255+
let sourceInfo = try moduleInfo(of: source)
256+
// If the source is our target, we are done
257+
guard source.moduleName != moduleName else {
258+
// If the source is a target Swift module, also check if it
259+
// depends on a corresponding Clang module with the same name.
260+
// If it does, add it to the path as well.
261+
var completePath = pathSoFar
262+
if let dependencies = sourceInfo.directDependencies,
263+
dependencies.contains(.clang(moduleName)) {
264+
completePath.append(.clang(moduleName))
265+
}
266+
results.append(completePath)
267+
return
268+
}
269+
270+
// If no more dependencies, this is a leaf node, we are done
271+
guard let dependencies = sourceInfo.directDependencies else {
272+
return
273+
}
274+
275+
for dependency in dependencies {
276+
try findAllPaths(source: dependency,
277+
to: moduleName,
278+
pathSoFar: pathSoFar + [dependency],
279+
results: &results)
280+
}
281+
}
282+
}

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ extension Driver {
140140
IncrementalCompilationState.InitialStateForPlanning?)
141141
throws -> InterModuleDependencyGraph? {
142142
let interModuleDependencyGraph: InterModuleDependencyGraph?
143-
if parsedOptions.contains(.driverExplicitModuleBuild) {
143+
if parsedOptions.contains(.driverExplicitModuleBuild) ||
144+
parsedOptions.contains(.explainModuleDependency) {
144145
interModuleDependencyGraph =
145146
try initialIncrementalState?.maybeUpToDatePriorInterModuleDependencyGraph ??
146147
gatherModuleDependencies()
@@ -195,13 +196,15 @@ extension Driver {
195196
dependencyGraph: InterModuleDependencyGraph?,
196197
addJob: (Job) -> Void)
197198
throws {
198-
// If asked, add jobs to precompile module dependencies
199-
guard parsedOptions.contains(.driverExplicitModuleBuild) else { return }
200199
guard let resolvedDependencyGraph = dependencyGraph else {
201-
fatalError("Attempting to plan explicit dependency build without a dependency graph")
200+
return
202201
}
203202
let modulePrebuildJobs =
204203
try generateExplicitModuleDependenciesJobs(dependencyGraph: resolvedDependencyGraph)
204+
// If asked, add jobs to precompile module dependencies. Otherwise exit.
205+
// We may have a dependency graph but not be required to add pre-compile jobs to the build plan,
206+
// for example when `-explain-dependency` is being used.
207+
guard parsedOptions.contains(.driverExplicitModuleBuild) else { return }
205208
modulePrebuildJobs.forEach(addJob)
206209
}
207210

Sources/makeOptions/makeOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ enum SwiftFlags {
6464
SwiftAPIDigesterOption = (1 << 17),
6565
NewDriverOnlyOption = (1 << 18),
6666
ModuleInterfaceOptionIgnorable = (1 << 19),
67-
ModuleInterfaceOptionIgnorablePrivate = (1 << 20),
67+
ModuleInterfaceOptionIgnorablePrivate = (1 << 20)
6868
};
6969

7070
static std::set<std::string> swiftKeywords = { "internal", "static" };

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,53 @@ final class ExplicitModuleBuildTests: XCTestCase {
16131613
XCTAssertEqual(moduleMap[1].sourceInfoPath!.path.description, "B.swiftsourceinfo")
16141614
XCTAssertEqual(moduleMap[1].isFramework, false)
16151615
}
1616+
1617+
1618+
func testTraceDependency() throws {
1619+
try withTemporaryDirectory { path in
1620+
try localFileSystem.changeCurrentWorkingDirectory(to: path)
1621+
let moduleCachePath = path.appending(component: "ModuleCache")
1622+
try localFileSystem.createDirectory(moduleCachePath)
1623+
let main = path.appending(component: "testTraceDependency.swift")
1624+
try localFileSystem.writeFileContents(main) {
1625+
$0 <<< "import C;"
1626+
$0 <<< "import E;"
1627+
$0 <<< "import G;"
1628+
}
1629+
1630+
let cHeadersPath: AbsolutePath =
1631+
testInputsPath.appending(component: "ExplicitModuleBuilds")
1632+
.appending(component: "CHeaders")
1633+
let swiftModuleInterfacesPath: AbsolutePath =
1634+
testInputsPath.appending(component: "ExplicitModuleBuilds")
1635+
.appending(component: "Swift")
1636+
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
1637+
var driver = try Driver(args: ["swiftc",
1638+
"-I", cHeadersPath.nativePathString(escaped: true),
1639+
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
1640+
"-explicit-module-build", "-v",
1641+
"-module-cache-path", moduleCachePath.nativePathString(escaped: true),
1642+
"-working-directory", path.nativePathString(escaped: true),
1643+
"-explain-module-dependency", "A",
1644+
main.nativePathString(escaped: true)] + sdkArgumentsForTesting,
1645+
env: ProcessEnv.vars)
1646+
let jobs = try driver.planBuild()
1647+
try driver.run(jobs: jobs)
1648+
XCTAssertTrue(!driver.diagnosticEngine.diagnostics.isEmpty)
1649+
XCTAssertTrue(driver.diagnosticEngine.diagnostics.contains { $0.behavior == .remark &&
1650+
$0.message.text == "Module 'testTraceDependency' depends on 'A'"})
1651+
1652+
for diag in driver.diagnosticEngine.diagnostics {
1653+
print(diag.behavior)
1654+
print(diag.message)
1655+
}
1656+
XCTAssertTrue(driver.diagnosticEngine.diagnostics.contains { $0.behavior == .note &&
1657+
$0.message.text == "[testTraceDependency] -> [A] -> [A](ObjC)"})
1658+
XCTAssertTrue(driver.diagnosticEngine.diagnostics.contains { $0.behavior == .note &&
1659+
$0.message.text == "[testTraceDependency] -> [C](ObjC) -> [B](ObjC) -> [A](ObjC)"})
1660+
}
1661+
}
1662+
16161663
// We only care about prebuilt modules in macOS.
16171664
#if os(macOS)
16181665
func testPrebuiltModuleGenerationJobs() throws {

0 commit comments

Comments
 (0)