--- Portfile.orig	2017-01-22 01:46:23.000000000 -0700
+++ Portfile	2017-01-25 17:25:43.000000000 -0700
@@ -12,6 +12,7 @@
 name                    llvm-${llvm_version}
 revision                2
 subport                 clang-${llvm_version} { revision 3 }
+subport                 clang-libstdcxx-${llvm_version} { revision 0 }
 set suffix              mp-${llvm_version}
 set sub_prefix          ${prefix}/libexec/llvm-${llvm_version}
 dist_subdir             llvm
@@ -20,6 +21,12 @@
 license                 NCSA
 maintainers             jeremyhu larryv
 
+if {${subport} eq "clang-${llvm_version}" || ${subport} eq "clang-libstdcxx-${llvm_version}"} {
+    set clang_subport   true
+} else {
+    set clang_subport   false
+}
+
 if {${subport} eq "llvm-${llvm_version}"} {
     homepage            http://llvm.org/
     description         llvm is a next generation compiler infrastructure
@@ -32,7 +39,7 @@
 
     depends_lib         port:libedit port:libffi port:ncurses port:xar port:zlib
     depends_run         bin:perl:perl5 port:llvm_select
-} elseif {${subport} eq "clang-${llvm_version}"} {
+} elseif {${clang_subport}} {
     homepage            http://clang.llvm.org/
     description         C, C++, Objective C and Objective C++ compiler
     long_description    Clang is an "LLVM native" C/C++/Objective-C compiler, \
@@ -92,7 +99,7 @@
         if {[variant_isset polly]} {
             distfiles-append     polly-${version}.src${extract.suffix}
         }
-    } elseif {${subport} eq "clang-${llvm_version}"} {
+    } elseif {${clang_subport}} {
         distfiles-append     cfe-${version}.src${extract.suffix} compiler-rt-${version}.src${extract.suffix} libcxx-${version}.src${extract.suffix} clang-tools-extra-${version}.src${extract.suffix}
     }
 }
@@ -127,7 +134,7 @@
     0007-CMake-Fix-llvm_setup_rpath-function.patch \
     0008-Fix-build-issues-pre-Lion-due-to-missing-a-strnlen-d.patch
 
-if {${subport} eq "clang-${llvm_version}"} {
+if {${clang_subport}} {
     patchfiles-append \
         1001-MacPorts-Only-Prepare-clang-format-for-replacement-w.patch \
         1002-MacPorts-Only-Fix-name-of-scan-view-executable-insid.patch \
@@ -210,7 +217,7 @@
 if {${subport} eq "llvm-${llvm_version}"} {
     select.group        llvm
     select.file         ${filespath}/mp-${subport}
-} elseif {${subport} eq "clang-${llvm_version}"} {
+} elseif {${clang_subport}} {
     select.group        clang
     select.file         ${filespath}/mp-${subport}
 
@@ -242,7 +249,7 @@
 # Xcode 6.2's clang (600.0.57) fails due to https://llvm.org/bugs/show_bug.cgi?id=25753
 compiler.blacklist *gcc* {clang < 602}
 
-if {${subport} eq "clang-${llvm_version}"} {
+if {${clang_subport}} {
     # clang older than 3.5 fail due to https://llvm.org/bugs/show_bug.cgi?id=25753
     compiler.blacklist-append macports-clang-3.3 macports-clang-3.4
 }
@@ -295,13 +302,13 @@
         configure.optflags    -Os
     }
 
-    if {${subport} eq "clang-${llvm_version}" && [vercmp $xcodeversion 5.0] < 0} {
+    if {${clang_subport} && [vercmp $xcodeversion 5.0] < 0} {
         # https://llvm.org/bugs/show_bug.cgi?id=13671
         patchfiles-append leopard-no-asan.patch
         configure.args-append -DCOMPILER_RT_BUILD_SANITIZERS=OFF
     }
 
-    if {${subport} eq "clang-${llvm_version}" && ${os.major} <= 9} {
+    if {${clang_subport} && ${os.major} <= 9} {
         patchfiles-append leopard-no-blocks.patch
     }
 
@@ -331,7 +338,7 @@
                 file rename ${workpath}/polly-${version}.src ${worksrcpath}/tools/polly
             }
         }
-    } elseif {${subport} eq "clang-${llvm_version}"} {
+    } elseif {${clang_subport}} {
         if {${worksrcdir} eq "trunk"} {
             system -W ${worksrcpath}/tools "svn ${proxy_args} co -r ${svn.revision} http://llvm.org/svn/llvm-project/cfe/trunk clang"
             system -W ${worksrcpath}/projects "svn ${proxy_args} co -r ${compiler_rt_rev} http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt"
@@ -351,7 +358,7 @@
     }
 }
 
-if {${subport} eq "clang-${llvm_version}"} {
+if {${clang_subport}} {
     destroot {
         system "cd ${destroot.dir}/tools/clang && ${destroot.cmd} ${destroot.pre_args} ${destroot.target} ${destroot.post_args}"
         system "cd ${destroot.dir}/projects/compiler-rt && ${destroot.cmd} ${destroot.pre_args} ${destroot.target} ${destroot.post_args}"
@@ -382,7 +389,7 @@
         if {[variant_isset polly]} {
             ln -s LLVMPolly.so ${destroot}${sub_prefix}/lib/LLVMPolly.dylib
         }
-    } elseif {${subport} eq "clang-${llvm_version}"} {
+    } elseif {${clang_subport}} {
         system "ditto ${worksrcpath}/projects/libcxx/include ${destroot}${sub_prefix}/lib/c++/v1"
 
         # http://trac.macports.org/ticket/33207
@@ -403,7 +410,7 @@
 
         destroot.args-append  OVERRIDE_libdir=${sub_prefix}/lib
     }
-} elseif {${subport} eq "clang-${llvm_version}"} {
+} elseif {${clang_subport}} {
     if {[variant_isset assertions]} {
         # Need to match llvm +-assertions
         require_active_variants port:llvm-${llvm_version} assertions
@@ -455,4 +462,19 @@
     }
 }
 
+if {${subport} eq "clang-libstdcxx-${llvm_version}"} {
+    depends_lib-append port:gcc6
+
+    patchfiles-append patch-macports-libstdcxx.diff
+
+    post-patch {
+        reinplace "s|@@MACPORTS_GCC_INCLUDE_DIR@@|${prefix}/include/gcc6/c++|g" \
+            ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
+        reinplace "s|@@MACPORTS_HOST_NAME@@|${build_arch}-apple-darwin${os.major}|g" \
+            ${worksrcpath}/tools/clang/lib/Frontend/InitHeaderSearch.cpp
+        reinplace "s|@@MACPORTS_libstdc++@@|${prefix}/lib/libgcc/libstdc++.6.dylib|g" \
+            ${worksrcpath}/tools/clang/lib/Driver/ToolChains.cpp
+    }
+}
+
 livecheck.type          none
