From ec976d3be4e78dbbb019b3be941066f74e826880 Mon Sep 17 00:00:00 2001 From: Sungkeun Cho Date: Fri, 4 Jun 2021 09:03:50 -0700 Subject: [PATCH] Apply patch for finding correct C++ header from Apple SDKs Reviewed By: jvillard Differential Revision: D28899204 fbshipit-source-id: c27c4d5dc --- facebook-clang-plugins/clang/setup.sh | 1 + .../clang/src/sysroot_cpp_headers.patch | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 facebook-clang-plugins/clang/src/sysroot_cpp_headers.patch diff --git a/facebook-clang-plugins/clang/setup.sh b/facebook-clang-plugins/clang/setup.sh index 2d7297e97..e16431af8 100755 --- a/facebook-clang-plugins/clang/setup.sh +++ b/facebook-clang-plugins/clang/setup.sh @@ -17,6 +17,7 @@ CLANG_PREBUILD_PATCHES=( "$SCRIPT_DIR/src/mangle_suppress_errors.patch" "$SCRIPT_DIR/src/AArch64SVEACLETypes.patch" "$SCRIPT_DIR/src/benchmark_register.patch" + "$SCRIPT_DIR/src/sysroot_cpp_headers.patch" ) CLANG_PREFIX="$SCRIPT_DIR/install" CLANG_INSTALLED_VERSION_FILE="$SCRIPT_DIR/installed.version" diff --git a/facebook-clang-plugins/clang/src/sysroot_cpp_headers.patch b/facebook-clang-plugins/clang/src/sysroot_cpp_headers.patch new file mode 100644 index 000000000..516c14549 --- /dev/null +++ b/facebook-clang-plugins/clang/src/sysroot_cpp_headers.patch @@ -0,0 +1,58 @@ +--- a/llvm-project/clang/lib/Driver/ToolChains/Darwin.cpp ++++ b/llvm-project/clang/lib/Driver/ToolChains/Darwin.cpp +@@ -2021,20 +2021,41 @@ + + switch (GetCXXStdlibType(DriverArgs)) { + case ToolChain::CST_Libcxx: { +- // On Darwin, libc++ is installed alongside the compiler in +- // include/c++/v1, so get from '/bin' to '/include/c++/v1'. +- { +- 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"); +- addSystemInclude(DriverArgs, CC1Args, P); +- } +- // Also add /usr/include/c++/v1 unless -nostdinc is used, +- // to match the legacy behavior in CC1. +- if (!DriverArgs.hasArg(options::OPT_nostdinc)) { +- llvm::SmallString<128> P = Sysroot; +- llvm::sys::path::append(P, "usr", "include", "c++", "v1"); +- addSystemInclude(DriverArgs, CC1Args, P); ++ // On Darwin, libc++ can be installed in one of the following two places: ++ // 1. Alongside the compiler in /include/c++/v1 ++ // 2. In a SDK (or a custom sysroot) in /usr/include/c++/v1 ++ // ++ // The precendence of paths is as listed above, i.e. we take the first path ++ // that exists. Also note that we never include libc++ twice -- we take the ++ // first path that exists and don't send the other paths to CC1 (otherwise ++ // include_next could break). ++ ++ // Check for (1) ++ // Get from '/bin' to '/include/c++/v1'. ++ // Note that InstallBin can be relative, so we use '..' instead of ++ // parent_path. ++ llvm::SmallString<128> InstallBin = ++ llvm::StringRef(getDriver().getInstalledDir()); // /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"; + } ++ ++ // Otherwise, check for (2) ++ llvm::SmallString<128> SysrootUsr = Sysroot; ++ llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1"); ++ if (getVFS().exists(SysrootUsr)) { ++ addSystemInclude(DriverArgs, CC1Args, SysrootUsr); ++ return; ++ } else if (DriverArgs.hasArg(options::OPT_v)) { ++ llvm::errs() << "ignoring nonexistent directory \"" << SysrootUsr ++ << "\"\n"; ++ } ++ ++ // Otherwise, don't add any path. + break; + }