Skip to content

Commit 70e9a6c

Browse files
committed
[Swift Build] Default to the package's declared deployment target if none is explicitly specified
1 parent dd50d0d commit 70e9a6c

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
@@ -1440,6 +1440,63 @@ struct BuildCommandTestCases {
14401440
}
14411441
}
14421442
}
1443+
1444+
@Test(.requireHostOS(.macOS), arguments: SupportedBuildSystemOnPlatform)
1445+
func buildingPackageWhichRequiresOlderDeploymentTarget(buildSystem: BuildSystemProvider.Kind) async throws {
1446+
// This fixture specifies a deployment target of macOS 12, and uses API obsoleted in macOS 13. The goal
1447+
// of this test is to ensure that SwiftPM respects the deployment target specified in the package manifest
1448+
// when passed no triple of an unversioned triple, rather than using the latests deployment target.
1449+
1450+
// No triple - build should pass
1451+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1452+
try await executeSwiftBuild(
1453+
path,
1454+
buildSystem: buildSystem,
1455+
throwIfCommandFails: true
1456+
)
1457+
}
1458+
1459+
let hostArch: String
1460+
#if arch(arm64)
1461+
hostArch = "arm64"
1462+
#else
1463+
hostArch = "x86_64"
1464+
#endif
1465+
1466+
// Unversioned triple - build should pass
1467+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1468+
try await executeSwiftBuild(
1469+
path,
1470+
extraArgs: ["--triple", "\(hostArch)-apple-macosx"],
1471+
buildSystem: buildSystem,
1472+
throwIfCommandFails: true
1473+
)
1474+
}
1475+
1476+
// Versioned triple with supported deployment target - build should pass
1477+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1478+
try await executeSwiftBuild(
1479+
path,
1480+
extraArgs: ["--triple", "\(hostArch)-apple-macosx12.0"],
1481+
buildSystem: buildSystem,
1482+
throwIfCommandFails: true
1483+
)
1484+
}
1485+
1486+
if buildSystem == .swiftbuild {
1487+
// Versioned triple with unsupported deployment target - build should fail
1488+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1489+
await #expect(throws: Error.self) {
1490+
try await executeSwiftBuild(
1491+
path,
1492+
extraArgs: ["--triple", "\(hostArch)-apple-macosx14.0"],
1493+
buildSystem: buildSystem,
1494+
throwIfCommandFails: true
1495+
)
1496+
}
1497+
}
1498+
}
1499+
}
14431500
}
14441501

14451502
extension Triple {

0 commit comments

Comments
 (0)