Skip to content

Commit 915d67b

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

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
@@ -1385,6 +1385,63 @@ struct BuildCommandTestCases {
13851385
}
13861386
}
13871387
}
1388+
1389+
@Test(.requireHostOS(.macOS), arguments: SupportedBuildSystemOnPlatform)
1390+
func buildingPackageWhichRequiresOlderDeploymentTarget(buildSystem: BuildSystemProvider.Kind) async throws {
1391+
// This fixture specifies a deployment target of macOS 12, and uses API obsoleted in macOS 13. The goal
1392+
// of this test is to ensure that SwiftPM respects the deployment target specified in the package manifest
1393+
// when passed no triple of an unversioned triple, rather than using the latests deployment target.
1394+
1395+
// No triple - build should pass
1396+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1397+
try await executeSwiftBuild(
1398+
path,
1399+
buildSystem: buildSystem,
1400+
throwIfCommandFails: true
1401+
)
1402+
}
1403+
1404+
let hostArch: String
1405+
#if arch(arm64)
1406+
hostArch = "arm64"
1407+
#else
1408+
hostArch = "x86_64"
1409+
#endif
1410+
1411+
// Unversioned triple - build should pass
1412+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1413+
try await executeSwiftBuild(
1414+
path,
1415+
extraArgs: ["--triple", "\(hostArch)-apple-macosx"],
1416+
buildSystem: buildSystem,
1417+
throwIfCommandFails: true
1418+
)
1419+
}
1420+
1421+
// Versioned triple with supported deployment target - build should pass
1422+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1423+
try await executeSwiftBuild(
1424+
path,
1425+
extraArgs: ["--triple", "\(hostArch)-apple-macosx12.0"],
1426+
buildSystem: buildSystem,
1427+
throwIfCommandFails: true
1428+
)
1429+
}
1430+
1431+
if buildSystem == .swiftbuild {
1432+
// Versioned triple with unsupported deployment target - build should fail
1433+
try await fixture(name: "Miscellaneous/RequiresOlderDeploymentTarget") { path in
1434+
await #expect(throws: Error.self) {
1435+
try await executeSwiftBuild(
1436+
path,
1437+
extraArgs: ["--triple", "\(hostArch)-apple-macosx14.0"],
1438+
buildSystem: buildSystem,
1439+
throwIfCommandFails: true
1440+
)
1441+
}
1442+
}
1443+
}
1444+
}
13881445
}
13891446

13901447
extension Triple {

0 commit comments

Comments
 (0)