From 250ee3cf3ee96307c494b62e93b93b9b63ec4db2 Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill@korins.ky>
Date: Thu, 18 Aug 2022 02:31:25 +0200
Subject: [PATCH] [clang] support macports-libstdcxx

---
 clang/include/clang/Driver/ToolChain.h  |  1 +
 clang/lib/Driver/ToolChain.cpp          |  3 +++
 clang/lib/Driver/ToolChains/Darwin.cpp  | 14 ++++++++++++++
 clang/lib/Driver/ToolChains/Hexagon.cpp |  2 ++
 clang/lib/Frontend/InitHeaderSearch.cpp |  2 +-
 5 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h
index 7495e08fe6e6..d3ce4ac51edb 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -93,6 +93,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 b8c12fc9241a..e5eab65b6d6d 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -846,6 +846,8 @@ ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
     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();
 
@@ -937,6 +939,7 @@ void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
     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 cfce415edc86..0a6c135a1086 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2066,6 +2066,16 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
     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++");
@@ -2133,6 +2143,10 @@ void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
     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 775f6e1094fa..6710659ab224 100644
--- a/clang/lib/Driver/ToolChains/Hexagon.cpp
+++ b/clang/lib/Driver/ToolChains/Hexagon.cpp
@@ -641,6 +641,8 @@ HexagonToolChain::GetCXXStdlibType(const ArgList &Args) const {
       return ToolChain::CST_Libstdcxx;
   }
   StringRef Value = A->getValue();
+  if (Value == "macports-libstdc++" || Value == "libstdc++_macports")
+    return ToolChain::CST_MacPortsLibstdcxx;
   if (Value != "libstdc++" && Value != "libc++")
     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/clang/lib/Frontend/InitHeaderSearch.cpp
+++ b/clang/lib/Frontend/InitHeaderSearch.cpp
@@ -108,7 +108,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.37.2

