Skip to content

[PackageGraph/Build] Remove BuildTriple and its remaining uses #7896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Build/BuildManifest/LLBuildManifestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public class LLBuildManifestBuilder {
case .swift(let desc):
try self.createSwiftCompileCommand(desc)
case .clang(let desc):
if desc.target.buildTriple == .tools {
if desc.destination == .host {
// Need the clang modules for tools
try self.createClangCompileCommand(desc)
} else {
Expand Down
89 changes: 39 additions & 50 deletions Sources/Build/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,19 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS

/// Compute the llbuild target name using the given subset.
func computeLLBuildTargetName(for subset: BuildSubset) async throws -> String {
func inferTestDestination(
testModule: ResolvedModule,
graph: ModulesGraph
) throws -> BuildParameters.Destination {
for product in graph.allProducts where product.type == .test {
if product.modules.contains(where: { $0.id == testModule.id }) {
return product.hasDirectMacroDependencies ? .host : .target
}
}

throw InternalError("Could not find a product for test module: \(testModule)")
}

switch subset {
case .allExcludingTests:
return LLBuildManifestBuilder.TargetKind.main.targetName
Expand All @@ -582,25 +595,22 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
// FIXME: This is super unfortunate that we might need to load the package graph.
let graph = try await getPackageGraph()

let buildTriple: BuildTriple? = if let destination {
destination == .host ? .tools : .destination
} else {
nil
}

let product = graph.product(
for: productName,
destination: buildTriple
)
let product = graph.product(for: productName)

guard let product else {
observabilityScope.emit(error: "no product named '\(productName)'")
throw Diagnostics.fatalError
}

let buildParameters = config.buildParameters(
for: product.buildTriple == .tools ? .host : .target
)
let buildParameters = if let destination {
config.buildParameters(for: destination)
} else if product.type == .macro || product.type == .plugin {
config.buildParameters(for: .host)
} else if product.type == .test {
config.buildParameters(for: product.hasDirectMacroDependencies ? .host : .target)
} else {
config.buildParameters(for: .target)
}

// If the product is automatic, we build the main target because automatic products
// do not produce a binary right now.
Expand All @@ -616,27 +626,24 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
// FIXME: This is super unfortunate that we might need to load the package graph.
let graph = try await getPackageGraph()

let buildTriple: BuildTriple? = if let destination {
destination == .host ? .tools : .destination
} else {
nil
}
let module = graph.module(for: targetName)

let target = graph.module(
for: targetName,
destination: buildTriple
)

guard let target else {
guard let module else {
observabilityScope.emit(error: "no target named '\(targetName)'")
throw Diagnostics.fatalError
}

let buildParameters = config.buildParameters(
for: target.buildTriple == .tools ? .host : .target
)
let buildParameters = if let destination {
config.buildParameters(for: destination)
} else if module.type == .macro || module.type == .plugin {
config.buildParameters(for: .host)
} else if module.type == .test {
try config.buildParameters(for: inferTestDestination(testModule: module, graph: graph))
} else {
config.buildParameters(for: .target)
}

return target.getLLBuildTargetName(buildParameters: buildParameters)
return module.getLLBuildTargetName(buildParameters: buildParameters)
}
}

Expand Down Expand Up @@ -1008,32 +1015,14 @@ extension BuildSubset {
return Array(graph.reachableModules)
case .allExcludingTests:
return graph.reachableModules.filter { $0.type != .test }
case .product(let productName, let destination):
let buildTriple: BuildTriple? = if let destination {
destination == .host ? .tools : .destination
} else {
nil
}

guard let product = graph.product(
for: productName,
destination: buildTriple
) else {
case .product(let productName, _):
guard let product = graph.product(for: productName) else {
observabilityScope.emit(error: "no product named '\(productName)'")
return nil
}
return try product.recursiveModuleDependencies()
case .target(let targetName, let destination):
let buildTriple: BuildTriple? = if let destination {
destination == .host ? .tools : .destination
} else {
nil
}

guard let target = graph.module(
for: targetName,
destination: buildTriple
) else {
case .target(let targetName, _):
guard let target = graph.module(for: targetName) else {
observabilityScope.emit(error: "no target named '\(targetName)'")
return nil
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/Build/BuildPlan/BuildPlan+Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,14 @@ extension BuildPlan {
packageAccess: true, // test target is allowed access to package decls by default
testDiscoverySrc: Sources(paths: discoveryPaths, root: discoveryDerivedDir)
)
var discoveryResolvedModule = ResolvedModule(
let discoveryResolvedModule = ResolvedModule(
packageIdentity: testProduct.packageIdentity,
underlying: discoveryTarget,
dependencies: testProduct.modules.map { .module($0, conditions: []) },
defaultLocalization: testProduct.defaultLocalization,
supportedPlatforms: testProduct.supportedPlatforms,
platformVersionProvider: testProduct.platformVersionProvider
)
discoveryResolvedModule.buildTriple = testProduct.buildTriple

let discoveryTargetBuildDescription = try SwiftModuleBuildDescription(
package: package,
Expand Down Expand Up @@ -135,15 +134,14 @@ extension BuildPlan {
packageAccess: true, // test target is allowed access to package decls
testEntryPointSources: entryPointSources
)
var entryPointResolvedTarget = ResolvedModule(
let entryPointResolvedTarget = ResolvedModule(
packageIdentity: testProduct.packageIdentity,
underlying: entryPointTarget,
dependencies: testProduct.modules.map { .module($0, conditions: []) } + resolvedTargetDependencies,
defaultLocalization: testProduct.defaultLocalization,
supportedPlatforms: testProduct.supportedPlatforms,
platformVersionProvider: testProduct.platformVersionProvider
)
entryPointResolvedTarget.buildTriple = testProduct.buildTriple

return try SwiftModuleBuildDescription(
package: package,
Expand Down
22 changes: 4 additions & 18 deletions Sources/Build/BuildPlan/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
switch $0 {
case .module(let moduleDependency, _):
if moduleDependency.type == .executable {
return graph.product(
for: moduleDependency.name,
destination: .tools
)
return graph.product(for: moduleDependency.name)
}
return nil
default:
Expand Down Expand Up @@ -613,7 +610,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
arguments.append("-l" + replProductName)

// The graph should have the REPL product.
assert(self.graph.product(for: replProductName, destination: .destination) != nil)
assert(self.graph.product(for: replProductName) != nil)

// Add the search path to the directory containing the modulemap file.
for target in self.targets {
Expand Down Expand Up @@ -958,7 +955,7 @@ extension BuildPlan {
case .macro, .plugin:
self = .product(product, .host)
case .test:
self = .product(product, product.modules.contains(where: Self.hasMacroDependency) ? .host : destination)
self = .product(product, product.hasDirectMacroDependencies ? .host : destination)
default:
self = .product(product, destination)
}
Expand All @@ -973,25 +970,14 @@ extension BuildPlan {
// Macros and plugins are ways built for host
self = .module(module, .host)
case .test:
self = .module(module, Self.hasMacroDependency(module: module) ? .host : destination)
self = .module(module, module.hasDirectMacroDependencies ? .host : destination)
default:
// By default assume the destination of the context.
// This means that i.e. test products that reference macros
// would force all of their successors to be `host`
self = .module(module, destination)
}
}

static func hasMacroDependency(module: ResolvedModule) -> Bool {
module.dependencies.contains(where: {
switch $0 {
case .product(let productDependency, _):
productDependency.type == .macro
case .module(let moduleDependency, _):
moduleDependency.type == .macro
}
})
}
}

/// Traverse the modules graph and find a destination for every product and module.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/Snippets/Cards/SnippetCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct SnippetCard: Card {
let buildSystem = try await swiftCommandState.createBuildSystem(explicitProduct: snippet.name, traitConfiguration: .init())
try await buildSystem.build(subset: .product(snippet.name))
let executablePath = try swiftCommandState.productsBuildParameters.buildPath.appending(component: snippet.name)
if let exampleTarget = try await buildSystem.getPackageGraph().module(for: snippet.name, destination: .destination) {
if let exampleTarget = try await buildSystem.getPackageGraph().module(for: snippet.name) {
try ProcessEnv.chdir(exampleTarget.sources.paths[0].parentDirectory)
}
try exec(path: executablePath.pathString, args: [])
Expand Down
2 changes: 1 addition & 1 deletion Sources/Commands/SwiftRunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
if let executable = options.executable {
// There should be only one product with the given name in the graph
// and it should be executable or snippet.
guard let product = graph.product(for: executable, destination: .destination),
guard let product = graph.product(for: executable),
product.type == .executable || product.type == .snippet
else {
throw RunError.executableNotFound(executable)
Expand Down
45 changes: 0 additions & 45 deletions Sources/PackageGraph/BuildTriple.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/PackageGraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

add_library(PackageGraph
BoundVersion.swift
BuildTriple.swift
DependencyMirrors.swift
Diagnostics.swift
GraphLoadingNode.swift
Expand Down
Loading