Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

clang ignores --sys-root when looking for libc++ headers #23690

Open Quuxplusone opened 9 years ago

Quuxplusone commented 9 years ago
Bugzilla Link PR23691
Status NEW
Importance P normal
Reported by Bernhard Rosenkraenzer (bero@linaro.org)
Reported on 2015-05-28 17:40:24 -0700
Last modified on 2015-05-28 17:52:38 -0700
Version trunk
Hardware PC Linux
CC llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
When trying to cross-compile a C++ application using libc++, clang includes
libc++ headers from the system instead of the target sysroot.

For example, trying to cross-compile something that does "#include <string>"
with

clang -target x86_64-openmandriva-linux-musl -std=c++11 -stdlib=libc++ test.cpp
--sysroot=/usr/x86_64-openmandriva-linux-musl/sys-root -lc++ -lc++abi

will include
/usr/bin/../include/c++/v1/string

even if /usr/x86_64-openmandriva-linux-musl/usr/include/c++/v1/string exists.
Quuxplusone commented 9 years ago
Looking at the code, this seems to be intentional (but I'd still argue it's
questionable), Linux::AddClangCXXStdlibIncludeArgs in
tools/clang/lib/Driver/ToolChains.cpp has the following code:

  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
    const std::string LibCXXIncludePathCandidates[] = {
      // The primary location is within the Clang installation.
      // FIXME: We shouldn't hard code 'v1' here to make Clang future proof to
      // newer ABI versions.
      getDriver().Dir + "/../include/c++/v1",

      // We also check the system as for a long time this is the only place Clang looked.
      // FIXME: We should really remove this. It doesn't make any sense.
      getDriver().SysRoot + "/usr/include/c++/v1"
    };

So it intentionally looks in the clang installation's directory first, and only
then considers anything sysroot-ed.

I'd argue the sysroot-ed version is preferable because there may be a need to
use a specific different version of libc++ headers on one particular target
(e.g. patched headers for a Linux+musl environment, or an experimental newer
version for a newly supported target), looking in the clang installation
directory forces the same libc++ headers on any target supported by this clang
installation.