Skip to content

Commit 5b0597d

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 4e791bb commit 5b0597d

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

@@ -555,6 +566,7 @@ public final class SwiftTargetBuildDescription {
555566
/// Create a new target description with target and build parameters.
556567
init(
557568
target: ResolvedTarget,
569+
toolsVersion: ToolsVersion,
558570
buildParameters: BuildParameters,
559571
pluginInvocationResults: [PluginInvocationResult] = [],
560572
prebuildCommandResults: [PrebuildCommandResult] = [],
@@ -564,6 +576,7 @@ public final class SwiftTargetBuildDescription {
564576
) throws {
565577
assert(target.underlyingTarget is SwiftTarget, "underlying target type mismatch \(target)")
566578
self.target = target
579+
self.toolsVersion = toolsVersion
567580
self.buildParameters = buildParameters
568581
// Unless mentioned explicitly, use the target type to determine if this is a test target.
569582
self.isTestTarget = isTestTarget ?? (target.type == .test)
@@ -1018,6 +1031,11 @@ public final class ProductBuildDescription {
10181031
/// The reference to the product.
10191032
public let product: ResolvedProduct
10201033

1034+
/// The tools version of the package that declared the product. This can
1035+
/// can be used to conditionalize semantically significant changes in how
1036+
/// a target is built.
1037+
public let toolsVersion: ToolsVersion
1038+
10211039
/// The build parameters.
10221040
let buildParameters: BuildParameters
10231041

@@ -1067,9 +1085,10 @@ public final class ProductBuildDescription {
10671085
let diagnostics: DiagnosticsEngine
10681086

10691087
/// Create a build description for a product.
1070-
init(product: ResolvedProduct, buildParameters: BuildParameters, fs: FileSystem, diagnostics: DiagnosticsEngine) {
1088+
init(product: ResolvedProduct, toolsVersion: ToolsVersion, buildParameters: BuildParameters, fs: FileSystem, diagnostics: DiagnosticsEngine) {
10711089
assert(product.type != .library(.automatic), "Automatic type libraries should not be described.")
10721090
self.product = product
1091+
self.toolsVersion = toolsVersion
10731092
self.buildParameters = buildParameters
10741093
self.fs = fs
10751094
self.diagnostics = diagnostics
@@ -1327,9 +1346,11 @@ public class BuildPlan {
13271346
// if test manifest exists, prefer that over test detection,
13281347
// this is designed as an escape hatch when test discovery is not appropriate
13291348
// and for backwards compatibility for projects that have existing test manifests (LinuxMain.swift)
1349+
let toolsVersion = graph.package(for: testProduct)?.manifest.toolsVersion ?? .vNext
13301350
if let testManifestTarget = testProduct.testManifestTarget, !generate {
13311351
let desc = try SwiftTargetBuildDescription(
13321352
target: testManifestTarget,
1353+
toolsVersion: toolsVersion,
13331354
buildParameters: buildParameters,
13341355
isTestTarget: true
13351356
)
@@ -1361,6 +1382,7 @@ public class BuildPlan {
13611382

13621383
let target = try SwiftTargetBuildDescription(
13631384
target: testManifestTarget,
1385+
toolsVersion: toolsVersion,
13641386
buildParameters: buildParameters,
13651387
isTestTarget: true,
13661388
testDiscoveryTarget: true
@@ -1403,18 +1425,24 @@ public class BuildPlan {
14031425
}
14041426
}
14051427
}
1428+
1429+
// Determine the appropriate tools version to use for the target.
1430+
// This can affect what flags to pass and other semantics.
1431+
let toolsVersion = graph.package(for: target)?.manifest.toolsVersion ?? .vNext
14061432

14071433
switch target.underlyingTarget {
14081434
case is SwiftTarget:
14091435
targetMap[target] = try .swift(SwiftTargetBuildDescription(
14101436
target: target,
1437+
toolsVersion: toolsVersion,
14111438
buildParameters: buildParameters,
14121439
pluginInvocationResults: pluginInvocationResults[target] ?? [],
14131440
prebuildCommandResults: prebuildCommandResults[target] ?? [],
14141441
fs: fileSystem))
14151442
case is ClangTarget:
14161443
targetMap[target] = try .clang(ClangTargetBuildDescription(
14171444
target: target,
1445+
toolsVersion: toolsVersion,
14181446
buildParameters: buildParameters,
14191447
fileSystem: fileSystem,
14201448
diagnostics: diagnostics))
@@ -1448,8 +1476,14 @@ public class BuildPlan {
14481476
// Create product description for each product we have in the package graph except
14491477
// for automatic libraries and plugins, because they don't produce any output.
14501478
for product in graph.allProducts where product.type != .library(.automatic) && product.type != .plugin {
1479+
1480+
// Determine the appropriate tools version to use for the product.
1481+
// This can affect what flags to pass and other semantics.
1482+
let toolsVersion = graph.package(for: product)?.manifest.toolsVersion ?? .vNext
14511483
productMap[product] = ProductBuildDescription(
1452-
product: product, buildParameters: buildParameters,
1484+
product: product,
1485+
toolsVersion: toolsVersion,
1486+
buildParameters: buildParameters,
14531487
fs: fileSystem,
14541488
diagnostics: diagnostics
14551489
)

0 commit comments

Comments
 (0)