Skip to content

PackageModel: clean up naming of Destination properties #5871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -842,13 +842,13 @@ public class SwiftTool {
}
// Apply any manual overrides.
if let triple = self.options.build.customCompileTriple {
destination.target = triple
destination.destinationTriple = triple
}
if let binDir = self.options.build.customCompileToolchain {
destination.binDir = binDir.appending(components: "usr", "bin")
destination.toolchainBinDir = binDir.appending(components: "usr", "bin")
}
if let sdk = self.options.build.customCompileSDK {
destination.sdk = sdk
destination.sdkRootDir = sdk
}
destination.archs = options.build.archs

Expand Down
79 changes: 53 additions & 26 deletions Sources/PackageModel/Destination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension DestinationError: CustomStringConvertible {
/// The compilation destination, has information about everything that's required for a certain destination.
public struct Destination: Encodable, Equatable {

/// The clang/LLVM triple describing the target OS and architecture.
/// The clang/LLVM triple describing the destination's target OS and architecture.
///
/// The triple has the general format <arch><sub>-<vendor>-<sys>-<abi>, where:
/// - arch = x86_64, i386, arm, thumb, mips, etc.
Expand All @@ -48,16 +48,41 @@ public struct Destination: Encodable, Equatable {
/// - abi = eabi, gnu, android, macho, elf, etc.
///
/// for more information see //https://clang.llvm.org/docs/CrossCompilation.html
public var target: Triple?
public var destinationTriple: Triple?

/// The clang/LLVM triple describing the host platform that supports this destination.
public let hostTriple: Triple?

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

/// The SDK used to compile for the destination.
public var sdk: AbsolutePath?
/// Root directory path of the SDK used to compile for the destination.
@available(*, deprecated, message: "use `sdkRootDir` instead")
public var sdk: AbsolutePath? {
get {
sdkRootDir
}
set {
sdkRootDir = newValue
}
}

/// Root directory path of the SDK used to compile for the destination.
public var sdkRootDir: AbsolutePath?

/// Path to a directory containing the toolchain (compilers/linker) to be used for the compilation.
@available(*, deprecated, message: "use `toolchainBinDir` instead")
public var binDir: AbsolutePath {
get {
toolchainBinDir
}
set {
toolchainBinDir = newValue
}
}

/// The binDir in the containing the compilers/linker to be used for the compilation.
public var binDir: AbsolutePath
/// Path to a directory containing the toolchain (compilers/linker) to be used for the compilation.
public var toolchainBinDir: AbsolutePath

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

/// Creates a compilation destination with the specified properties.
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraFlags)` instead")
@available(*, deprecated, message: "use `init(destinationTriple:sdkRootDir:toolchainBinDir:extraFlags)` instead")
public init(
target: Triple? = nil,
sdk: AbsolutePath?,
Expand All @@ -90,9 +115,10 @@ public struct Destination: Encodable, Equatable {
extraSwiftCFlags: [String] = [],
extraCPPFlags: [String]
) {
self.target = target
self.sdk = sdk
self.binDir = binDir
self.hostTriple = nil
self.destinationTriple = target
self.sdkRootDir = sdk
self.toolchainBinDir = binDir
self.extraFlags = BuildFlags(
cCompilerFlags: extraCCFlags,
cxxCompilerFlags: extraCPPFlags,
Expand All @@ -102,14 +128,16 @@ public struct Destination: Encodable, Equatable {

/// Creates a compilation destination with the specified properties.
public init(
target: Triple? = nil,
sdk: AbsolutePath?,
binDir: AbsolutePath,
hostTriple: Triple? = nil,
destinationTriple: Triple? = nil,
sdkRootDir: AbsolutePath?,
toolchainBinDir: AbsolutePath,
extraFlags: BuildFlags = BuildFlags()
) {
self.target = target
self.sdk = sdk
self.binDir = binDir
self.hostTriple = hostTriple
self.destinationTriple = destinationTriple
self.sdkRootDir = sdkRootDir
self.toolchainBinDir = toolchainBinDir
self.extraFlags = extraFlags
}

Expand Down Expand Up @@ -180,9 +208,8 @@ public struct Destination: Encodable, Equatable {
#endif

return Destination(
target: nil,
sdk: sdkPath,
binDir: binDir,
sdkRootDir: sdkPath,
toolchainBinDir: binDir,
extraFlags: BuildFlags(cCompilerFlags: extraCCFlags, swiftCompilerFlags: extraSwiftCFlags)
)
}
Expand Down Expand Up @@ -217,13 +244,13 @@ public struct Destination: Encodable, Equatable {
/// Returns a default destination of a given target environment
public static func defaultDestination(for triple: Triple, host: Destination) -> Destination? {
if triple.isWASI() {
let wasiSysroot = host.binDir
let wasiSysroot = host.toolchainBinDir
.parentDirectory // usr
.appending(components: "share", "wasi-sysroot")
return Destination(
target: triple,
sdk: wasiSysroot,
binDir: host.binDir
destinationTriple: triple,
sdkRootDir: wasiSysroot,
toolchainBinDir: host.toolchainBinDir
)
}
return nil
Expand All @@ -241,9 +268,9 @@ extension Destination {
}
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfo.self)
try self.init(
target: destination.target.map{ try Triple($0) },
sdk: destination.sdk,
binDir: destination.binDir,
destinationTriple: destination.target.map{ try Triple($0) },
sdkRootDir: destination.sdk,
toolchainBinDir: destination.binDir,
extraFlags: BuildFlags(
cCompilerFlags: destination.extraCCFlags,
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
Expand Down
16 changes: 8 additions & 8 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public final class UserToolchain: Toolchain {

// Then, check the toolchain.
do {
if let toolPath = try? UserToolchain.getTool("clang", binDir: self.destination.binDir) {
if let toolPath = try? UserToolchain.getTool("clang", binDir: self.destination.toolchainBinDir) {
self._clangCompiler = toolPath
return toolPath
}
Expand Down Expand Up @@ -258,7 +258,7 @@ public final class UserToolchain: Toolchain {
}

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

// Get the binDir from destination.
let binDir = destination.binDir
let binDir = destination.toolchainBinDir

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

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

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

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

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

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

let applicationPath = destination.binDir
let applicationPath = destination.toolchainBinDir

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

if let sdk = destination.sdk {
if let sdk = destination.sdkRootDir {
sdkroot = sdk
} else if let SDKROOT = environment["SDKROOT"], let sdk = try? AbsolutePath(validating: SDKROOT) {
sdkroot = sdk
Expand Down
6 changes: 3 additions & 3 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3372,8 +3372,8 @@ final class BuildPlanTests: XCTestCase {
XCTAssertNoDiagnostics(observability.diagnostics)

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

let exe = try result.target(for: "exe").swiftTarget().compileArguments()
Expand Down
10 changes: 5 additions & 5 deletions Tests/PackageModelTests/PackageModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ class PackageModelTests: XCTestCase {
}

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

let destination = Destination(
target: target,
sdk: sdk,
binDir: toolchainPath.appending(components: "usr", "bin")
destinationTriple: triple,
sdkRootDir: sdk,
toolchainBinDir: toolchainPath.appending(components: "usr", "bin")
)

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