--- qt5-1.0.tcl.orig	2016-12-25 14:44:41.000000000 -0700
+++ qt5-1.0.tcl	2016-12-28 22:25:49.000000000 -0700
@@ -37,77 +37,103 @@
 global available_qt_versions
 set available_qt_versions {
     qt5
+    qt56
     qt55
+    qt5-kde
 }
 
 # standard Qt5 name
 global qt_name
 
-if { ![info exists qt_name] } {
+# default to kde?
+global qt5.using_kde
+
+proc qt5.get_default_name {} {
+    global qt5.using_kde os.major cxx_stdlib
+
+    if { ![tbool qt5.using_kde] } {
 
-    if { ${os.major} <= 7 } {
-        #
-        # Qt 5 does not support ppc
-        # see http://doc.qt.io/qt-5/osx-requirements.html
-        #
-        set qt_name qt5
-        #
-    } elseif { ${os.major} <= 9 } {
-        #
-        # Mac OS X Tiger (10.4)
-        # Mac OS X Leopard (10.5)
-        #
-        # never supported by Qt 5
-        #
-        set qt_name qt5
-        #
-    } elseif { ${os.major} == 10 } {
-        #
-        # Mac OS X Snow Leopard (10.6)
-        #
-        #     Qt 5.3: Deployment only
-        # Qt 5.0-5.2: Occasionally tested
-        #
-        set qt_name qt5
-        #
-    } elseif { ${os.major} == 11 } {
-        #
-        # Mac OS X Lion (10.7)
-        #
-        # Qt 5.6: Deployment only
-        # Qt 5.5: Occasionally tested
-        # Qt 5.4: Supported
-        #
-        set qt_name qt5
-        #
-    } elseif { ${os.major} <= 12 } {
-        #
-        # OS X Mountain Lion (10.8)
-        # OS X Mavericks (10.9)
-        # OS X Yosemite (10.10)
-        # OS X El Capitan (10.11)
-        #
-        # Qt 5.7: Supported
-        # Qt 5.6: Supported
-        #
-        set qt_name qt5
-        #
-    } elseif { ${os.major} <= 16 } {
-        #
-        # macOS Sierra (10.12)
-        # as of Qt version 5.7, there is no official support
-        #
-        set qt_name qt5
-        #
+        if { ${os.major} <= 7 } {
+            #
+            # Qt 5 does not support ppc
+            # see http://doc.qt.io/qt-5/osx-requirements.html
+            #
+            return qt55
+            #
+        } elseif { ${os.major} <= 9 } {
+            #
+            # Mac OS X Tiger (10.4)
+            # Mac OS X Leopard (10.5)
+            #
+            # never supported by Qt 5
+            #
+            return qt55
+            #
+        } elseif { ${os.major} == 10 } {
+            #
+            # Mac OS X Snow Leopard (10.6)
+            #
+            #     Qt 5.3: Deployment only
+            # Qt 5.0-5.2: Occasionally tested
+            #
+            return qt55
+            #
+        } elseif { ${os.major} == 11 } {
+            #
+            # Mac OS X Lion (10.7)
+            #
+            # Qt 5.6: Deployment only
+            # Qt 5.5: Occasionally tested
+            # Qt 5.4: Supported
+            #
+            return qt56
+            #
+        } elseif { ${os.major} <= 15 } {
+            #
+            # OS X Mountain Lion (10.8)
+            # OS X Mavericks (10.9)
+            # OS X Yosemite (10.10)
+            # OS X El Capitan (10.11)
+            #
+            # Qt 5.7: Supported
+            # Qt 5.6: Supported
+            #
+            if {${cxx_stdlib} eq "libstdc++"} {
+                return qt56
+            } else {
+                return qt5
+            }
+            #
+        } elseif { ${os.major} <= 16 } {
+            #
+            # macOS Sierra (10.12)
+            # as of Qt version 5.7, there is no official support
+            #
+            if {${cxx_stdlib} eq "libstdc++"} {
+                return qt56
+            } else {
+                return qt5
+            }
+            #
+        } else {
+            #
+            # macOS ??? (???)
+            #
+            if {${cxx_stdlib} eq "libstdc++"} {
+                return qt56
+            } else {
+                return qt5
+            }
+        }
     } else {
-        #
-        # macOS ??? (???)
-        #
-        set qt_name qt5
-        #
+        return qt5-kde
     }
 }
 
+if { ![info exists qt_name] } {
+    set qt_name [qt5.get_default_name]
+}
+
 # Qt has what is calls reference configurations, which are said to be thoroughly tested
 # Qt also has configurations which are "occasionally tested" or are "[d]eployment only"
 # see http://doc.qt.io/qt-5/supported-platforms.html#reference-configurations
@@ -117,19 +143,31 @@
 global qt5_max_reference_version
 
 # see http://doc.qt.io/qt-5/supported-platforms-and-configurations.html
-switch ${qt_name} {
+switch -exact ${qt_name} {
     qt5 {
         set qt5_min_tested_version     12
         set qt5_max_tested_version     15
         set qt5_min_reference_version  12
         set qt5_max_reference_version  15
     }
+    qt56 {
+        set qt5_min_tested_version     11
+        set qt5_max_tested_version     15
+        set qt5_min_reference_version  12
+        set qt5_max_reference_version  15
+    }
     qt55 {
         set qt5_min_tested_version     11
         set qt5_max_tested_version     14
         set qt5_min_reference_version  13
         set qt5_max_reference_version  14
     }
+    qt5-kde {
+        set qt5_min_tested_version     11
+        set qt5_max_tested_version     15
+        set qt5_min_reference_version  12
+        set qt5_max_reference_version  15
+    }
 }
 
 if {[tbool just_want_qt5_version_info]} {
@@ -243,6 +281,151 @@
     return
 }
 
+# a procedure for declaring dependencies on Qt5 components, which will expand them
+# into the appropriate subports for the Qt5 flavour installed
+# e.g. qt5.depends_component qtbase qtsvg qtdeclarative
+proc qt5.depends_component-append {first args} {
+    global qt_name qt5.using_kde
+
+    set args [linsert $args[set list {}] 0 ${first}]
+    foreach comp ${args} {
+
+        if { ![tbool qt5.using_kde] } {
+            set qt_port_name ${qt_name}-${comp}
+        } else {
+            switch -exact ${comp} {
+                qtwebkit -
+                qtwebengine -
+                qtwebview {
+                    # these components are subports
+                    set qt_port_name ${qt_name}-${comp}
+                }
+                default {
+                    # qt5*-kde provides all components except those above
+                    set qt_port_name ${qt_name}
+                }
+            }
+        }
+
+        switch -exact ${comp} {
+            qtquickcontrols2 {
+                depends_lib-append path:lib/pkgconfig/Qt5QuickControls2.pc:${qt_port_name}
+            }
+            qtbase {
+                depends_lib-append path:lib/pkgconfig/Qt5Core.pc:${qt_port_name}
+            }
+            qtwebengine {
+                depends_lib-append path:lib/pkgconfig/Qt5WebEngine.pc:${qt_port_name}
+            }
+            qtdeclarative {
+                depends_lib-append path:lib/pkgconfig/Qt5Qml.pc:${qt_port_name}
+            }
+            qtserialport {
+                depends_lib-append path:lib/pkgconfig/Qt5SerialPort.pc:${qt_port_name}
+            }
+            qtsensors {
+                depends_lib-append path:lib/pkgconfig/Qt5Sensors.pc:${qt_port_name}
+            }
+            qtcharts {
+                depends_lib-append path:lib/pkgconfig/Qt5Charts.pc:${qt_port_name}
+            }
+            qtwebkit {
+                depends_lib-append path:lib/pkgconfig/Qt5WebKit.pc:${qt_port_name}
+            }
+            qtwebchannel {
+                depends_lib-append path:lib/pkgconfig/Qt5WebChannel.pc:${qt_port_name}
+            }
+            qtdeclarative-render2d {
+                depends_lib-append path:lib/cmake/Qt5Quick/Qt5Quick_ContextPlugin.cmake:${qt_port_name}
+            }
+            qtimageformats {
+                depends_lib-append path:lib/cmake/Qt5Gui/Qt5Gui_QTiffPlugin.cmake:${qt_port_name}
+            }
+            qtvirtualkeyboard {
+                depends_lib-append path:lib/cmake/Qt5Gui/Qt5Gui_QVirtualKeyboardPlugin.cmake:${qt_port_name}
+            }
+            qtsvg {
+                depends_lib-append path:lib/pkgconfig/Qt5Svg.pc:${qt_port_name}
+            }
+            qtmacextras {
+                depends_lib-append path:lib/pkgconfig/Qt5MacExtras.pc:${qt_port_name}
+            }
+            qtlocation {
+                depends_lib-append path:lib/pkgconfig/Qt5Location.pc:${qt_port_name}
+            }
+            qtxmlpatterns {
+                depends_lib-append path:lib/pkgconfig/Qt5XmlPatterns.pc:${qt_port_name}
+            }
+            qtserialbus {
+                depends_lib-append path:lib/pkgconfig/Qt5SerialBus.pc:${qt_port_name}
+            }
+            qtmultimedia {
+                depends_lib-append path:lib/pkgconfig/Qt5Multimedia.pc:${qt_port_name}
+            }
+            qtcanvas3d {
+                depends_lib-append path:libexec/qt5/qml/QtCanvas3D/libqtcanvas3d.dylib:${qt_port_name}
+            }
+            qtdatavis3d {
+                depends_lib-append path:lib/pkgconfig/Qt5DataVisualization.pc:${qt_port_name}
+            }
+            qtgraphicaleffects {
+                depends_lib-append path:libexec/qt5/qml/QtGraphicalEffects/libqtgraphicaleffectsplugin.dylib:${qt_port_name}
+            }
+            qtpurchasing {
+                depends_lib-append path:lib/pkgconfig/Qt5Purchasing.pc:${qt_port_name}
+            }
+            qtscript {
+                depends_lib-append path:lib/pkgconfig/Qt5Script.pc:${qt_port_name}
+            }
+            qtscxml {
+                depends_lib-append path:lib/pkgconfig/Qt5Scxml.pc:${qt_port_name}
+            }
+            qt3d {
+                depends_lib-append path:lib/pkgconfig/Qt53DCore.pc:${qt_port_name}
+            }
+            qtconnectivity {
+                depends_lib-append path:lib/pkgconfig/Qt5Bluetooth.pc:${qt_port_name}
+            }
+            qtgamepad {
+                depends_lib-append path:lib/pkgconfig/Qt5Gamepad.pc:${qt_port_name}
+            }
+            qtwebview {
+                depends_lib-append path:lib/pkgconfig/Qt5WebView.pc:${qt_port_name}
+            }
+            qttools {
+                depends_lib-append path:lib/pkgconfig/Qt5Designer.pc:${qt_port_name}
+            }
+            qtquickcontrols {
+                depends_lib-append path:libexec/qt5/qml/QtQuick/Controls/libqtquickcontrolsplugin.dylib:${qt_port_name}
+            }
+            qtwebsockets {
+                depends_lib-append path:lib/pkgconfig/Qt5WebSockets.pc:${qt_port_name}
+            }
+            qttranslations {
+                depends_lib-append path:libexec/qt5/translations/qt_ar.qm:${qt_port_name}
+            }
+            mysql-plugin {
+                depends_lib-append path:lib/cmake/Qt5Sql/Qt5Sql_QMYSQLDriverPlugin.cmake:${qt_port_name}
+            }
+            psql-plugin {
+                depends_lib-append path:lib/cmake/Qt5Sql/Qt5Sql_QPSQLDriverPlugin.cmake:${qt_port_name}
+            }
+            sqlite-plugin {
+                depends_lib-append path:lib/cmake/Qt5Sql/Qt5Sql_QSQLiteDriverPlugin.cmake:${qt_port_name}
+            }
+            qtquick1 {
+                depends_lib-append path:lib/pkgconfig/Qt5Declarative.pc:${qt_port_name}
+            }
+            qtenginio {
+                depends_lib-append path:lib/pkgconfig/Enginio.pc:${qt_port_name}
+            }
+            default {
+                return -code error "unknown component"
+            }
+        }
+    }
+}
+
 # no universal binary support in Qt 5
 #     see http://lists.qt-project.org/pipermail/interest/2012-December/005038.html
 #     and https://bugreports.qt.io/browse/QTBUG-24952
@@ -304,7 +487,7 @@
 }
 
 if {![info exists building_qt5]} {
-    depends_lib-append path:lib/pkgconfig/Qt5Core.pc:${qt_name}-qtbase
+    qt5.depends_component-append qtbase
 }
 
 # use PKGCONFIG for Qt discovery in configure scripts
