[clang] [clang][driver] Prevent clang picking the system headers/libraries when started via a symlink (PR #68091)

Liviu Ionescu via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 4 15:08:48 PDT 2023


ilg-ul wrote:

> I wonder if this would need to be somewhat target specific.

I took a first look, and it seems target specific.

The strategy was to search for `InstalledDir` and see how it is used.

It looks like in many places both locations are used, which means things are not that bad:

```c++
  getProgramPaths().push_back(getDriver().getInstalledDir());
  if (getDriver().getInstalledDir() != D.Dir)
    getProgramPaths().push_back(D.Dir);
```

At a closer look, my specific issue is related only to picking the system headers, the libraries seem to be identified correctly. This now clearly explains the errors I encountered, compiling with the system headers and linking with the clang own libraries cannot work if the versions are too far apart.

For example the code identifying the C++ headers is in Darwin.cpp, where only `InstalledDir` is checked:

```
    // Get from '<install>/bin' to '<install>/include/c++/v1'.
    // Note that InstallBin can be relative, so we use '..' instead of
    // parent_path.
    llvm::SmallString<128> InstallBin =
        llvm::StringRef(getDriver().getInstalledDir()); // <install>/bin
    llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
    if (getVFS().exists(InstallBin)) {
      addSystemInclude(DriverArgs, CC1Args, InstallBin);
      return;
    } else if (DriverArgs.hasArg(options::OPT_v)) {
      llvm::errs() << "ignoring nonexistent directory \"" << InstallBin
                   << "\"\n";
    }
```

I'll extend the code to also check `Dir` and this should fix my problem. 

I'll take a look at the other targets too, but I'm not sure that I'll be able to provide fixes for all, since I don't know them, and I don't want to mess anything.



https://github.com/llvm/llvm-project/pull/68091


More information about the cfe-commits mailing list