Skip to content

Commit 9aa1213

Browse files
committed
Derive executable file name from Toolchain
1 parent 2c9716b commit 9aa1213

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ import SwiftOptions
2020
public final class DarwinToolchain: Toolchain {
2121
public let env: [String: String]
2222

23-
/// Doubles as path cache and point for overriding normal lookup
24-
private var toolPaths = [Tool: AbsolutePath]()
25-
2623
/// The executor used to run processes used to find tools and retrieve target info.
2724
public let executor: DriverExecutor
2825

2926
/// The file system to use for any file operations.
3027
public let fileSystem: FileSystem
3128

29+
/// The suffix of executable files.
30+
public let executableSuffix = ""
31+
32+
/// Doubles as path cache and point for overriding normal lookup
33+
private var toolPaths = [Tool: AbsolutePath]()
34+
3235
public init(env: [String: String], executor: DriverExecutor, fileSystem: FileSystem = localFileSystem) {
3336
self.env = env
3437
self.executor = executor

Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public final class GenericUnixToolchain: Toolchain {
2121
/// The file system to use for queries.
2222
public let fileSystem: FileSystem
2323

24+
/// The suffix of executable files.
25+
public let executableSuffix = ""
26+
2427
/// Doubles as path cache and point for overriding normal lookup
2528
private var toolPaths = [Tool: AbsolutePath]()
2629

Sources/SwiftDriver/Toolchains/Toolchain.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public protocol Toolchain {
3838

3939
var executor: DriverExecutor { get }
4040

41+
var executableSuffix: String { get }
42+
4143
/// Retrieve the absolute path to a particular tool.
4244
func getToolPath(_ tool: Tool) throws -> AbsolutePath
4345

@@ -129,26 +131,25 @@ extension Toolchain {
129131
/// looks in the `executableDir`, `xcrunFind` or in the `searchPaths`.
130132
/// - Parameter executable: executable to look for [i.e. `swift`].
131133
func lookup(executable: String) throws -> AbsolutePath {
134+
let filename = executable + executableSuffix
132135
if let overrideString = envVar(forExecutable: executable) {
133136
return try AbsolutePath(validating: overrideString)
134-
} else if let path = lookupExecutablePath(filename: executable, searchPaths: [executableDir]) {
137+
} else if let path = lookupExecutablePath(filename: filename, searchPaths: [executableDir]) {
135138
return path
136139
} else if let path = try? xcrunFind(executable: executable) {
137140
return path
138-
} else if !["swift-frontend", "swift", "swift-frontend.exe", "swift.exe"].contains(executable),
139-
let parentDirectory = try? getToolPath(.swiftCompiler).parentDirectory,
140-
parentDirectory != executableDir,
141-
let path = lookupExecutablePath(filename: executable, searchPaths: [parentDirectory]) {
141+
} else if !["swift-frontend", "swift"].contains(executable),
142+
let parentDirectory = try? getToolPath(.swiftCompiler).parentDirectory,
143+
parentDirectory != executableDir,
144+
let path = lookupExecutablePath(filename: filename, searchPaths: [parentDirectory]) {
142145
// If the driver library's client and the frontend are in different directories,
143146
// try looking for tools next to the frontend.
144147
return path
145-
} else if let path = lookupExecutablePath(filename: executable, searchPaths: searchPaths) {
148+
} else if let path = lookupExecutablePath(filename: filename, searchPaths: searchPaths) {
146149
return path
147-
// Temporary shim: fall back to looking for "swift" before failing.
148150
} else if executable == "swift-frontend" {
151+
// Temporary shim: fall back to looking for "swift" before failing.
149152
return try lookup(executable: "swift")
150-
} else if executable == "swift-frontend.exe" {
151-
return try lookup(executable: "swift.exe")
152153
} else if fallbackToExecutableDefaultPath {
153154
return AbsolutePath("/usr/bin/" + executable)
154155
} else {

Sources/SwiftDriver/Toolchains/WindowsToolchain.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public final class WindowsToolchain: Toolchain {
2121
/// The file system to use for queries.
2222
public let fileSystem: FileSystem
2323

24+
/// The suffix of executable files.
25+
public let executableSuffix = ".exe"
26+
2427
/// Doubles as path cache and point for overriding normal lookup
2528
private var toolPaths = [Tool: AbsolutePath]()
2629

@@ -53,24 +56,24 @@ public final class WindowsToolchain: Toolchain {
5356
private func lookupToolPath(_ tool: Tool) throws -> AbsolutePath {
5457
switch tool {
5558
case .swiftCompiler:
56-
return try lookup(executable: "swift-frontend.exe")
59+
return try lookup(executable: "swift-frontend")
5760
case .staticLinker:
58-
return try lookup(executable: "lib.exe")
61+
return try lookup(executable: "lib")
5962
case .dynamicLinker:
6063
// FIXME: This needs to look in the tools_directory first.
61-
return try lookup(executable: "link.exe")
64+
return try lookup(executable: "link")
6265
case .clang:
63-
return try lookup(executable: "clang.exe")
66+
return try lookup(executable: "clang")
6467
case .swiftAutolinkExtract:
6568
fatalError("Trying to look up \"swift-autolink-extract\" on Windows")
6669
case .dsymutil:
6770
fatalError("Trying to look up \"dsymutil\" on Windows")
6871
case .lldb:
69-
return try lookup(executable: "lldb.exe")
72+
return try lookup(executable: "lldb")
7073
case .dwarfdump:
71-
return try lookup(executable: "llvm-dwarfdump.exe")
74+
return try lookup(executable: "llvm-dwarfdump")
7275
case .swiftHelp:
73-
return try lookup(executable: "swift-help.exe")
76+
return try lookup(executable: "swift-help")
7477
}
7578
}
7679

0 commit comments

Comments
 (0)