Skip to content

Commit 1e51571

Browse files
authored
Revert "[Build] NFC: Start using ModuleBuildDescription.{recursive}Dependencies"
This reverts commit 659199f.
1 parent 3a54751 commit 1e51571

File tree

4 files changed

+70
-83
lines changed

4 files changed

+70
-83
lines changed

Sources/Build/BuildManifest/LLBuildManifestBuilder+Clang.swift

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,30 @@ extension LLBuildManifestBuilder {
3232
inputs.append(resourcesNode)
3333
}
3434

35-
func addStaticTargetInputs(_ description: ModuleBuildDescription?) {
36-
if case .swift(let desc) = description, desc.target.type == .library {
35+
func addStaticTargetInputs(_ target: ResolvedModule) {
36+
if case .swift(let desc)? = self.plan.targetMap[target.id], target.type == .library {
3737
inputs.append(file: desc.moduleOutputPath)
3838
}
3939
}
4040

41-
for dependency in target.dependencies(using: self.plan) {
41+
for dependency in target.target.dependencies(satisfying: target.buildEnvironment) {
4242
switch dependency {
43-
case .module(_, let description):
44-
addStaticTargetInputs(description)
43+
case .module(let target, _):
44+
addStaticTargetInputs(target)
4545

46-
case .product(let product, let productDescription):
46+
case .product(let product, _):
4747
switch product.type {
4848
case .executable, .snippet, .library(.dynamic), .macro:
49-
guard let productDescription else {
50-
throw InternalError("No build description for product: \(product)")
49+
guard let planProduct = plan.productMap[product.id] else {
50+
throw InternalError("unknown product \(product)")
5151
}
5252
// Establish a dependency on binary of the product.
53-
try inputs.append(file: productDescription.binaryPath)
53+
let binary = try planProduct.binaryPath
54+
inputs.append(file: binary)
5455

5556
case .library(.automatic), .library(.static), .plugin:
56-
for module in product.modules {
57-
guard let dependencyDescription = self.plan.description(
58-
for: module,
59-
context: product.type == .plugin ? .host : target.destination
60-
) else
61-
{
62-
throw InternalError("unknown module: \(module)")
63-
}
64-
addStaticTargetInputs(dependencyDescription)
57+
for target in product.modules {
58+
addStaticTargetInputs(target)
6559
}
6660
case .test:
6761
break

Sources/Build/BuildManifest/LLBuildManifestBuilder+Swift.swift

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ 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 allPackageDependencies = self.plan.targets.flatMap { $0.recursiveDependencies(using: self.plan) }
195-
194+
let nodes = self.plan.targets.compactMap {
195+
ResolvedModule.Dependency.module($0.module, conditions: [])
196+
}
197+
let allPackageDependencies = try topologicalSort(nodes, successors: { $0.dependencies })
196198
// Instantiate the inter-module dependency oracle which will cache commonly-scanned
197199
// modules across targets' Driver instances.
198200
let dependencyOracle = InterModuleDependencyOracle()
@@ -204,15 +206,14 @@ extension LLBuildManifestBuilder {
204206

205207
// Create commands for all module descriptions in the plan.
206208
for dependency in allPackageDependencies.reversed() {
207-
guard case .module(let module, let description) = dependency else {
209+
guard case .module(let target, _) = dependency else {
208210
// Product dependency build jobs are added after the fact.
209211
// Targets that depend on product dependencies will expand the corresponding
210212
// product into its constituent targets.
211213
continue
212214
}
213-
214-
guard module.underlying.type != .systemModule,
215-
module.underlying.type != .binary
215+
guard target.underlying.type != .systemModule,
216+
target.underlying.type != .binary
216217
else {
217218
// Much like non-Swift targets, system modules will consist of a modulemap
218219
// somewhere in the filesystem, with the path to that module being either
@@ -224,11 +225,9 @@ extension LLBuildManifestBuilder {
224225
// be able to detect such targets' modules.
225226
continue
226227
}
227-
228-
guard let description else {
229-
throw InternalError("Expected description for module \(module)")
228+
guard let description = plan.targetMap[target.id] else {
229+
throw InternalError("Expected description for target \(target)")
230230
}
231-
232231
switch description {
233232
case .swift(let desc):
234233
try self.createExplicitSwiftTargetCompileCommand(
@@ -320,29 +319,32 @@ extension LLBuildManifestBuilder {
320319
for targetDescription: ModuleBuildDescription,
321320
dependencyModuleDetailsMap: inout SwiftDriver.ExternalTargetModuleDetailsMap
322321
) throws {
323-
for dependency in targetDescription.dependencies(using: self.plan) {
322+
for dependency in targetDescription.module.dependencies(satisfying: targetDescription.buildParameters.buildEnvironment) {
324323
switch dependency {
325-
case .product(let product, let productDescription):
326-
for productDependency in product.modules {
327-
guard let dependencyModuleDescription = self.plan.description(
328-
for: productDependency,
329-
context: productDescription?.destination ?? targetDescription.destination
330-
) else
331-
{
332-
throw InternalError("unknown dependency target for \(productDependency)")
324+
case .product:
325+
// Product dependencies are broken down into the targets that make them up.
326+
guard let dependencyProduct = dependency.product else {
327+
throw InternalError("unknown dependency product for \(dependency)")
328+
}
329+
for dependencyProductTarget in dependencyProduct.modules {
330+
guard let dependencyTargetDescription = self.plan.targetMap[dependencyProductTarget.id] else {
331+
throw InternalError("unknown dependency target for \(dependencyProductTarget)")
333332
}
334333
try self.addTargetDependencyInfo(
335-
for: dependencyModuleDescription,
334+
for: dependencyTargetDescription,
336335
dependencyModuleDetailsMap: &dependencyModuleDetailsMap
337336
)
338337
}
339-
case .module(let dependencyModule, let dependencyDescription):
340-
guard let dependencyDescription else {
341-
throw InternalError("No build description for module: \(dependencyModule)")
342-
}
338+
case .module:
343339
// Product dependencies are broken down into the targets that make them up.
340+
guard
341+
let dependencyTarget = dependency.module,
342+
let dependencyTargetDescription = self.plan.targetMap[dependencyTarget.id]
343+
else {
344+
throw InternalError("unknown dependency target for \(dependency)")
345+
}
344346
try self.addTargetDependencyInfo(
345-
for: dependencyDescription,
347+
for: dependencyTargetDescription,
346348
dependencyModuleDetailsMap: &dependencyModuleDetailsMap
347349
)
348350
}
@@ -420,73 +422,63 @@ extension LLBuildManifestBuilder {
420422

421423
let prepareForIndexing = target.buildParameters.prepareForIndexing
422424

423-
func addStaticTargetInputs(_ module: ResolvedModule, _ description: ModuleBuildDescription?) throws {
425+
func addStaticTargetInputs(_ target: ResolvedModule) throws {
424426
// Ignore C Modules.
425-
if module.underlying is SystemLibraryModule { return }
427+
if target.underlying is SystemLibraryModule { return }
426428
// Ignore Binary Modules.
427-
if module.underlying is BinaryModule { return }
429+
if target.underlying is BinaryModule { return }
428430
// Ignore Plugin Modules.
429-
if module.underlying is PluginModule { return }
430-
431-
guard let description else {
432-
throw InternalError("No build description for module: \(module)")
433-
}
431+
if target.underlying is PluginModule { return }
434432

435433
// Depend on the binary for executable targets.
436-
if module.type == .executable && prepareForIndexing == .off {
437-
// FIXME: Optimize. Build plan could build a mapping between executable modules
438-
// and their products to speed up search here, which is inefficient if the plan
439-
// contains a lot of products.
434+
if target.type == .executable && prepareForIndexing == .off {
435+
// FIXME: Optimize.
440436
if let productDescription = try plan.productMap.values.first(where: {
441-
try $0.product.type == .executable &&
442-
$0.product.executableModule.id == module.id &&
443-
$0.destination == description.destination
437+
try $0.product.type == .executable && $0.product.executableModule.id == target.id
444438
}) {
445439
try inputs.append(file: productDescription.binaryPath)
446440
}
447441
return
448442
}
449443

450-
switch description {
451-
case .swift(let swiftDescription):
452-
inputs.append(file: swiftDescription.moduleOutputPath)
453-
case .clang(let clangDescription):
444+
switch self.plan.targetMap[target.id] {
445+
case .swift(let target)?:
446+
inputs.append(file: target.moduleOutputPath)
447+
case .clang(let target)?:
454448
if prepareForIndexing != .off {
455449
// In preparation, we're only building swiftmodules
456450
// propagate the dependency to the header files in this target
457-
for header in clangDescription.clangTarget.headers {
451+
for header in target.clangTarget.headers {
458452
inputs.append(file: header)
459453
}
460454
} else {
461-
for object in try clangDescription.objects {
455+
for object in try target.objects {
462456
inputs.append(file: object)
463457
}
464458
}
459+
case nil:
460+
throw InternalError("unexpected: target \(target) not in target map \(self.plan.targetMap)")
465461
}
466462
}
467463

468-
for dependency in target.dependencies(using: self.plan) {
464+
for dependency in target.target.dependencies(satisfying: target.buildParameters.buildEnvironment) {
469465
switch dependency {
470-
case .module(let module, let description):
471-
try addStaticTargetInputs(module, description)
466+
case .module(let target, _):
467+
try addStaticTargetInputs(target)
472468

473-
case .product(let product, let productDescription):
469+
case .product(let product, _):
474470
switch product.type {
475471
case .executable, .snippet, .library(.dynamic), .macro:
476-
guard let productDescription else {
477-
throw InternalError("No description for product: \(product)")
472+
guard let planProduct = plan.productMap[product.id] else {
473+
throw InternalError("unknown product \(product)")
478474
}
479475
// Establish a dependency on binary of the product.
480-
try inputs.append(file: productDescription.binaryPath)
476+
try inputs.append(file: planProduct.binaryPath)
481477

482478
// For automatic and static libraries, and plugins, add their targets as static input.
483479
case .library(.automatic), .library(.static), .plugin:
484-
for module in product.modules {
485-
let description = self.plan.description(
486-
for: module,
487-
context: product.type == .plugin ? .host : target.destination
488-
)
489-
try addStaticTargetInputs(module, description)
480+
for target in product.modules {
481+
try addStaticTargetInputs(target)
490482
}
491483

492484
case .test:

Sources/Build/BuildPlan/BuildPlan+Clang.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import class PackageModel.SystemLibraryModule
1818
extension BuildPlan {
1919
/// Plan a Clang target.
2020
func plan(clangTarget: ClangModuleBuildDescription) throws {
21-
let dependencies = clangTarget.recursiveDependencies(using: self)
21+
let dependencies = try clangTarget.target.recursiveDependencies(satisfying: clangTarget.buildEnvironment)
2222

23-
for case .module(let dependency, let description) in dependencies {
23+
for case .module(let dependency, _) in dependencies {
2424
switch dependency.underlying {
2525
case is SwiftModule:
26-
if case let .swift(dependencyTargetDescription)? = description {
26+
if case let .swift(dependencyTargetDescription)? = targetMap[dependency.id] {
2727
if let moduleMap = dependencyTargetDescription.moduleMap {
2828
clangTarget.additionalFlags += ["-fmodule-map-file=\(moduleMap.pathString)"]
2929
}
@@ -34,7 +34,7 @@ extension BuildPlan {
3434
clangTarget.additionalFlags += ["-I", target.includeDir.pathString]
3535

3636
// Add the modulemap of the dependency if it has one.
37-
if case let .clang(dependencyTargetDescription)? = description {
37+
if case let .clang(dependencyTargetDescription)? = targetMap[dependency.id] {
3838
if let moduleMap = dependencyTargetDescription.moduleMap {
3939
clangTarget.additionalFlags += ["-fmodule-map-file=\(moduleMap.pathString)"]
4040
}

Sources/Build/BuildPlan/BuildPlan+Swift.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ extension BuildPlan {
2020
func plan(swiftTarget: SwiftModuleBuildDescription) throws {
2121
// We need to iterate recursive dependencies because Swift compiler needs to see all the targets a target
2222
// depends on.
23-
for case .module(let dependency, let description) in swiftTarget.recursiveDependencies(using: self) {
23+
let environment = swiftTarget.buildParameters.buildEnvironment
24+
for case .module(let dependency, _) in try swiftTarget.target.recursiveDependencies(satisfying: environment) {
2425
switch dependency.underlying {
2526
case let underlyingTarget as ClangModule where underlyingTarget.type == .library:
26-
guard case let .clang(target)? = description else {
27+
guard case let .clang(target)? = targetMap[dependency.id] else {
2728
throw InternalError("unexpected clang target \(underlyingTarget)")
2829
}
2930
// Add the path to modulemap of the dependency. Currently we require that all Clang targets have a

0 commit comments

Comments
 (0)