# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0

name                qt513

categories          aqua
platforms           macosx
maintainers         {mcalhoun @MarcusCalhoun-Lopez} openmaintainer

# for OpenSSLException, see source and header files in src/network/ssl of qtbase
license             {LGPL-3 GPL-3 OpenSSLException}

homepage            https://www.qt.io

version             5.13.2
set middle_name     everywhere
if { ${subport} eq "${name}-qtwebkit" ||
     ${subport} eq "${name}-qtwebkit-examples" ||
     ${subport} eq "${name}-qtwebkit-docs" } {
    version         5.9.1
    set middle_name opensource
}

PortGroup qt5_variables 1.0

# get Qt's version numbers
set branch          [join [lrange [split ${version} .] 0 1] .]
set qt_major        [lindex [split ${version} .] 0]

# see https://wiki.qt.io/New_Features_in_Qt_5.7
compiler.cxx_standard 2011

master_sites        \
    https://download.qt.io/official_releases/qt/${branch}/${version}/submodules

# see https://trac.macports.org/ticket/53952
dist_subdir qt5

# file sizes are significantly smaller using xz
if { ${subport} ne ${name} && ${subport} ne "${name}-docs" } {
    use_xz yes
}

conflicts qt3 qt3-mac
# conflict with all other version of qt5
foreach {qt_test_name qt_test_info} [array get available_qt_versions] {
    if {${name} ne ${qt_test_name}} {
        conflicts-append [lindex ${qt_test_info} 0]
    }
}

# qt513-qtbase replaces qt5-qtbase, so can not conflict
if { ${os.platform} eq "darwin" && ${os.major} == 16 } {
    conflicts-delete qt5-qtbase
}

############################################################################### Modules Not Considered
#
# No qtactiveqt      (Windows Only)
# No qtandroidextras (Android Only)
# No qtwayland       (Linux Only)
# No qtx11extras     (X11 Only)
# No qtwinextras     (Windows Only)
#
###############################################################################

############################################################################### Finding Dependencies
#
# grep -r "%dependencies" *
# find ./ -name sync.profile
#
# grep -r qtCompileTest *
#
# find ./ -name config.tests
#
# find ./ -name Find\*.cmake
#
# grep -r qtHaveModule *
#
# grep -r packagesExist *
#
# port provides `find ./ -name \*.dylib -exec otool -L {} \; | grep /opt/local/libexec/qt5 | cut -d ' ' -f1` | cut -d : -f2 | sort -u
# port provides `find ./ -name \*.dylib -exec otool -L {} \; | grep /opt/local/lib/ | cut -d ' ' -f1` | cut -d : -f2 | sort -u
#
# https://code.qt.io/cgit/qt/qt5.git/tree/.gitmodules?h=5.13
#
###############################################################################

############################################################################### Notes
#
# qtconnectivity checks for bluetooth libraries
#    bluez and bluez_le (Linux Only)
#
# qtconnectivity depends on
#    qtandroidextras    (Android Only)
#
# qtserialport depends on
#    ntddmodm           (Windows Only)
#
# qttools depends on
#    qtactiveqt         (Windows Only)
#
# qtwebkit has tests for libraries in:
#     Tools/qmake/mkspecs/features/configure.prf
#     Tools/qmake/config.tests/
#
###############################################################################

############################################################################### TODO
#
# TODO: possible to trim dependencies of qtbase?
#
###############################################################################

