Skip to content

[Toolchains] - look for tools relative to the executable path. #25

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 2 commits into from
Dec 16, 2019
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
12 changes: 6 additions & 6 deletions Sources/SwiftDriver/Toolchains/DarwinToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public final class DarwinToolchain: Toolchain {
public func getToolPath(_ tool: Tool) throws -> AbsolutePath {
switch tool {
case .swiftCompiler:
return try lookup(exec: "swift")
return try lookup(executable: "swift")

case .dynamicLinker:
return try lookup(exec: "ld")
return try lookup(executable: "ld")

case .staticLinker:
return try lookup(exec: "libtool")
return try lookup(executable: "libtool")

case .dsymutil:
return try lookup(exec: "dsymutil")
return try lookup(executable: "dsymutil")

case .clang:
let result = try Process.checkNonZeroExit(
Expand All @@ -43,13 +43,13 @@ public final class DarwinToolchain: Toolchain {
).spm_chomp()
return AbsolutePath(result)
case .swiftAutolinkExtract:
return try lookup(exec: "swift-autolink-extract")
return try lookup(executable: "swift-autolink-extract")
}
}

/// Swift compiler path.
public lazy var swiftCompiler: Result<AbsolutePath, Swift.Error> = Result {
try lookup(exec: "swift")
try lookup(executable: "swift")
}

/// SDK path.
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ public final class GenericUnixToolchain: Toolchain {
public func getToolPath(_ tool: Tool) throws -> AbsolutePath {
switch tool {
case .swiftCompiler:
return try lookup(exec: "swift")
return try lookup(executable: "swift")
case .staticLinker:
return try lookup(exec: "ar")
return try lookup(executable: "ar")
case .dynamicLinker:
// FIXME: This needs to look in the tools_directory first.
return try lookup(exec: "clang")
return try lookup(executable: "clang")
case .clang:
return try lookup(exec: "clang")
return try lookup(executable: "clang")
case .swiftAutolinkExtract:
return try lookup(exec: "swift-autolink-extract")
return try lookup(executable: "swift-autolink-extract")
case .dsymutil:
return try lookup(exec: "dsymutil")
return try lookup(executable: "dsymutil")
}
}

Expand Down
31 changes: 16 additions & 15 deletions Sources/SwiftDriver/Toolchains/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,35 @@ extension Toolchain {
return "SWIFT_DRIVER_\(toolName.uppercased())_EXEC"
}

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

private func xcrunFind(exec: String) throws -> AbsolutePath {
#if os(macOS)
private func xcrunFind(executable: String) throws -> AbsolutePath {
let xcrun = "xcrun"
guard lookupExecutablePath(filename: xcrun, searchPaths: searchPaths) != nil else {
throw ToolchainError.unableToFind(tool: xcrun)
}

let path = try Process.checkNonZeroExit(
arguments: ["xcrun", "-sdk", "macosx", "--find", exec],
arguments: [xcrun, "-sdk", "macosx", "--find", executable],
environment: env
).spm_chomp()
return AbsolutePath(path)
#else
throw ToolchainError.unableToFind(tool: exec)
#endif
}
}

Expand Down