From da76a22ae9ff984a2038940b17e0322219bce25f Mon Sep 17 00:00:00 2001 From: Amy Huang Date: Fri, 25 Aug 2023 15:12:56 -0700 Subject: [PATCH] [llvm-rc] Continue to use Argv[0] to resolve executable path In internal google builds, MainExecPath doesn't go to the directory with `clang`. Fall back to using Argv0 if MainExecPath doesn't find any clangs. Differential Revision: https://reviews.llvm.org/D158901 (cherry picked from commit e4eb8d97e8afcb879dc5cd0da7a937dbb26fbf12) --- llvm/tools/llvm-rc/llvm-rc.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/llvm/tools/llvm-rc/llvm-rc.cpp b/llvm/tools/llvm-rc/llvm-rc.cpp index 4a77f4bd88cce0..233b888546a81e 100644 --- a/llvm/tools/llvm-rc/llvm-rc.cpp +++ b/llvm/tools/llvm-rc/llvm-rc.cpp @@ -142,20 +142,24 @@ ErrorOr findClang(const char *Argv0, StringRef Triple) { if (MainExecPath.empty()) MainExecPath = Argv0; - StringRef Parent = llvm::sys::path::parent_path(MainExecPath); ErrorOr Path = std::error_code(); std::string TargetClang = (Triple + "-clang").str(); std::string VersionedClang = ("clang-" + Twine(LLVM_VERSION_MAJOR)).str(); - if (!Parent.empty()) { - // First look for the tool with all potential names in the specific - // directory of Argv0, if known - for (const auto *Name : - {TargetClang.c_str(), VersionedClang.c_str(), "clang", "clang-cl"}) { + for (const auto *Name : + {TargetClang.c_str(), VersionedClang.c_str(), "clang", "clang-cl"}) { + for (const StringRef Parent : + {llvm::sys::path::parent_path(MainExecPath), + llvm::sys::path::parent_path(Argv0)}) { + // Look for various versions of "clang" first in the MainExecPath parent + // directory and then in the argv[0] parent directory. + // On Windows (but not Unix) argv[0] is overwritten with the eqiuvalent + // of MainExecPath by InitLLVM. Path = sys::findProgramByName(Name, Parent); if (Path) return Path; } } + // If no parent directory known, or not found there, look everywhere in PATH for (const auto *Name : {"clang", "clang-cl"}) { Path = sys::findProgramByName(Name);