Skip to content

Commit 148c20a

Browse files
committed
Add options for debug printing of the explicit module dependency graph.
Add '-print-explicit-dependency-graph' to output the final dependency graph as JSON and '-print-preprocessed-explicit-dependency-graph' to output the initial dependency graph before external (placeholder) dependencies are resolved.
1 parent c40cbd5 commit 148c20a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/InterModuleDependencies/InterModuleDependencyGraph.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import class Foundation.JSONEncoder
14+
1315
/// A map from a module identifier to its info
1416
public typealias ModuleInfoMap = [ModuleDependencyId: ModuleInfo]
1517

@@ -272,6 +274,21 @@ public struct InterModuleDependencyGraph: Codable {
272274
public var mainModule: ModuleInfo { modules[.swift(mainModuleName)]! }
273275
}
274276

277+
internal extension InterModuleDependencyGraph {
278+
func toJSONString() throws -> String {
279+
let encoder = JSONEncoder()
280+
#if os(Linux) || os(Android)
281+
encoder.outputFormatting = [.prettyPrinted]
282+
#else
283+
if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
284+
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
285+
}
286+
#endif
287+
let data = try encoder.encode(self)
288+
return String(data: data, encoding: .utf8)!
289+
}
290+
}
291+
275292
public struct InterModuleDependencyImports: Codable {
276293
public var imports: [String]
277294

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ extension Driver {
637637
throws -> InterModuleDependencyGraph {
638638
var dependencyGraph = try performDependencyScan()
639639

640+
if parsedOptions.hasArgument(.printPreprocessedExplicitDependencyGraph) {
641+
try stdoutStream <<< dependencyGraph.toJSONString()
642+
stdoutStream.flush()
643+
}
644+
640645
if let externalTargetDetails = externalTargetModuleDetailsMap {
641646
// Resolve external dependencies in the dependency graph, if any.
642647
try dependencyGraph.resolveExternalDependencies(for: externalTargetDetails)
@@ -648,6 +653,11 @@ extension Driver {
648653
// Set dependency modules' paths to be saved in the module cache.
649654
try resolveDependencyModulePaths(dependencyGraph: &dependencyGraph)
650655

656+
if parsedOptions.hasArgument(.printExplicitDependencyGraph) {
657+
try stdoutStream <<< dependencyGraph.toJSONString()
658+
stdoutStream.flush()
659+
}
660+
651661
return dependencyGraph
652662
}
653663

Sources/SwiftOptions/ExtraOptions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extension Option {
2222
public static let emitModuleSerializeDiagnosticsPath: Option = Option("-emit-module-serialize-diagnostics-path", .separate, attributes: [.argumentIsPath, .supplementaryOutput], metaVar: "<path>", helpText: "Emit a serialized diagnostics file for the emit-module task to <path>")
2323
public static let emitModuleDependenciesPath: Option = Option("-emit-module-dependencies-path", .separate, attributes: [.argumentIsPath, .supplementaryOutput], metaVar: "<path>", helpText: "Emit a discovered dependencies file for the emit-module task to <path>")
2424
public static let useFrontendParseableOutput: Option = Option("-use-frontend-parseable-output", .flag, attributes: [.helpHidden], helpText: "Emit parseable-output from swift-frontend jobs instead of from the driver")
25+
public static let printExplicitDependencyGraph: Option = Option("-print-explicit-dependency-graph", .flag, attributes: [.helpHidden], helpText: "Print the result of module dependency scanning after external module resolution to output")
26+
public static let printPreprocessedExplicitDependencyGraph: Option = Option("-print-preprocessed-explicit-dependency-graph", .flag, attributes: [.helpHidden], helpText: "Print the result of module dependency scanning to output")
2527

2628
// API digester operations
2729
public static let emitDigesterBaseline: Option = Option("-emit-digester-baseline", .flag, attributes: [.noInteractive, .supplementaryOutput], helpText: "Emit a baseline file for the module using the API digester")
@@ -42,6 +44,8 @@ extension Option {
4244
Option.emitModuleSeparatelyWMO,
4345
Option.noEmitModuleSeparatelyWMO,
4446
Option.useFrontendParseableOutput,
47+
Option.printExplicitDependencyGraph,
48+
Option.printPreprocessedExplicitDependencyGraph,
4549
Option.emitDigesterBaseline,
4650
Option.emitDigesterBaselinePath,
4751
Option.compareToBaselinePath,

0 commit comments

Comments
 (0)