Skip to content

Add API needed for background indexing in SourceKit-LSP #7562

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 3 commits into from
May 15, 2024
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
8 changes: 8 additions & 0 deletions Sources/PackageGraph/ModulesGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public struct ModulesGraph {
/// Returns all the targets in the graph, regardless if they are reachable from the root targets or not.
public let allTargets: IdentifiableSet<ResolvedTarget>

/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
/// dependencies).
package var allTargetsInTopologicalOrder: [ResolvedTarget] {
get throws {
try topologicalSort(Array(allTargets)) { $0.dependencies.compactMap { $0.target } }.reversed()
}
}

/// Returns all the products in the graph, regardless if they are reachable from the root targets or not.

public let allProducts: IdentifiableSet<ResolvedProduct>
Expand Down
32 changes: 32 additions & 0 deletions Sources/SourceKitLSPAPI/BuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ import class Build.ClangTargetBuildDescription
import class Build.SwiftTargetBuildDescription
import struct PackageGraph.ResolvedTarget
import struct PackageGraph.ModulesGraph
import enum PackageGraph.BuildTriple

public typealias BuildTriple = PackageGraph.BuildTriple

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

/// The name of the target. It should be possible to build a target by passing this name to `swift build --target`
var name: String { get }

var buildTriple: BuildTriple { get }

/// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
var isPartOfRootPackage: Bool { get }

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

public var name: String {
return description.clangTarget.name
}

public var buildTriple: BuildTriple {
return description.target.buildTriple
}

public func compileArguments(for fileURL: URL) throws -> [String] {
let filePath = try resolveSymlinks(try AbsolutePath(validating: fileURL.path))
let commandLine = try description.emitCommandLine(for: filePath)
Expand All @@ -63,6 +79,14 @@ private struct WrappedSwiftTargetBuildDescription: BuildTarget {
self.isPartOfRootPackage = isPartOfRootPackage
}

public var name: String {
return description.target.name
}

public var buildTriple: BuildTriple {
return description.target.buildTriple
}

var sources: [URL] {
return description.sources.map { URL(fileURLWithPath: $0.pathString) }
}
Expand Down Expand Up @@ -110,4 +134,12 @@ public struct BuildDescription {
return nil
}
}

/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
/// dependencies).
public func allTargetsInTopologicalOrder(in modulesGraph: ModulesGraph) throws -> [BuildTarget] {
try modulesGraph.allTargetsInTopologicalOrder.compactMap {
getBuildTarget(for: $0, in: modulesGraph)
}
}
}
9 changes: 9 additions & 0 deletions Sources/SourceKitLSPAPI/PluginTargetBuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import struct PackageGraph.ResolvedTarget
private import class PackageLoading.ManifestLoader
internal import struct PackageModel.ToolsVersion
private import class PackageModel.UserToolchain
import enum PackageGraph.BuildTriple

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

var name: String {
return target.name
}

var buildTriple: BuildTriple {
return target.buildTriple
}

func compileArguments(for fileURL: URL) throws -> [String] {
// FIXME: This is very odd and we should clean this up by merging `ManifestLoader` and `DefaultPluginScriptRunner` again.
let loader = ManifestLoader(toolchain: try UserToolchain(swiftSDK: .hostSwiftSDK()))
Expand Down