Skip to content

Commit 4e5b3a1

Browse files
committed
Use BuildTargetIdentifier from BSP instead of ConfiguredTarget
1 parent 48e15d9 commit 4e5b3a1

18 files changed

+238
-183
lines changed

Sources/BuildSystemIntegration/BuildServerBuildSystem.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
269269
/// server yet or if no build settings are available for this file.
270270
package func buildSettings(
271271
for document: DocumentURI,
272-
in target: ConfiguredTarget,
272+
in target: BuildTargetIdentifier,
273273
language: Language
274274
) async -> FileBuildSettings? {
275275
return buildSettings[document]
@@ -283,24 +283,24 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
283283
return nil
284284
}
285285

286-
package func configuredTargets(for document: DocumentURI) async -> [ConfiguredTarget] {
287-
return [ConfiguredTarget(targetID: "dummy", runDestinationID: "dummy")]
286+
package func targets(for document: DocumentURI) async -> [BuildTargetIdentifier] {
287+
return [BuildTargetIdentifier.dummy]
288288
}
289289

290290
package func generateBuildGraph() {}
291291

292292
package func waitForUpToDateBuildGraph() async {}
293293

294-
package func topologicalSort(of targets: [ConfiguredTarget]) async -> [ConfiguredTarget]? {
294+
package func topologicalSort(of targets: [BuildTargetIdentifier]) async -> [BuildTargetIdentifier]? {
295295
return nil
296296
}
297297

298-
package func targets(dependingOn targets: [ConfiguredTarget]) -> [ConfiguredTarget]? {
298+
package func targets(dependingOn targets: [BuildTargetIdentifier]) -> [BuildTargetIdentifier]? {
299299
return nil
300300
}
301301

302302
package func prepare(
303-
targets: [ConfiguredTarget],
303+
targets: [BuildTargetIdentifier],
304304
logMessageToIndexLog: @Sendable (_ taskID: IndexTaskID, _ message: String) -> Void
305305
) async throws {
306306
throw PrepareNotSupportedError()

Sources/BuildSystemIntegration/BuildSystem.swift

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,6 @@ package struct SourceFileInfo: Sendable {
5050
}
5151
}
5252

53-
/// A target / run destination combination. For example, a configured target can represent building the target
54-
/// `MyLibrary` for iOS.
55-
package struct ConfiguredTarget: Hashable, Sendable, CustomLogStringConvertible {
56-
/// An opaque string that represents the target.
57-
///
58-
/// The target's ID should be generated by the build system that handles the target and only interpreted by that
59-
/// build system.
60-
package let targetID: String
61-
62-
/// An opaque string that represents the run destination.
63-
///
64-
/// The run destination's ID should be generated by the build system that handles the target and only interpreted by
65-
/// that build system.
66-
package let runDestinationID: String
67-
68-
package init(targetID: String, runDestinationID: String) {
69-
self.targetID = targetID
70-
self.runDestinationID = runDestinationID
71-
}
72-
73-
package var description: String {
74-
"\(targetID)-\(runDestinationID)"
75-
}
76-
77-
package var redactedDescription: String {
78-
"\(targetID.hashForLogging)-\(runDestinationID.hashForLogging)"
79-
}
80-
}
81-
8253
/// An error build systems can throw from `prepare` if they don't support preparation of targets.
8354
package struct PrepareNotSupportedError: Error, CustomStringConvertible {
8455
package init() {}
@@ -135,12 +106,12 @@ package protocol BuiltInBuildSystem: AnyObject, Sendable {
135106
/// file or if it hasn't computed build settings for the file yet.
136107
func buildSettings(
137108
for document: DocumentURI,
138-
in target: ConfiguredTarget,
109+
in target: BuildTargetIdentifier,
139110
language: Language
140111
) async throws -> FileBuildSettings?
141112

142113
/// Return the list of targets and run destinations that the given document can be built for.
143-
func configuredTargets(for document: DocumentURI) async -> [ConfiguredTarget]
114+
func targets(for document: DocumentURI) async -> [BuildTargetIdentifier]
144115

145116
/// Re-generate the build graph.
146117
func generateBuildGraph() async throws
@@ -154,7 +125,7 @@ package protocol BuiltInBuildSystem: AnyObject, Sendable {
154125
/// index data to be available earlier.
155126
///
156127
/// `nil` if the build system doesn't support topological sorting of targets.
157-
func topologicalSort(of targets: [ConfiguredTarget]) async -> [ConfiguredTarget]?
128+
func topologicalSort(of targets: [BuildTargetIdentifier]) async -> [BuildTargetIdentifier]?
158129

159130
/// Returns the list of targets that might depend on the given target and that need to be re-prepared when a file in
160131
/// `target` is modified.
@@ -163,12 +134,12 @@ package protocol BuiltInBuildSystem: AnyObject, Sendable {
163134
/// necessary by scheduling re-preparation of a target where it isn't necessary.
164135
///
165136
/// Returning `nil` indicates that all targets should be considered depending on the given target.
166-
func targets(dependingOn targets: [ConfiguredTarget]) async -> [ConfiguredTarget]?
137+
func targets(dependingOn targets: [BuildTargetIdentifier]) async -> [BuildTargetIdentifier]?
167138

168139
/// Prepare the given targets for indexing and semantic functionality. This should build all swift modules of target
169140
/// dependencies.
170141
func prepare(
171-
targets: [ConfiguredTarget],
142+
targets: [BuildTargetIdentifier],
172143
logMessageToIndexLog: @escaping @Sendable (_ taskID: IndexTaskID, _ message: String) -> Void
173144
) async throws
174145

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,22 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate {
160160
}
161161
}
162162

163-
/// Returns all the `ConfiguredTarget`s that the document is part of.
164-
package func configuredTargets(for document: DocumentURI) async -> [ConfiguredTarget] {
165-
return await buildSystem?.underlyingBuildSystem.configuredTargets(for: document) ?? []
166-
}
167-
168-
/// Returns the `ConfiguredTarget` that should be used for semantic functionality of the given document.
169-
package func canonicalConfiguredTarget(for document: DocumentURI) async -> ConfiguredTarget? {
170-
// Sort the configured targets to deterministically pick the same `ConfiguredTarget` every time.
171-
// We could allow the user to specify a preference of one target over another. For now this is not necessary because
172-
// no build system currently returns multiple targets for a source file.
173-
return await configuredTargets(for: document)
174-
.sorted { ($0.targetID, $0.runDestinationID) < ($1.targetID, $1.runDestinationID) }
163+
/// Returns all the `BuildTargetIdentifier`s that the document is part of.
164+
package func targets(for document: DocumentURI) async -> [BuildTargetIdentifier] {
165+
return await buildSystem?.underlyingBuildSystem.targets(for: document) ?? []
166+
}
167+
168+
/// Returns the `BuildTargetIdentifier` that should be used for semantic functionality of the given document.
169+
package func canonicalTarget(for document: DocumentURI) async -> BuildTargetIdentifier? {
170+
// Sort the targets to deterministically pick the same `BuildTargetIdentifier` every time.
171+
// We could allow the user to specify a preference of one target over another.
172+
return await targets(for: document)
173+
.sorted { $0.uri.stringValue < $1.uri.stringValue }
175174
.first
176175
}
177176

178-
/// Returns the target's module name as parsed from the `ConfiguredTarget`'s compiler arguments.
179-
package func moduleName(for document: DocumentURI, in target: ConfiguredTarget) async -> String? {
177+
/// Returns the target's module name as parsed from the `BuildTargetIdentifier`'s compiler arguments.
178+
package func moduleName(for document: DocumentURI, in target: BuildTargetIdentifier) async -> String? {
180179
guard let language = await self.defaultLanguage(for: document),
181180
let buildSettings = await buildSettings(for: document, in: target, language: language)
182181
else {
@@ -211,7 +210,7 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate {
211210
/// Implementation detail of `buildSettings(for:language:)`.
212211
private func buildSettingsFromPrimaryBuildSystem(
213212
for document: DocumentURI,
214-
in target: ConfiguredTarget?,
213+
in target: BuildTargetIdentifier?,
215214
language: Language
216215
) async throws -> FileBuildSettings? {
217216
guard let buildSystem, let target else {
@@ -232,7 +231,7 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate {
232231
/// don't have build settings by themselves.
233232
package func buildSettings(
234233
for document: DocumentURI,
235-
in target: ConfiguredTarget?,
234+
in target: BuildTargetIdentifier?,
236235
language: Language
237236
) async -> FileBuildSettings? {
238237
do {
@@ -271,7 +270,7 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate {
271270
language: Language
272271
) async -> FileBuildSettings? {
273272
let mainFile = await mainFile(for: document, language: language)
274-
let target = await canonicalConfiguredTarget(for: mainFile)
273+
let target = await canonicalTarget(for: mainFile)
275274
guard var settings = await buildSettings(for: mainFile, in: target, language: language) else {
276275
return nil
277276
}
@@ -292,16 +291,16 @@ package actor BuildSystemManager: BuiltInBuildSystemAdapterDelegate {
292291
await self.buildSystem?.underlyingBuildSystem.waitForUpToDateBuildGraph()
293292
}
294293

295-
package func topologicalSort(of targets: [ConfiguredTarget]) async throws -> [ConfiguredTarget]? {
294+
package func topologicalSort(of targets: [BuildTargetIdentifier]) async throws -> [BuildTargetIdentifier]? {
296295
return await buildSystem?.underlyingBuildSystem.topologicalSort(of: targets)
297296
}
298297

299-
package func targets(dependingOn targets: [ConfiguredTarget]) async -> [ConfiguredTarget]? {
298+
package func targets(dependingOn targets: [BuildTargetIdentifier]) async -> [BuildTargetIdentifier]? {
300299
return await buildSystem?.underlyingBuildSystem.targets(dependingOn: targets)
301300
}
302301

303302
package func prepare(
304-
targets: [ConfiguredTarget],
303+
targets: [BuildTargetIdentifier],
305304
logMessageToIndexLog: @escaping @Sendable (_ taskID: IndexTaskID, _ message: String) -> Void
306305
) async throws {
307306
try await buildSystem?.underlyingBuildSystem.prepare(targets: targets, logMessageToIndexLog: logMessageToIndexLog)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import BuildServerProtocol
14+
import SKLogging
15+
16+
extension BuildTargetIdentifier {
17+
package static let dummy: BuildTargetIdentifier = BuildTargetIdentifier(uri: try! URI(string: "dummy://dummy"))
18+
}
19+
20+
extension BuildTargetIdentifier: CustomLogStringConvertible {
21+
public var description: String {
22+
return uri.stringValue
23+
}
24+
25+
public var redactedDescription: String {
26+
return uri.stringValue.hashForLogging
27+
}
28+
}

Sources/BuildSystemIntegration/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_library(BuildSystemIntegration STATIC
44
BuildSystem.swift
55
BuildSystemDelegate.swift
66
BuildSystemManager.swift
7+
BuildTargetIdentifierExtensions.swift
78
BuiltInBuildSystemAdapter.swift
89
CompilationDatabase.swift
910
CompilationDatabaseBuildSystem.swift

Sources/BuildSystemIntegration/CompilationDatabaseBuildSystem.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extension CompilationDatabaseBuildSystem: BuiltInBuildSystem {
108108

109109
package func buildSettings(
110110
for document: DocumentURI,
111-
in buildTarget: ConfiguredTarget,
111+
in buildTarget: BuildTargetIdentifier,
112112
language: Language
113113
) async -> FileBuildSettings? {
114114
guard let db = database(for: document),
@@ -128,12 +128,12 @@ extension CompilationDatabaseBuildSystem: BuiltInBuildSystem {
128128
return nil
129129
}
130130

131-
package func configuredTargets(for document: DocumentURI) async -> [ConfiguredTarget] {
132-
return [ConfiguredTarget(targetID: "dummy", runDestinationID: "dummy")]
131+
package func targets(for document: DocumentURI) async -> [BuildTargetIdentifier] {
132+
return [BuildTargetIdentifier.dummy]
133133
}
134134

135135
package func prepare(
136-
targets: [ConfiguredTarget],
136+
targets: [BuildTargetIdentifier],
137137
logMessageToIndexLog: @Sendable (_ taskID: IndexTaskID, _ message: String) -> Void
138138
) async throws {
139139
throw PrepareNotSupportedError()
@@ -143,11 +143,11 @@ extension CompilationDatabaseBuildSystem: BuiltInBuildSystem {
143143

144144
package func waitForUpToDateBuildGraph() async {}
145145

146-
package func topologicalSort(of targets: [ConfiguredTarget]) -> [ConfiguredTarget]? {
146+
package func topologicalSort(of targets: [BuildTargetIdentifier]) -> [BuildTargetIdentifier]? {
147147
return nil
148148
}
149149

150-
package func targets(dependingOn targets: [ConfiguredTarget]) -> [ConfiguredTarget]? {
150+
package func targets(dependingOn targets: [BuildTargetIdentifier]) -> [BuildTargetIdentifier]? {
151151
return nil
152152
}
153153

0 commit comments

Comments
 (0)