diff --git a/llvm/tools/llvm-rc/llvm-rc.cpp b/llvm/tools/llvm-rc/llvm-rc.cpp index 4a77f4bd88cce..233b888546a81 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);