Skip to content

Commit fcb5bad

Browse files
authored
Add API needed for background indexing in SourceKit-LSP (#7562)
- **Explanation**: Adds a couple of APIs to the `SourceKitLSPAPI` module that are needed for background indexing in SourceKit-LSP - **Scope**: Purely additive API - **Risk**: Very low, just adds new API - **Testing**: n/a - **Issue**: n/a - **Reviewer**: @MaxDesiatov and @bnbarham on #7540 #7555 respectively
1 parent 4f19176 commit fcb5bad

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public struct ModulesGraph {
7474
/// Returns all the targets in the graph, regardless if they are reachable from the root targets or not.
7575
public let allTargets: IdentifiableSet<ResolvedTarget>
7676

77+
/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
78+
/// dependencies).
79+
package var allTargetsInTopologicalOrder: [ResolvedTarget] {
80+
get throws {
81+
try topologicalSort(Array(allTargets)) { $0.dependencies.compactMap { $0.target } }.reversed()
82+
}
83+
}
84+
7785
/// Returns all the products in the graph, regardless if they are reachable from the root targets or not.
7886

7987
public let allProducts: IdentifiableSet<ResolvedProduct>

Sources/SourceKitLSPAPI/BuildDescription.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ import class Build.ClangTargetBuildDescription
2323
import class Build.SwiftTargetBuildDescription
2424
import struct PackageGraph.ResolvedTarget
2525
import struct PackageGraph.ModulesGraph
26+
import enum PackageGraph.BuildTriple
27+
28+
public typealias BuildTriple = PackageGraph.BuildTriple
2629

2730
public protocol BuildTarget {
2831
var sources: [URL] { get }
2932

33+
/// The name of the target. It should be possible to build a target by passing this name to `swift build --target`
34+
var name: String { get }
35+
36+
var buildTriple: BuildTriple { get }
37+
3038
/// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
3139
var isPartOfRootPackage: Bool { get }
3240

@@ -46,6 +54,14 @@ private struct WrappedClangTargetBuildDescription: BuildTarget {
4654
return (try? description.compilePaths().map { URL(fileURLWithPath: $0.source.pathString) }) ?? []
4755
}
4856

57+
public var name: String {
58+
return description.clangTarget.name
59+
}
60+
61+
public var buildTriple: BuildTriple {
62+
return description.target.buildTriple
63+
}
64+
4965
public func compileArguments(for fileURL: URL) throws -> [String] {
5066
let filePath = try resolveSymlinks(try AbsolutePath(validating: fileURL.path))
5167
let commandLine = try description.emitCommandLine(for: filePath)
@@ -63,6 +79,14 @@ private struct WrappedSwiftTargetBuildDescription: BuildTarget {
6379
self.isPartOfRootPackage = isPartOfRootPackage
6480
}
6581

82+
public var name: String {
83+
return description.target.name
84+
}
85+
86+
public var buildTriple: BuildTriple {
87+
return description.target.buildTriple
88+
}
89+
6690
var sources: [URL] {
6791
return description.sources.map { URL(fileURLWithPath: $0.pathString) }
6892
}
@@ -110,4 +134,12 @@ public struct BuildDescription {
110134
return nil
111135
}
112136
}
137+
138+
/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
139+
/// dependencies).
140+
public func allTargetsInTopologicalOrder(in modulesGraph: ModulesGraph) throws -> [BuildTarget] {
141+
try modulesGraph.allTargetsInTopologicalOrder.compactMap {
142+
getBuildTarget(for: $0, in: modulesGraph)
143+
}
144+
}
113145
}

Sources/SourceKitLSPAPI/PluginTargetBuildDescription.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import struct PackageGraph.ResolvedTarget
1717
private import class PackageLoading.ManifestLoader
1818
internal import struct PackageModel.ToolsVersion
1919
private import class PackageModel.UserToolchain
20+
import enum PackageGraph.BuildTriple
2021

2122
struct PluginTargetBuildDescription: BuildTarget {
2223
private let target: ResolvedTarget
@@ -34,6 +35,14 @@ struct PluginTargetBuildDescription: BuildTarget {
3435
return target.sources.paths.map { URL(fileURLWithPath: $0.pathString) }
3536
}
3637

38+
var name: String {
39+
return target.name
40+
}
41+
42+
var buildTriple: BuildTriple {
43+
return target.buildTriple
44+
}
45+
3746
func compileArguments(for fileURL: URL) throws -> [String] {
3847
// FIXME: This is very odd and we should clean this up by merging `ManifestLoader` and `DefaultPluginScriptRunner` again.
3948
let loader = ManifestLoader(toolchain: try UserToolchain(swiftSDK: .hostSwiftSDK()))

0 commit comments

Comments
 (0)