Skip to content

Commit 37aa266

Browse files
committed
[PackageGraph/Build] Remove BuildTriple and its remaining uses
Update tests to remove `BuildTriple` use and replace it with `BuildParameters.Destination`
1 parent 12b92cc commit 37aa266

20 files changed

+141
-446
lines changed

Sources/Build/BuildManifest/LLBuildManifestBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public class LLBuildManifestBuilder {
138138
case .swift(let desc):
139139
try self.createSwiftCompileCommand(desc)
140140
case .clang(let desc):
141-
if desc.target.buildTriple == .tools {
141+
if desc.destination == .host {
142142
// Need the clang modules for tools
143143
try self.createClangCompileCommand(desc)
144144
} else {

Sources/Build/BuildPlan/BuildPlan+Test.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,14 @@ extension BuildPlan {
9292
packageAccess: true, // test target is allowed access to package decls by default
9393
testDiscoverySrc: Sources(paths: discoveryPaths, root: discoveryDerivedDir)
9494
)
95-
var discoveryResolvedModule = ResolvedModule(
95+
let discoveryResolvedModule = ResolvedModule(
9696
packageIdentity: testProduct.packageIdentity,
9797
underlying: discoveryTarget,
9898
dependencies: testProduct.modules.map { .module($0, conditions: []) },
9999
defaultLocalization: testProduct.defaultLocalization,
100100
supportedPlatforms: testProduct.supportedPlatforms,
101101
platformVersionProvider: testProduct.platformVersionProvider
102102
)
103-
discoveryResolvedModule.buildTriple = testProduct.buildTriple
104103

105104
let discoveryTargetBuildDescription = try SwiftModuleBuildDescription(
106105
package: package,
@@ -135,15 +134,14 @@ extension BuildPlan {
135134
packageAccess: true, // test target is allowed access to package decls
136135
testEntryPointSources: entryPointSources
137136
)
138-
var entryPointResolvedTarget = ResolvedModule(
137+
let entryPointResolvedTarget = ResolvedModule(
139138
packageIdentity: testProduct.packageIdentity,
140139
underlying: entryPointTarget,
141140
dependencies: testProduct.modules.map { .module($0, conditions: []) } + resolvedTargetDependencies,
142141
defaultLocalization: testProduct.defaultLocalization,
143142
supportedPlatforms: testProduct.supportedPlatforms,
144143
platformVersionProvider: testProduct.platformVersionProvider
145144
)
146-
entryPointResolvedTarget.buildTriple = testProduct.buildTriple
147145

148146
return try SwiftModuleBuildDescription(
149147
package: package,

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
419419
switch $0 {
420420
case .module(let moduleDependency, _):
421421
if moduleDependency.type == .executable {
422-
return graph.product(
423-
for: moduleDependency.name,
424-
destination: .tools
425-
)
422+
return graph.product(for: moduleDependency.name)
426423
}
427424
return nil
428425
default:
@@ -613,7 +610,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
613610
arguments.append("-l" + replProductName)
614611

615612
// The graph should have the REPL product.
616-
assert(self.graph.product(for: replProductName, destination: .destination) != nil)
613+
assert(self.graph.product(for: replProductName) != nil)
617614

618615
// Add the search path to the directory containing the modulemap file.
619616
for target in self.targets {

Sources/Commands/Snippets/Cards/SnippetCard.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct SnippetCard: Card {
9696
let buildSystem = try await swiftCommandState.createBuildSystem(explicitProduct: snippet.name, traitConfiguration: .init())
9797
try await buildSystem.build(subset: .product(snippet.name))
9898
let executablePath = try swiftCommandState.productsBuildParameters.buildPath.appending(component: snippet.name)
99-
if let exampleTarget = try await buildSystem.getPackageGraph().module(for: snippet.name, destination: .destination) {
99+
if let exampleTarget = try await buildSystem.getPackageGraph().module(for: snippet.name) {
100100
try ProcessEnv.chdir(exampleTarget.sources.paths[0].parentDirectory)
101101
}
102102
try exec(path: executablePath.pathString, args: [])

Sources/Commands/SwiftRunCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public struct SwiftRunCommand: AsyncSwiftCommand {
231231
if let executable = options.executable {
232232
// There should be only one product with the given name in the graph
233233
// and it should be executable or snippet.
234-
guard let product = graph.product(for: executable, destination: .destination),
234+
guard let product = graph.product(for: executable),
235235
product.type == .executable || product.type == .snippet
236236
else {
237237
throw RunError.executableNotFound(executable)

Sources/PackageGraph/BuildTriple.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.

Sources/PackageGraph/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
add_library(PackageGraph
1010
BoundVersion.swift
11-
BuildTriple.swift
1211
DependencyMirrors.swift
1312
Diagnostics.swift
1413
GraphLoadingNode.swift

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 10 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -166,65 +166,19 @@ public struct ModulesGraph {
166166

167167
/// Find a product given a name and an optional destination. If a destination is not specified
168168
/// this method uses `.destination` and falls back to `.tools` for macros, plugins, and tests.
169-
public func product(for name: String, destination: BuildTriple? = .none) -> ResolvedProduct? {
170-
func findProduct(name: String, destination: BuildTriple) -> ResolvedProduct? {
171-
self.allProducts.first { $0.name == name && $0.buildTriple == destination }
172-
}
173-
174-
if let destination {
175-
return findProduct(name: name, destination: destination)
176-
}
177-
178-
if let product = findProduct(name: name, destination: .destination) {
179-
return product
180-
}
181-
182-
// It's possible to request a build of a macro, a plugin, or a test via `swift build`
183-
// which won't have the right destination set because it's impossible to indicate it.
184-
//
185-
// Same happens with `--test-product` - if one of the test modules directly references
186-
// a macro then all if its modules and the product itself become `host`.
187-
if let toolsProduct = findProduct(name: name, destination: .tools),
188-
toolsProduct.type == .macro || toolsProduct.type == .plugin || toolsProduct.type == .test
189-
{
190-
return toolsProduct
191-
}
192-
193-
return nil
169+
public func product(for name: String) -> ResolvedProduct? {
170+
self.allProducts.first { $0.name == name }
194171
}
195172

196-
@available(*, deprecated, renamed: "module(for:destination:)")
197-
public func target(for name: String, destination: BuildTriple? = .none) -> ResolvedModule? {
198-
self.module(for: name, destination: destination)
173+
@available(*, deprecated, renamed: "module(for:)")
174+
public func target(for name: String) -> ResolvedModule? {
175+
self.module(for: name)
199176
}
200177

201178
/// Find a module given a name and an optional destination. If a destination is not specified
202179
/// this method uses `.destination` and falls back to `.tools` for macros, plugins, and tests.
203-
public func module(for name: String, destination: BuildTriple? = .none) -> ResolvedModule? {
204-
func findModule(name: String, destination: BuildTriple) -> ResolvedModule? {
205-
self.allModules.first { $0.name == name && $0.buildTriple == destination }
206-
}
207-
208-
if let destination {
209-
return findModule(name: name, destination: destination)
210-
}
211-
212-
if let module = findModule(name: name, destination: .destination) {
213-
return module
214-
}
215-
216-
// It's possible to request a build of a macro, a plugin or a test via `swift build`
217-
// which won't have the right destination set because it's impossible to indicate it.
218-
//
219-
// Same happens with `--test-product` - if one of the test modules directly references
220-
// a macro then all if its modules and the product itself become `host`.
221-
if let toolsModule = findModule(name: name, destination: .tools),
222-
toolsModule.type == .macro || toolsModule.type == .plugin || toolsModule.type == .test
223-
{
224-
return toolsModule
225-
}
226-
227-
return nil
180+
public func module(for name: String) -> ResolvedModule? {
181+
self.allModules.first { $0.name == name }
228182
}
229183

230184
/// All root and root dependency packages provided as input to the graph.
@@ -250,61 +204,16 @@ public struct ModulesGraph {
250204
var allModules = IdentifiableSet<ResolvedModule>()
251205
var allProducts = IdentifiableSet<ResolvedProduct>()
252206
for package in self.packages {
253-
let modulesToInclude: [ResolvedModule]
254-
if rootPackages.contains(id: package.id) {
255-
modulesToInclude = Array(package.modules)
207+
let modulesToInclude = if rootPackages.contains(id: package.id) {
208+
Array(package.modules)
256209
} else {
257210
// Don't include tests modules from non-root packages so swift-test doesn't
258211
// try to run them.
259-
modulesToInclude = package.modules.filter { $0.type != .test }
212+
package.modules.filter { $0.type != .test }
260213
}
261214

262215
for module in modulesToInclude {
263216
allModules.insert(module)
264-
265-
// Explicitly include dependencies of host tools in the maps of all modules or all products
266-
if module.buildTriple == .tools {
267-
for dependency in try module.recursiveDependencies() {
268-
switch dependency {
269-
case .module(let moduleDependency, _):
270-
allModules.insert(moduleDependency)
271-
case .product(let productDependency, _):
272-
allProducts.insert(productDependency)
273-
}
274-
}
275-
}
276-
277-
// Create a new executable product if plugin depends on an executable module.
278-
// This is necessary, even though PackageBuilder creates one already, because
279-
// that product is going to be built for `destination`, and this one has to
280-
// be built for `tools`.
281-
if module.underlying is PluginModule {
282-
for dependency in module.dependencies {
283-
switch dependency {
284-
case .product(_, conditions: _):
285-
break
286-
287-
case .module(let module, conditions: _):
288-
if module.type != .executable {
289-
continue
290-
}
291-
292-
var product = try ResolvedProduct(
293-
packageIdentity: module.packageIdentity,
294-
product: .init(
295-
package: module.packageIdentity,
296-
name: module.name,
297-
type: .executable,
298-
modules: [module.underlying]
299-
),
300-
modules: IdentifiableSet([module])
301-
)
302-
product.buildTriple = .tools
303-
304-
allProducts.insert(product)
305-
}
306-
}
307-
}
308217
}
309218

310219
if rootPackages.contains(id: package.id) {

Sources/PackageGraph/Resolution/ResolvedModule.swift

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ public struct ResolvedModule {
168168
@_spi(SwiftPMInternal)
169169
public let platformVersionProvider: PlatformVersionProvider
170170

171-
/// Triple for which this resolved module should be compiled for.
172-
public package(set) var buildTriple: BuildTriple {
173-
didSet {
174-
self.updateBuildTriplesOfDependencies()
175-
}
176-
}
177-
178171
package var hasDirectMacroDependencies: Bool {
179172
self.dependencies.contains(where: {
180173
switch $0 {
@@ -201,50 +194,6 @@ public struct ResolvedModule {
201194
self.defaultLocalization = defaultLocalization
202195
self.supportedPlatforms = supportedPlatforms
203196
self.platformVersionProvider = platformVersionProvider
204-
205-
if underlying.type == .test {
206-
// Make sure that test products are built for the tools triple if it has tools as direct dependencies.
207-
// Without this workaround, `assertMacroExpansion` in tests can't be built, as it requires macros
208-
// and SwiftSyntax to be built for the same triple as the tests.
209-
// See https://github.com/swiftlang/swift-package-manager/pull/7349 for more context.
210-
var inferredBuildTriple = BuildTriple.destination
211-
loop: for dependency in dependencies {
212-
switch dependency {
213-
case .module(let moduleDependency, _):
214-
if moduleDependency.type == .macro {
215-
inferredBuildTriple = .tools
216-
break loop
217-
}
218-
case .product(let productDependency, _):
219-
if productDependency.type == .macro {
220-
inferredBuildTriple = .tools
221-
break loop
222-
}
223-
}
224-
}
225-
self.buildTriple = inferredBuildTriple
226-
} else {
227-
self.buildTriple = underlying.buildTriple
228-
}
229-
self.updateBuildTriplesOfDependencies()
230-
}
231-
232-
mutating func updateBuildTriplesOfDependencies() {
233-
if self.buildTriple == .tools {
234-
for (i, dependency) in dependencies.enumerated() {
235-
let updatedDependency: Dependency
236-
switch dependency {
237-
case .module(var module, let conditions):
238-
module.buildTriple = self.buildTriple
239-
updatedDependency = .module(module, conditions: conditions)
240-
case .product(var product, let conditions):
241-
product.buildTriple = self.buildTriple
242-
updatedDependency = .product(product, conditions: conditions)
243-
}
244-
245-
dependencies[i] = updatedDependency
246-
}
247-
}
248197
}
249198

250199
public func getSupportedPlatform(for platform: Platform, usingXCTest: Bool) -> SupportedPlatform {
@@ -258,7 +207,7 @@ public struct ResolvedModule {
258207

259208
extension ResolvedModule: CustomStringConvertible {
260209
public var description: String {
261-
return "<ResolvedModule: \(self.name), \(self.type), \(self.buildTriple)>"
210+
return "<ResolvedModule: \(self.name), \(self.type)>"
262211
}
263212
}
264213

@@ -333,11 +282,10 @@ extension ResolvedModule: Identifiable {
333282

334283
public let moduleName: String
335284
let packageIdentity: PackageIdentity
336-
public var buildTriple: BuildTriple
337285
}
338286

339287
public var id: ID {
340-
ID(moduleName: self.name, packageIdentity: self.packageIdentity, buildTriple: self.buildTriple)
288+
ID(moduleName: self.name, packageIdentity: self.packageIdentity)
341289
}
342290
}
343291

0 commit comments

Comments
 (0)