--- orig/tools/clang/include/clang/Lex/HeaderSearchOptions.h	2015-11-17 23:54:13.000000000 -0700
+++ new/tools/clang/include/clang/Lex/HeaderSearchOptions.h	2017-01-04 17:50:27.000000000 -0700
@@ -158,6 +158,9 @@
   /// Use libc++ instead of the default libstdc++.
   unsigned UseLibcxx : 1;
 
+  /// Use MacPorts libstdc++ instead of default system libstdc++.
+  unsigned UseMacPortsLibstdcxx : 1;
+
   /// Whether header search information should be output as for -v.
   unsigned Verbose : 1;
 
--- orig/tools/clang/lib/Frontend/CompilerInvocation.cpp	2016-07-13 15:32:15.000000000 -0700
+++ new/tools/clang/lib/Frontend/CompilerInvocation.cpp	2017-01-04 17:52:18.000000000 -0700
@@ -1355,8 +1355,10 @@
   Opts.UseBuiltinIncludes = !Args.hasArg(OPT_nobuiltininc);
   Opts.UseStandardSystemIncludes = !Args.hasArg(OPT_nostdsysteminc);
   Opts.UseStandardCXXIncludes = !Args.hasArg(OPT_nostdincxx);
-  if (const Arg *A = Args.getLastArg(OPT_stdlib_EQ))
+  if (const Arg *A = Args.getLastArg(OPT_stdlib_EQ)) {
     Opts.UseLibcxx = (strcmp(A->getValue(), "libc++") == 0);
+    Opts.UseMacPortsLibstdcxx = (strcmp(A->getValue(), "macports-libstdc++") == 0);
+  }
   Opts.ResourceDir = Args.getLastArgValue(OPT_resource_dir);
   Opts.ModuleCachePath = Args.getLastArgValue(OPT_fmodules_cache_path);
   Opts.ModuleUserBuildPath = Args.getLastArgValue(OPT_fmodules_user_build_path);
--- orig/tools/clang/lib/Frontend/InitHeaderSearch.cpp	2016-05-16 10:22:25.000000000 -0700
+++ new/tools/clang/lib/Frontend/InitHeaderSearch.cpp	2017-01-04 18:04:43.000000000 -0700
@@ -109,7 +109,7 @@
 #if defined(LLVM_ON_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
 }

@@ -362,20 +362,31 @@
 
     case llvm::Triple::ppc:
     case llvm::Triple::ppc64:
+      if (HSOpts.UseMacPortsLibstdcxx) {
+        AddGnuCPlusPlusIncludePaths("@@MACPORTS_GCC_INCLUDE_DIR@@",
+                                    "@@MACPORTS_HOST_NAME@@", "", "ppc64",
+                                    triple);
+      } else {
       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
                                   "powerpc-apple-darwin10", "", "ppc64",
                                   triple);
       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
                                   "powerpc-apple-darwin8", "", "ppc64",
                                   triple);
+      }
       break;
 
     case llvm::Triple::x86:
     case llvm::Triple::x86_64:
+      if (HSOpts.UseMacPortsLibstdcxx) {
+        AddGnuCPlusPlusIncludePaths("@@MACPORTS_GCC_INCLUDE_DIR@@",
+                                    "@@MACPORTS_HOST_NAME@@", "i386", "", triple);
+      } else {
       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
                                   "i686-apple-darwin10", "", "x86_64", triple);
       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
                                   "i686-apple-darwin8", "", "x86_64", triple);
+      }
       break;
 
     case llvm::Triple::arm:
--- orig/tools/clang/include/clang/Driver/ToolChain.h	2016-06-16 03:36:09.000000000 -0700
+++ new/tools/clang/include/clang/Driver/ToolChain.h	2017-01-04 18:08:42.000000000 -0700
@@ -51,7 +51,8 @@
 
   enum CXXStdlibType {
     CST_Libcxx,
-    CST_Libstdcxx
+    CST_Libstdcxx,
+    CST_MacPortsLibstdcxx
   };
 
   enum RuntimeLibType {
--- orig/tools/clang/lib/Driver/ToolChains.cpp	2016-11-23 14:25:50.000000000 -0700
+++ new/tools/clang/lib/Driver/ToolChains.cpp	2017-01-04 20:52:25.000000000 -0700
@@ -773,6 +773,13 @@
     // Otherwise, let the linker search.
     CmdArgs.push_back("-lstdc++");
     break;
+  case ToolChain::CST_MacPortsLibstdcxx:
+    if (getVFS().exists("@@MACPORTS_libstdc++@@")) {
+      CmdArgs.push_back("@@MACPORTS_libstdc++@@");
+      return;
+    }
+    CmdArgs.push_back("-lstdc++");
+    break;
   }
 }
 
@@ -3015,6 +3022,8 @@
     return ToolChain::CST_Libstdcxx;
 
   StringRef Value = A->getValue();
+  if (Value == "macports-libstdc++" )
+      return ToolChain::CST_MacPortsLibstdcxx;
   if (Value != "libstdc++")
     getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
 
--- orig/tools/clang/lib/Driver/ToolChain.cpp	2016-07-15 16:13:27.000000000 -0700
+++ new/tools/clang/lib/Driver/ToolChain.cpp	2017-01-05 08:14:23.000000000 -0700
@@ -545,6 +545,8 @@
     Type = ToolChain::CST_Libcxx;
   else if (Name == "libstdc++")
     Type = ToolChain::CST_Libstdcxx;
+  else if (Name == "macports-libstdc++")
+    Type = ToolChain::CST_MacPortsLibstdcxx;
   else
     return false;
 
@@ -640,6 +642,7 @@
     break;
 
   case ToolChain::CST_Libstdcxx:
+  case ToolChain::CST_MacPortsLibstdcxx:
     CmdArgs.push_back("-lstdc++");
     break;
   }
