Skip to content

Commit 356052a

Browse files
committed
[Build] Use BuildPlan data in more places instead of referring to the modules graph
Build plan data structures refer to the underling module/product in their descriptions so some of the uses of module graph's `allTargets` and `allProducts` could be removed in favor of getting the same information directly from descriptions in the build plan.
1 parent 8a8ec82 commit 356052a

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

Sources/Build/BuildDescription/PluginDescription.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public final class PluginDescription: Codable {
2929
/// the plugin).
3030
public let targetName: String
3131

32+
/// The language-level target name.
33+
public let targetC99Name: String
34+
3235
/// The names of any plugin products in that package that vend the plugin
3336
/// to other packages.
3437
public let productNames: [String]
@@ -56,6 +59,7 @@ public final class PluginDescription: Codable {
5659

5760
self.package = package.identity
5861
self.targetName = target.name
62+
self.targetC99Name = target.c99name
5963
self.productNames = products.map(\.name)
6064
self.toolsVersion = toolsVersion
6165
self.sources = target.sources

Sources/Build/BuildManifest/LLBuildManifestBuilder+Swift.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,8 @@ extension LLBuildManifestBuilder {
191191
public func addTargetsToExplicitBuildManifest() throws {
192192
// Sort the product targets in topological order in order to collect and "bubble up"
193193
// their respective dependency graphs to the depending targets.
194-
let nodes: [ResolvedModule.Dependency] = try self.plan.targetMap.keys.compactMap {
195-
guard let target = self.plan.graph.allTargets[$0] else {
196-
throw InternalError("unknown target \($0)")
197-
}
198-
return ResolvedModule.Dependency.target(target, conditions: [])
194+
let nodes = self.plan.targets.compactMap {
195+
ResolvedModule.Dependency.target($0.target, conditions: [])
199196
}
200197
let allPackageDependencies = try topologicalSort(nodes, successors: { $0.dependencies })
201198
// Instantiate the inter-module dependency oracle which will cache commonly-scanned
@@ -430,14 +427,10 @@ extension LLBuildManifestBuilder {
430427
// Depend on the binary for executable targets.
431428
if target.type == .executable {
432429
// FIXME: Optimize.
433-
let product = try plan.graph.allProducts.first {
434-
try $0.type == .executable && $0.executableTarget.id == target.id
435-
}
436-
if let product {
437-
guard let planProduct = plan.productMap[product.id] else {
438-
throw InternalError("unknown product \(product)")
439-
}
440-
try inputs.append(file: planProduct.binaryPath)
430+
if let productDescription = try plan.productMap.values.first(where: {
431+
try $0.product.type == .executable && $0.product.executableTarget.id == target.id
432+
}) {
433+
try inputs.append(file: productDescription.binaryPath)
441434
}
442435
return
443436
}

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,22 +390,22 @@ public struct BuildDescription: Codable {
390390
}
391391
var targetCommandLines: [TargetName: [CommandLineFlag]] = [:]
392392
var generatedSourceTargets: [TargetName] = []
393-
for (targetID, description) in plan.targetMap {
394-
guard case .swift(let desc) = description, let target = plan.graph.allTargets[targetID] else {
393+
for description in plan.targets {
394+
guard case .swift(let desc) = description else {
395395
continue
396396
}
397-
let buildParameters = plan.buildParameters(for: target)
398-
targetCommandLines[target.c99name] =
397+
let buildParameters = description.buildParameters
398+
targetCommandLines[desc.target.c99name] =
399399
try desc.emitCommandLine(scanInvocation: true) + [
400400
"-driver-use-frontend-path", buildParameters.toolchain.swiftCompilerPath.pathString
401401
]
402402
if case .discovery = desc.testTargetRole {
403-
generatedSourceTargets.append(target.c99name)
403+
generatedSourceTargets.append(desc.target.c99name)
404404
}
405405
}
406406
generatedSourceTargets.append(
407-
contentsOf: plan.graph.allTargets.filter { $0.type == .plugin }
408-
.map(\.c99name)
407+
contentsOf: plan.pluginDescriptions
408+
.map(\.targetC99Name)
409409
)
410410
self.swiftTargetScanArgs = targetCommandLines
411411
self.generatedSourceTargetSet = Set(generatedSourceTargets)

Sources/Commands/SwiftRunCommand.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,11 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
218218
/// Returns the path to the correct executable based on options.
219219
private func findProductName(in graph: ModulesGraph) throws -> String {
220220
if let executable = options.executable {
221-
let executableExists = graph.allProducts.contains { ($0.type == .executable || $0.type == .snippet) && $0.name == executable }
222-
guard executableExists else {
221+
// There should be only one product with the given name in the graph
222+
// and it should be executable or snippet.
223+
guard let product = graph.product(for: executable),
224+
product.type == .executable || product.type == .snippet
225+
else {
223226
throw RunError.executableNotFound(executable)
224227
}
225228
return executable

0 commit comments

Comments
 (0)