-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Output Swift Build PIF JSON for Graphviz visualization #8539
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec3b099
Output Swift Build PIF as Graphviz
pmattos ce5d21d
Increase DPI to improve text quality
pmattos 1c8f65c
Minor typo
pmattos 7a781fb
Remove dependency on ArgumentParser from SwiftBuildSupport
pmattos 036f5d8
Typo
pmattos 8b29909
Add FreeBSD support now in #8550
pmattos 2b263cd
PR feedback: Replace Radar with GitHub issue instead
pmattos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
// | ||
// DotPIFSerializer.swift | ||
// SwiftPM | ||
// | ||
// Created by Paulo Mattos on 2025-04-18. | ||
// | ||
|
||
import Basics | ||
import Foundation | ||
import protocol TSCBasic.OutputByteStream | ||
|
||
#if canImport(SwiftBuild) | ||
import SwiftBuild | ||
|
||
/// Serializes the specified PIF as a **Graphviz** directed graph. | ||
/// | ||
/// * [DOT command line](https://graphviz.org/doc/info/command.html) | ||
/// * [DOT language specs](https://graphviz.org/doc/info/lang.html) | ||
func writePIF(_ workspace: PIF.Workspace, toDOT outputStream: OutputByteStream) { | ||
var graph = DotPIFSerializer() | ||
|
||
graph.node( | ||
id: workspace.id, | ||
label: "<workspace>\n\(workspace.id)", | ||
shape: "box3d", | ||
color: .black, | ||
fontsize: 7 | ||
) | ||
|
||
for project in workspace.projects.map(\.underlying) { | ||
graph.edge(from: workspace.id, to: project.id, color: .lightskyblue) | ||
graph.node( | ||
id: project.id, | ||
label: "<project>\n\(project.id)", | ||
shape: "box3d", | ||
color: .gray56, | ||
fontsize: 7 | ||
) | ||
|
||
for target in project.targets { | ||
graph.edge(from: project.id, to: target.id, color: .lightskyblue) | ||
|
||
switch target { | ||
case .target(let target): | ||
graph.node( | ||
id: target.id, | ||
label: "<target>\n\(target.id)\nproduct type: \(target.productType)\n\(target.buildPhases.summary)", | ||
shape: "box", | ||
color: .gray88, | ||
fontsize: 5 | ||
) | ||
|
||
case .aggregate: | ||
graph.node( | ||
id: target.id, | ||
label: "<aggregate target>\n\(target.id)", | ||
shape: "folder", | ||
color: .gray88, | ||
fontsize: 5, | ||
style: "bold" | ||
) | ||
} | ||
|
||
for targetDependency in target.common.dependencies { | ||
let linked = target.isLinkedAgainst(dependencyId: targetDependency.targetId) | ||
graph.edge(from: target.id, to: targetDependency.targetId, color: .gray40, style: linked ? "filled" : "dotted") | ||
} | ||
} | ||
} | ||
|
||
graph.write(to: outputStream) | ||
} | ||
|
||
fileprivate struct DotPIFSerializer { | ||
private var objects: [String] = [] | ||
|
||
mutating func write(to outputStream: OutputByteStream) { | ||
func write(_ object: String) { outputStream.write("\(object)\n") } | ||
|
||
write("digraph PIF {") | ||
write(" dpi=400;") // i.e., MacBook Pro 16" is 226 pixels per inch (3072 x 1920). | ||
for object in objects { | ||
write(" \(object);") | ||
} | ||
write("}") | ||
} | ||
|
||
mutating func node( | ||
id: PIF.GUID, | ||
label: String? = nil, | ||
shape: String? = nil, | ||
color: Color? = nil, | ||
fontname: String? = "SF Mono Light", | ||
fontsize: Int? = nil, | ||
style: String? = nil, | ||
margin: Int? = nil | ||
) { | ||
var attributes: [String] = [] | ||
|
||
if let label { attributes.append("label=\(label.quote)") } | ||
if let shape { attributes.append("shape=\(shape)") } | ||
if let color { attributes.append("color=\(color)") } | ||
|
||
if let fontname { attributes.append("fontname=\(fontname.quote)") } | ||
if let fontsize { attributes.append("fontsize=\(fontsize)") } | ||
|
||
if let style { attributes.append("style=\(style)") } | ||
if let margin { attributes.append("margin=\(margin)") } | ||
|
||
var node = "\(id.quote)" | ||
if !attributes.isEmpty { | ||
let attributesList = attributes.joined(separator: ", ") | ||
node += " [\(attributesList)]" | ||
} | ||
objects.append(node) | ||
} | ||
|
||
mutating func edge( | ||
from left: PIF.GUID, | ||
to right: PIF.GUID, | ||
color: Color? = nil, | ||
style: String? = nil | ||
) { | ||
var attributes: [String] = [] | ||
|
||
if let color { attributes.append("color=\(color)") } | ||
if let style { attributes.append("style=\(style)") } | ||
|
||
var edge = "\(left.quote) -> \(right.quote)" | ||
if !attributes.isEmpty { | ||
let attributesList = attributes.joined(separator: ", ") | ||
edge += " [\(attributesList)]" | ||
} | ||
objects.append(edge) | ||
} | ||
|
||
/// Graphviz default color scheme is **X11**: | ||
/// * https://graphviz.org/doc/info/colors.html | ||
enum Color: String { | ||
case black | ||
case gray | ||
case gray40 | ||
case gray56 | ||
case gray88 | ||
case lightskyblue | ||
} | ||
} | ||
|
||
// MARK: - Helpers | ||
|
||
fileprivate extension ProjectModel.BaseTarget { | ||
func isLinkedAgainst(dependencyId: ProjectModel.GUID) -> Bool { | ||
for buildPhase in self.common.buildPhases { | ||
switch buildPhase { | ||
case .frameworks(let frameworksPhase): | ||
for buildFile in frameworksPhase.files { | ||
switch buildFile.ref { | ||
case .reference(let id): | ||
if dependencyId == id { return true } | ||
case .targetProduct(let id): | ||
if dependencyId == id { return true } | ||
} | ||
} | ||
|
||
case .sources, .shellScript, .headers, .copyFiles, .copyBundleResources: | ||
break | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
|
||
fileprivate extension [ProjectModel.BuildPhase] { | ||
var summary: String { | ||
var phases: [String] = [] | ||
|
||
for buildPhase in self { | ||
switch buildPhase { | ||
case .sources(let sourcesPhase): | ||
var sources = "sources: " | ||
if sourcesPhase.files.count == 1 { | ||
sources += "1 source file" | ||
} else { | ||
sources += "\(sourcesPhase.files.count) source files" | ||
} | ||
phases.append(sources) | ||
|
||
case .frameworks(let frameworksPhase): | ||
var frameworks = "frameworks: " | ||
if frameworksPhase.files.count == 1 { | ||
frameworks += "1 linked target" | ||
} else { | ||
frameworks += "\(frameworksPhase.files.count) linked targets" | ||
} | ||
phases.append(frameworks) | ||
|
||
case .shellScript: | ||
phases.append("shellScript: 1 shell script") | ||
|
||
case .headers, .copyFiles, .copyBundleResources: | ||
break | ||
} | ||
} | ||
|
||
guard !phases.isEmpty else { return "" } | ||
return phases.joined(separator: "\n") | ||
} | ||
} | ||
|
||
fileprivate extension PIF.GUID { | ||
var quote: String { | ||
self.value.quote | ||
} | ||
} | ||
|
||
fileprivate extension String { | ||
/// Quote the name and escape the quotes and backslashes. | ||
var quote: String { | ||
"\"" + self | ||
.replacing("\"", with: "\\\"") | ||
.replacing("\\", with: "\\\\") | ||
.replacing("\n", with: "\\n") + | ||
"\"" | ||
} | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main entry point for construction the Graphviz compatible PIF graph.