From db35d17b6caa6348a379fa6e86bf71dc15b94a92 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 23 Apr 2024 22:24:29 -0700 Subject: [PATCH] Don't canonicalize prefixes when locating the driver binary. (#3912) The approach here directly follows the one used by Clang's driver. --- toolchain/driver/driver_main.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/toolchain/driver/driver_main.cpp b/toolchain/driver/driver_main.cpp index c02ffee13d3f..d3ac278a83b5 100644 --- a/toolchain/driver/driver_main.cpp +++ b/toolchain/driver/driver_main.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Path.h" +#include "llvm/Support/Program.h" #include "toolchain/driver/driver.h" auto main(int argc, char** argv) -> int { @@ -18,6 +19,16 @@ auto main(int argc, char** argv) -> int { return EXIT_FAILURE; } + // Find the executable without resolving symlinks. Do a PATH lookup if argv[0] + // isn't a valid path. + llvm::SmallString<128> exe_path(argv[0]); + if (!llvm::sys::fs::exists(exe_path)) { + if (llvm::ErrorOr path = + llvm::sys::findProgramByName(exe_path)) { + exe_path = *path; + } + } + Carbon::SetWorkingDirForBazel(); // Printing to stderr should flush stdout. This is most noticeable when stderr @@ -28,10 +39,7 @@ auto main(int argc, char** argv) -> int { auto fs = llvm::vfs::getRealFileSystem(); // Construct the data directory relative to the executable location. - static int static_for_main_addr; - std::string exe = - llvm::sys::fs::getMainExecutable(argv[0], &static_for_main_addr); - llvm::SmallString<256> data_dir(llvm::sys::path::parent_path(exe)); + llvm::SmallString<256> data_dir(llvm::sys::path::parent_path(exe_path)); llvm::sys::path::append(data_dir, llvm::sys::path::Style::posix, "carbon.runfiles/_main/");