From 4e850191efa6659de85a4ff9be7c896cfb0bb1ec Mon Sep 17 00:00:00 2001
Date: Mon, 17 May 2021 21:28:40 -0700
Subject: [PATCH 05/24] clang: support macports-libstdcxx

requires reinplace of search paths
---
 clang/include/clang/Driver/ToolChain.h        |  3 +-
 clang/lib/Driver/ToolChain.cpp                |  3 ++
 clang/lib/Driver/ToolChains/Darwin.cpp        | 40 ++++++++++++++-----
 clang/lib/Driver/ToolChains/Hexagon.cpp       |  2 +
 clang/lib/Frontend/InitHeaderSearch.cpp       |  2 +-
 7 files changed, 63 insertions(+), 11 deletions(-)
 create mode 100644 clang/lib/Frontend/CompilerInvocation.cpp.rej

diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h
index 28c37a44e1eb..78fcaec7bfb0 100644
--- a/tools/clang/include/clang/Driver/ToolChain.h
+++ b/tools//clang/include/clang/Driver/ToolChain.h
@@ -91,6 +91,7 @@ public:
 
   enum CXXStdlibType {
     CST_Libcxx,
+    CST_MacPortsLibstdcxx,
     CST_Libstdcxx
   };
 
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index b2ddef141a75..6160843b6885 100644
--- a/tools/clang/lib/Driver/ToolChain.cpp
+++ b/tools/clang/lib/Driver/ToolChain.cpp
@@ -769,6 +769,8 @@
     return ToolChain::CST_Libcxx;
   else if (LibName == "libstdc++")
     return ToolChain::CST_Libstdcxx;
+  else if (LibName == "macports-libstdc++" || LibName == "libstdc++_macports")
+    return ToolChain::CST_MacPortsLibstdcxx;
   else if (LibName == "platform")
     return GetDefaultCXXStdlibType();
 
@@ -850,6 +852,7 @@
     break;
 
   case ToolChain::CST_Libstdcxx:
+  case ToolChain::CST_MacPortsLibstdcxx:
     CmdArgs.push_back("-lstdc++");
     break;
   }
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index eb7bd4aec898..daa8e520e404 100644
--- a/tools/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/tools/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1969,6 +1969,16 @@
     break;
   }
 
+  case ToolChain::CST_MacPortsLibstdcxx: {
+    bool IsBaseFoundMacPorts = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, llvm::StringRef("@@MACPORTS_GCC_INCLUDE_DIR@@"),
+						   "",
+						   "@@MACPORTS_HOST_NAME@@",
+						   @@MACPORTS_TEST_32_64@@);
+    if (!IsBaseFoundMacPorts) {
+      getDriver().Diag(diag::warn_drv_libstdcxx_not_found);
+    }}
+    break;
+
   case ToolChain::CST_Libstdcxx:
     llvm::SmallString<128> UsrIncludeCxx = Sysroot;
     llvm::sys::path::append(UsrIncludeCxx, "usr", "include", "c++");
@@ -2036,6 +2046,10 @@
     CmdArgs.push_back("-lc++");
     break;
 
+  case ToolChain::CST_MacPortsLibstdcxx:
+    CmdArgs.push_back("@@MACPORTS_libstdc++@@");
+    break;
+
   case ToolChain::CST_Libstdcxx:
     // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
     // it was previously found in the gcc lib dir. However, for all the Darwin

diff --git a/clang/lib/Driver/ToolChains/Hexagon.cpp b/clang/lib/Driver/ToolChains/Hexagon.cpp
index fb54f73bcd4c..0260a32096af 100644
--- a/tools/clang/lib/Driver/ToolChains/Hexagon.cpp
+++ b/tools/clang/lib/Driver/ToolChains/Hexagon.cpp
@@ -551,6 +551,8 @@
     return ToolChain::CST_Libstdcxx;
 
   StringRef Value = A->getValue();
+  if (Value == "macports-libstdc++" || Value == "libstdc++_macports")
+    return ToolChain::CST_MacPortsLibstdcxx;
   if (Value != "libstdc++")
     getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
 
diff --git a/clang/lib/Frontend/InitHeaderSearch.cpp b/clang/lib/Frontend/InitHeaderSearch.cpp
index bc31445d6d08..abc3750edec0 100644
--- a/tools/clang/lib/Frontend/InitHeaderSearch.cpp
+++ b/tools/clang/lib/Frontend/InitHeaderSearch.cpp
@@ -110,7 +110,7 @@ static bool CanPrefixSysroot(StringRef Path) {
 #if defined(_WIN32)
   return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
 #else
-  return llvm::sys::path::is_absolute(Path);
+  return llvm::sys::path::is_absolute(Path) && Path.find("@@MACPORTS_GCC_INCLUDE_DIR@@")!=0;
 #endif
 }
 
-- 
2.21.1 (Apple Git-122.3)
