Skip to content

Commit 83c3282

Browse files
authored
Merge pull request #488 from nkcsgexi/refactor-executable-path
Allow libSwiftDriver client passing down a compiler executable directory
2 parents 00fada4 + 130542c commit 83c3282

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ public struct Driver {
353353
fileSystem: FileSystem = localFileSystem,
354354
executor: DriverExecutor,
355355
integratedDriver: Bool = true,
356+
compilerExecutableDir: AbsolutePath? = nil,
356357
// FIXME: Duplication with externalBuildArtifacts and externalTargetModulePathMap
357358
// is a temporary backwards-compatibility shim to help transition SwiftPM to the new API
358359
externalBuildArtifacts: ExternalBuildArtifacts? = nil,
@@ -415,7 +416,8 @@ public struct Driver {
415416
&self.parsedOptions, diagnosticsEngine: diagnosticEngine,
416417
compilerMode: self.compilerMode, env: env,
417418
executor: self.executor, fileSystem: fileSystem,
418-
useStaticResourceDir: self.useStaticResourceDir)
419+
useStaticResourceDir: self.useStaticResourceDir,
420+
compilerExecutableDir: compilerExecutableDir)
419421

420422
// Compute the host machine's triple
421423
self.hostTriple =
@@ -2180,7 +2182,8 @@ extension Driver {
21802182
env: [String: String],
21812183
executor: DriverExecutor,
21822184
fileSystem: FileSystem,
2183-
useStaticResourceDir: Bool
2185+
useStaticResourceDir: Bool,
2186+
compilerExecutableDir: AbsolutePath?
21842187
) throws -> (Toolchain, FrontendTargetInfo, [String]) {
21852188
let explicitTarget = (parsedOptions.getLastArgument(.target)?.asSingle)
21862189
.map {
@@ -2208,6 +2211,7 @@ extension Driver {
22082211
}
22092212
let toolchain = toolchainType.init(env: env, executor: executor,
22102213
fileSystem: fileSystem,
2214+
compilerExecutableDir: compilerExecutableDir,
22112215
toolDirectory: toolDir)
22122216

22132217
let frontendOverride = try FrontendOverride(&parsedOptions, diagnosticsEngine)

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@ import SwiftOptions
2929
/// The file system to use for any file operations.
3030
public let fileSystem: FileSystem
3131

32+
// An externally provided path from where we should find compiler
33+
public let compilerExecutableDir: AbsolutePath?
34+
3235
// An externally provided path from where we should find tools like ld
3336
public let toolDirectory: AbsolutePath?
3437

3538
public let dummyForTestingObjectFormat = Triple.ObjectFormat.macho
3639

37-
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem, toolDirectory: AbsolutePath? = nil) {
40+
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem,
41+
compilerExecutableDir: AbsolutePath? = nil, toolDirectory: AbsolutePath? = nil) {
3842
self.env = env
3943
self.executor = executor
4044
self.fileSystem = fileSystem
45+
self.compilerExecutableDir = compilerExecutableDir
4146
self.toolDirectory = toolDirectory
4247
}
4348

Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ import TSCBasic
2424
/// Doubles as path cache and point for overriding normal lookup
2525
private var toolPaths = [Tool: AbsolutePath]()
2626

27+
// An externally provided path from where we should find compiler
28+
public let compilerExecutableDir: AbsolutePath?
29+
2730
public let toolDirectory: AbsolutePath?
2831

2932
public let dummyForTestingObjectFormat = Triple.ObjectFormat.elf
3033

31-
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem, toolDirectory: AbsolutePath? = nil) {
34+
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem, compilerExecutableDir: AbsolutePath? = nil, toolDirectory: AbsolutePath? = nil) {
3235
self.env = env
3336
self.executor = executor
3437
self.fileSystem = fileSystem
38+
self.compilerExecutableDir = compilerExecutableDir
3539
self.toolDirectory = toolDirectory
3640
}
3741

Sources/SwiftDriver/Toolchains/Toolchain.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public enum Tool: Hashable {
2828
/// Describes a toolchain, which includes information about compilers, linkers
2929
/// and other tools required to build Swift code.
3030
@_spi(Testing) public protocol Toolchain {
31-
init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem, toolDirectory: AbsolutePath?)
31+
init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem, compilerExecutableDir: AbsolutePath?, toolDirectory: AbsolutePath?)
3232

3333
var env: [String: String] { get }
3434

@@ -38,6 +38,9 @@ public enum Tool: Hashable {
3838

3939
var executor: DriverExecutor { get }
4040

41+
/// Where we should find compiler executables, e.g. XcodeDefault.xctoolchain/usr/bin
42+
var compilerExecutableDir: AbsolutePath? { get }
43+
4144
var toolDirectory: AbsolutePath? { get }
4245

4346
/// Retrieve the absolute path to a particular tool.
@@ -104,6 +107,12 @@ extension Toolchain {
104107

105108
/// Returns the `executablePath`'s directory.
106109
public var executableDir: AbsolutePath {
110+
// If the path is given via the initializer, use that.
111+
if let givenDir = compilerExecutableDir {
112+
return givenDir
113+
}
114+
// If the path isn't given, we are running the driver as an executable,
115+
// so assuming the compiler is adjacent to the driver.
107116
guard let path = Bundle.main.executablePath else {
108117
fatalError("Could not find executable path.")
109118
}

Sources/SwiftDriver/Toolchains/WebAssemblyToolchain.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ import SwiftOptions
4545
/// Doubles as path cache and point for overriding normal lookup
4646
private var toolPaths = [Tool: AbsolutePath]()
4747

48+
public let compilerExecutableDir: AbsolutePath?
49+
4850
public let toolDirectory: AbsolutePath?
4951

5052
public let dummyForTestingObjectFormat = Triple.ObjectFormat.wasm
5153

52-
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem, toolDirectory: AbsolutePath? = nil) {
54+
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem, compilerExecutableDir: AbsolutePath? = nil, toolDirectory: AbsolutePath? = nil) {
5355
self.env = env
5456
self.executor = executor
5557
self.fileSystem = fileSystem
58+
self.compilerExecutableDir = compilerExecutableDir
5659
self.toolDirectory = toolDirectory
5760
}
5861

0 commit comments

Comments
 (0)