Skip to content

Commit

Permalink
[llvm-rc] Continue to use Argv[0] to resolve executable path
Browse files Browse the repository at this point in the history
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 e4eb8d9)
  • Loading branch information
amykhuang authored and tru committed Aug 29, 2023
1 parent ed108ee commit da76a22
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions llvm/tools/llvm-rc/llvm-rc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,24 @@ ErrorOr<std::string> findClang(const char *Argv0, StringRef Triple) {
if (MainExecPath.empty())
MainExecPath = Argv0;

StringRef Parent = llvm::sys::path::parent_path(MainExecPath);
ErrorOr<std::string> 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);
Expand Down

0 comments on commit da76a22

Please sign in to comment.