--- /opt/local/var/macports/sources/git.macports.org/dports/lang/gcc6/Portfile	2017-03-30 15:25:57.451720245 +0200
+++ lang/gcc6/Portfile	2017-05-13 19:21:21.269593596 +0200
@@ -52,7 +52,8 @@
     configure.pre_args-append --build=${build_arch}-apple-darwin${os.major}
 
     # see https://gcc.gnu.org/ml/gcc-patches/2012-05/msg00672.html
-    patchfiles-append         patch-float128.diff
+    patchfiles-append         patch-float128.diff \
+                              patch-constchar.diff
 }
 
 configure.dir       ${workpath}/build
@@ -213,7 +214,56 @@
         ln -s libgcc/libstdc++.6.dylib ${destroot}${prefix}/lib/libstdc++.6.dylib
     }
 } else {
-depends_run-append         port:gcc_select
+
+foreach cdv {{} 3.9 4.0} {
+    set cv [join [split ${cdv} "."] ""]
+    if {${cdv} eq {}} {
+        variant libcxx description "Build to use libc++ and the corresponding headers from Xcode" {}
+    } else {
+        variant libcxx${cv} description "Build to use libc++ and the corresponding headers from port:clang-${cdv}" {}
+    }
+    if {[variant_isset libcxx${cv}]} {
+        if {${cdv} ne {}} {
+            depends_run-append  port:clang-${cdv}
+            set libcxx_headers "${prefix}/libexec/llvm-${cdv}/include/c++/v1"
+        } else {
+            set libcxx_headers "[file dir [exec xcrun --find clang]]/../include/c++/v1"
+        }
+        patchfiles-append   patch-use-libcxx.diff
+        post-patch {
+            reinplace "s|@LLVMHEADERPATH@|${libcxx_headers}|g" ${worksrcpath}/gcc/incpath.c
+        }
+        # user is interested in being able to use G++ as broadly as possible;
+        # configure GCC to use gccas and if necessary install it for the same clang version
+        default_variants +gccas
+        if {[variant_isset gccas]} {
+            variant clang${cv} description "install gccas for the preferred clang version" {}
+            default_variants-append +clang${cv}
+        }
+        notes-append "G++ will search the libc++ headers in ${libcxx_headers} by default;\
+                        this can be overridden by setting LIBCPP_INCLUDE_PATH. The original libstdc++\
+                        headers are in ${prefix}/include/${name}-stdinc/c++ ."
+    }
+}
+
+variant gccas description {use the clang assembler instead of as} {}
+if {[variant_isset gccas]} {
+    depends_lib-replace \
+                        port:cctools \
+                        port:gccas
+    depends_skip_archcheck-replace \
+                        cctools \
+                        gccas
+    configure.args-replace \
+                        --with-as=${prefix}/bin/as \
+                        --with-as=${prefix}/bin/gccas
+}
+
+# used when a +libcxx variant is selected; it causes g++ to add
+build.env-append        GCC_IS_BUILDING_ITSELF=1
+destroot.env-append     GCC_IS_BUILDING_ITSELF=1
+
+depends_run-append          port:gcc_select
 
 post-destroot {
     file delete ${destroot}${prefix}/share/info/dir
@@ -243,6 +293,14 @@
             }
         }
     }
+    foreach cdv {{} 3.9 4.0} {
+        set cv [join [split ${cdv} "."] ""]
+        if {[variant_isset libcxx${cv}]} {
+            # move the stdc++ headers out of the way
+            xinstall -m 755 -d ${destroot}${prefix}/include/${name}-stdinc
+            file rename ${destroot}${prefix}/include/${name}/c++ ${destroot}${prefix}/include/${name}-stdinc/c++
+        }
+    }
 }
 
 select.group        gcc
@@ -276,6 +334,39 @@
 configure.universal_cxxflags
 configure.universal_ldflags
 configure.universal_args
+# let the build use our selected compiler options. It must be possible
+# to set only 1 or 2 variables and cover all build stages, but in practice
+# that doesn't turn out to be true.
+configure.env-append \
+    BOOT_CFLAGS=${configure.optflags} \
+    STAGE_CFLAGS=${configure.optflags} \
+    STAGE1_CFLAGS=${configure.optflags} \
+    STAGE2_CFLAGS=${configure.optflags} \
+    STAGE3_CFLAGS=${configure.optflags} \
+    STAGE4_CFLAGS=${configure.optflags} \
+    BOOT_CXXFLAGS=${configure.optflags} \
+    STAGE_CXXFLAGS=${configure.optflags} \
+    STAGE1_CXXFLAGS=${configure.optflags} \
+    STAGE2_CXXFLAGS=${configure.optflags} \
+    STAGE3_CXXFLAGS=${configure.optflags} \
+    STAGE4_CXXFLAGS=${configure.optflags} \
+    CXXFLAGS_FOR_TARGET=${configure.optflags} \
+    CFLAGS_FOR_TARGET=${configure.optflags}
+build.env-append \
+    BOOT_CFLAGS=${configure.optflags} \
+    STAGE_CFLAGS=${configure.optflags} \
+    STAGE1_CFLAGS=${configure.optflags} \
+    STAGE2_CFLAGS=${configure.optflags} \
+    STAGE3_CFLAGS=${configure.optflags} \
+    STAGE4_CFLAGS=${configure.optflags} \
+    BOOT_CXXFLAGS=${configure.optflags} \
+    STAGE_CXXFLAGS=${configure.optflags} \
+    STAGE1_CXXFLAGS=${configure.optflags} \
+    STAGE2_CXXFLAGS=${configure.optflags} \
+    STAGE3_CXXFLAGS=${configure.optflags} \
+    STAGE4_CXXFLAGS=${configure.optflags} \
+    CXXFLAGS_FOR_TARGET=${configure.optflags} \
+    CFLAGS_FOR_TARGET=${configure.optflags}
 
 livecheck.type      regex
 livecheck.url       http://ftp.gnu.org/gnu/gcc/
