Skip to content

Add options for debug printing of the explicit module dependency graph. #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

import class Foundation.JSONEncoder

/// A map from a module identifier to its info
public typealias ModuleInfoMap = [ModuleDependencyId: ModuleInfo]

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

internal extension InterModuleDependencyGraph {
func toJSONString() throws -> String {
let encoder = JSONEncoder()
#if os(Linux) || os(Android)
encoder.outputFormatting = [.prettyPrinted]
#else
if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
}
#endif
let data = try encoder.encode(self)
return String(data: data, encoding: .utf8)!
}
}

public struct InterModuleDependencyImports: Codable {
public var imports: [String]

Expand Down
10 changes: 10 additions & 0 deletions Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,11 @@ extension Driver {
throws -> InterModuleDependencyGraph {
var dependencyGraph = try performDependencyScan()

if parsedOptions.hasArgument(.printPreprocessedExplicitDependencyGraph) {
try stdoutStream <<< dependencyGraph.toJSONString()
stdoutStream.flush()
}

if let externalTargetDetails = externalTargetModuleDetailsMap {
// Resolve external dependencies in the dependency graph, if any.
try dependencyGraph.resolveExternalDependencies(for: externalTargetDetails)
Expand All @@ -648,6 +653,11 @@ extension Driver {
// Set dependency modules' paths to be saved in the module cache.
try resolveDependencyModulePaths(dependencyGraph: &dependencyGraph)

if parsedOptions.hasArgument(.printExplicitDependencyGraph) {
try stdoutStream <<< dependencyGraph.toJSONString()
stdoutStream.flush()
}

return dependencyGraph
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftOptions/ExtraOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extension Option {
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>")
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>")
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")
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")
public static let printPreprocessedExplicitDependencyGraph: Option = Option("-print-preprocessed-explicit-dependency-graph", .flag, attributes: [.helpHidden], helpText: "Print the result of module dependency scanning to output")

// API digester operations
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")
Expand All @@ -42,6 +44,8 @@ extension Option {
Option.emitModuleSeparatelyWMO,
Option.noEmitModuleSeparatelyWMO,
Option.useFrontendParseableOutput,
Option.printExplicitDependencyGraph,
Option.printPreprocessedExplicitDependencyGraph,
Option.emitDigesterBaseline,
Option.emitDigesterBaselinePath,
Option.compareToBaselinePath,
Expand Down