Skip to content

Commit 42bafb2

Browse files
committed
Allow SwiftTargetDescription, ClangTargetDescription, and ProductDescription to know the tools version of the package in which they are defined. This allows compiler flags and other semantically significant changes to be conditionalized on the tools version.
In the cases where tools versions are synthesized, they get the tools version of the package defining the product or target for which they are being synthesized. The fallback for anything that cannot be determined at all is always `.vNext`, which is the same as has been the case until now.
1 parent d5e6fea commit 42bafb2

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ public final class ClangTargetBuildDescription {
195195
public var clangTarget: ClangTarget {
196196
return target.underlyingTarget as! ClangTarget
197197
}
198+
199+
/// The tools version of the package that declared the target. This can
200+
/// can be used to conditionalize semantically significant changes in how
201+
/// a target is built.
202+
public let toolsVersion: ToolsVersion
198203

199204
/// The build parameters.
200205
let buildParameters: BuildParameters
@@ -249,11 +254,12 @@ public final class ClangTargetBuildDescription {
249254
}
250255

251256
/// Create a new target description with target and build parameters.
252-
init(target: ResolvedTarget, buildParameters: BuildParameters, fileSystem: FileSystem = localFileSystem, diagnostics: DiagnosticsEngine) throws {
257+
init(target: ResolvedTarget, toolsVersion: ToolsVersion, buildParameters: BuildParameters, fileSystem: FileSystem = localFileSystem, diagnostics: DiagnosticsEngine) throws {
253258
assert(target.underlyingTarget is ClangTarget, "underlying target type mismatch \(target)")
254259
self.fileSystem = fileSystem
255260
self.diagnostics = diagnostics
256261
self.target = target
262+
self.toolsVersion = toolsVersion
257263
self.buildParameters = buildParameters
258264
self.tempsPath = buildParameters.buildPath.appending(component: target.c99name + ".build")
259265
self.derivedSources = Sources(paths: [], root: tempsPath.appending(component: "DerivedSources"))
@@ -472,6 +478,11 @@ public final class SwiftTargetBuildDescription {
472478
/// The target described by this target.
473479
public let target: ResolvedTarget
474480

481+
/// The tools version of the package that declared the target. This can
482+
/// can be used to conditionalize semantically significant changes in how
483+
/// a target is built.
484+
public let toolsVersion: ToolsVersion
485+
475486
/// The build parameters.
476487
let buildParameters: BuildParameters
477488

@@ -547,6 +558,7 @@ public final class SwiftTargetBuildDescription {
547558
/// Create a new target description with target and build parameters.
548559
init(
549560
target: ResolvedTarget,
561+
toolsVersion: ToolsVersion,
550562
buildParameters: BuildParameters,
551563
pluginInvocationResults: [PluginInvocationResult] = [],
552564
isTestTarget: Bool? = nil,
@@ -555,6 +567,7 @@ public final class SwiftTargetBuildDescription {
555567
) throws {
556568
assert(target.underlyingTarget is SwiftTarget, "underlying target type mismatch \(target)")
557569
self.target = target
570+
self.toolsVersion = toolsVersion
558571
self.buildParameters = buildParameters
559572
// Unless mentioned explicitly, use the target type to determine if this is a test target.
560573
self.isTestTarget = isTestTarget ?? (target.type == .test)
@@ -1003,6 +1016,11 @@ public final class ProductBuildDescription {
10031016
/// The reference to the product.
10041017
public let product: ResolvedProduct
10051018

1019+
/// The tools version of the package that declared the product. This can
1020+
/// can be used to conditionalize semantically significant changes in how
1021+
/// a target is built.
1022+
public let toolsVersion: ToolsVersion
1023+
10061024
/// The build parameters.
10071025
let buildParameters: BuildParameters
10081026

@@ -1052,9 +1070,10 @@ public final class ProductBuildDescription {
10521070
let diagnostics: DiagnosticsEngine
10531071

10541072
/// Create a build description for a product.
1055-
init(product: ResolvedProduct, buildParameters: BuildParameters, fs: FileSystem, diagnostics: DiagnosticsEngine) {
1073+
init(product: ResolvedProduct, toolsVersion: ToolsVersion, buildParameters: BuildParameters, fs: FileSystem, diagnostics: DiagnosticsEngine) {
10561074
assert(product.type != .library(.automatic), "Automatic type libraries should not be described.")
10571075
self.product = product
1076+
self.toolsVersion = toolsVersion
10581077
self.buildParameters = buildParameters
10591078
self.fs = fs
10601079
self.diagnostics = diagnostics
@@ -1308,9 +1327,11 @@ public class BuildPlan {
13081327
// if test manifest exists, prefer that over test detection,
13091328
// this is designed as an escape hatch when test discovery is not appropriate
13101329
// and for backwards compatibility for projects that have existing test manifests (LinuxMain.swift)
1330+
let toolsVersion = graph.package(for: testProduct)?.manifest.toolsVersion ?? .vNext
13111331
if let testManifestTarget = testProduct.testManifestTarget, !generate {
13121332
let desc = try SwiftTargetBuildDescription(
13131333
target: testManifestTarget,
1334+
toolsVersion: toolsVersion,
13141335
buildParameters: buildParameters,
13151336
isTestTarget: true
13161337
)
@@ -1342,6 +1363,7 @@ public class BuildPlan {
13421363

13431364
let target = try SwiftTargetBuildDescription(
13441365
target: testManifestTarget,
1366+
toolsVersion: toolsVersion,
13451367
buildParameters: buildParameters,
13461368
isTestTarget: true,
13471369
testDiscoveryTarget: true
@@ -1382,17 +1404,23 @@ public class BuildPlan {
13821404
}
13831405
}
13841406
}
1407+
1408+
// Determine the appropriate tools version to use for the target.
1409+
// This can affect what flags to pass and other semantics.
1410+
let toolsVersion = graph.package(for: target)?.manifest.toolsVersion ?? .vNext
13851411

13861412
switch target.underlyingTarget {
13871413
case is SwiftTarget:
13881414
targetMap[target] = try .swift(SwiftTargetBuildDescription(
13891415
target: target,
1416+
toolsVersion: toolsVersion,
13901417
buildParameters: buildParameters,
13911418
pluginInvocationResults: pluginInvocationResults[target] ?? [],
13921419
fs: fileSystem))
13931420
case is ClangTarget:
13941421
targetMap[target] = try .clang(ClangTargetBuildDescription(
13951422
target: target,
1423+
toolsVersion: toolsVersion,
13961424
buildParameters: buildParameters,
13971425
fileSystem: fileSystem,
13981426
diagnostics: diagnostics))
@@ -1426,8 +1454,14 @@ public class BuildPlan {
14261454
// Create product description for each product we have in the package graph except
14271455
// for automatic libraries and plugins, because they don't produce any output.
14281456
for product in graph.allProducts where product.type != .library(.automatic) && product.type != .plugin {
1457+
1458+
// Determine the appropriate tools version to use for the product.
1459+
// This can affect what flags to pass and other semantics.
1460+
let toolsVersion = graph.package(for: product)?.manifest.toolsVersion ?? .vNext
14291461
productMap[product] = ProductBuildDescription(
1430-
product: product, buildParameters: buildParameters,
1462+
product: product,
1463+
toolsVersion: toolsVersion,
1464+
buildParameters: buildParameters,
14311465
fs: fileSystem,
14321466
diagnostics: diagnostics
14331467
)

0 commit comments

Comments
 (0)