Skip to content

Commit 720098d

Browse files
authored
Merge pull request #42619 from stevapple/driver-search
[Driver] More robust tool searching logic
2 parents 67b23d7 + a1df4ba commit 720098d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/DriverTool/driver.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "swift/FrontendTool/FrontendTool.h"
3333
#include "swift/DriverTool/DriverTool.h"
3434
#include "llvm/ADT/SmallVector.h"
35+
#include "llvm/ADT/Triple.h"
3536
#include "llvm/Support/CommandLine.h"
3637
#include "llvm/Support/ConvertUTF.h"
3738
#include "llvm/Support/Errno.h"
@@ -168,12 +169,13 @@ static bool appendSwiftDriverName(SmallString<256> &buffer) {
168169
return true;
169170
}
170171

171-
llvm::sys::path::append(buffer, "swift-driver");
172+
StringRef execSuffix(llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows() ? ".exe" : "");
173+
llvm::sys::path::append(buffer, "swift-driver" + execSuffix);
172174
if (llvm::sys::fs::exists(buffer)) {
173175
return true;
174176
}
175177
llvm::sys::path::remove_filename(buffer);
176-
llvm::sys::path::append(buffer, "swift-driver-new");
178+
llvm::sys::path::append(buffer, "swift-driver-new" + execSuffix);
177179
if (llvm::sys::fs::exists(buffer)) {
178180
return true;
179181
}
@@ -379,15 +381,16 @@ int swift::mainEntry(int argc_, const char **argv_) {
379381
subCommandArgs.erase(&subCommandArgs[1]);
380382
// We are running as a subcommand, try to find the subcommand adjacent to
381383
// the executable we are running as.
382-
SmallString<256> SubcommandPath(
383-
llvm::sys::path::parent_path(getExecutablePath(argv[0])));
384-
llvm::sys::path::append(SubcommandPath, SubcommandName);
385-
386-
// If we didn't find the tool there, let the OS search for it.
387-
if (!llvm::sys::fs::exists(SubcommandPath)) {
384+
SmallString<256> SubcommandPath(SubcommandName);
385+
auto result = llvm::sys::findProgramByName(SubcommandName,
386+
{ llvm::sys::path::parent_path(getExecutablePath(argv[0])) });
387+
if (!result.getError()) {
388+
SubcommandPath = *result;
389+
} else {
390+
// If we didn't find the tool there, let the OS search for it.
391+
result = llvm::sys::findProgramByName(SubcommandName);
388392
// Search for the program and use the path if found. If there was an
389393
// error, ignore it and just let the exec fail.
390-
auto result = llvm::sys::findProgramByName(SubcommandName);
391394
if (!result.getError())
392395
SubcommandPath = *result;
393396
}

0 commit comments

Comments
 (0)