Skip to content

Commit ee59dfb

Browse files
authored
PackageModel: clean up naming of Destination properties (#5871)
Introduced `sdkRootDir` replacing `sdk`, `toolchainBinDir` replacing `binDir`, and `destinationTriple` replacing `target` old names are marked as deprecated. Also added the new `hostTriple` property.
1 parent d53f013 commit ee59dfb

File tree

5 files changed

+72
-45
lines changed

5 files changed

+72
-45
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,13 +842,13 @@ public class SwiftTool {
842842
}
843843
// Apply any manual overrides.
844844
if let triple = self.options.build.customCompileTriple {
845-
destination.target = triple
845+
destination.destinationTriple = triple
846846
}
847847
if let binDir = self.options.build.customCompileToolchain {
848-
destination.binDir = binDir.appending(components: "usr", "bin")
848+
destination.toolchainBinDir = binDir.appending(components: "usr", "bin")
849849
}
850850
if let sdk = self.options.build.customCompileSDK {
851-
destination.sdk = sdk
851+
destination.sdkRootDir = sdk
852852
}
853853
destination.archs = options.build.archs
854854

Sources/PackageModel/Destination.swift

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension DestinationError: CustomStringConvertible {
3838
/// The compilation destination, has information about everything that's required for a certain destination.
3939
public struct Destination: Encodable, Equatable {
4040

41-
/// The clang/LLVM triple describing the target OS and architecture.
41+
/// The clang/LLVM triple describing the destination's target OS and architecture.
4242
///
4343
/// The triple has the general format <arch><sub>-<vendor>-<sys>-<abi>, where:
4444
/// - arch = x86_64, i386, arm, thumb, mips, etc.
@@ -48,16 +48,41 @@ public struct Destination: Encodable, Equatable {
4848
/// - abi = eabi, gnu, android, macho, elf, etc.
4949
///
5050
/// for more information see //https://clang.llvm.org/docs/CrossCompilation.html
51-
public var target: Triple?
51+
public var destinationTriple: Triple?
52+
53+
/// The clang/LLVM triple describing the host platform that supports this destination.
54+
public let hostTriple: Triple?
5255

5356
/// The architectures to build for. We build for host architecture if this is empty.
5457
public var archs: [String] = []
5558

56-
/// The SDK used to compile for the destination.
57-
public var sdk: AbsolutePath?
59+
/// Root directory path of the SDK used to compile for the destination.
60+
@available(*, deprecated, message: "use `sdkRootDir` instead")
61+
public var sdk: AbsolutePath? {
62+
get {
63+
sdkRootDir
64+
}
65+
set {
66+
sdkRootDir = newValue
67+
}
68+
}
69+
70+
/// Root directory path of the SDK used to compile for the destination.
71+
public var sdkRootDir: AbsolutePath?
72+
73+
/// Path to a directory containing the toolchain (compilers/linker) to be used for the compilation.
74+
@available(*, deprecated, message: "use `toolchainBinDir` instead")
75+
public var binDir: AbsolutePath {
76+
get {
77+
toolchainBinDir
78+
}
79+
set {
80+
toolchainBinDir = newValue
81+
}
82+
}
5883

59-
/// The binDir in the containing the compilers/linker to be used for the compilation.
60-
public var binDir: AbsolutePath
84+
/// Path to a directory containing the toolchain (compilers/linker) to be used for the compilation.
85+
public var toolchainBinDir: AbsolutePath
6186

6287
/// Additional flags to be passed to the C compiler.
6388
@available(*, deprecated, message: "use `extraFlags.cCompilerFlags` instead")
@@ -81,7 +106,7 @@ public struct Destination: Encodable, Equatable {
81106
public let extraFlags: BuildFlags
82107

83108
/// Creates a compilation destination with the specified properties.
84-
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraFlags)` instead")
109+
@available(*, deprecated, message: "use `init(destinationTriple:sdkRootDir:toolchainBinDir:extraFlags)` instead")
85110
public init(
86111
target: Triple? = nil,
87112
sdk: AbsolutePath?,
@@ -90,9 +115,10 @@ public struct Destination: Encodable, Equatable {
90115
extraSwiftCFlags: [String] = [],
91116
extraCPPFlags: [String]
92117
) {
93-
self.target = target
94-
self.sdk = sdk
95-
self.binDir = binDir
118+
self.hostTriple = nil
119+
self.destinationTriple = target
120+
self.sdkRootDir = sdk
121+
self.toolchainBinDir = binDir
96122
self.extraFlags = BuildFlags(
97123
cCompilerFlags: extraCCFlags,
98124
cxxCompilerFlags: extraCPPFlags,
@@ -102,14 +128,16 @@ public struct Destination: Encodable, Equatable {
102128

103129
/// Creates a compilation destination with the specified properties.
104130
public init(
105-
target: Triple? = nil,
106-
sdk: AbsolutePath?,
107-
binDir: AbsolutePath,
131+
hostTriple: Triple? = nil,
132+
destinationTriple: Triple? = nil,
133+
sdkRootDir: AbsolutePath?,
134+
toolchainBinDir: AbsolutePath,
108135
extraFlags: BuildFlags = BuildFlags()
109136
) {
110-
self.target = target
111-
self.sdk = sdk
112-
self.binDir = binDir
137+
self.hostTriple = hostTriple
138+
self.destinationTriple = destinationTriple
139+
self.sdkRootDir = sdkRootDir
140+
self.toolchainBinDir = toolchainBinDir
113141
self.extraFlags = extraFlags
114142
}
115143

@@ -180,9 +208,8 @@ public struct Destination: Encodable, Equatable {
180208
#endif
181209

182210
return Destination(
183-
target: nil,
184-
sdk: sdkPath,
185-
binDir: binDir,
211+
sdkRootDir: sdkPath,
212+
toolchainBinDir: binDir,
186213
extraFlags: BuildFlags(cCompilerFlags: extraCCFlags, swiftCompilerFlags: extraSwiftCFlags)
187214
)
188215
}
@@ -217,13 +244,13 @@ public struct Destination: Encodable, Equatable {
217244
/// Returns a default destination of a given target environment
218245
public static func defaultDestination(for triple: Triple, host: Destination) -> Destination? {
219246
if triple.isWASI() {
220-
let wasiSysroot = host.binDir
247+
let wasiSysroot = host.toolchainBinDir
221248
.parentDirectory // usr
222249
.appending(components: "share", "wasi-sysroot")
223250
return Destination(
224-
target: triple,
225-
sdk: wasiSysroot,
226-
binDir: host.binDir
251+
destinationTriple: triple,
252+
sdkRootDir: wasiSysroot,
253+
toolchainBinDir: host.toolchainBinDir
227254
)
228255
}
229256
return nil
@@ -241,9 +268,9 @@ extension Destination {
241268
}
242269
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfo.self)
243270
try self.init(
244-
target: destination.target.map{ try Triple($0) },
245-
sdk: destination.sdk,
246-
binDir: destination.binDir,
271+
destinationTriple: destination.target.map{ try Triple($0) },
272+
sdkRootDir: destination.sdk,
273+
toolchainBinDir: destination.binDir,
247274
extraFlags: BuildFlags(
248275
cCompilerFlags: destination.extraCCFlags,
249276
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.

Sources/PackageModel/UserToolchain.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public final class UserToolchain: Toolchain {
201201

202202
// Then, check the toolchain.
203203
do {
204-
if let toolPath = try? UserToolchain.getTool("clang", binDir: self.destination.binDir) {
204+
if let toolPath = try? UserToolchain.getTool("clang", binDir: self.destination.toolchainBinDir) {
205205
self._clangCompiler = toolPath
206206
return toolPath
207207
}
@@ -258,7 +258,7 @@ public final class UserToolchain: Toolchain {
258258
}
259259

260260
internal static func deriveSwiftCFlags(triple: Triple, destination: Destination, environment: EnvironmentVariables) throws -> [String] {
261-
guard let sdk = destination.sdk else {
261+
guard let sdk = destination.sdkRootDir else {
262262
if triple.isWindows() {
263263
// Windows uses a variable named SDKROOT to determine the root of
264264
// the SDK. This is not the same value as the SDKROOT parameter
@@ -366,14 +366,14 @@ public final class UserToolchain: Toolchain {
366366
}
367367

368368
// Get the binDir from destination.
369-
let binDir = destination.binDir
369+
let binDir = destination.toolchainBinDir
370370

371371
let swiftCompilers = try UserToolchain.determineSwiftCompilers(binDir: binDir, useXcrun: useXcrun, environment: environment, searchPaths: envSearchPaths)
372372
self.swiftCompilerPath = swiftCompilers.compile
373373
self.archs = destination.archs
374374

375375
// Use the triple from destination or compute the host triple using swiftc.
376-
var triple = destination.target ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
376+
var triple = destination.destinationTriple ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
377377

378378
self.librarianPath = try UserToolchain.determineLibrarian(triple: triple, binDir: binDir, useXcrun: useXcrun, environment: environment, searchPaths: envSearchPaths)
379379

@@ -389,7 +389,7 @@ public final class UserToolchain: Toolchain {
389389

390390
self.extraFlags.swiftCompilerFlags = try Self.deriveSwiftCFlags(triple: triple, destination: destination, environment: environment)
391391

392-
if let sdk = destination.sdk {
392+
if let sdk = destination.sdkRootDir {
393393
self.extraFlags.cCompilerFlags = [
394394
triple.isDarwin() ? "-isysroot" : "--sysroot", sdk.pathString
395395
] + destination.extraFlags.cCompilerFlags
@@ -444,7 +444,7 @@ public final class UserToolchain: Toolchain {
444444
swiftCompilerFlags: self.extraFlags.swiftCompilerFlags,
445445
swiftCompilerEnvironment: environment,
446446
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
447-
sdkRootPath: self.destination.sdk,
447+
sdkRootPath: self.destination.sdkRootDir,
448448
xctestPath: xctestPath
449449
)
450450
}
@@ -482,7 +482,7 @@ public final class UserToolchain: Toolchain {
482482
// an alternative cloud be to force explicit locations to always be set explicitly when running in XCode/SwiftPM
483483
// debug and assert if not set but we detect that we are in this mode
484484

485-
let applicationPath = destination.binDir
485+
let applicationPath = destination.toolchainBinDir
486486

487487
// this is the normal case when using the toolchain
488488
let librariesPath = applicationPath.parentDirectory.appending(components: "lib", "swift", "pm")
@@ -523,7 +523,7 @@ public final class UserToolchain: Toolchain {
523523
} else if triple.isWindows() {
524524
let sdkroot: AbsolutePath
525525

526-
if let sdk = destination.sdk {
526+
if let sdk = destination.sdkRootDir {
527527
sdkroot = sdk
528528
} else if let SDKROOT = environment["SDKROOT"], let sdk = try? AbsolutePath(validating: SDKROOT) {
529529
sdkroot = sdk

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,8 +3372,8 @@ final class BuildPlanTests: XCTestCase {
33723372
XCTAssertNoDiagnostics(observability.diagnostics)
33733373

33743374
let userDestination = Destination(
3375-
sdk: AbsolutePath(path: "/fake/sdk"),
3376-
binDir: try UserToolchain.default.destination.binDir,
3375+
sdkRootDir: AbsolutePath(path: "/fake/sdk"),
3376+
toolchainBinDir: try UserToolchain.default.destination.toolchainBinDir,
33773377
extraFlags: BuildFlags(
33783378
cCompilerFlags: ["-I/fake/sdk/sysroot", "-clang-flag-from-json"],
33793379
swiftCompilerFlags: ["-swift-flag-from-json"]
@@ -3400,7 +3400,7 @@ final class BuildPlanTests: XCTestCase {
34003400
#else
34013401
args += ["--sysroot"]
34023402
#endif
3403-
args += ["\(userDestination.sdk!)", "-I/fake/sdk/sysroot", "-clang-flag-from-json", .anySequence, "-clang-command-line-flag"]
3403+
args += ["\(userDestination.sdkRootDir!)", "-I/fake/sdk/sysroot", "-clang-flag-from-json", .anySequence, "-clang-command-line-flag"]
34043404
XCTAssertMatch(try lib.basicArguments(isCXX: false), args)
34053405

34063406
let exe = try result.target(for: "exe").swiftTarget().compileArguments()

Tests/PackageModelTests/PackageModelTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ class PackageModelTests: XCTestCase {
5656
}
5757

5858
func testAndroidCompilerFlags() throws {
59-
let target = try Triple("x86_64-unknown-linux-android")
59+
let triple = try Triple("x86_64-unknown-linux-android")
6060
let sdk = AbsolutePath(path: "/some/path/to/an/SDK.sdk")
6161
let toolchainPath = AbsolutePath(path: "/some/path/to/a/toolchain.xctoolchain")
6262

6363
let destination = Destination(
64-
target: target,
65-
sdk: sdk,
66-
binDir: toolchainPath.appending(components: "usr", "bin")
64+
destinationTriple: triple,
65+
sdkRootDir: sdk,
66+
toolchainBinDir: toolchainPath.appending(components: "usr", "bin")
6767
)
6868

69-
XCTAssertEqual(try UserToolchain.deriveSwiftCFlags(triple: target, destination: destination, environment: .process()), [
69+
XCTAssertEqual(try UserToolchain.deriveSwiftCFlags(triple: triple, destination: destination, environment: .process()), [
7070
// Needed when cross‐compiling for Android. 2020‐03‐01
7171
"-sdk", sdk.pathString,
7272
])

0 commit comments

Comments
 (0)