Skip to content

Commit e6739d3

Browse files
committed
Public API for getting information about build targets
Until now, SourceKit-LSP has been using a couple of internal data structures of the build plan, which were incidentally public, for this. This changes those data structures to internal and exposes a new public API for accessing information about build targets, including those for plugins. rdar://112120976
1 parent 63e7362 commit e6739d3

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import struct SPMBuildCore.PrebuildCommandResult
2121
import enum TSCBasic.ProcessEnv
2222

2323
/// Target description for a Clang target i.e. C language family target.
24-
public final class ClangTargetBuildDescription {
24+
public final class ClangTargetBuildDescription: BuildTarget {
2525
/// The target described by this target.
2626
public let target: ResolvedTarget
2727

@@ -425,4 +425,14 @@ public final class ClangTargetBuildDescription {
425425
string: headerContent
426426
)
427427
}
428+
429+
// MARK: - `BuildTarget`
430+
431+
public var sources: [AbsolutePath] {
432+
return (try? compilePaths().map { $0.source }) ?? []
433+
}
434+
435+
public func compileArguments() throws -> [String] {
436+
return try self.basicArguments()
437+
}
428438
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import struct Basics.AbsolutePath
14+
import class PackageGraph.ResolvedTarget
15+
import class PackageLoading.ManifestLoader
16+
import struct PackageModel.ToolsVersion
17+
import class PackageModel.UserToolchain
18+
19+
struct PluginTargetBuildDescription: BuildTarget {
20+
public let target: ResolvedTarget
21+
private let toolsVersion: ToolsVersion
22+
23+
init(target: ResolvedTarget, toolsVersion: ToolsVersion) {
24+
assert(target.type == .plugin)
25+
self.target = target
26+
self.toolsVersion = toolsVersion
27+
}
28+
29+
var sources: [AbsolutePath] {
30+
return target.sources.paths
31+
}
32+
33+
func compileArguments() throws -> [String] {
34+
// FIXME: This is very odd and we should clean this up by merging `ManifestLoader` and `DefaultPluginScriptRunner` again.
35+
let loader = ManifestLoader(toolchain: try UserToolchain(swiftSDK: .hostSwiftSDK()))
36+
return loader.interpreterFlags(for: self.toolsVersion)
37+
}
38+
39+
40+
}

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import SPMBuildCore
2121
import struct TSCBasic.ByteString
2222

2323
/// Target description for a Swift target.
24-
public final class SwiftTargetBuildDescription {
24+
public final class SwiftTargetBuildDescription: BuildTarget {
2525
/// The package this target belongs to.
2626
public let package: ResolvedPackage
2727

Sources/Build/BuildDescription/TargetBuildDescription.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,10 @@ public enum TargetBuildDescription {
9292
}
9393
}
9494
}
95+
96+
public protocol BuildTarget {
97+
var target: ResolvedTarget { get }
98+
var sources: [AbsolutePath] { get }
99+
100+
func compileArguments() throws -> [String]
101+
}

Sources/Build/BuildPlan.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,20 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
186186
}
187187

188188
/// The package graph.
189-
public let graph: PackageGraph
189+
let graph: PackageGraph
190190

191191
/// The target build description map.
192-
public let targetMap: [ResolvedTarget: TargetBuildDescription]
192+
let targetMap: [ResolvedTarget: TargetBuildDescription]
193193

194194
/// The product build description map.
195-
public let productMap: [ResolvedProduct: ProductBuildDescription]
195+
let productMap: [ResolvedProduct: ProductBuildDescription]
196196

197197
/// The plugin descriptions. Plugins are represented in the package graph
198198
/// as targets, but they are not directly included in the build graph.
199-
public let pluginDescriptions: [PluginDescription]
199+
let pluginDescriptions: [PluginDescription]
200200

201201
/// The build targets.
202-
public var targets: AnySequence<TargetBuildDescription> {
202+
var targets: AnySequence<TargetBuildDescription> {
203203
return AnySequence(targetMap.values)
204204
}
205205

@@ -209,11 +209,11 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
209209
}
210210

211211
/// The results of invoking any build tool plugins used by targets in this build.
212-
public let buildToolPluginInvocationResults: [ResolvedTarget: [BuildToolPluginInvocationResult]]
212+
let buildToolPluginInvocationResults: [ResolvedTarget: [BuildToolPluginInvocationResult]]
213213

214214
/// The results of running any prebuild commands for the targets in this build. This includes any derived
215215
/// source files as well as directories to which any changes should cause us to reevaluate the build plan.
216-
public let prebuildCommandResults: [ResolvedTarget: [PrebuildCommandResult]]
216+
let prebuildCommandResults: [ResolvedTarget: [PrebuildCommandResult]]
217217

218218
private var derivedTestTargetsMap: [ResolvedProduct: [ResolvedTarget]] = [:]
219219

@@ -1040,6 +1040,22 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
10401040
return execInfos.filter{!$0.supportedTriples.isEmpty}
10411041
}
10421042
}
1043+
1044+
public func getBuildTarget(for target: ResolvedTarget) -> BuildTarget? {
1045+
if let description = targetMap[target] {
1046+
switch description {
1047+
case .clang(let description):
1048+
return description
1049+
case .swift(let description):
1050+
return description
1051+
}
1052+
} else {
1053+
if target.type == .plugin, let package = self.graph.package(for: target) {
1054+
return PluginTargetBuildDescription(target: target, toolsVersion: package.manifest.toolsVersion)
1055+
}
1056+
return nil
1057+
}
1058+
}
10431059
}
10441060

10451061
private extension PackageModel.SwiftTarget {

0 commit comments

Comments
 (0)