diff --git a/Sources/Commands/Options.swift b/Sources/Commands/Options.swift index 77a82f7990c..c2b40c6e3df 100644 --- a/Sources/Commands/Options.swift +++ b/Sources/Commands/Options.swift @@ -50,6 +50,12 @@ public class ToolOptions { /// Path to the compilation destination describing JSON file. public var customCompileDestination: AbsolutePath? + /// The compilation destination’s target triple. + public var customCompileTriple: Triple? + /// Path to the compilation destination’s SDK. + public var customCompileSDK: AbsolutePath? + /// Path to the compilation destination’s toolchain. + public var customCompileToolchain: AbsolutePath? /// If should link the Swift stdlib statically. public var shouldLinkStaticSwiftStdlib = false diff --git a/Sources/Commands/SwiftTool.swift b/Sources/Commands/SwiftTool.swift index 4aeb1f3488b..d98ef5b6c2b 100644 --- a/Sources/Commands/SwiftTool.swift +++ b/Sources/Commands/SwiftTool.swift @@ -379,6 +379,15 @@ public class SwiftTool { binder.bind( option: parser.add(option: "--destination", kind: PathArgument.self), to: { $0.customCompileDestination = $1.path }) + binder.bind( + option: parser.add(option: "--triple", kind: String.self), + to: { $0.customCompileTriple = try Triple($1) }) + binder.bind( + option: parser.add(option: "--sdk", kind: PathArgument.self), + to: { $0.customCompileSDK = $1.path }) + binder.bind( + option: parser.add(option: "--toolchain", kind: PathArgument.self), + to: { $0.customCompileToolchain = $1.path }) // FIXME: We need to allow -vv type options for this. binder.bind( @@ -782,14 +791,31 @@ public class SwiftTool { /// Lazily compute the destination toolchain. private lazy var _destinationToolchain: Result = { - // Create custom toolchain if present. - if let customDestination = self.options.customCompileDestination { - return Result(catching: { - try UserToolchain(destination: Destination(fromFile: customDestination)) - }) + var destination: Destination + do { + // Create custom toolchain if present. + if let customDestination = self.options.customCompileDestination { + destination = try Destination(fromFile: customDestination) + } else { + // Otherwise use the host toolchain. + destination = try Destination.hostDestination( + originalWorkingDirectory: self.originalWorkingDirectory + ) + } + } catch { + return .failure(error) + } + // Apply any manual overrides. + if let triple = self.options.customCompileTriple { + destination.target = triple + } + if let binDir = self.options.customCompileToolchain { + destination.binDir = binDir.appending(components: "usr", "bin") + } + if let sdk = self.options.customCompileSDK { + destination.sdk = sdk } - // Otherwise use the host toolchain. - return self._hostToolchain + return Result(catching: { try UserToolchain(destination: destination) }) }() /// Lazily compute the host toolchain used to compile the package description. diff --git a/Sources/Workspace/Destination.swift b/Sources/Workspace/Destination.swift index e9020b8f472..86062c55288 100644 --- a/Sources/Workspace/Destination.swift +++ b/Sources/Workspace/Destination.swift @@ -35,13 +35,13 @@ public struct Destination: Encodable { /// - abi = eabi, gnu, android, macho, elf, etc. /// /// for more information see //https://clang.llvm.org/docs/CrossCompilation.html - public let target: Triple? + public var target: Triple? /// The SDK used to compile for the destination. - public let sdk: AbsolutePath + public var sdk: AbsolutePath /// The binDir in the containing the compilers/linker to be used for the compilation. - public let binDir: AbsolutePath + public var binDir: AbsolutePath /// Additional flags to be passed to the C compiler. public let extraCCFlags: [String]