Skip to content

Commit 04ab3dc

Browse files
committed
[Swift Build] Default to the package's declared deployment target if none is explicitly specified
1 parent 8b1cfc9 commit 04ab3dc

File tree

9 files changed

+97
-9
lines changed

9 files changed

+97
-9
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@available(macOS, obsoleted: 13.0)
2+
func foo() {
3+
4+
}
5+
6+
func bar() {
7+
foo()
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// swift-tools-version:6.1
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "Foo",
6+
platforms: [.macOS(.v12)],
7+
products: [
8+
.library(name: "Foo", targets: ["Foo"]),
9+
],
10+
targets: [
11+
.target(name: "Foo", path: "./"),
12+
]
13+
)

Sources/Basics/Triple+Basics.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,16 @@ extension Triple {
8585
}
8686

8787
/// Determine the versioned host triple using the Swift compiler.
88-
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) throws -> Triple {
88+
public static func getVersionedHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) throws -> Triple {
89+
try Self.getHostTriple(usingSwiftCompiler: swiftCompiler, versioned: true)
90+
}
91+
92+
/// Determine the unversioned host triple using the Swift compiler.
93+
public static func getUnversionedHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) throws -> Triple {
94+
try Self.getHostTriple(usingSwiftCompiler: swiftCompiler, versioned: false)
95+
}
96+
97+
private static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath, versioned: Bool) throws -> Triple {
8998
// Call the compiler to get the target info JSON.
9099
let compilerOutput: String
91100
do {
@@ -106,7 +115,7 @@ extension Triple {
106115
// Get the triple string from the parsed JSON.
107116
let tripleString: String
108117
do {
109-
tripleString = try parsedTargetInfo.get("target").get("triple")
118+
tripleString = try parsedTargetInfo.get("target").get(versioned ? "triple" : "unversionedTriple")
110119
} catch {
111120
throw InternalError(
112121
"Target info does not contain a triple string (\(error.interpolationDescription)).\nTarget info: \(parsedTargetInfo)"

Sources/Commands/PackageCommands/AuditBinaryArtifact.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct AuditBinaryArtifact: AsyncSwiftCommand {
3737
let hostToolchain = try swiftCommandState.getHostToolchain()
3838
let clang = try hostToolchain.getClangCompiler()
3939
let objdump = try hostToolchain.getLLVMObjdump()
40-
let hostTriple = try Triple.getHostTriple(
40+
let hostTriple = try Triple.getVersionedHostTriple(
4141
usingSwiftCompiler: hostToolchain.swiftCompilerPath)
4242
let fileSystem = swiftCommandState.fileSystem
4343

Sources/PackageModel/UserToolchain.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ public final class UserToolchain: Toolchain {
200200
}
201201
}
202202

203-
private static func getHostTriple(targetInfo: JSON) throws -> Basics.Triple {
203+
private static func getHostTriple(targetInfo: JSON, versioned: Bool) throws -> Basics.Triple {
204204
// Get the triple string from the target info.
205205
let tripleString: String
206206
do {
207-
tripleString = try targetInfo.get("target").get("triple")
207+
tripleString = try targetInfo.get("target").get(versioned ? "triple" : "unversionedTriple")
208208
} catch {
209209
throw InternalError(
210210
"Target info does not contain a triple string (\(error.interpolationDescription)).\nTarget info: \(targetInfo)"
@@ -741,7 +741,7 @@ public final class UserToolchain: Toolchain {
741741
// targetInfo from the compiler
742742
let targetInfo = try customTargetInfo ?? Self.getTargetInfo(swiftCompiler: swiftCompilers.compile)
743743
self._targetInfo = targetInfo
744-
triple = try swiftSDK.targetTriple ?? Self.getHostTriple(targetInfo: targetInfo)
744+
triple = try swiftSDK.targetTriple ?? Self.getHostTriple(targetInfo: targetInfo, versioned: false)
745745
}
746746

747747
// Change the triple to the specified arch if there's exactly one of them.

Sources/SPMBuildCore/BuildParameters/BuildParameters.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public struct BuildParameters: Encodable {
173173
testingParameters: Testing = .init(),
174174
apiDigesterMode: APIDigesterMode? = nil
175175
) throws {
176-
let triple = try triple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
176+
// Default to the unversioned triple if none is provided so that we defer to the package's requested deployment target.
177+
let triple = try triple ?? .getUnversionedHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
177178
self.debuggingParameters = debuggingParameters ?? .init(
178179
triple: triple,
179180
shouldEnableDebuggingEntitlement: configuration == .debug,

Sources/SwiftSDKCommand/ConfigureSwiftSDK.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct ConfigureSwiftSDK: AsyncParsableCommand {
117117
let swiftSDKsDirectory = try self.getOrCreateSwiftSDKsDirectory()
118118

119119
let hostToolchain = try UserToolchain(swiftSDK: SwiftSDK.hostSwiftSDK())
120-
let triple = try Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
120+
let triple = try Triple.getVersionedHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
121121

122122
var commandError: Error? = nil
123123
do {

Sources/SwiftSDKCommand/SwiftSDKSubcommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ extension SwiftSDKSubcommand {
6868
),
6969
environment: environment
7070
)
71-
let triple = try Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
71+
let triple = try Triple.getVersionedHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
7272

7373
var commandError: Error? = nil
7474
do {

Tests/CommandsTests/BuildCommandTests.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,63 @@ struct BuildCommandTestCases {
13961396
}
13971397
}
13981398
}
1399+
1400+
@Test(.requireHostOS(.macOS), arguments: SupportedBuildSystemOnPlatform)
1401+
func buildingPackageWhichRequiresOlderDeploymentTarget(buildSystem: BuildSystemProvider.Kind) async throws {
1402+
// This fixture specifies a deployment target of macOS 12, and uses API obsoleted in macOS 13. The goal
1403+
// of this test is to ensure that SwiftPM respects the deployment target specified in the package manifest
1404+
// when passed no triple of an unversioned triple, rather than using the latests deployment target.
1405+
1406+
// No triple - build should pass
1407+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1408+
try await executeSwiftBuild(
1409+
path,
1410+
buildSystem: buildSystem,
1411+
throwIfCommandFails: true
1412+
)
1413+
}
1414+
1415+
let hostArch: String
1416+
#if arch(arm64)
1417+
hostArch = "arm64"
1418+
#else
1419+
hostArch = "x86_64"
1420+
#endif
1421+
1422+
// Unversioned triple - build should pass
1423+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1424+
try await executeSwiftBuild(
1425+
path,
1426+
extraArgs: ["--triple", "\(hostArch)-apple-macosx"],
1427+
buildSystem: buildSystem,
1428+
throwIfCommandFails: true
1429+
)
1430+
}
1431+
1432+
// Versioned triple with supported deployment target - build should pass
1433+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1434+
try await executeSwiftBuild(
1435+
path,
1436+
extraArgs: ["--triple", "\(hostArch)-apple-macosx12.0"],
1437+
buildSystem: buildSystem,
1438+
throwIfCommandFails: true
1439+
)
1440+
}
1441+
1442+
if buildSystem == .swiftbuild {
1443+
// Versioned triple with unsupported deployment target - build should fail
1444+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1445+
await #expect(throws: Error.self) {
1446+
try await executeSwiftBuild(
1447+
path,
1448+
extraArgs: ["--triple", "\(hostArch)-apple-macosx14.0"],
1449+
buildSystem: buildSystem,
1450+
throwIfCommandFails: true
1451+
)
1452+
}
1453+
}
1454+
}
1455+
}
13991456
}
14001457

14011458
extension Triple {

0 commit comments

Comments
 (0)