Skip to content

Commit b0e724b

Browse files
committed
[Package/Build] Remove toolsSwiftVersion from SwiftTarget in favor of a build setting
Start using "default" assignments to represent tools version based swift language version setting that is going to be produced by `BuildSettings.Scope` if no other assignments match build environment conditions. This removes ambiguity from `-swift-version` selection for downstream clients.
1 parent eb3abcd commit b0e724b

File tree

7 files changed

+39
-37
lines changed

7 files changed

+39
-37
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ package final class SwiftTargetBuildDescription {
146146
/// Any addition flags to be added. These flags are expected to be computed during build planning.
147147
var additionalFlags: [String] = []
148148

149-
/// The swift language version that is computed for this target based on tools version of the manifest.
150-
var toolsSwiftVersion: SwiftLanguageVersion {
151-
self.swiftTarget.toolSwiftVersion
152-
}
153-
154149
/// Describes the purpose of a test target, including any special roles such as containing a list of discovered
155150
/// tests or serving as the manifest target which contains the main entry point.
156151
package enum TestTargetRole {
@@ -580,11 +575,6 @@ package final class SwiftTargetBuildDescription {
580575
// Add arguments from declared build settings.
581576
args += try self.buildSettingsFlags()
582577

583-
// Fallback to package wide setting if there is no target specific version.
584-
if args.firstIndex(of: "-swift-version") == nil {
585-
args += ["-swift-version", self.toolsSwiftVersion.rawValue]
586-
}
587-
588578
// Add the output for the `.swiftinterface`, if requested or if library evolution has been enabled some other
589579
// way.
590580
if self.defaultBuildParameters.driverParameters.enableParseableModuleInterfaces || args.contains("-enable-library-evolution") {

Sources/Build/BuildPlan/BuildPlan+Test.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ private extension PackageModel.SwiftTarget {
268268
sources: sources,
269269
dependencies: dependencies,
270270
packageAccess: packageAccess,
271-
toolsSwiftVersion: .v5,
272271
usesUnsafeFlags: false
273272
)
274273
}

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,8 @@ public final class PackageBuilder {
906906
let buildSettings = try self.buildSettings(
907907
for: manifestTarget,
908908
targetRoot: potentialModule.path,
909-
cxxLanguageStandard: self.manifest.cxxLanguageStandard
909+
cxxLanguageStandard: self.manifest.cxxLanguageStandard,
910+
toolsSwiftVersion: self.toolsSwiftVersion()
910911
)
911912

912913
// Compute the path to public headers directory.
@@ -997,7 +998,6 @@ public final class PackageBuilder {
997998
others: others,
998999
dependencies: dependencies,
9991000
packageAccess: potentialModule.packageAccess,
1000-
toolsSwiftVersion: self.toolsSwiftVersion(),
10011001
declaredSwiftVersions: self.declaredSwiftVersions(),
10021002
buildSettings: buildSettings,
10031003
buildSettingsDescription: manifestTarget.settings,
@@ -1055,11 +1055,18 @@ public final class PackageBuilder {
10551055
func buildSettings(
10561056
for target: TargetDescription?,
10571057
targetRoot: AbsolutePath,
1058-
cxxLanguageStandard: String? = nil
1058+
cxxLanguageStandard: String? = nil,
1059+
toolsSwiftVersion: SwiftLanguageVersion
10591060
) throws -> BuildSettings.AssignmentTable {
10601061
var table = BuildSettings.AssignmentTable()
10611062
guard let target else { return table }
10621063

1064+
// First let's add a default assignments for tools swift version.
1065+
var versionAssignment = BuildSettings.Assignment(default: true)
1066+
versionAssignment.values = [toolsSwiftVersion.rawValue]
1067+
1068+
table.add(versionAssignment, for: .SWIFT_VERSION)
1069+
10631070
// Process each setting.
10641071
for setting in target.settings {
10651072
let decl: BuildSettings.Declaration
@@ -1728,17 +1735,17 @@ extension PackageBuilder {
17281735
)
17291736
buildSettings = try self.buildSettings(
17301737
for: targetDescription,
1731-
targetRoot: sourceFile.parentDirectory
1738+
targetRoot: sourceFile.parentDirectory,
1739+
toolsSwiftVersion: self.toolsSwiftVersion()
17321740
)
17331741

1734-
return try SwiftTarget(
1742+
return SwiftTarget(
17351743
name: name,
17361744
type: .snippet,
17371745
path: .root,
17381746
sources: sources,
17391747
dependencies: dependencies,
17401748
packageAccess: false,
1741-
toolsSwiftVersion: self.toolsSwiftVersion(),
17421749
buildSettings: buildSettings,
17431750
buildSettingsDescription: targetDescription.settings,
17441751
usesUnsafeFlags: false

Sources/PackageModel/Target/SwiftTarget.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public final class SwiftTarget: Target {
2323
}
2424

2525
public init(name: String, dependencies: [Target.Dependency], packageAccess: Bool, testDiscoverySrc: Sources) {
26-
self.toolSwiftVersion = .v5
2726
self.declaredSwiftVersions = []
2827

2928
super.init(
@@ -40,9 +39,6 @@ public final class SwiftTarget: Target {
4039
)
4140
}
4241

43-
/// The swift language version that is computed for this target based on tools version of the manifest.
44-
public let toolSwiftVersion: SwiftLanguageVersion
45-
4642
/// The list of swift versions declared by the manifest.
4743
public let declaredSwiftVersions: [SwiftLanguageVersion]
4844

@@ -57,14 +53,12 @@ public final class SwiftTarget: Target {
5753
others: [AbsolutePath] = [],
5854
dependencies: [Target.Dependency] = [],
5955
packageAccess: Bool,
60-
toolsSwiftVersion: SwiftLanguageVersion,
6156
declaredSwiftVersions: [SwiftLanguageVersion] = [],
6257
buildSettings: BuildSettings.AssignmentTable = .init(),
6358
buildSettingsDescription: [TargetBuildSettingDescription.Setting] = [],
6459
pluginUsages: [PluginUsage] = [],
6560
usesUnsafeFlags: Bool
6661
) {
67-
self.toolSwiftVersion = toolsSwiftVersion
6862
self.declaredSwiftVersions = declaredSwiftVersions
6963
super.init(
7064
name: name,
@@ -99,12 +93,22 @@ public final class SwiftTarget: Target {
9993
return target.type == .test
10094
}.flatMap { $0.target as? SwiftTarget }
10195

102-
// FIXME: This is not very correct but doesn't matter much in practice.
10396
// We need to select the latest Swift language version that can
10497
// satisfy the current tools version but there is not a good way to
10598
// do that currently.
106-
self.toolSwiftVersion = swiftTestTarget?
107-
.toolSwiftVersion ?? SwiftLanguageVersion(string: String(SwiftVersion.current.major)) ?? .v4
99+
var buildSettings: BuildSettings.AssignmentTable = .init()
100+
do {
101+
let toolsSwiftVersion = swiftTestTarget?.buildSettings.assignments[.SWIFT_VERSION]?
102+
.filter(\.default)
103+
.filter(\.conditions.isEmpty)
104+
.flatMap(\.values)
105+
106+
var versionAssignment = BuildSettings.Assignment()
107+
versionAssignment.values = toolsSwiftVersion ?? [String(SwiftVersion.current.major)]
108+
109+
buildSettings.add(versionAssignment, for: .SWIFT_VERSION)
110+
}
111+
108112
self.declaredSwiftVersions = []
109113
let sources = Sources(paths: [testEntryPointPath], root: testEntryPointPath.parentDirectory)
110114

@@ -115,28 +119,25 @@ public final class SwiftTarget: Target {
115119
sources: sources,
116120
dependencies: dependencies,
117121
packageAccess: packageAccess,
118-
buildSettings: .init(),
122+
buildSettings: buildSettings,
119123
buildSettingsDescription: [],
120124
pluginUsages: [],
121125
usesUnsafeFlags: false
122126
)
123127
}
124128

125129
private enum CodingKeys: String, CodingKey {
126-
case swiftVersion
127130
case declaredSwiftVersions
128131
}
129132

130133
override public func encode(to encoder: Encoder) throws {
131134
var container = encoder.container(keyedBy: CodingKeys.self)
132-
try container.encode(self.toolSwiftVersion, forKey: .swiftVersion)
133135
try container.encode(self.declaredSwiftVersions, forKey: .declaredSwiftVersions)
134136
try super.encode(to: encoder)
135137
}
136138

137139
public required init(from decoder: Decoder) throws {
138140
let container = try decoder.container(keyedBy: CodingKeys.self)
139-
self.toolSwiftVersion = try container.decode(SwiftLanguageVersion.self, forKey: .swiftVersion)
140141
self.declaredSwiftVersions = try container.decode([SwiftLanguageVersion].self, forKey: .declaredSwiftVersions)
141142
try super.init(from: decoder)
142143
}

Sources/SPMTestSupport/ResolvedTarget+Mock.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ extension ResolvedModule {
2929
sources: Sources(paths: [], root: "/"),
3030
dependencies: [],
3131
packageAccess: false,
32-
toolsSwiftVersion: .v4,
3332
usesUnsafeFlags: false
3433
),
3534
dependencies: deps.map { .target($0, conditions: conditions) },

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4068,13 +4068,13 @@ final class BuildPlanTests: XCTestCase {
40684068
bar,
40694069
[
40704070
.anySequence,
4071+
"-swift-version", "5",
40714072
"-DLINUX",
40724073
"-Isfoo",
40734074
"-L", "sbar",
40744075
"-cxx-interoperability-mode=default",
40754076
"-Xcc", "-std=c++17",
40764077
"-enable-upcoming-feature", "BestFeature",
4077-
"-swift-version", "5",
40784078
"-g",
40794079
"-Xcc", "-g",
40804080
"-Xcc", "-fno-omit-frame-pointer",
@@ -4133,14 +4133,14 @@ final class BuildPlanTests: XCTestCase {
41334133
bar,
41344134
[
41354135
.anySequence,
4136+
"-swift-version", "5",
41364137
"-DLINUX",
41374138
"-Isfoo",
41384139
"-L", "sbar",
41394140
"-cxx-interoperability-mode=default",
41404141
"-Xcc", "-std=c++17",
41414142
"-enable-upcoming-feature",
41424143
"BestFeature",
4143-
"-swift-version", "5",
41444144
"-g",
41454145
"-Xcc", "-g",
41464146
"-Xcc", "-fomit-frame-pointer",
@@ -4190,14 +4190,14 @@ final class BuildPlanTests: XCTestCase {
41904190
bar,
41914191
[
41924192
.anySequence,
4193+
"-swift-version", "5",
41934194
"-DLINUX",
41944195
"-Isfoo",
41954196
"-L", "sbar",
41964197
"-cxx-interoperability-mode=default",
41974198
"-Xcc", "-std=c++17",
41984199
"-enable-upcoming-feature",
41994200
"BestFeature",
4200-
"-swift-version", "5",
42014201
"-g",
42024202
"-Xcc", "-g",
42034203
"-Xcc", "-fno-omit-frame-pointer",
@@ -4234,14 +4234,14 @@ final class BuildPlanTests: XCTestCase {
42344234
bar,
42354235
[
42364236
.anySequence,
4237+
"-swift-version", "5",
42374238
"-DDMACOS",
42384239
"-Isfoo",
42394240
"-L", "sbar",
42404241
"-cxx-interoperability-mode=default",
42414242
"-Xcc", "-std=c++17",
42424243
"-enable-upcoming-feature", "BestFeature",
42434244
"-enable-upcoming-feature", "WorstFeature",
4244-
"-swift-version", "5",
42454245
"-g",
42464246
"-Xcc", "-g",
42474247
.end,

Tests/PackageLoadingTests/PackageBuilderTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,8 +3012,12 @@ final class PackageBuilderTests: XCTestCase {
30123012
assignment.values = ["YOLO"]
30133013
assignment.conditions = [PackageCondition(platforms: [.custom(name: "bestOS", oldestSupportedVersion: .unknown)])]
30143014

3015+
var versionAssignment = BuildSettings.Assignment(default: true)
3016+
versionAssignment.values = ["4"]
3017+
30153018
var settings = BuildSettings.AssignmentTable()
30163019
settings.add(assignment, for: .SWIFT_ACTIVE_COMPILATION_CONDITIONS)
3020+
settings.add(versionAssignment, for: .SWIFT_VERSION)
30173021

30183022
PackageBuilderTester(manifest, in: fs) { package, _ in
30193023
package.checkModule("Foo") { module in
@@ -3081,7 +3085,7 @@ final class PackageBuilderTests: XCTestCase {
30813085
package.target.buildSettings,
30823086
environment: BuildEnvironment(platform: .macOS, configuration: .release)
30833087
)
3084-
XCTAssertEqual(macosReleaseScope.evaluate(.SWIFT_VERSION), [])
3088+
XCTAssertEqual(macosReleaseScope.evaluate(.SWIFT_VERSION), ["5"])
30853089
}
30863090
}
30873091
}
@@ -3303,7 +3307,9 @@ final class PackageBuilderTester {
33033307
guard case let swiftTarget as SwiftTarget = target else {
33043308
return XCTFail("\(target) is not a swift target", file: file, line: line)
33053309
}
3306-
XCTAssertEqual(SwiftLanguageVersion(string: swiftVersion)!, swiftTarget.toolSwiftVersion, file: file, line: line)
3310+
let versionAssignments = swiftTarget.buildSettings.assignments[.SWIFT_VERSION]?
3311+
.filter { $0.conditions.isEmpty }.flatMap(\.values)
3312+
XCTAssertNotNil(versionAssignments?.contains(swiftVersion), file: file, line: line)
33073313
}
33083314

33093315
func check(pluginCapability: PluginCapability, file: StaticString = #file, line: UInt = #line) {

0 commit comments

Comments
 (0)