############################################################################### Module Format
#
# "Qt Module Name" {
#     {
#         checksum, rmd160
#         checksum, sha256
#         checksum, size
#     }
#     dependencies, build
#     dependencies, lib
#     dependencies, Qt module name
#     Qt components provided
#     included in "standard" installation of Qt (empty string is no, explanation string is yes)
#     variant overrides
#     revision number
#     license replacement
# }
#
# module info found at https://doc.qt.io/qt-5.13/qtmodules.html
#
###############################################################################
array set modules {
    qt3d {
        {
            44ace68d9969a6aa2f7996383fc6950f56fe462d
            d1b9fc0be82264b5f76c3a5330901dff95c1f2bcb67916c3fac6bad4ff69adc9
            81537856
        }
        ""
        "port:assimp"
        "qtbase qtdeclarative qtimageformats qtgamepad"
        {"Qt 3D"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtbase {
        {
            94749bf68e78cf3e3fa84a8df73910b501869293
            26b6b686d66a7ad28eaca349e55e2894e5a735f3831e45f2049e93b1daa92121
            48735704
        }
        ""
        "port:zlib port:libpng path:include/turbojpeg.h:libjpeg-turbo port:freetype path:bin/dbus-daemon:dbus path:lib/pkgconfig/glib-2.0.pc:glib2 path:lib/pkgconfig/icu-uc.pc:icu port:pcre2 path:lib/pkgconfig/harfbuzz.pc:harfbuzz port:double-conversion"
        ""
        {"Qt Core" "Qt GUI" "Qt Network" "Qt SQL" "Qt Test" "Qt Widgets" "Qt Concurrent" "Qt D-Bus" "Qt OpenGL" "Qt Platform Headers" "Qt Print Support" "Qt XML"}
        ""
        "variant overrides: "
        "revision 12"
        "License: "
    }
    qtcharts {
        {
            37bc5548361ff55cefcc472e9eddd25826119992
            3bad81c3cfb32cf72fb0ce2ac2794d031cf78a3902b4715f89c09b2d0e041e87
            4244092
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Charts"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtconnectivity {
        {
            5001272b8ba4bf8ca0699a71a160bf99808dcc51
            644e234eb64f602ef5cdeb8802ef9b58e75478f1124f001937275d62ed692e17
            2777504
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Bluetooth" "Qt NFC"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtdatavis3d {
        {
            f9b6d842aed45aa64a51edd39721e7a76332f52c
            ecf934c3eb07173505781734f95cfc2cf9d89e6b1b98516ac98a137797f4ef28
            5182216
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Data Visualization"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtdeclarative {
        {
            0ffe0f6c54cfb6ae8d05b9d1023a2b29bcf62c25
            d9a524f45fe9e136cda2252f9d7013ec17046d907e3f39606db920987c22d1fd
            20940748
        }
        "port:python27"
        ""
        "qtbase qtsvg"
        {"Qt QML" "Qt Quick" "Qt Quick Layouts" "Qt Quick Widgets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtdoc {
        {
            c15972fa405ae8f8cd2dfb77b442168a863de15a
            be09543143d5dca984d2c2d9085751317e7a95b78fdbe9b6a30f2faa03543e46
            5747084
        }
        ""
        ""
        "qtdeclarative qttools qtmultimedia qtquickcontrols qtquickcontrols2 sqlite-plugin"
        {"Qt Reference Documentation"}
        "requires all documentation"
        "variant overrides: ~examples ~tests ~debug noarch ~docs"
        "revision 0"
        "License: "
    }
    qtgamepad {
        {
            7323d110e3090bc189c0b5a05d5737514dc2f4c2
            75164885433e36a90b557aa7f319ff3d6278c3616469f67b42dda6eb8b92e76e
            386668
        }
        ""
        "port:libsdl2"
        "qtbase qtdeclarative"
        {"Qt Gamepad"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtgraphicaleffects {
        {
            5740edaf153498882157449602c74171937a58ab
            297a89bb6c771f849c4ce866e5c98dadf665163b3dab03bc48a58f51424e7e66
            14845928
        }
        ""
        ""
        "qtdeclarative"
        {"Qt Graphical Effects"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtimageformats {
        {
            b9118a89a786418f8227981674a3d1ce80b62713
            6968649fdccc7d633cf9ada25f1bdaf0a2fb45a214d58c2c47896166d4553ac0
            1803796
        }
        ""
        "port:jasper port:libmng port:tiff port:webp"
        "qtbase"
        {"Qt Image Formats"}
        ""
        "variant overrides: "
        "revision 3"
        "License: "
    }
    qtlocation {
        {
            69484f3b23039dfc94a52ef9adac79ade1e1afbb
            5fe4b824d3dc6c800682ff986333ec09edb9c27582066e928b1862b4d58212e3
            6050044
        }
        ""
        "path:lib/pkgconfig/icu-uc.pc:icu port:zlib"
        "qtbase qtdeclarative qtquickcontrols qtquickcontrols2 qtserialport"
        {"Qt Location" "Qt Positioning"}
        ""
        "variant overrides: "
        "revision 7"
        "License: "
    }
    qtlottie  {
        {
            be5e31c30b35d25841c8da4ac3f69884d7779ec5
            af9a10aaa110767ffd88e02cb8ad0a3930ccfeb61f0c125afbf3791136d36253
            82052
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"A QML API for rendering graphics and animations in JSON format"}
        "technology preview module"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtmacextras {
        {
            b816c248bca3a3576dde2a79f1f124e9e9059694
            545ede0783ebc2405c6efa6ed0b2b7147a1d7e60eb7fcb853abfca0a9cb65ed5
            69164
        }
        ""
        ""
        "qtbase"
        {"Qt Mac Extras"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtmultimedia {
        {
            6b6f99e01c307b260bb1c8d906a27e67e1c81e7b
            5f885459d46ea2f2bb24597678befd4913d8f88029730d01984f51f9c367b9f4
            3820660
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Multimedia" "Qt Multimedia Widgets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtnetworkauth {
        {
            95b35ac7e7d619c71056e5961bbb71de414026c7
            6326943e5c1a3e6eac8dcc44d088f1a3d1c45d14100ff1e95f833b0463243af3
            141172
        }
        ""
        ""
        "qtbase"
        {"Qt Network Authorization"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtpurchasing {
        {
            e4fcdeecfaa2ea185557dd76cd83b7329c74dadc
            e9ef800b7d79f660c72f7024f82a2915b1111b47417f93c3dc2354b9bf307ff4
            208096
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Purchasing"}
        "in-app purchases is of limited value on desktop"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtquickcontrols {
        {
            5c4fa9e83cede4e873c23172502a36d22936062e
            d6e64d8db13ada55fcb0d5e8ca26097085680087d931435c6c6db859d87493ea
            5955448
        }
        ""
        ""
        "qtdeclarative qtgraphicaleffects"
        {"Qt Quick Controls" "Qt Quick Dialogs" "Qt Quick Extras"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtquickcontrols2 {
        {
            75863e6ec38e4e5319efc3a8c4fccfcceeabddf6
            90ee8be7b66cc65f3f22e71a0b35adab5c169ac4f8ebc6f9e7685228bf8a7d70
            9252912
        }
        ""
        ""
        "qtgraphicaleffects qtimageformats"
        {"Qt Quick Controls 2"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtremoteobjects {
        {
            20097626c8ea2d6cf8de9b318e45bd716f597869
            bd24db9d2bb08ae7955291af52ee7510aa193d0f72ed0e3ff74f7cf7e81f4d65
            367032
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Remote Objects"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtscript {
        {
            71e26d3b2ce11bfa51d0748ff2e73983e33c26f3
            3d92691c25577fbf7511d40253de0cd9d31444ea5a0aa860f65efb9fa0f3ce70
            2696452
        }
        ""
        ""
        "qtbase"
        {"Qt Script" "Qt Script Tools"}
        "deprecated in favor QJS* in Qt QML"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtscxml {
        {
            a77625982d484e0ca8a43f986522788f769318f0
            e489dff21e18c1e7f72ffd31af82404f845bd90824c6b978140591366398a9d9
            435428
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt SCXML"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtsensors {
        {
            8e722c909dfdf14df32fbe93e4ec8d227cf13297
            46f73bbfddfd78a419c6424d6839318774b84c955a7f7a96c12c590f6d8fb177
            2046708
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Sensors"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialbus {
        {
            2ff0ed6d010569251cfa0937c74bf7bbf28c1bc4
            48159393b1368976b5324eac48424e2a6e5d63c783194d0576a978151f882da3
            338428
        }
        ""
        ""
        "qtserialport"
        {"Qt Serial Bus"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialport {
        {
            7238c5988a02b6ea2c9d2e01e37614fe362c7e38
            7677ffc1dce3b75c0a98d07d215588a8dccb5c276c55b4ecd60c35ed1c89cb34
            310736
        }
        ""
        ""
        "qtbase"
        {"Qt Serial Port"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtspeech {
        {
            8f41ce8fec774203e5bbf9ef59748d3ae3d42239
            a0aca08feeac6925ead7f841319bdcfd60bfc126de07807edaf267c2ca8d13d5
            100312
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Speech"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtsvg {
        {
            8bc01bd283fe156a09e594ee432c30ee74ce8ab6
            849e2e357e1333b64ecb728352f652cd8d916f029ed848f775b15d0b500cb7d7
            1890380
        }
        ""
        ""
        "qtbase"
        {"Qt SVG"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttools {
        {
            a5b422b534c00437a9adb1cfe4ccb7afd2e8854a
            919a2713b6d2d7873a09ad85bd93cf4282606e5bf84d5884250f665a253ec06e
            9110256
        }
        ""
        "port:clang-9.0"
        "qtbase qtdeclarative"
        {"Qt Designer" "Qt Help" "Qt UI Tools"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttranslations {
        {
            4bd7a4da9034603ec6de5f72aca2e3188720f876
            25755941a2525de2d7ae48e0011d04db7cc09e4e73fe83293206ceafa0aa82d9
            1365880
        }
        "port:qt513-qttools"
        ""
        ""
        {"translation files"}
        ""
        "variant overrides: ~examples ~tests ~debug noarch ~docs"
        "revision 2"
        "License: "
    }
    qtvirtualkeyboard {
        {
            abd0f990c262ac13e7f0e0ad9cc372f9878437d8
            8e368dde237d21666f6cc8512a060e205388e519a4d7559c0505cc75224ed62b
            10923292
        }
        ""
        "port:hunspell"
        "qtbase qtdeclarative qtsvg qtmultimedia qtquickcontrols"
        {"Qt Virtual Keyboard"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtwebchannel {
        {
            624790b4ce487ff62e5ca4db2bdf94aa4f7638bc
            f452d419d8b19b566835f316bbf282dabc8b1c2a74c9c98c1aec4184465e9c13
            192652
        }
        ""
        ""
        "qtbase qtdeclarative qtwebsockets"
        {"Qt WebChannel"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebengine {
        {
            db36dd8fade1ea196e273304855292df46e3f24e
            adcf56b5de6f34744bba2307b696fc75611884f4270e87dfa760d6e99dd711bb
            255593964
        }
        "port:python27 port:py27-ply path:bin/ninja:ninja port:gperf port:bison port:flex"
        "port:fontconfig port:dbus path:lib/pkgconfig/harfbuzz.pc:harfbuzz path:lib/pkgconfig/glib-2.0.pc:glib2 port:zlib port:minizip port:libevent port:libxml2 port:jsoncpp port:protobuf3-cpp path:lib/pkgconfig/poppler.pc:poppler port:pulseaudio path:lib/libavcodec.dylib:ffmpeg port:libopus port:webp port:libpng port:lcms2 port:freetype port:re2 port:snappy"
        "qtdeclarative qtquickcontrols qtquickcontrols2 qtlocation qtwebchannel qttools"
        {"Qt WebEngine"}
        "very large and relatively new"
        "variant overrides: "
        "revision 11"
        "License: "
    }
    qtwebglplugin {
        {
            95daeefa4fe3b98994dcc0218efb6180e472e7ac
            ae369929d5a3356391bccd25cac22846dc44cbb726ce7b60924f53944addabb7
            73608
        }
        ""
        ""
        "qtbase qtwebsockets qtdeclarative"
        {"QPA plugin for running an application via a browser using streamed WebGL commands"}
        "technology preview module; GPLv3 license only"
        "variant overrides: ~docs ~examples"
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtwebkit {
        {
            72cab2a8473801dcbbe8fc41ecf8dc45775f8b18
            28a560becd800a4229bfac317c2e5407cd3cc95308bc4c3ca90dba2577b052cf
            35300628
        }
        "port:python27 port:bison"
        "port:fontconfig path:lib/pkgconfig/icu-uc.pc:icu port:leveldb port:webp port:libxml2 port:libxslt port:zlib port:sqlite3"
        "qtbase qtdeclarative qtlocation qtmultimedia qtsensors qtwebchannel qtxmlpatterns"
        {"Qt WebKit" "Qt WebKit Widgets"}
        "community support only (use Qt WebEngine)"
        "variant overrides: "
        "revision 7"
        "License: "
    }
    qtwebkit-examples {
        {
            21e9ec7d6b6ebe0e96dde0f59324a814b57048bc
            e4ce4de6b468243abad2baabbe8ddfb05d25b186529bfe88cb8662fc983f54d0
            3426504
        }
        ""
        ""
        "qtwebkit qtdeclarative qtlocation qttools qtwebchannel"
        {"examples for Qt WebKit"}
        "community support only (use Qt WebEngine)"
        "variant overrides: ++examples ~docs "
        "revision 1"
        "License: "
    }
    qtwebsockets {
        {
            985bd55daed9c4f99af165986379a40532e477fc
            fc11236c8bf69b27a4e237a27b9ea27fc90d8c25d81d1d451ce3a0b63cbc02d3
            244668
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt WebSockets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebview {
        {
            44df0e0dc744c1018529a5c9b4ac88689e81949c
            394cc7a9cb29b7883816ff3137b022f30568151aada0739fc99a23789c21c015
            131168
        }
        ""
        ""
        "qtdeclarative qtwebengine"
        {"Qt WebView"}
        "new in 5.6.0; requires qtwebengine"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtxmlpatterns {
        {
            d0be90cf4c12d789731b5a1cbdb86992c0b811ad
            1e87d8c004ec0beea3b29d13f0964c563aa9fae51c392c998750ca4e386374e2
            1392192
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt XML Patterns"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
}

############################################################################### SQL Plugin Format
#
# {
#     variant name
#     dependency, library
#     include directory
#     library directory
#     library name
#     obsolete? (empty string is no)
# }
#
###############################################################################
array set sql_plugins {
    {sqlite SQLite "revision 0"} {
        {
            "sqlite3"
            "port:sqlite3"
            "${prefix}/include"
            "${prefix}/lib"
            "-lsqlite3"
            ""
        }
    }
    {psql PostgreSQL "revision 0"} {
        {
            "postgresql16"
            "port:postgresql16"
            "${prefix}/include/postgresql16"
            "${prefix}/lib/postgresql16"
            "-lpq"
            ""
        }
    }
    {mysql MySQL "revision 0"} {
        {
            "mariadb55"
            "port:mariadb"
            "${prefix}/include/mariadb/mysql"
            "${prefix}/lib/mariadb/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mysql57"
            "port:mysql57"
            "${prefix}/include/mysql57/mysql"
            "${prefix}/lib/mysql57/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mysql56"
            "port:mysql56"
            "${prefix}/include/mysql56/mysql"
            "${prefix}/lib/mysql56/mysql"
            "-lmysqlclient_r"
            ""
        }
    }
}

# because CPATH is set, pkgconfig does not include ${prefix}/lib and ${prefix}/include even when requested
# this means that files in ${prefix}/lib and ${prefix}/include are the *last* to be found
# this causes problems when there are files in ${worksrpath} that are unintentionally found instead
configure.env-append \
    PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 \
    PKG_CONFIG_ALLOW_SYSTEM_LIBS=1
build.env-append \
    PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 \
    PKG_CONFIG_ALLOW_SYSTEM_LIBS=1

if { ${os.platform} eq "darwin" && ${os.major} < 16 } {
    known_fail  yes
    pre-fetch {
        ui_error "${subport} requires macOS 10.12 or later"
        return -code error "incompatible OS version"
    }
}

foreach {module module_info} [array get modules] {

    set revision_string [string trim [lindex ${module_info} 7]]
    set revision_string [string range ${revision_string} 1+[string last " " ${revision_string}] end]

    subport ${name}-${module} {

        distname        ${module}-${middle_name}-src-${version}

        revision ${revision_string}

        # see https://bugreports.qt.io/browse/QTBUG-35514
        build.target

        checksums                                        \
            rmd160  [lindex [lindex ${module_info} 0] 0] \
            sha256  [lindex [lindex ${module_info} 0] 1] \
            size    [lindex [lindex ${module_info} 0] 2]

        set license_string [lindex ${module_info} 8]
        set license_string [string range ${license_string} 1+[string first ":" ${license_string}] end]
        set license_string [string trim  ${license_string}]

        if { ${license_string} ne "" } {
            license {*}${license_string}
        }

        foreach deps [lindex ${module_info} 1] {
            depends_build-append [subst ${deps}]
        }

        foreach deps [lindex ${module_info} 2] {
            depends_lib-append [subst ${deps}]
        }

        foreach qtdeps [lindex ${module_info} 3] {
            if { ${qtdeps} ne "qtbase" } {
                depends_lib-append port:${name}-${qtdeps}
            }
        }

        description       Tools and Module(s) for Qt Tool Kit ${qt_major}

        set modules_provided_list [lindex ${module_info} 4]
        if { [llength ${modules_provided_list}] == 1 } {
            set modules_provided_join [lindex ${modules_provided_list} 0]
        } elseif { [llength ${modules_provided_list}] == 2 } {
            set modules_provided_join [join ${modules_provided_list} " and "]
        } else {
            set modules_provided_join [join [list [join [lrange ${modules_provided_list} 0 end-1] ", "] [lindex ${modules_provided_list} end]] ", and "]
        }

        long_description  "Tools and Module(s) for Qt Tool Kit ${qt_major}: ${modules_provided_join}"

        if { ${module} eq "qtbase" } {
            # this subport uses configure script (NOT qmake)
            PortGroup           qt5 1.0
            PortGroup           developerversion 1.0

            if { ${os.platform} ne "darwin" } {
                pre-fetch {
                    ui_warn "${subport} is untested on \"${os.platform}\"."
                }
            }

            if { [variant_isset universal] } {
                pre-fetch {
                    ui_warn "Multiple architectures is not a Reference Configuration for Qt."
                    ui_warn "See https://doc.qt.io/qt-5/supported-platforms.html#reference-configurations"
                }
            }

            # see https://trac.macports.org/ticket/59315
            minimum_developerversions 16 9

            # Neither "pkg-config --cflags Qt5Core" nor "pkg-config --cflags-only-I Qt5Core" will allow
            #     a configure script to find CPPFLAGS since the include mechanism relies on
            #     -F and -framework BOTH being set
            # See e.g. octave
            # See the files mkspecs/features/qt_module.prf and qmake/generators/makefile.cpp
            patchfiles-append patch-pc_files.diff

            # see https://trac.macports.org/ticket/63805#comment:13
            patchfiles-append patch-sdk-no-stderr.diff

            # Backported upstream patch to build with the macOS 12 SDK.
            # https://github.com/qt/qtbase/commit/dece6f5840463ae2ddf927d65eb1b3680e34a547
            patchfiles-append patch-qiosurfacegraphicsbuffer.h.diff

            # find the Rez program
            patchfiles-append patch-find_rez.diff
            post-patch {
                reinplace \
                    "s|__MACPORTS_Rez__|[exec -ignorestderr xcrun --find Rez]|g" \
                    mkspecs/common/mac.conf \
                    mkspecs/features/mac/rez.prf
            }

            # fix futimens check
            patchfiles-append patch-futimens.diff

            #-----------------------------------------------------------------------------
            # qtbase is used for:
            #    1) building qtbase
            #    2) building MacPorts projects via qmake
            #    3) building end-user projects
            #
            # 1 & 2 require consistency with the MacPorts environment
            # 3 requires consistency with the default Qt installation
            #
            # 2 can be achieved via environment variables
            #    (e.g. QMAKE_MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target})
            #
            # the only way 3 can be achieved is if no changes are made to the build system
            #
            # the following is an attempt to achieve 1 without destroying 3
            #-----------------------------------------------------------------------------

            # save default spec files
            post-extract {
                copy ${worksrcpath}/mkspecs ${worksrcpath}/mkspecs-save
            }

            # ensure qmake builds for the correct architecture
            patchfiles-append patch-qmake_flags.diff
            post-patch {
                if {[variant_exists universal] && [variant_isset universal]} {
                    set arch_replace_cxx [portconfigure::configure_get_universal_archflags]
                    set arch_replace_ld  [portconfigure::configure_get_universal_archflags]
                } else {
                    set arch_replace_cxx [portconfigure::configure_get_archflags cxx]
                    set arch_replace_ld  [portconfigure::configure_get_ld_archflags]
                }
                reinplace \
                    "s|__MACPORTS_CXX_ARCH_FLAGS__|${arch_replace_cxx}|g" \
                    ${worksrcpath}/qmake/Makefile.unix.macos
                reinplace \
                    "s|__MACPORTS_LD_ARCH_FLAGS__|${arch_replace_ld}|g" \
                    ${worksrcpath}/qmake/Makefile.unix.macos
            }

            # respect MacPorts build variables
            patchfiles-append patch-mkspecs.diff

            # respect configure.compiler
            post-patch {
                reinplace \
                    "s|__MACPORTS_CC__|${configure.cc}|g" \
                    ${worksrcpath}/mkspecs/common/clang.conf

                reinplace \
                    "s|__MACPORTS_CXX__|${configure.cxx}|g" \
                    ${worksrcpath}/mkspecs/common/clang.conf
            }

            post-patch {
                # respect configure.cxx_stdlib
                reinplace \
                    "s|__MACPORTS_CXX_STDLIB__|${configure.cxx_stdlib}|g" \
                    ${worksrcpath}/mkspecs/common/clang-mac.conf

                # respect macosx_deployment_target
                reinplace \
                    "s|__MACPORTS_DEPLOYMENT_TARGET__|${macosx_deployment_target}|g" \
                    ${worksrcpath}/mkspecs/common/macx.conf

                #respect configure.optflags
                reinplace \
                    "s|__MACPORTS_OPTFLAGS__|${configure.optflags}|g" \
                    ${worksrcpath}/mkspecs/common/gcc-base.conf
            }

            # respect configure.sdk_version
            post-patch {
                reinplace \
                    "s|__MACPORTS_MAC_SDK__|${qt5.mac_sdk}|g" \
                    ${worksrcpath}/mkspecs/common/macx.conf
            }

            # respect configure.universal_archs or build_arch
            post-patch {
                if {[variant_exists universal] && [variant_isset universal]} {
                    set arch_replace ${configure.universal_archs}
                } else {
                    set arch_replace ${build_arch}
                }
                reinplace \
                    "s|__MACPORTS_DEVICE_ARCHS__|${arch_replace}|g" \
                    ${worksrcpath}/mkspecs/common/macx.conf
            }

            # use MacPorts X11
            post-patch {
                foreach spec {macx-clang} {
                    reinplace \
                        "s|__MACPORTS_PREFIX__|${prefix}|g" \
                        ${worksrcpath}/mkspecs/${spec}/qmake.conf
                }
            }

            # see https://bugreports.qt.io/browse/QTBUG-72903
            post-configure {
                foreach del {QMAKE_LIBS_OPENGL QMAKE_INCDIR_OPENGL} {
                    reinplace \
                        "/${del}\ =/d" \
                        ${worksrcpath}/src/gui/qtgui-config.pri
                }
            }
            post-destroot {
                foreach del {QMAKE_LIBS_OPENGL QMAKE_INCDIR_OPENGL} {
                    reinplace \
                        "/${del}\ =/d" \
                        ${destroot}${qt_mkspecs_dir}/modules/qt_lib_gui_private.pri
                }
            }

            # return modified spec files to the default values
            post-build {
                foreach conf {clang.conf macx.conf clang-mac.conf gcc-base.conf} {
                    move -force ${worksrcpath}/mkspecs-save/common/${conf} ${worksrcpath}/mkspecs/common/${conf}
                }
                foreach spec {macx-clang macx-g++ macx-icc} {
                        move -force ${worksrcpath}/mkspecs-save/${spec}/qmake.conf ${worksrcpath}/mkspecs/${spec}/qmake.conf
                    }
                }

            # --prefix is not recognized.
            configure.pre_args-delete       --prefix=${prefix}

            # --disable-dependency-tracking is not recognized.
            configure.universal_args-delete --disable-dependency-tracking

            # Installation options:
            #-extprefix     SYSROOT/PREFIX
            #-hostprefix    EXTPREFIX
            #-libexecdir    ARCHDATADIR/libexec
            #-hostbindir    HOSTPREFIX/bin
            #-hostlibdir    HOSTPREFIX/lib
            configure.args-append                      \
                -prefix         ${qt_dir}              \
                -bindir         ${qt_bins_dir}         \
                -headerdir      ${qt_includes_dir}     \
                -libdir         ${qt_libs_dir}         \
                -archdatadir    ${qt_archdata_dir}     \
                -plugindir      ${qt_plugins_dir}      \
                -importdir      ${qt_imports_dir}      \
                -qmldir         ${qt_qml_dir}          \
                -datadir        ${qt_data_dir}         \
                -docdir         ${qt_docs_dir}         \
                -translationdir ${qt_translations_dir} \
                -sysconfdir     ${qt_sysconf_dir}      \
                -examplesdir    ${qt_examples_dir}     \
                -testsdir       ${qt_tests_dir}        \
                -hostdatadir    ${qt_host_data_dir}

            # Configure options:
            configure.args-append \
                -release          \
                -opensource       \
                -confirm-license  \
                -shared           \
                -accessibility

            foreach driver { db2 ibase mysql oci odbc psql sqlite sqlite2 tds } {
                configure.args-append -no-sql-${driver}
            }

                configure.args-append "-platform ${qt_qmake_spec}"

            # use -Os instead of -O2
            configure.args-append -optimize-size

            configure.args-append \
                -no-testcocoon    \
                -no-gcov          \
                -force-pkg-config

            # turn off CFG_CLOEXEC if gnulib is installed

            # Third Party Libraries:
            configure.args-append   \
                -system-zlib        \
                -no-mtdev           \
                -no-journald        \
                -no-syslog          \
                -system-libpng      \
                -system-libjpeg     \
                -system-freetype    \
                -system-harfbuzz    \
                -no-openssl         \
                -no-libproxy        \
                -system-pcre        \
                --disable-xcb       \
                -glib               \
                -no-gtk

            # from the configure script:
            #     NOTE: -optimized-tools is not useful in -release mode.

            # http://lists.qt-project.org/pipermail/development/2017-January/028392.html
            # Prefer ICU over iconv
            # ICU requires C++17
            compiler.cxx_standard   2017
            configure.args-append   QMAKE_CXXFLAGS_GNUCXX11=-std=c++17

            # Additional options:
            configure.args-append       \
                {-make libs}            \
                {-make tools}           \
                {-nomake examples}      \
                {-nomake tests}         \
                -gui                    \
                -widgets                \
                -no-rpath               \
                -verbose                \
                -cups                   \
                -no-iconv               \
                -no-evdev               \
                -no-tslib               \
                -icu                    \
                -no-fontconfig          \
                -strip                  \
                -no-pch                 \
                -no-ltcg                \
                -dbus-linked            \
                -no-use-gold-linker     \
                -no-separate-debug-info \
                -no-xcb                 \
                -no-eglfs               \
                -no-gbm                 \
                -no-directfb            \
                -no-linuxfb             \
                -no-kms                 \
                -no-mirclient           \
                -no-libinput            \
                -no-system-proxies

            # do not opportunistically enable Vulkan support
            # (TODO: is Vulkan support desirable?)
            # see https://trac.macports.org/ticket/62104
            configure.args-append \
                -no-feature-vulkan

            # MacOS/iOS options:
            configure.args-append    \
                -framework           \
                -securetransport

            # do not use ${configure.sdk_version}
            # SDK is recorded in ${qt_mkspecs_dir}/qdevice.pri
            # SDK may then be removed if Xcode is updated
            # see https://trac.macports.org/ticket/54044
            # see https://trac.macports.org/ticket/55195
            configure.args-append \
                -sdk macosx

            # configure options that don't show up in configure --help
            configure.args-append \
                -no-libudev       \
                -no-egl

            # Qt builds part of the system using environment provided by MacPorts.
            # It builds the rest using its own internal environment.
            # For consistency, clear MacPorts environment.
            #configure.cxx_stdlib
            proc portconfigure::should_add_stdlib {} {return no}
            proc portconfigure::should_add_cxx_abi {} {return no}
            compiler.limit_flags yes
            configure.cc_archflags
            configure.cxx_archflags
            configure.objc_archflags
            configure.objcxx_archflags
            configure.ld_archflags
            configure.cppflags
            configure.cflags
            configure.cxxflags
            configure.objcflags
            configure.objcxxflags
            configure.ldflags
            configure.pipe  no
            configure.march
            configure.mtune
            configure.universal_ldflags
            configure.universal_cflags
            configure.universal_cxxflags
            configure.universal_cppflags

            # configure script looks for perl but doesn't seem to use it for our configuration

            # configure script uses gawk if it can find it,
            #    so require it for consistency
            depends_build-append port:gawk

            # unless overridden, configure script uses gmake if it can find it
            configure.env-append MAKE=${build.cmd}

            variant tests description {Enable tests} {
                configure.args-replace {-nomake tests} {-make tests}
            }

            variant examples description {Build examples} {
                configure.args-replace {-nomake examples} {-make examples}
            }

            variant debug description {Build both release and debug libraries} {
                configure.args-replace -release -debug-and-release
            }

            variant openssl description {Use OpenSSL instead of Secure Transport} {
                configure.args-delete -securetransport -no-openssl
                configure.args-append -openssl-linked

                # see https://trac.macports.org/ticket/51358
                #     for why not a path dependency
                depends_lib-append path:lib/libssl.dylib:openssl
            }
            default_variants-append +openssl

        } else {
            # these subports use qmake
            PortGroup            qmake5 1.0

            # determine which variants are to be turned off
            set request_examples true
            set request_tests    true
            set def_var          ""

            if { [lsearch -exact [lindex ${module_info} 6] "~universal"] != -1 } {
                universal_variant no
            }

            if { [lsearch -exact [lindex ${module_info} 6] "noarch"] != -1 } {
                supported_archs   noarch
                platforms         any
            }

            if { [lsearch -exact [lindex ${module_info} 6] "~debug"] != -1 } {
                qt5.debug_variant no
            }

            if { [lsearch -exact [lindex ${module_info} 6] "~examples"] != -1 } {
                set request_examples false
            }

            if { [lsearch -exact [lindex ${module_info} 6] "~tests"] != -1 } {
                set request_tests    false
            }

            if { [lsearch -exact [lindex ${module_info} 6] "++examples"] != -1 } {
                set request_examples true
                lappend def_var "+examples"
            }

            if { ${request_examples} } {
                variant examples description {Build examples} {}
            }

            if { ${request_tests} } {
                variant tests description {Enable tests} {}
            }

            if { ${def_var} ne "" } {
                default_variants-append ${def_var}
            }

            # accommodating variant request varies depending on how qtbase was built
            pre-configure {

                # determine if qmake builds examples by default (set via variants)
                if {[active_variants ${name}-qtbase examples ""]} {
                    set base_examples true
                } else {
                    set base_examples false
                }

                # determine if qmake runs tests by default (set via variants)
                if {[active_variants ${name}-qtbase tests ""]} {
                    set base_tests true
                } else {
                    set base_tests false
                }

                # determine if the user wants to build examples
                if { [variant_exists examples] && [variant_isset examples] } {
                    set this_examples true
                } else {
                    set this_examples false
                }

                # determine if the user wants to run tests
                if { [variant_exists tests] && [variant_isset tests] } {
                    set this_tests true
                } else {
                    set this_tests false
                }

                # determine if qmake's default and user requests are compatible; override qmake if necessary
                if { ${this_examples} && !${base_examples}  } {
                    configure.args-append "QT_BUILD_PARTS+=\"examples\""
                }

                if { !${this_examples} && ${base_examples}  } {
                    configure.args-append "QT_BUILD_PARTS-=\"examples\""
                }

                if { ${this_tests} && !${base_tests}  } {
                    configure.args-append "QT_BUILD_PARTS+=\"tests\""
                }

                if { !${this_tests} && ${base_tests}  } {
                    configure.args-append "QT_BUILD_PARTS-=\"tests\""
                }
            }

            ###############################################################################
            # Special Cases
            ###############################################################################

            # special case
            if { ${module} eq "qtdoc" } {
                build.target    docs
                destroot.target install_docs

                # in the process of building the html files, qtdoc looks for all installed modules
                #    with no clear way to select only a subset
                # unless a way is found, ReproducibleBuilds (https://trac.macports.org/wiki/ReproducibleBuilds)
                #    requires qtdoc depend on all the documentation modules
                foreach {module_doc module_doc_info} [array get modules] {
                    if { [lsearch -exact [lindex ${module_doc_info} 6] "~docs"] == -1 } {
                        depends_lib-append port:${name}-${module_doc}-docs
                    }
                }
            }

            # special case
            if { ${module} eq "qtquickcontrols2" } {
                # attempts to link against installed version, which causes an error when upgrading 5.8.0->5.9.1
                qt5.frameworkpaths-append ${worksrcpath}/lib
            }

            # special case
            if { ${module} eq "qtdeclarative" } {
                # https://trac.macports.org/ticket/62806
                # error: typedef redefinition with different types ('char16_t' vs 'unsigned short')
                patchfiles-append  patch-qtdeclarative-masm.diff

                # attempts to link against installed version, which causes an error when upgrading 5.9.3->5.10.0
                qt5.frameworkpaths-append ${worksrcpath}/lib
            }

            # special case
            if { ${module} eq "qtlocation" } {
                PortGroup       conflicts_build 1.0

                # do not allow ${prefix}/include/boost to conflict with bundled boost (in bundled mapbox-gl-native)
                conflicts_build boost

                # https://trac.macports.org/ticket/68508
                # do not allow ${prefix}/include/rapidjson to conflict with bundled rapidjson (in bundled mapbox-gl-native)
                conflicts_build rapidjson rapidjson-devel

                # https://trac.macports.org/ticket/67417
                # include/mbgl/util/unique_any.hpp: error: no member named 'move' in namespace 'std'
                patchfiles-append  patch-qtlocation-mbgl-unique_any.hpp.diff

                # fix compiling with C++17
                patchfiles-append  patch-qtlocation-cxx17.diff

                # qtlocation uses
                #    Gypsy (https://gypsy.freedesktop.org/wiki/)
                #    if they can be found
                # Ensure that test fails even if software is installed
                post-patch {
                    foreach test { gypsy } {
                        reinplace "s|return 0;|return 0;\\\n#error turn off test|g" ${worksrcpath}/config.tests/${test}/main.cpp
                    }
                }

                # avoid unnecessary dependency on OpenSSL
                configure.args-append "QMAKE_LIBS_OPENSSL="

                # ICU requires C++17
                compiler.cxx_standard   2017
                configure.args-append   QMAKE_CXXFLAGS_CXX14=-std=c++17
                configure.args-append   QMAKE_CXXFLAGS+=-Wno-error=enum-constexpr-conversion
            }

            # special case
            if { ${module} eq "qtwebkit" } {

                # use MacPorts icu
                #
                # qmake uses pkgconfig to look for icu
                # this feature does not work without "CONFIG += link_pkgconfig"
                patchfiles-append patch-icu.diff

                # QtWebKit is supported by the community and
                #    relies on "implementation detail[s]," which
                #    "may change from version to version"
                patchfiles-append patch-qtwebkit_internal_api.diff

                # find OpenGL headers
                patchfiles-append patch-qtwebkit_opengl_location.diff

                # see https://bugs.webkit.org/show_bug.cgi?id=171612
                patchfiles-append patch-qtwebkit_fix_icu.diff

                # backport upstream WebKit changes to avoid
                # error: "Unsupported PAGE_SHIFT amount"
                # see https://trac.macports.org/ticket/62027#comment:49
                patchfiles-append patch-qtwebkit_fix_page_shift.diff

                # work around https://trac.macports.org/ticket/60975
                patchfiles-append patch-webkit_XPathGrammar.y.diff
                patchfiles-append patch-webkit_makegrammar.pl.diff

                # fix compiling with C++17
                patchfiles-append patch-qtwebkit-cxx17.diff

                # qtwebkit uses
                #    glx
                #    libXcomposite
                #    libXrender
                #    if they can be found
                # Ensure that test fails even if software is installed
                post-patch {
                    foreach test { glx libXcomposite libXrender } {
                        reinplace "s|return 0;|return 0;\\\n#error turn off test|g" ${worksrcpath}/Tools/qmake/config.tests/${test}/${test}.cpp
                    }
                }

                # ICU requires C++17
                compiler.cxx_standard   2017
                configure.args-append   QMAKE_CXXFLAGS+=-Wno-error=register
            }

            # special case
            if { ${module} eq "qtwebkit-examples" } {
                # in Qt 5.12, qmake changed how it generated Makefiles
                patchfiles-append patch-qtwebkit-examples_header_location.diff
            }

            # special case
            if { ${module} eq "qttools" } {
                # prevent qttools from opportunistically using qtwebkit if it can be found
                patchfiles-append patch-no_qtwebkit.diff

                variant qtwebkit description {build with QtWebkit support} {
                    patchfiles-delete patch-no_qtwebkit.diff
                    depends_lib-append port:${name}-qtwebkit
                }

                post-extract {
                    # these folders are installed and conflict with ${name}-qttools-docs
                    delete ${worksrcpath}/examples/assistant/simpletextviewer/documentation
                    delete ${worksrcpath}/examples/help/contextsensitivehelp/docs
                }

                # see #44934 (and #35067 for the qt4-mac version)
                set framework_list [split ${qt_frameworks_dir} '/']
                set qt_list        [split ${qt_dir} '/']

                while {[llength ${qt_list}] && [llength ${framework_list}]} {
                    set var_qt        [lindex $qt_list        0]
                    set var_framework [lindex $framework_list 0]

                    if { ${var_qt} ne ${var_framework} } {
                        break
                    }

                    # remove first element from list
                    set qt_list        [lreplace ${qt_list}        0 0]
                    set framework_list [lreplace ${framework_list} 0 0]
                }
                set libreplace [string repeat ../ [llength ${qt_list}]][join ${framework_list} /]
                if { ${libreplace} ne "lib" } {
                    patchfiles-append patch-shared.diff
                    post-patch {
                        reinplace "s|__MACPORTS_FRAMWORK_DIR__|${libreplace}|g" ${worksrcpath}/src/macdeployqt/shared/shared.cpp
                    }
                }

                # qdoc now uses libclang for parsing C/C++ code
                # see https://wiki.qt.io/New_Features_in_Qt_5.11
                configure.env-append LLVM_INSTALL_DIR=${prefix}/libexec/llvm-9.0
            }

            # special case
            if { ${module} eq "qtwebengine" } {
                PortGroup           conflicts_build 1.0
                # see https://doc.qt.io/qt-5/qtwebengine-platform-notes.html
                # see https://trac.macports.org/ticket/60080
                PortGroup           xcodeversion 1.0
                minimum_xcodeversions   {16 9.0}
                if { ${os.platform} eq "darwin" && ${os.major} < 16 } {
                    known_fail  yes
                    pre-fetch {
                        ui_error "${subport} requires macOS 10.12 or later"
                        return -code error "incompatible OS version"
                    }
                }

                # UsingTheRightCompiler (https://trac.macports.org/wiki/UsingTheRightCompiler)
                build.env-append      CXX=${configure.cxx}
                build.env-append      CC=${configure.cc}
                build.env-append      SAMUFLAGS=-j${build.jobs}
                configure.args-append QMAKE_LINK=${configure.cxx}

                # avoid
                #     Undefined symbols for architecture x86_64
                conflicts_build-append ${subport}

                # either have VNRequest forward declarations or include correct header file
                patchfiles-append     patch-qtwebengine_forward_declarations.diff

                # do not opportunistically find MacPorts libraries (e.g. X11 libraries)
                patchfiles-append patch-qtwebengine_tests.diff

                # see https://trac.macports.org/ticket/61922
                patchfiles-append patch-rule_bison.py.diff

                # see https://trac.macports.org/ticket/61924
                patchfiles-append patch-qtwebengine_sdk.diff

                # chromium/base/process/process_metrics_mac.cc assumes PAGE_SIZE is a constexpr
                patchfiles-append patch-qtwebengine_chromium_static_page_size.diff

                # backport harfbuzz fix needed for Xcode 13.3 clang
                # error: variable 'supp_size' set but not used [-Werror,-Wunused-but-set-variable]
                patchfiles-append patch-qtwebengine_harfbuzz_no_unused_variable.diff

                # Fix build with gperf ≥ 3.2
                # See also https://trac.macports.org/ticket/72447
                patchfiles-append gperf.patch

                # see https://trac.macports.org/ticket/59294
                use_xcode yes

                configure.post_args-append         \
                    --                             \
                    -webengine-native-spellchecker \
                    -webengine-kerberos \
                    -system-webengine-icu \
                    -system-webengine-ffmpeg
            }

            # special case
            if { ${module} eq "qtvirtualkeyboard" } {
                # do not opportunistically use xcb
                patchfiles-append patch-qtvirtualkeyboard_noxcb.diff
            }

            # special case
            if { ${module} eq "qtmultimedia" } {

                # In the file config.tests/openal/openal.pro, defining HEADER_OPENAL_PREFIX means
                #    include directives use macOS OpenAL Framework.
                # In src/multimedia/configure.json, pkgconfig is relied upon, which finds MacPorts openal
                #    for linking.
                # We must choose one or the other.
                # see https://trac.macports.org/ticket/54592
                patchfiles-append patch-qtmultimedia_no_macports_openal.diff

                variant pulseaudio description {Compile PulseAudio support} {
                    depends_lib-append port:pulseaudio
                }

                variant gstreamer description {Compile GStreamer support} {
                    depends_lib-append port:gstreamer1 port:gstreamer1-gst-plugins-base port:gstreamer1-gst-plugins-bad
                }

                # for commands must follow -spec set in PortGroup
                configure.post_args-append --
                if { ![variant_isset pulseaudio] } {
                    configure.post_args-append -no-pulseaudio
                }
                if { ![variant_isset gstreamer] } {
                    configure.post_args-append -no-gstreamer
                }
            }

            # special case
            if { ${module} eq "qt3d" } {
                # the following libraries, even when available, do not seem to affect the build
                # so turn them off
                configure.post_args-append  \
                    --                      \
                    -system-assimp
            }

            # special case
            if { ${module} eq "qtwebkit-examples" && ![variant_isset examples] } {
                # create a dummy file so the port can be successfully activated
                post-destroot {
                    xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${subport}
                    set docfile [open ${destroot}${prefix}/share/doc/${subport}/README.txt "w"]
                    puts ${docfile} "Without the examples variant, no examples are installed"
                    puts ${docfile} "${long_description}"
                    close ${docfile}
                }
            }

            # special case
            if { ${module} eq "qtscript" } {
                # see https://trac.macports.org/ticket/54453
                patchfiles-append patch-qtscript_ceil.diff

                # See https://code.qt.io/cgit/qt/qt5.git/commit/?id=758d922716ebdedeaa6fa26369c8dbb9dff4bae4
                if {[variant_isset examples]} {
                    depends_lib-append  port:${name}-qttools
                }
            }

            # special case
            if { ${module} eq "qtimageformats" } {
                # https://trac.macports.org/ticket/65821
                patchfiles-append patch-qtimageformats-qjp2handler.cpp.diff
            }
        }
    }

    if { [lsearch -exact [lindex ${module_info} 6] "~docs"] == -1 } {

        subport ${name}-${module}-docs {

            supported_archs   noarch
            installs_libs     no
            platforms         any

            distname        ${module}-${middle_name}-src-${version}

            revision ${revision_string}

            description       Documentation for the port ${name}-${module}
            long_description  ${description}

            build.target    docs
            destroot.target install_docs

            checksums                                        \
                rmd160  [lindex [lindex ${module_info} 0] 0] \
                sha256  [lindex [lindex ${module_info} 0] 1] \
                size    [lindex [lindex ${module_info} 0] 2]

            depends_build-append            \
                port:${name}-qttools        \
                port:${name}-sqlite-plugin

            # depend on the modules for which this subport provides documentation
            # also have the same build dependencies
            depends_build-append port:${name}-${module}
            foreach deps [lindex ${module_info} 1] {
                depends_build-append [subst ${deps}]
            }

            PortGroup           qmake5 1.0

            qt5.debug_variant   no

            # special cases
            if { ${module} eq "qttools" } {
                post-extract {
                    # generated makefiles assume full Qt was built locally
                    xinstall -d -m 0755             ${worksrcpath}/bin/
                    foreach bin {qdoc qhelpgenerator qtattributionsscanner} {
                        ln -s ${qt_bins_dir}/${bin} ${worksrcpath}/bin/
                    }
                }
            } elseif { ${module} eq "qtwebengine" } {
                # UsingTheRightCompiler (https://trac.macports.org/wiki/UsingTheRightCompiler)
                build.env-append      CXX=${configure.cxx}
                build.env-append      CC=${configure.cc}
                configure.args-append QMAKE_LINK=${configure.cxx}
                # see https://trac.macports.org/ticket/59294
                use_xcode yes
                # see https://trac.macports.org/ticket/61924
                patchfiles-append patch-qtwebengine_sdk.diff
            } elseif { ${module} eq "qtwebkit" } {
                post-extract {
                    # without this file, the makefile ${worksrcpath}/qtwebkit/Source/WebCore/Makefile.WebCore.Target
                    #    keeps generating itself over and over again
                    # this file is only created when the library is being built, however
                    xinstall -d -m 0755 ${worksrcpath}/Source/WebCore/generated
                    touch ${worksrcpath}/Source/WebCore/generated/InspectorBackendCommands.qrc
                }
            }
        }
    }
}

# see https://doc.qt.io/qt-5/sql-driver.html for info on building SQL Database Drivers
foreach {sql_names sql_info} [array get sql_plugins] {
    set driver          [lindex ${sql_names} 0]
    set dbms            [lindex ${sql_names} 1]
    set revision_string [lindex ${sql_names} 2]
    set revision_string [string range ${revision_string} 1+[string last " " ${revision_string}] end]

    subport ${name}-${driver}-plugin {

        PortGroup qmake5 1.0

        distname        qtbase-${middle_name}-src-${version}

        revision ${revision_string}

        checksums                                          \
            rmd160  [lindex [lindex $modules(qtbase) 0] 0] \
            sha256  [lindex [lindex $modules(qtbase) 0] 1] \
            size    [lindex [lindex $modules(qtbase) 0] 2]

        description       ${dbms} Database Driver for Qt Tool Kit ${qt_major}
        long_description  ${dbms} Database Driver for Qt Tool Kit ${qt_major}

        # special case
        if {${driver} eq "sqlite"} {
        patchfiles-append patch-sql_plugins.diff
        }

        # if there is more than one version of the database system, create variants for each version
        if { [llength ${sql_info}] > 1 } {

            set any_variant_set false

            foreach variant_info ${sql_info} {

                set varName [lindex ${variant_info} 0]

                # find every other variant so it can be marked as conflicting
                set conflicts_list ""
                foreach variant_info2 ${sql_info} {
                    set varName2 [lindex ${variant_info2} 0]

                    if { ${varName} ne ${varName2} } {
                        lappend conflicts_list ${varName2}
                    }
                }

                # get only the numbers from the name
                regexp {[0-9].} ${varName} varVer

                variant ${varName} conflicts ${conflicts_list} description "use version ${varVer} of ${dbms}" {}

                # check if any variant has been set
                if { [variant_isset ${varName}] } {
                    set any_variant_set true
                }
            }

            # ensure at least one variant is set
            if { !${any_variant_set} } {
                default_variants-append +[lindex [lindex ${sql_info} 0] 0]
            }
        }

        # as of Qt 5.8, QMAKE_USE is used to find external libraries
        set use_name [string toupper ${driver}]

        foreach variant_info ${sql_info} {
            set varName [lindex ${variant_info} 0]

            if { ( [variant_exists ${varName}] && [variant_isset ${varName}] ) || [llength ${sql_info}]==1 } {
                depends_lib-append [lindex ${variant_info} 1]

                configure.args-append                                                                \
                    [subst QMAKE_INCDIR_${use_name}+=\"[lindex ${variant_info} 2]\"]  \
                    [subst QMAKE_LIBDIR+=\"[lindex ${variant_info} 3]\"]  \
                    [subst QMAKE_LIBS_${use_name}+=\"[lindex ${variant_info} 4]\"]
            }
        }

        # see https://trac.macports.org/ticket/53248
        # N.B.: changing worksrcdir must come *after* changing qt5.top_level
        qt5.top_level ${worksrcpath}/src/plugins/sqldrivers

        worksrcdir ${worksrcdir}/src/plugins/sqldrivers/${driver}
    }
}

subport ${name}-docs {
    # meta-port to install documentation for various modules

    revision          0

    description       Documentation for Qt Tool Kit ${qt_major}
    long_description  {*}${description}

    master_sites
    distfiles
    use_configure     no
    supported_archs   noarch
    installs_libs     no
    platforms         any

    build {}

    # create a dummy file so the port can be successfully activated
    destroot {
        xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${subport}
        set docfile   [open ${destroot}${prefix}/share/doc/${subport}/README.txt "w"]
        puts ${docfile} "Meta-port for ${name}"
        puts ${docfile} "${long_description}"
        close ${docfile}
    }

    foreach {module module_info} [array get modules] {
        if { [lindex ${module_info} 5] eq "" } {
            if { [lsearch -exact [lindex ${module_info} 6] "~docs"] == -1 } {
                depends_run-append port:${name}-${module}-docs
            }
        }
    }
}

if { ${subport} eq ${name} } {
    # the main port is Meta-port to install various modules

    revision 0

    description         Qt Tool Kit ${qt_major}
    long_description    Qt Tool Kit: A cross-platform framework \
        (headers, data, and libraries) for writing \
        cross-platform GUI-based applications.

    master_sites
    distfiles
    use_configure     no
    supported_archs   noarch
    installs_libs     no
    platforms         any

    build {}

    # create a dummy file so the port can be successfully activated
    destroot {
        xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${subport}
        set docfile   [open ${destroot}${prefix}/share/doc/${subport}/README.txt "w"]
        puts ${docfile} "Meta-port for ${name}"
        puts ${docfile} "${long_description}"
        close ${docfile}
    }

    foreach {module module_info} [array get modules] {
        if { [lindex ${module_info} 5] eq "" } {
            depends_run-append port:${name}-${module}
        }
    }

    depends_run-append port:${name}-sqlite-plugin
}

set python_framework ""
set depends_check ""
if { [info exists depends_build] } {
    set depends_check "${depends_check} ${depends_build}"
}
if { [info exists depends_lib] } {
    set depends_check "${depends_check} ${depends_lib}"
}

foreach deps ${depends_check} {
    if { [string first ":python27" ${deps}] >= 0 } {
        # If Qt components use Python, ensure that MacPorts python27 is used
        #
        # a better solution would be to force components to use a specific python program
        # how to accomplish such a thing is not entirely clear
        #
        # see #49838
        #
        # version 5.5.1 (at least) of qtwebengine must find a python version in the interval [2.7, 3)
        #    (see mkspecs/features/functions.prf)
        # versions 5.5.1 of qtdeclarative and qtwebkit also use python with no way of specifying which one
        #
        set python_framework ${frameworks_dir}/Python.framework/Versions/2.7
    }
}
if { ${python_framework} ne "" } {
    configure.env-append PATH=${python_framework}/bin:$env(PATH)
    build.env-append     PATH=${python_framework}/bin:$env(PATH)
}
unset python_framework

post-destroot {
    # see #44204
    foreach f [glob -nocomplain -tails -directory ${destroot}${qt_libs_dir} *.framework] {
        set framework [file rootname ${f}]

        set include_list   [split ${qt_includes_dir}   '/']
        set framework_list [split ${qt_libs_dir} '/']

        while {[llength ${include_list}] && [llength ${framework_list}]} {
            set var_include   [lindex $include_list   0]
            set var_framework [lindex $framework_list 0]

            if { ${var_include} ne ${var_framework} } {
                break
            }

            # remove first element from list
            set include_list   [lreplace ${include_list} 0 0]
            set framework_list [lreplace ${framework_list} 0 0]
        }

        xinstall -d -m 0755 ${destroot}${qt_includes_dir}
        ln -s [string repeat ../ [llength ${include_list}]][join ${framework_list} /]/${f}/Headers ${destroot}${qt_includes_dir}/${framework}
    }

    # .app and non-.app programs are both put in qt_bins_dir
    # put a link of any .app programs in the ${qt_apps_dir}
    if { ${qt_bins_dir} ne ${qt_apps_dir} } {
        xinstall -d -m 0755 ${destroot}${qt_apps_dir}
        foreach app [glob -nocomplain -tails -directory ${destroot}${qt_bins_dir} *.app] {
            ln -s ${qt_bins_dir}/${app} ${destroot}${qt_apps_dir}
        }
    }

    # put configuration files in places they will be found automatically
    if { ${qt_libs_dir} ne "${prefix}/lib" } {

        # put link to pkgconfig files in place where pkgconfig will find it
        # most Qt 5 pkgconfig files begin with Qt5, so link should not conflict with any other Qt installations
        #    exceptions: Enginio (new in Qt 5.3, removed in Qt 5.7)
        xinstall -d -m 0755 ${destroot}${prefix}/lib/pkgconfig
        foreach pcfile [glob -nocomplain -tails -directory ${destroot}${qt_libs_dir}/pkgconfig *.pc] {
            ln -s ${qt_libs_dir}/pkgconfig/${pcfile} ${destroot}${prefix}/lib/pkgconfig
        }

        # put link to cmake files in place where cmake will find it
        # most Qt 5 cmake directories begin with Qt5, so link should not conflict with any other Qt installations
        #    exceptions: Enginio (new in Qt 5.3, removed in Qt 5.7)
        xinstall -d -m 0755 ${destroot}${prefix}/lib/cmake
        foreach cmakedir [glob -type d -nocomplain -tails -directory ${destroot}${qt_libs_dir}/cmake *] {
            xinstall -d -m 0755 ${destroot}${prefix}/lib/cmake/${cmakedir}
            foreach cmakefile [glob -tails -directory ${destroot}${qt_libs_dir}/cmake/${cmakedir} *.cmake] {
                ln -s ${qt_libs_dir}/cmake/${cmakedir}/${cmakefile} ${destroot}${prefix}/lib/cmake/${cmakedir}/
            }
        }

        # if cmake finds configuration files in ${prefix}/lib/cmake, CMAKE_CURRENT_LIST_DIR expands to ${prefix}/lib/cmake/xxx
        # cmake configuration files actually installed in ${qt_cmake_module_dir}/xxx
        foreach cmakedir [glob -type d -nocomplain -tails -directory ${destroot}${qt_libs_dir}/cmake *] {
            foreach cmakefile [glob -nocomplain -directory ${destroot}${qt_libs_dir}/cmake/${cmakedir} *.cmake] {
                reinplace -q "s|\\\${CMAKE_CURRENT_LIST_DIR}|${qt_cmake_module_dir}/${cmakedir}|g" ${cmakefile}
            }
        }
    }
}

if {${subport} eq ${name}} {
    livecheck.type      regex
    livecheck.url       https://download.qt.io/archive/qt/${branch}/
    livecheck.regex     (\\d+(\\.\\d+)+)
} else {
    livecheck.type      none
}
