Skip to content

Commit cd2eb1b

Browse files
authored
Merge pull request #25 from JhonnyBillM/where-are-the-tools
[Toolchains] - look for tools relative to the executable path.
2 parents 521275f + 5c1b876 commit cd2eb1b

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public final class DarwinToolchain: Toolchain {
2525
public func getToolPath(_ tool: Tool) throws -> AbsolutePath {
2626
switch tool {
2727
case .swiftCompiler:
28-
return try lookup(exec: "swift")
28+
return try lookup(executable: "swift")
2929

3030
case .dynamicLinker:
31-
return try lookup(exec: "ld")
31+
return try lookup(executable: "ld")
3232

3333
case .staticLinker:
34-
return try lookup(exec: "libtool")
34+
return try lookup(executable: "libtool")
3535

3636
case .dsymutil:
37-
return try lookup(exec: "dsymutil")
37+
return try lookup(executable: "dsymutil")
3838

3939
case .clang:
4040
let result = try Process.checkNonZeroExit(
@@ -43,13 +43,13 @@ public final class DarwinToolchain: Toolchain {
4343
).spm_chomp()
4444
return AbsolutePath(result)
4545
case .swiftAutolinkExtract:
46-
return try lookup(exec: "swift-autolink-extract")
46+
return try lookup(executable: "swift-autolink-extract")
4747
}
4848
}
4949

5050
/// Swift compiler path.
5151
public lazy var swiftCompiler: Result<AbsolutePath, Swift.Error> = Result {
52-
try lookup(exec: "swift")
52+
try lookup(executable: "swift")
5353
}
5454

5555
/// SDK path.

Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public final class GenericUnixToolchain: Toolchain {
3030
public func getToolPath(_ tool: Tool) throws -> AbsolutePath {
3131
switch tool {
3232
case .swiftCompiler:
33-
return try lookup(exec: "swift")
33+
return try lookup(executable: "swift")
3434
case .staticLinker:
35-
return try lookup(exec: "ar")
35+
return try lookup(executable: "ar")
3636
case .dynamicLinker:
3737
// FIXME: This needs to look in the tools_directory first.
38-
return try lookup(exec: "clang")
38+
return try lookup(executable: "clang")
3939
case .clang:
40-
return try lookup(exec: "clang")
40+
return try lookup(executable: "clang")
4141
case .swiftAutolinkExtract:
42-
return try lookup(exec: "swift-autolink-extract")
42+
return try lookup(executable: "swift-autolink-extract")
4343
case .dsymutil:
44-
return try lookup(exec: "dsymutil")
44+
return try lookup(executable: "dsymutil")
4545
}
4646
}
4747

Sources/SwiftDriver/Toolchains/Toolchain.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,34 +107,35 @@ extension Toolchain {
107107
return "SWIFT_DRIVER_\(toolName.uppercased())_EXEC"
108108
}
109109

110-
/// Looks for the executable in the `SWIFT_DRIVER_TOOLNAME_EXEC` enviroment variable, if found nothing,
110+
/// Looks for the executable in the `SWIFT_DRIVER_TOOLNAME_EXEC` environment variable, if found nothing,
111111
/// looks in the `executableDir`, `xcrunFind` or in the `searchPaths`.
112-
/// - Parameter exec: executable to look for [i.e. `swift`].
113-
func lookup(exec: String) throws -> AbsolutePath {
114-
if let overrideString = envVar(forExecutable: exec) {
112+
/// - Parameter executable: executable to look for [i.e. `swift`].
113+
func lookup(executable: String) throws -> AbsolutePath {
114+
if let overrideString = envVar(forExecutable: executable) {
115115
return try AbsolutePath(validating: overrideString)
116-
} else if let path = lookupExecutablePath(filename: exec, searchPaths: [executableDir]) {
116+
} else if let path = lookupExecutablePath(filename: executable, searchPaths: [executableDir]) {
117117
return path
118-
} else if let path = try? xcrunFind(exec: exec) {
118+
} else if let path = try? xcrunFind(executable: executable) {
119119
return path
120-
} else if let path = lookupExecutablePath(filename: exec, searchPaths: searchPaths) {
120+
} else if let path = lookupExecutablePath(filename: executable, searchPaths: searchPaths) {
121121
return path
122122
} else {
123-
// This is a hack so our tests work on linux. We need a better way for looking up tools in general.
124-
return AbsolutePath("/usr/bin/" + exec)
123+
// This is a hack so our tests work on linux.
124+
return AbsolutePath("/usr/bin/" + executable)
125125
}
126126
}
127127

128-
private func xcrunFind(exec: String) throws -> AbsolutePath {
129-
#if os(macOS)
128+
private func xcrunFind(executable: String) throws -> AbsolutePath {
129+
let xcrun = "xcrun"
130+
guard lookupExecutablePath(filename: xcrun, searchPaths: searchPaths) != nil else {
131+
throw ToolchainError.unableToFind(tool: xcrun)
132+
}
133+
130134
let path = try Process.checkNonZeroExit(
131-
arguments: ["xcrun", "-sdk", "macosx", "--find", exec],
135+
arguments: [xcrun, "-sdk", "macosx", "--find", executable],
132136
environment: env
133137
).spm_chomp()
134138
return AbsolutePath(path)
135-
#else
136-
throw ToolchainError.unableToFind(tool: exec)
137-
#endif
138139
}
139140
}
140141

0 commit comments

Comments
 (0)