Skip to content

Commit 95e4c4f

Browse files
authored
[Swift Build] Default to the package's declared deployment target if none is explicitly specified (#9139)
Default to an unversioned triple if the user hasn't provided one so we don't raise the deployment target to a version the package may not support closes #9095
1 parent 4540e78 commit 95e4c4f

File tree

11 files changed

+117
-9
lines changed

11 files changed

+117
-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/SPMBuildCore/Plugins/DefaultPluginScriptRunner.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,19 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
466466

467467
var env = Environment.current
468468

469+
// FIXME: This is largely a workaround for improper rpath setup on Linux. It should be
470+
// removed once the Swift Build backend switches to use swiftc as the linker driver
471+
// for targets with Swift sources. For now, limit the scope to non-macOS, so that
472+
// plugins do not inadvertently use the toolchain stdlib instead of the OS stdlib
473+
// when built with a Swift.org toolchain.
474+
#if !os(macOS)
469475
// Update the environment for any runtime library paths that tools compiled
470476
// for the command plugin might require after they have been built.
471477
let runtimeLibPaths = self.toolchain.runtimeLibraryPaths
472478
for libPath in runtimeLibPaths {
473479
env.appendPath(key: .libraryPath, value: libPath.pathString)
474480
}
481+
#endif
475482

476483
#if os(Windows)
477484
let pluginLibraryPath = self.toolchain.swiftPMLibrariesLocation.pluginLibraryPath.pathString

Sources/SwiftBuildSupport/PIFBuilder.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,19 @@ public final class PIFBuilder {
333333
buildCommands.append(contentsOf: result.buildCommands.map( { buildCommand in
334334
var newEnv: Environment = buildCommand.configuration.environment
335335

336+
// FIXME: This is largely a workaround for improper rpath setup on Linux. It should be
337+
// removed once the Swift Build backend switches to use swiftc as the linker driver
338+
// for targets with Swift sources. For now, limit the scope to non-macOS, so that
339+
// plugins do not inadvertently use the toolchain stdlib instead of the OS stdlib
340+
// when built with a Swift.org toolchain.
341+
#if !os(macOS)
336342
let runtimeLibPaths = buildParameters.toolchain.runtimeLibraryPaths
337343

338344
// Add paths to swift standard runtime libraries to the library path so that they can be found at runtime
339345
for libPath in runtimeLibPaths {
340346
newEnv.appendPath(key: .libraryPath, value: libPath.pathString)
341347
}
348+
#endif
342349

343350
// Append the system path at the end so that necessary system tool paths can be found
344351
if let pathValue = Environment.current[EnvironmentKey.path] {

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 {

0 commit comments

Comments
 (0)