|
1 | 1 | /*
|
2 | 2 | This source file is part of the Swift.org open source project
|
3 | 3 |
|
4 |
| - Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 4 | + Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
5 | 5 | Licensed under Apache License v2.0 with Runtime Library Exception
|
6 | 6 |
|
7 | 7 | See http://swift.org/LICENSE.txt for license information
|
8 | 8 | See http://swift.org/CONTRIBUTORS.txt for Swift project authors
|
9 | 9 | */
|
10 | 10 |
|
11 |
| -import Foundation |
12 |
| - |
13 |
| -import TSCBasic |
14 |
| -import TSCUtility |
15 |
| - |
16 |
| -import SPMBuildCore |
| 11 | +import ArgumentParser |
17 | 12 | import Build
|
18 | 13 | import PackageGraph
|
19 | 14 | import PackageModel
|
20 |
| -import SourceControl |
21 |
| -import Workspace |
22 |
| -import ArgumentParser |
| 15 | +import SPMBuildCore |
| 16 | +import TSCBasic |
| 17 | +import TSCUtility |
23 | 18 |
|
24 | 19 | /// A wrapper for swift-symbolgraph-extract tool.
|
25 | 20 | public struct SymbolGraphExtract {
|
26 | 21 | let tool: AbsolutePath
|
27 |
| - |
28 |
| - init(tool: AbsolutePath) { |
29 |
| - self.tool = tool |
| 22 | + var skipSynthesizedMembers: Bool = false |
| 23 | + var minimumAccessLevel: AccessLevel = .public |
| 24 | + var skipInheritedDocs: Bool = false |
| 25 | + var includeSPISymbols: Bool = false |
| 26 | + var prettyPrintOutputJSON: Bool = false |
| 27 | + |
| 28 | + /// Access control levels. |
| 29 | + public enum AccessLevel: String, RawRepresentable, CustomStringConvertible, CaseIterable, ExpressibleByArgument { |
| 30 | + // The cases reflect those found in `include/swift/AST/AttrKind.h` of the swift compiler (at commit 03f55d7bb4204ca54841218eb7cc175ae798e3bd) |
| 31 | + case `private`, `fileprivate`, `internal`, `public`, `open` |
| 32 | + |
| 33 | + public var description: String { rawValue } |
30 | 34 | }
|
31 | 35 |
|
32 |
| - public func dumpSymbolGraph( |
| 36 | + /// Creates a symbol graph for `target` in `outputDirectory` using the build information from `buildPlan`. The `outputDirection` determines how the output from the tool subprocess is handled, and `verbosity` specifies how much console output to ask the tool to emit. |
| 37 | + public func extractSymbolGraph( |
| 38 | + target: ResolvedTarget, |
33 | 39 | buildPlan: BuildPlan,
|
34 |
| - prettyPrint: Bool, |
35 |
| - skipSynthesisedMembers: Bool, |
36 |
| - minimumAccessLevel: AccessLevel, |
37 |
| - skipInheritedDocs: Bool, |
38 |
| - includeSPISymbols: Bool |
| 40 | + outputRedirection: Process.OutputRedirection = .none, |
| 41 | + verbose: Bool, |
| 42 | + outputDirectory: AbsolutePath |
39 | 43 | ) throws {
|
40 | 44 | let buildParameters = buildPlan.buildParameters
|
41 |
| - let symbolGraphDirectory = buildPlan.buildParameters.symbolGraph |
42 |
| - try localFileSystem.createDirectory(symbolGraphDirectory, recursive: true) |
43 |
| - |
44 |
| - // Run the tool for each target in the root package. |
45 |
| - let targets = buildPlan.graph.rootPackages.flatMap{ $0.targets }.filter{ $0.type == .library || $0.type == .executable } |
46 |
| - for target in targets { |
47 |
| - var args = [String]() |
48 |
| - args += ["-module-name", target.c99name] |
49 |
| - args += try buildParameters.targetTripleArgs(for: target) |
50 |
| - |
51 |
| - args += buildPlan.createAPIToolCommonArgs(includeLibrarySearchPaths: true) |
52 |
| - args += ["-module-cache-path", buildParameters.moduleCache.pathString] |
53 |
| - |
54 |
| - args += ["-output-dir", symbolGraphDirectory.pathString] |
55 |
| - |
56 |
| - if prettyPrint { args.append("-pretty-print") } |
57 |
| - if skipSynthesisedMembers { args.append("-skip-synthesized-members") } |
58 |
| - if minimumAccessLevel != SwiftPackageTool.DumpSymbolGraph.defaultMinimumAccessLevel { |
59 |
| - args += ["-minimum-access-level", minimumAccessLevel.rawValue] |
60 |
| - } |
61 |
| - if skipInheritedDocs { args.append("-skip-inherited-docs") } |
62 |
| - if includeSPISymbols { args.append("-include-spi-symbols") } |
63 |
| - |
64 |
| - print("-- Emitting symbol graph for", target.name) |
65 |
| - try runTool(args) |
| 45 | + try localFileSystem.createDirectory(outputDirectory, recursive: true) |
| 46 | + |
| 47 | + // Construct arguments for extracting symbols for a single target. |
| 48 | + var commandLine = [self.tool.pathString] |
| 49 | + commandLine += ["-module-name", target.c99name] |
| 50 | + commandLine += try buildParameters.targetTripleArgs(for: target) |
| 51 | + commandLine += buildPlan.createAPIToolCommonArgs(includeLibrarySearchPaths: true) |
| 52 | + commandLine += ["-module-cache-path", buildParameters.moduleCache.pathString] |
| 53 | + if verbose { |
| 54 | + commandLine += ["-v"] |
66 | 55 | }
|
67 |
| - print("Files written to", symbolGraphDirectory.pathString) |
68 |
| - } |
| 56 | + commandLine += ["-minimum-access-level", minimumAccessLevel.rawValue] |
| 57 | + if skipSynthesizedMembers { |
| 58 | + commandLine += ["-skip-synthesized-members"] |
| 59 | + } |
| 60 | + if skipInheritedDocs { |
| 61 | + commandLine += ["-skip-inherited-docs"] |
| 62 | + } |
| 63 | + if includeSPISymbols { |
| 64 | + commandLine += ["-include-spi-symbols"] |
| 65 | + } |
| 66 | + if prettyPrintOutputJSON { |
| 67 | + commandLine += ["-pretty-print"] |
| 68 | + } |
| 69 | + commandLine += ["-output-dir", outputDirectory.pathString] |
69 | 70 |
|
70 |
| - func runTool(_ args: [String]) throws { |
71 |
| - let arguments = [tool.pathString] + args |
| 71 | + // Run the extraction. |
72 | 72 | let process = Process(
|
73 |
| - arguments: arguments, |
74 |
| - outputRedirection: .none, |
75 |
| - verbose: verbosity != .concise |
76 |
| - ) |
| 73 | + arguments: commandLine, |
| 74 | + outputRedirection: outputRedirection, |
| 75 | + verbose: verbose) |
77 | 76 | try process.launch()
|
78 | 77 | try process.waitUntilExit()
|
79 | 78 | }
|
80 | 79 | }
|
81 |
| - |
82 |
| -/// Access control levels. |
83 |
| -public enum AccessLevel: String, RawRepresentable, CustomStringConvertible, CaseIterable { |
84 |
| - // The cases reflect those found in `include/swift/AST/AttrKind.h` of the swift compiler (at commit 03f55d7bb4204ca54841218eb7cc175ae798e3bd) |
85 |
| - case `private`, `fileprivate`, `internal`, `public`, `open` |
86 |
| - |
87 |
| - public var description: String { rawValue } |
88 |
| -} |
89 |
| - |
90 |
| -extension AccessLevel: ExpressibleByArgument {} |
91 |
| - |
92 |
| -extension BuildParameters { |
93 |
| - /// The directory containing artifacts generated by the symbolgraph-extract tool. |
94 |
| - var symbolGraph: AbsolutePath { |
95 |
| - dataPath.appending(component: "symbolgraph") |
96 |
| - } |
97 |
| -} |
0 commit comments