Skip to content

Commit 3a54751

Browse files
authored
Revert "[Build] BuildPlan: Start using id from module/product build descriptions for build plan"
This reverts commit 0c1bed8.
1 parent 0c1bed8 commit 3a54751

File tree

9 files changed

+65
-83
lines changed

9 files changed

+65
-83
lines changed

Sources/Basics/Collections/IdentifiableSet.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
4242
Index(storageIndex: self.storage.elements.endIndex)
4343
}
4444

45-
public var values: some Sequence<Element> {
46-
self.storage.values
47-
}
48-
4945
public subscript(position: Index) -> Element {
5046
self.storage.elements[position.storageIndex].value
5147
}

Sources/Build/BuildManifest/LLBuildManifestBuilder.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class LLBuildManifestBuilder {
102102
try addTargetsToExplicitBuildManifest()
103103
} else {
104104
// Create commands for all target descriptions in the plan.
105-
for description in self.plan.targetMap {
105+
for (_, description) in self.plan.targetMap {
106106
switch description {
107107
case .swift(let desc):
108108
try self.createSwiftCompileCommand(desc)
@@ -116,7 +116,7 @@ public class LLBuildManifestBuilder {
116116
try self.addTestEntryPointGenerationCommand()
117117

118118
// Create command for all products in the plan.
119-
for description in self.plan.productMap {
119+
for (_, description) in self.plan.productMap {
120120
try self.createProductCommand(description)
121121
}
122122

@@ -133,7 +133,7 @@ public class LLBuildManifestBuilder {
133133

134134
addPackageStructureCommand()
135135

136-
for description in self.plan.targetMap {
136+
for (_, description) in self.plan.targetMap {
137137
switch description {
138138
case .swift(let desc):
139139
try self.createSwiftCompileCommand(desc)
@@ -148,7 +148,7 @@ public class LLBuildManifestBuilder {
148148
}
149149
}
150150

151-
for description in self.plan.productMap {
151+
for (_, description) in self.plan.productMap {
152152
// Need to generate macro products
153153
switch description.product.type {
154154
case .macro, .plugin:
@@ -271,7 +271,7 @@ extension LLBuildManifestBuilder {
271271
private func addTestDiscoveryGenerationCommand() throws {
272272
for testDiscoveryTarget in self.plan.targets.compactMap(\.testDiscoveryTargetBuildDescription) {
273273
let testTargets = testDiscoveryTarget.target.dependencies
274-
.compactMap(\.module).compactMap { self.plan.description(for: $0, context: testDiscoveryTarget.destination) }
274+
.compactMap(\.module).compactMap { self.plan.targetMap[$0.id] }
275275
let objectFiles = try testTargets.flatMap { try $0.objects }.sorted().map(Node.file)
276276
let outputs = testDiscoveryTarget.target.sources.paths
277277

@@ -288,22 +288,18 @@ extension LLBuildManifestBuilder {
288288
}
289289

290290
private func addTestEntryPointGenerationCommand() throws {
291-
for module in self.plan.targets {
292-
guard case .swift(let swiftModule) = module,
293-
case .entryPoint(let isSynthesized) = swiftModule.testTargetRole,
291+
for target in self.plan.targets {
292+
guard case .swift(let target) = target,
293+
case .entryPoint(let isSynthesized) = target.testTargetRole,
294294
isSynthesized else { continue }
295295

296-
let testEntryPointTarget = swiftModule
296+
let testEntryPointTarget = target
297297

298298
// Get the Swift target build descriptions of all discovery modules this synthesized entry point target
299299
// depends on.
300-
let discoveredTargetDependencyBuildDescriptions = module.dependencies(using: self.plan)
301-
.compactMap {
302-
if case .module(_, let description) = $0 {
303-
return description
304-
}
305-
return nil
306-
}
300+
let discoveredTargetDependencyBuildDescriptions = testEntryPointTarget.target.dependencies
301+
.compactMap(\.module?.id)
302+
.compactMap { self.plan.targetMap[$0] }
307303
.compactMap(\.testDiscoveryTargetBuildDescription)
308304

309305
// The module outputs of the discovery modules this synthesized entry point target depends on are

Sources/Build/BuildPlan/BuildPlan+Product.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ extension BuildPlan {
7777
}
7878
}
7979

80-
for module in dependencies.staticTargets {
81-
switch module.underlying {
80+
for target in dependencies.staticTargets {
81+
switch target.underlying {
8282
case is SwiftModule:
8383
// Swift targets are guaranteed to have a corresponding Swift description.
84-
guard case .swift(let description) = self.description(for: module, context: buildProduct.destination) else {
85-
throw InternalError("unknown module \(module)")
84+
guard case .swift(let description) = self.targetMap[target.id] else {
85+
throw InternalError("unknown target \(target)")
8686
}
8787

8888
// Based on the debugging strategy, we either need to pass swiftmodule paths to the
@@ -103,16 +103,16 @@ extension BuildPlan {
103103

104104
buildProduct.staticTargets = dependencies.staticTargets
105105
buildProduct.dylibs = try dependencies.dylibs.map {
106-
guard let product = self.description(for: $0, context: buildProduct.destination) else {
106+
guard let product = self.productMap[$0.id] else {
107107
throw InternalError("unknown product \($0)")
108108
}
109109
return product
110110
}
111-
buildProduct.objects += try dependencies.staticTargets.flatMap { module -> [AbsolutePath] in
112-
guard let description = self.description(for: module, context: buildProduct.destination) else {
113-
throw InternalError("unknown module \(module)")
111+
buildProduct.objects += try dependencies.staticTargets.flatMap { targetName -> [AbsolutePath] in
112+
guard let target = self.targetMap[targetName.id] else {
113+
throw InternalError("unknown target \(targetName)")
114114
}
115-
return try description.objects
115+
return try target.objects
116116
}
117117
buildProduct.libraryBinaryPaths = dependencies.libraryBinaryPaths
118118

Sources/Build/BuildPlan/BuildPlan+Test.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import protocol TSCBasic.FileSystem
3434

3535
extension BuildPlan {
3636
static func makeDerivedTestTargets(
37-
testProducts: [ProductBuildDescription],
37+
testProducts: [(product: ResolvedProduct, buildDescription: ProductBuildDescription)],
3838
destinationBuildParameters: BuildParameters,
3939
toolsBuildParameters: BuildParameters,
4040
shouldDisableSandbox: Bool,
@@ -51,8 +51,7 @@ extension BuildPlan {
5151

5252
var isDiscoveryEnabledRedundantly = explicitlyEnabledDiscovery && !isEntryPointPathSpecifiedExplicitly
5353
var result: [(ResolvedProduct, SwiftModuleBuildDescription?, SwiftModuleBuildDescription)] = []
54-
for testBuildDescription in testProducts {
55-
let testProduct = testBuildDescription.product
54+
for (testProduct, testBuildDescription) in testProducts {
5655
let package = testBuildDescription.package
5756

5857
isDiscoveryEnabledRedundantly = isDiscoveryEnabledRedundantly && nil == testProduct.testEntryPointModule

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
197197
public let graph: ModulesGraph
198198

199199
/// The target build description map.
200-
public let targetMap: IdentifiableSet<ModuleBuildDescription>
200+
public let targetMap: [ResolvedModule.ID: ModuleBuildDescription]
201201

202202
/// The product build description map.
203-
public let productMap: IdentifiableSet<ProductBuildDescription>
203+
public let productMap: [ResolvedProduct.ID: ProductBuildDescription]
204204

205205
/// The plugin descriptions. Plugins are represented in the package graph
206206
/// as targets, but they are not directly included in the build graph.
@@ -290,12 +290,13 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
290290
var prebuildCommandResults: [ResolvedModule.ID: [CommandPluginResult]] = [:]
291291

292292
// Create product description for each product we have in the package graph that is eligible.
293-
var productMap = IdentifiableSet<ProductBuildDescription>()
293+
var productMap: [ResolvedProduct.ID: (product: ResolvedProduct, buildDescription: ProductBuildDescription)] =
294+
[:]
294295
// Create build target description for each target which we need to plan.
295296
// Plugin targets are noted, since they need to be compiled, but they do
296297
// not get directly incorporated into the build description that will be
297298
// given to LLBuild.
298-
var targetMap = IdentifiableSet<ModuleBuildDescription>()
299+
var targetMap = [ResolvedModule.ID: ModuleBuildDescription]()
299300
var pluginDescriptions = [PluginBuildDescription]()
300301
var shouldGenerateTestObservation = true
301302

@@ -311,7 +312,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
311312
throw InternalError("Package not found for product: \(product.name)")
312313
}
313314

314-
try productMap.insert(ProductBuildDescription(
315+
productMap[product.id] = try (product, ProductBuildDescription(
315316
package: package,
316317
product: product,
317318
toolsVersion: package.manifest.toolsVersion,
@@ -383,7 +384,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
383384
shouldGenerateTestObservation = false // Only generate the code once.
384385
}
385386

386-
try targetMap.insert(.swift(
387+
targetMap[module.id] = try .swift(
387388
SwiftModuleBuildDescription(
388389
package: package,
389390
target: module,
@@ -398,9 +399,9 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
398399
fileSystem: fileSystem,
399400
observabilityScope: planningObservabilityScope
400401
)
401-
))
402+
)
402403
case is ClangModule:
403-
try targetMap.insert(.clang(
404+
targetMap[module.id] = try .clang(
404405
ClangModuleBuildDescription(
405406
package: package,
406407
target: module,
@@ -412,7 +413,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
412413
fileSystem: fileSystem,
413414
observabilityScope: planningObservabilityScope
414415
)
415-
))
416+
)
416417
case is PluginModule:
417418
try module.dependencies.compactMap {
418419
switch $0 {
@@ -428,7 +429,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
428429
return nil
429430
}
430431
}.forEach {
431-
try productMap.insert(ProductBuildDescription(
432+
productMap[$0.id] = try ($0, ProductBuildDescription(
432433
package: package,
433434
product: $0,
434435
toolsVersion: package.manifest.toolsVersion,
@@ -467,7 +468,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
467468

468469
// Plan the derived test targets, if necessary.
469470
let derivedTestTargets = try Self.makeDerivedTestTargets(
470-
testProducts: productMap.filter {
471+
testProducts: productMap.values.filter {
471472
$0.product.type == .test
472473
},
473474
destinationBuildParameters: destinationBuildParameters,
@@ -479,12 +480,12 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
479480
for item in derivedTestTargets {
480481
var derivedTestTargets = [item.entryPointTargetBuildDescription.target]
481482

482-
targetMap.insert(.swift(
483+
targetMap[item.entryPointTargetBuildDescription.target.id] = .swift(
483484
item.entryPointTargetBuildDescription
484-
))
485+
)
485486

486487
if let discoveryTargetBuildDescription = item.discoveryTargetBuildDescription {
487-
targetMap.insert(.swift(discoveryTargetBuildDescription))
488+
targetMap[discoveryTargetBuildDescription.target.id] = .swift(discoveryTargetBuildDescription)
488489
derivedTestTargets.append(discoveryTargetBuildDescription.target)
489490
}
490491

@@ -494,7 +495,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
494495
self.buildToolPluginInvocationResults = buildToolPluginInvocationResults
495496
self.prebuildCommandResults = prebuildCommandResults
496497

497-
self.productMap = productMap
498+
self.productMap = productMap.mapValues(\.buildDescription)
498499
self.targetMap = targetMap
499500
self.pluginDescriptions = pluginDescriptions
500501

@@ -710,28 +711,14 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
710711
for product: ResolvedProduct,
711712
context: BuildParameters.Destination
712713
) -> ProductBuildDescription? {
713-
let destination: BuildParameters.Destination = switch product.type {
714-
case .macro, .plugin:
715-
.host
716-
default:
717-
context
718-
}
719-
720-
return self.productMap[.init(productID: product.id, destination: destination)]
714+
return self.productMap[product.id]
721715
}
722716

723717
public func description(
724718
for module: ResolvedModule,
725719
context: BuildParameters.Destination
726720
) -> ModuleBuildDescription? {
727-
let destination: BuildParameters.Destination = switch module.type {
728-
case .macro, .plugin:
729-
.host
730-
default:
731-
context
732-
}
733-
734-
return self.targetMap[.init(moduleID: module.id, destination: destination)]
721+
return self.targetMap[module.id]
735722
}
736723
}
737724

Sources/SourceKitLSPAPI/BuildDescription.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import struct Foundation.URL
1515
private import struct Basics.AbsolutePath
1616
private import func Basics.resolveSymlinks
1717

18-
internal import SPMBuildCore
18+
private import SPMBuildCore
1919

2020
// FIXME: should import these module with `private` or `internal` access control
2121
import class Build.BuildPlan
@@ -127,12 +127,9 @@ public struct BuildDescription {
127127
self.inputs = buildPlan.inputs
128128
}
129129

130-
func getBuildTarget(
131-
for module: ResolvedModule,
132-
destination: BuildParameters.Destination
133-
) -> BuildTarget? {
134-
if let description = self.buildPlan.description(for: module, context: destination) {
135-
let modulesGraph = self.buildPlan.graph
130+
// FIXME: should not use `ResolvedTarget` in the public interface
131+
public func getBuildTarget(for target: ResolvedModule, in modulesGraph: ModulesGraph) -> BuildTarget? {
132+
if let description = buildPlan.targetMap[target.id] {
136133
switch description {
137134
case .clang(let description):
138135
return WrappedClangTargetBuildDescription(
@@ -146,10 +143,9 @@ public struct BuildDescription {
146143
)
147144
}
148145
} else {
149-
if module.type == .plugin, let package = self.buildPlan.graph.package(for: module) {
150-
let modulesGraph = self.buildPlan.graph
146+
if target.type == .plugin, let package = self.buildPlan.graph.package(for: target) {
151147
return PluginTargetBuildDescription(
152-
target: module,
148+
target: target,
153149
toolsVersion: package.manifest.toolsVersion,
154150
toolchain: buildPlan.toolsBuildParameters.toolchain,
155151
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(package.id)
@@ -162,14 +158,17 @@ public struct BuildDescription {
162158
public func traverseModules(
163159
callback: (any BuildTarget, _ parent: (any BuildTarget)?, _ depth: Int) -> Void
164160
) {
161+
// TODO: Once the `targetMap` is switched over to use `IdentifiableSet<ModuleBuildDescription>`
162+
// we can introduce `BuildPlan.description(ResolvedModule, BuildParameters.Destination)`
163+
// and start using that here.
165164
self.buildPlan.traverseModules { module, parent, depth in
166165
let parentDescription: (any BuildTarget)? = if let parent {
167-
getBuildTarget(for: parent.0, destination: parent.1)
166+
getBuildTarget(for: parent.0, in: self.buildPlan.graph)
168167
} else {
169168
nil
170169
}
171170

172-
if let description = getBuildTarget(for: module.0, destination: module.1) {
171+
if let description = getBuildTarget(for: module.0, in: self.buildPlan.graph) {
173172
callback(description, parentDescription, depth)
174173
}
175174
}

Sources/_InternalTestSupport/MockBuildTestHelper.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,13 @@ public struct BuildPlanResult {
297297
)
298298
self.targetMap = try Dictionary(
299299
throwingUniqueKeysWithValues: plan.targetMap.compactMap {
300-
($0.module.id, $0)
300+
guard
301+
let target = plan.graph.allModules[$0] ??
302+
IdentifiableSet(plan.derivedTestTargetsMap.values.flatMap { $0 })[$0]
303+
else {
304+
throw BuildError.error("Target \($0) not found.")
305+
}
306+
return (target.id, $1)
301307
}
302308
)
303309
}

Tests/BuildTests/CrossCompilationBuildPlanTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ final class CrossCompilationBuildPlanTests: XCTestCase {
297297
)
298298

299299
// Make sure that build plan doesn't have any "target" tests.
300-
for description in plan.targetMap where description.module.underlying.type == .test {
300+
for (_, description) in plan.targetMap where description.module.underlying.type == .test {
301301
XCTAssertEqual(description.buildParameters.destination, .host)
302302
}
303303

0 commit comments

Comments
 (0)