[cfe-dev] clang built from source in Mac OSX 10.14 with apple-clang/Xcode missing stdlib from include search path

Jens Jorgensen via cfe-dev cfe-dev at lists.llvm.org
Tue Dec 10 19:09:31 PST 2019


Apply my patch and build like :

cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles"
-DCMAKE_BUILD_TYPE=Release
-DCLANG_XCODE_TOOLCHAIN_ROOT=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
../llvm-project/llvm

On 12/10/19 7:45 AM, Sean McBride wrote:
> On Wed, 25 Sep 2019 14:49:57 +0200, Hans Wennborg via cfe-dev said:
>
>> I'm always confused when trying to get locally-built Clang to find
>> stdlib headers on my Mac.
>>
>> Last time, after upgrading to Mojave, following the TL;DR advice from
>> this answer made it work for me: https://stackoverflow.com/a/52530212
> Seems that advice is obsolete. :(  Nothing else I found online worked either.
>
>> It would certainly be nice if this could somehow work out of the box.
> Until then, anyone know how to make one's own build of clang find C++ headers on macOS?
>
> Thanks,
>
> Sean
>
>
-- 
Jens B. Jorgensen
jbj1 at ultraemail.net

-------------- next part --------------
diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 60937aa9db3..88f44f6c589 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -255,6 +255,8 @@ if (NOT(CLANG_DEFAULT_CXX_STDLIB STREQUAL "" OR
     "Default C++ stdlib to use (\"libstdc++\" or \"libc++\", empty for platform default" FORCE)
 endif()
 
+set(CLANG_XCODE_TOOLCHAIN_ROOT "" CACHE PATH "Xcode toolchain directory.")
+
 set(CLANG_DEFAULT_RTLIB "" CACHE STRING
   "Default runtime library to use (\"libgcc\" or \"compiler-rt\", empty for platform default)")
 if (NOT(CLANG_DEFAULT_RTLIB STREQUAL "" OR
diff --git a/clang/include/clang/Config/config.h.cmake b/clang/include/clang/Config/config.h.cmake
index 68125dbc6d4..953511e145d 100644
--- a/clang/include/clang/Config/config.h.cmake
+++ b/clang/include/clang/Config/config.h.cmake
@@ -20,6 +20,9 @@
 /* Default C++ stdlib to use. */
 #define CLANG_DEFAULT_CXX_STDLIB "${CLANG_DEFAULT_CXX_STDLIB}"
 
+/* On Darwin the c++ std library not in /usr with recent Xcode. */
+#define CLANG_XCODE_TOOLCHAIN_ROOT "${CLANG_XCODE_TOOLCHAIN_ROOT}"
+
 /* Default runtime library to use. */
 #define CLANG_DEFAULT_RTLIB "${CLANG_DEFAULT_RTLIB}"
 
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index b43766e98bf..97b9ff1c700 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1922,7 +1922,12 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
   case ToolChain::CST_Libcxx: {
     // On Darwin, libc++ is installed alongside the compiler in
     // include/c++/v1, so get from '<install>/bin' to '<install>/include/c++/v1'.
-    {
+	if (*CLANG_XCODE_TOOLCHAIN_ROOT) {
+      llvm::SmallString<128> P(CLANG_XCODE_TOOLCHAIN_ROOT);
+      // Note that P can be relative, so we have to '..' and not parent_path.
+      llvm::sys::path::append(P, "usr", "include", "c++", "v1");
+      addSystemInclude(DriverArgs, CC1Args, P);
+	} else {
       llvm::SmallString<128> P = llvm::StringRef(getDriver().getInstalledDir());
       // Note that P can be relative, so we have to '..' and not parent_path.
       llvm::sys::path::append(P, "..", "include", "c++", "v1");


More information about the cfe-dev mailing list