# -*- 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                qt5

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

# When updating, also update py-pyside2
set qt_version      5.15.10
version             ${qt_version}
set middle_name     everywhere-opensource
set worksrcdir_middle_name everywhere

if { ${subport} eq "${name}-qtwebkit" ||
     ${subport} eq "${name}-qtwebkit-docs" } {
    version         5.9.2
    set middle_name opensource
}
# qtwebkit-examples only has no download for 5.9.2...
if { ${subport} eq "${name}-qtwebkit-examples" } {
    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 \
    https://download.qt.io/snapshots/ci/qtwebkit/${branch}/latest/src/submodules/

# latest qtwebengine 5.15.x releases are only tagged on git
if { ${subport} in [list "${name}-qtwebengine" "${name}-qtwebengine-docs"] } {

    version         5.15.14
    # set revision in module info below

    # fetch archives generated by GitHub mirrors: repository uses submodule qtwebengine-chromium
    # which has to be fetched separately and extracted into src/3rdparty
    set qtwebengine_ref v${version}-lts
    # keep this ref synchronized with qt5-qtwebengine-gn to allow distfile reuse
    set qtwebengine_chromium_ref 18c9261dc5b8aa57a0bdd5b62ce6f648cca1ef5e

    distname        qtwebengine-${version}-lts
    set qtwebengine_distfile ${distname}${extract.suffix}
    set qtwebengine_chromium_distname qtwebengine-chromium-${qtwebengine_chromium_ref}
    set qtwebengine_chromium_distfile ${qtwebengine_chromium_distname}${extract.suffix}

    distfiles       \
       ${qtwebengine_distfile}:tagone \
       ${qtwebengine_chromium_distfile}:tagtwo

    master_sites    \
       https://github.com/qt/qtwebengine/archive/${qtwebengine_ref}:tagone \
       https://github.com/qt/qtwebengine-chromium/archive/${qtwebengine_chromium_ref}:tagtwo

    checksums       ${qtwebengine_distfile} \
                    rmd160  b756572d85b9488e99b239b0846c7e152f35be13 \
                    sha256  245a032e0cf2109fedadb43960e319cecfb306ec8fb8adf91473078f1491a55e \
                    size    3778040 \
                    ${qtwebengine_chromium_distfile} \
                    rmd160  98ee4a5bf1fd1824465a8354b1f5b3932bdaae16 \
                    sha256  3973dc02be49699538a07a1bf7327881aa7457274533803131f4ec8b8310efab \
                    size    463363521

    # Using system ninja: no need to extract bundled ninja
    extract.post_args-append --exclude=${distname}/ninja/

    # Using separately-built GN: no need to extract
    extract.post_args-append --exclude=${distname}/gn/

    post-extract {
        ui_debug "delete ${worksrcpath}/src/3rdparty"
        delete ${worksrcpath}/src/3rdparty
        ui_debug "move ${workpath}/${qtwebengine_chromium_distname} ${worksrcpath}/src/3rdparty"
        move ${workpath}/${qtwebengine_chromium_distname} ${worksrcpath}/src/3rdparty
    }
}

# file sizes are significantly smaller using xz
if { ${subport} ni [list ${name} "${name}-docs" "${name}-qtwebengine" "${name}-qtwebengine-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]
    }
}

# MacPorts LLVM/Clang version to use when required.
set llvm_version 16

############################################################################### 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.14
#
###############################################################################

############################################################################### 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.14/qtmodules.html
#
###############################################################################
array set modules {
    qt3d {
        {
            ba4234eef98e1994542cd3f599e1910d7b11df35
            225c06128b9b3574fd96d87890bd28c856198c157b91619e1e8ea8de41958c5f
            118262456
        }
        ""
        "port:assimp"
        "qtbase qtdeclarative qtimageformats qtgamepad"
        {"Qt 3D"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtbase {
        {
            77a3ff43ce3efcec532792ef1dac70ab0c681859
            c0d06cb18d20f10bf7ad53552099e097ec39362d30a5d6f104724f55fa1c8fb9
            50422688
        }
        ""
        "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 port:zstd"
        ""
        {"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 1"
        "License: "
    }
    qtcharts {
        {
            1b821f2de9031686484f7cdfaa338381cd3007ee
            d074be49e2ec9a1f87c979b3fd6510c81379a2d59556be96982bae1896c2a12b
            4254496
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Charts"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtconnectivity {
        {
            029673e9659d8df6d65f9bd724fefb559df1517f
            3fdb1c0315eadc7674d583aa3af6708609ac157a108fce4bc4283bd98ee1b9e9
            2828256
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Bluetooth" "Qt NFC"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtdatavis3d {
        {
            0325fa65bc0f86c20132cfd028d4311ff27a67ca
            e515605e599b4989399a350a085ce8c65a23ed9b9faf33836015550a01351ed2
            5193796
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Data Visualization"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtdeclarative {
        {
            9e3ef1a14b266f4f5ad4f3b04a6e328bc806ceb1
            239b4c3c4cc49b174391445ad4b8320c11c8655300977955febd5dde69d33df4
            21596352
        }
        "port:python311"
        ""
        "qtbase qtsvg"
        {"Qt QML" "Qt Quick" "Qt Quick Layouts" "Qt Quick Widgets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtdoc {
        {
            82a70011d937167a0b44d1ba38d67d167ae98786
            6df8cc5b199e949de8a3efc59de2bb57607cf36d47279fda3daa4256b9595ddf
            5751072
        }
        ""
        ""
        "qtdeclarative qttools qtmultimedia qtquickcontrols qtquickcontrols2 sqlite-plugin"
        {"Qt Reference Documentation"}
        "requires all documentation"
        "variant overrides: ~examples ~tests ~debug noarch ~docs"
        "revision 0"
        "License: "
    }
    qtgamepad {
        {
            ba484f7ca99cd5788462fed9db9f0c49ad6f4134
            6808beb2d78a4346793043358afbfd46a064fce3fe11f79491347434c48a6a76
            389844
        }
        ""
        "port:libsdl2"
        "qtbase qtdeclarative"
        {"Qt Gamepad"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtgraphicaleffects {
        {
            97764dbfafaba2f3e068414bfa2be9aa815c44f0
            ab2cb196b2f4da5a415daa364691c474700c07bbaabae6282da990c31e7dc005
            14045288
        }
        ""
        ""
        "qtdeclarative"
        {"Qt Graphical Effects"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtimageformats {
        {
            46a0904fd3601294dea5fa75615c6474906c12eb
            4f248a48c467d684cecedf36c6fd7d1f0edb47748fd7854c24a178b41154d1b2
            1845088
        }
        ""
        "port:jasper port:libmng port:tiff port:webp"
        "qtbase"
        {"Qt Image Formats"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtlocation {
        {
            7f357c52a1f05df8009e21f084e2ad086ef1f9d5
            8816d26b947fc2b9ddb936acba3135ccb7517b4f9567d8a59916f3fc80bb6c33
            6552240
        }
        ""
        "path:lib/pkgconfig/icu-uc.pc:icu port:zlib"
        "qtbase qtdeclarative qtquickcontrols qtquickcontrols2 qtserialport"
        {"Qt Location" "Qt Positioning"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtlottie  {
        {
            88b2e21efb8513d0a9b8b2cc487e3e7cb673913c
            1fd3ab2a54223be60a57498fa0f213fb7dd4829f918928cc7d3f328ff34ad5a5
            85380
        }
        ""
        ""
        "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 {
        {
            e1a944495b659b694b35201f4fdbf086dc00ea2c
            0c839ff69c3b9e73c9ff134b3b2bd2ec302127272e47cd67411524a388200e92
            72444
        }
        ""
        ""
        "qtbase"
        {"Qt Mac Extras"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtmultimedia {
        {
            422fa1ce45f438ef162537b29894492488c600eb
            1106e0bd8ec65b2c0a89170e452ea104ca9431050f507da5e874be2cb54f29b6
            3832156
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Multimedia" "Qt Multimedia Widgets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtnetworkauth {
        {
            1b681dd9f3a48522d29ec4249d535303c0eb4398
            91462651054a078e98b87cff510b56bc636fd0fa209843dd887ed17c206ec961
            144780
        }
        ""
        ""
        "qtbase"
        {"Qt Network Authorization"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtpurchasing {
        {
            83371bc2914f6029e45d281c0380b96c713940d4
            8b297f0f0a235c36e9e833648901e84a6da88b5c0d0644406ab6909e32fbfd3f
            309732
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Purchasing"}
        "in-app purchases is of limited value on desktop"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtquick3d {
        {
            c6e86a2b7b8b3be8f70372387690699a442ad73f
            6db37612f9a37d0fa6e8af302411f058b1fdd73c8bc9be1ba18b7f765f81d893
            55756692
        }
        ""
        "port:assimp"
        "qtbase qtdeclarative"
        {"Provides a high-level API for creating 3D content or UIs based on Qt Quick."}
        "technology preview module; GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtquickcontrols {
        {
            2774ac937385f65ff2878cb620574536c0f68aa4
            a06a6e73109857f0fce7e4d6a75fbe6b8ce4e27bc4f99802a6948a86daa9efeb
            5984520
        }
        ""
        ""
        "qtdeclarative qtgraphicaleffects"
        {"Qt Quick Controls" "Qt Quick Dialogs" "Qt Quick Extras"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtquickcontrols2 {
        {
            21256c02131198f636cb0534397ef260d48cce96
            97954c92fee9031ec31341812ccf53afb3c132e2b271bcef1174367fa002fc8e
            8293436
        }
        ""
        ""
        "qtgraphicaleffects qtimageformats"
        {"Qt Quick Controls 2"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtquicktimeline {
        {
            f718ea57e6c9f92ce26829ac397a2b89f089f7e3
            a0effcbf39c68591e5b788edb96ba3f1315df0eb6bd6f0c6b43594b32b640011
            105584
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"A platform target to compile Qt applications for the web browsers using WebAssembly."}
        "technology preview module; GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtremoteobjects {
        {
            ca2a270ee9d3c2468455f2b2805839126d4b2dfb
            36fa891c1d9299cb13b422ac5626291db30f799f481ec50a493392d4618ce9c2
            379348
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Remote Objects"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtscript {
        {
            16bdfc929adde38d8f160419638224dc767a4b1b
            642dcd4506dd4ff19de6f671c3687ad5cce682a1ddc55dd7e9cfb840ac96703b
            2666052
        }
        ""
        ""
        "qtbase"
        {"Qt Script" "Qt Script Tools"}
        "deprecated in favor QJS* in Qt QML"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtscxml {
        {
            2bc200f3641aa566b0b3984cbe67a0b1004f2bc8
            f1ffc78b1fd25bc813c79992e22590099f8cc65ce4360c1bdd22f7d97adb9d82
            437404
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt SCXML"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtsensors {
        {
            7785497633063ea756f188ab42911e859b6976d5
            cd3c2ac4d6097f506f7864718b8823dbb0cd0b34fac4a6ca8a23bac8fa59ff01
            2059812
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Sensors"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialbus {
        {
            db3e9338e601e3d585a62602c9182cebf704cdfd
            c9c13a71de5bf3be1a18b91218686f9aa08a7f429a526cb2067ea54b6d61ffc4
            359256
        }
        ""
        ""
        "qtbase qtserialport"
        {"Qt Serial Bus"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialport {
        {
            4033caad38ff2bddcf4bbd385b29e83d5a42ae0c
            fc6f4e2a4585dd24898c3a823ab82f05f7337c0e0fd710bbde7745bc80f068fe
            324256
        }
        ""
        ""
        "qtbase"
        {"Qt Serial Port"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtspeech {
        {
            4452e4c18a57e1713537ac3c2db9be77b52c8f7e
            8af34b46ae8a81749e455acd3e3602ae53983b01df76138172dba76a3bb64ce3
            104944
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Speech"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtsvg {
        {
            8f7adebf09542cd6c9165c3bb0b0daae6e690312
            cf13e3835b8a767779d041e556b0942c2d5aeb3b5a5d325ae5d2028c37004ae8
            1889960
        }
        ""
        ""
        "qtbase"
        {"Qt SVG"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttools {
        {
            c8961544c2d76dd6142b7968fd4a5f4693901ce4
            66f46c9729c831dce431778a9c561cca32daceaede1c7e58568d7a5898167dae
            8899372
        }
        ""
        "port:clang-${llvm_version}"
        "qtbase qtdeclarative"
        {"Qt Designer" "Qt Help" "Qt UI Tools"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttranslations {
        {
            74c10465a1037406fc18500e7d5fbae9cb8c093c
            38b942bc7e62794dd072945c8a92bb9dfffed24070aea300327a3bb42f855609
            1635736
        }
        ""
        "port:qt5-qttools"
        ""
        {"translation files"}
        ""
        "variant overrides: ~examples ~tests ~debug noarch ~docs"
        "revision 0"
        "License: "
    }
    qtvirtualkeyboard {
        {
            9d03f170ba2cd8dcbd7c8da4d19436956da7f851
            4571faa716c36a3db0211379881d70c2ef510f7649289d434633e03ac3b43da8
            10958048
        }
        ""
        "port:hunspell"
        "qtbase qtdeclarative qtsvg qtmultimedia qtquickcontrols"
        {"Qt Virtual Keyboard"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtwebchannel {
        {
            fe78508267059b73705350133fbcc471d4e3d5c4
            e25702f842057063075e7dd8460b3143b6c6666ba76d274f69bb039c2f8effe5
            211876
        }
        ""
        ""
        "qtbase qtdeclarative qtwebsockets"
        {"Qt WebChannel"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebengine {
        {}
        "port:python27 port:py27-ply path:bin/ninja:ninja port:gperf port:bison port:flex port:qt5-qtwebengine-gn"
        "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 path:lib/pkgconfig/poppler.pc:poppler port:pulseaudio path:lib/pkgconfig/icu-uc.pc:icu 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 3"
        "License: "
    }
    qtwebglplugin {
        {
            c6d47766f7056ddac01f0e56e1db3d53a46ab60f
            19a9b6007dc74c3eda9304a4b94e6cdafb31677b835c6073dff1e6e20bde6c8a
            77096
        }
        ""
        ""
        "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 {
        {
            8a26829a50529383256107133bd2bee44981b547
            c7ce0bbaea5ed89bba04fdf893e3030c8887f15f45dee5958d645a9f577952e1
            35301876
        }
        "port:python27 port:bison port:flex"
        "port:fontconfig path:lib/pkgconfig/icu-uc.pc:icu port:leveldb port:webp port:libxml2 port:libxslt port:zlib port:sqlite3 path:include/turbojpeg.h:libjpeg-turbo"
        "qtbase qtdeclarative qtlocation qtmultimedia qtsensors qtwebchannel qtxmlpatterns"
        {"Qt WebKit" "Qt WebKit Widgets"}
        "community support only (use Qt WebEngine)"
        "variant overrides: "
        "revision 5"
        "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 5"
        "License: "
    }
    qtwebsockets {
        {
            77cbe8127395b4cead65053639253abbc7025c97
            2b9562db9e5c4b4585ab017f9b46b59ddc75b4d7222c5b03ebbc479652f6d98a
            261792
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt WebSockets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebview {
        {
            cf707aff1b2ba2b98b241c451e11b544becfc235
            2458c606dd9caa6e92b2fb136cc0f74ebb0757c1616a4c5180b2857d42328400
            136584
        }
        ""
        ""
        "qtdeclarative qtwebengine"
        {"Qt WebView"}
        "new in 5.6.0; requires qtwebengine"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtxmlpatterns {
        {
            0c29c63c381a4a5b41c8afc3fb6faf8d885ec3ae
            03577573a2bbc035192a75917ceceadfd787899c3622d91e6a8844ad29d1da0d
            5195076
        }
        ""
        ""
        "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"} {
        {
            "postgresql13"
            "port:postgresql13"
            "${prefix}/include/postgresql13"
            "${prefix}/lib/postgresql13"
            "-lpq"
            ""
        }
        {
            "postgresql12"
            "port:postgresql12"
            "${prefix}/include/postgresql12"
            "${prefix}/lib/postgresql12"
            "-lpq"
            ""
        }
        {
            "postgresql11"
            "port:postgresql11"
            "${prefix}/include/postgresql11"
            "${prefix}/lib/postgresql11"
            "-lpq"
            ""
        }
        {
            "postgresql10"
            "port:postgresql10"
            "${prefix}/include/postgresql10"
            "${prefix}/lib/postgresql10"
            "-lpq"
            ""
        }
        {
            "postgresql96"
            "port:postgresql96"
            "${prefix}/include/postgresql96"
            "${prefix}/lib/postgresql96"
            "-lpq"
            ""
        }
        {
            "postgresql95"
            "port:postgresql95"
            "${prefix}/include/postgresql95"
            "${prefix}/lib/postgresql95"
            "-lpq"
            ""
        }
        {
            "postgresql94"
            "port:postgresql94"
            "${prefix}/include/postgresql94"
            "${prefix}/lib/postgresql94"
            "-lpq"
            ""
        }
        {
            "postgresql84"
            "port:postgresql84"
            "${prefix}/include/postgresql84"
            "${prefix}/lib/postgresql84"
            "-lpq"
            ""
        }
    }
    {mysql MySQL "revision 0"} {
        {
            "mariadb55"
            "port:mariadb"
            "${prefix}/include/mariadb/mysql"
            "${prefix}/lib/mariadb/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_10"
            "port:mariadb-10.10"
            "${prefix}/include/mariadb-10.10/mysql"
            "${prefix}/lib/mariadb-10.10/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_9"
            "port:mariadb-10.9"
            "${prefix}/include/mariadb-10.9/mysql"
            "${prefix}/lib/mariadb-10.9/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_8"
            "port:mariadb-10.8"
            "${prefix}/include/mariadb-10.8/mysql"
            "${prefix}/lib/mariadb-10.8/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_7"
            "port:mariadb-10.7"
            "${prefix}/include/mariadb-10.7/mysql"
            "${prefix}/lib/mariadb-10.7/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_6"
            "port:mariadb-10.6"
            "${prefix}/include/mariadb-10.6/mysql"
            "${prefix}/lib/mariadb-10.6/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_5"
            "port:mariadb-10.5"
            "${prefix}/include/mariadb-10.5/mysql"
            "${prefix}/lib/mariadb-10.5/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_4"
            "port:mariadb-10.4"
            "${prefix}/include/mariadb-10.4/mysql"
            "${prefix}/lib/mariadb-10.4/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_3"
            "port:mariadb-10.3"
            "${prefix}/include/mariadb-10.3/mysql"
            "${prefix}/lib/mariadb-10.3/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_2"
            "port:mariadb-10.2"
            "${prefix}/include/mariadb-10.2/mysql"
            "${prefix}/lib/mariadb-10.2/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_1"
            "port:mariadb-10.1"
            "${prefix}/include/mariadb-10.1/mysql"
            "${prefix}/lib/mariadb-10.1/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mysql8"
            "port:mysql8"
            "${prefix}/include/mysql8/mysql"
            "${prefix}/lib/mysql8/mysql"
            "-lmysqlclient"
            ""
        }
        {
            "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} {

        if { ${module} ne "qtwebengine" } {
            distname ${module}-${middle_name}-src-${version}
            if { ${module} ni [list "qtwebkit" "qtwebkit-examples"] } {
                worksrcdir ${module}-${worksrcdir_middle_name}-src-${version}
            }
        }

        revision ${revision_string}

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

        if { ${module} ne "qtwebengine" } {
            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

            patchfiles-append {*}{
                CVE-2023-32762-qtbase-5.15.diff
                CVE-2023-32763-qtbase-5.15.diff
                CVE-2023-33285-qtbase-5.15.diff
                CVE-2023-37369-qtbase-5.15.diff
                CVE-2023-38197-qtbase-5.15.diff
            }

            # see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=270715
            patchfiles-append qtbase-5.15.9-work-around-pyside2-brokenness.patch

            # 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

            # fix build where "backtrace_from_fp" is not defined
            # this patch is specific to version 5.15
            patchfiles-append patch-qt515-highsierra1.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
            }

            #-----------------------------------------------------------------------------
            # 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
            if { ${configure.compiler} eq "clang" } {
                # let xargs find correct compiler (default behaviour)
                post-patch {
                    # let xargs find correct compiler
                    reinplace \
                        "s|__MACPORTS_CC__|clang|g" \
                        ${worksrcpath}/mkspecs/common/clang.conf

                    reinplace \
                        "s|__MACPORTS_CXX__|clang++|g" \
                        ${worksrcpath}/mkspecs/common/clang.conf
                }
            } else {
                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
                }
            }

            if { [string match macports-clang-* ${configure.compiler}] && [vercmp ${xcodeversion} "7.0"] >= 0 } {
                # non-Xcode clang does not seem to be able to understand tbd files
                # for an explanation of tbd files, see
                #    https://stackoverflow.com/questions/31450690/why-xcode-7-shows-tbd-instead-of-dylib
                # see https://trac.macports.org/ticket/53151
                pre-fetch {
                    ui_error "This configuration is known to fail"
                    ui_error "See https://trac.macports.org/ticket/53151"
                    ui_error "As a workaround, do not set configure.compiler manually"
                    return -code error "incompatible configuration"
                }
            }

            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        \
                -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

            # 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-libinput            \
                -no-xkbcommon           \
                -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
                universal_variant no
            }

            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/67417
                # include/mbgl/util/unique_any.hpp: error: no member named 'move' in namespace 'std'
                patchfiles-append  patch-qtlocation-mbgl-unique_any.hpp.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="
            }

            # special case
            if { ${module} eq "qtquick3d" } {
                # see https://bugreports.qt.io/browse/QTBUG-84037
                patchfiles-append patch-qtquick3d-assimp.diff

                # see config_help.txt
                configure.post_args-append   -- \
                                             -system-quick3d-assimp
            }

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

                # Compile fails on case insensitve filesystems due to similar header files from other
                # macports headers found on the include search path.  In particular event.h and archive.h

                post-patch {
                    ui_debug "qt5 Portfile: Forcing qtwebkit's Archive.h and Event.h to the front of the header search path."
                    file mkdir "${worksrcpath}/preferredheaders"
                    file copy ${worksrcpath}/Source/WebCore/loader/archive/Archive.h ${worksrcpath}/preferredheaders/
                    file copy ${worksrcpath}/Source/WebCore/dom/Event.h ${worksrcpath}/preferredheaders/
                }

                configure.cppflags-prepend -I${worksrcpath}/preferredheaders

                # 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

                # avoid: Project ERROR: Could not find feature mirclient.
                patchfiles-append patch-qtwebkit_fix_mirclient_test.diff

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

                # work around changed vm_param.h in MacOSX.sdk 11+ -- the older code in 2014-vintage qtwebkit expects
                # PAGE_SHIFT to be set to I386_PAGE_SHIFT when on x86_64
                # the 11.0 + SDK defines that only if MAC_OS_X_VERSION_MIN_REQUIRED is < 101600 (to support arm64 after that)
                if {${os.platform} eq "darwin" && ${os.major} >= 20} {
                    patchfiles-append patch-qtwebkit-FastMalloc.diff
                    patchfiles-append patch-qtwebkit-Platform.h.diff
                    patchfiles-append patch-qtwebkit-MachineStackMarker.cpp.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
                    }
                }
            }

            # 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

                # in Qt 5.15.2, need one additional header
                patchfiles-append patch-qtwebkit-examples-browser-qpainterpath.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-${llvm_version}
            }

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

                # see https://doc.qt.io/qt-5/qtwebengine-platform-notes.html
                PortGroup           xcodeversion 1.0

                # see https://trac.macports.org/ticket/61921
                minimum_xcodeversions   {17 10.0.0}

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

                # build requires Node.js as of 5.15.3
                # nodejs12 (which chromium 87 uses by default) or later are known to work
                # prefer latest LTS Node.js as fallback
                depends_build-append path:bin/node:nodejs20

                # 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}
                #
                # Disabled for now; not sure why this was needed
                # See https://github.com/macports/macports-ports/commit/bd858ac96b41#commitcomment-61755245

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

                # run syncqt.pl to generate headers needed to build from git snapshot (rather than release tarball)
                # system Perl is sufficient, since syncqt.pl only uses standard core modules
                # (although future macOS may not include Perl)
                depends_patch-append port:${name}-qtbase
                post-patch {
                    system -W ${worksrcpath} \
                        "/usr/bin/perl ${prefix}/libexec/${name}/bin/syncqt.pl -version ${version} ."
                }

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

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

                # see config_help.txt
                configure.post_args-append         \
                    --                             \
                    -webengine-native-spellchecker \
                    -webengine-kerberos \
                    -webengine-proprietary-codecs \
                    -system-webengine-icu \
                    -system-webengine-ffmpeg

                # missing file spell_check_host_chrome_impl_mac.cc
                # TODO: investigate further
                configure.post_args-delete \
                    -webengine-native-spellchecker

                # GN bundled with qtwebengine-chromium has Qt-specific additions
                # To avoid building the bundled GN from scratch when building qtwebengine,
                # the bundled GN is built separately as the qt5-qtwebengine-gn port
                # and qtwebengine is patched to build using the separately-built GN
                # These steps are duplicated in special case for qt5-qtwebengine-docs
                patchfiles-append patch-qtwebengine_gn_binary_path.diff
                post-patch {
                    reinplace -W ${worksrcpath} \
                        "s|@@GN_BINARY@@|${qt_bins_dir}/gn-qtwebengine5.15|g" \
                        configure.pri \
                        src/core/gn_run.pro \
                        src/pdf/gn_run.pro
                }
                # override `"autoDetect": "false"` in src/buildtools/configure.json
                configure.post_args-append \
                    -feature-webengine-system-gn

                # do not depend on commercial LTS release version, as described in
                # https://www.qt.io/blog/building-qt-webengine-against-other-qt-versions
                # based on example in Arch Linux: https://git.io/JMScA
                post-build {
                    foreach webengine_module [list Pdf PdfWidgets WebEngine WebEngineCore WebEngineWidgets] {
                        reinplace "s|${version} |${qt_version} |g" \
                            ${worksrcpath}/lib/cmake/Qt5${webengine_module}/Qt5${webengine_module}Config.cmake
                    }
                }
            }

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

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

                # avfmediaplayerserviceplugin.h:55: Error: Undefined interface
                conflicts_build-append ${subport}

                # 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 "qtsvg" } {
                patchfiles-append CVE-2023-32573-qtsvg-5.15.diff
            }
        }
    }

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

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

            supported_archs   noarch
            installs_libs     no
            universal_variant no

            if { ${module} ne "qtwebengine" } {
                distname    ${module}-${middle_name}-src-${version}
                if { ${module} ne "qtwebkit" } {
                    worksrcdir ${module}-${worksrcdir_middle_name}-src-${version}
                }
            }

            revision ${revision_string}

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

            build.target    docs
            destroot.target install_docs

            if { ${module} ne "qtwebengine" } {
                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
                # Use bundled GN from separately-built qt5-qtwebengine-gn port
                # Duplicated from special case for qt5-qtwebengine
                patchfiles-append patch-qtwebengine_gn_binary_path.diff
                post-patch {
                    reinplace -W ${worksrcpath} "s|@@GN_BINARY@@|${qt_bins_dir}/gn-qtwebengine5.15|g" \
                        configure.pri src/core/gn_run.pro src/pdf/gn_run.pro
                }
                configure.post_args-append -- -feature-webengine-system-gn
            } elseif { ${module} eq "qtwebkit" } {
                # avoid: Project ERROR: Could not find feature mirclient.
                patchfiles-append patch-qtwebkit_fix_mirclient_test.diff
                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
                }
            } elseif { ${module} eq "qtquick3d" } {
                # see https://bugreports.qt.io/browse/QTBUG-84037
                patchfiles-append patch-qtquick3d-assimp.diff
            }
        }
    }
}

# 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}
        worksrcdir qtbase-${worksrcdir_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
    universal_variant no

    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
    universal_variant no

    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 { [string first ":python311" ${deps}] >= 0 } {
        # If Qt components use Python 3, ensure that MacPorts python311 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
        #
        set python_framework ${frameworks_dir}/Python.framework/Versions/3.11
    }
}
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+)+)
} elseif {${subport} eq "${name}-qtwebengine"} {
    livecheck.type      regex
    livecheck.url       https://api.github.com/repos/qt/qtwebengine/git/refs/tags
    livecheck.regex     \"refs/tags/v(\\d+(\\.\\d+)+)-lts\"
} else {
    livecheck.type      none
}

foreach {component component_info} [array get qt5pg::qt5_component_lib] {

    set suffix [lindex ${component_info} 3]

    set qt_version_introduced [lindex ${component_info} 0]
    set qt_version_removed    [lindex ${component_info} 1]

    if { [vercmp ${qt_version_removed} ${qt_version}] <= 0 } {
        subport ${name}-${component}${suffix} {
            PortGroup obsolete 1.0

        }

        if { ${component} ne "qtquick1" && ${component} ne "qtwebkit-examples" && ${component} ne "qtdoc" && ${suffix} ne "-plugin" } {
            subport ${name}-${component}${suffix}-docs {
                PortGroup obsolete 1.0

            }
        }
    }
}

# Qt 5.7 will not work on Mac OS X 10.7
# Qt 5.8 will not work on OS X 10.8
# Qt 5.9 will not work on OS X 10.9
# Qt 5.10 will not work on OS X 10.10
# Qt 5.12 will not work on OS X 10.11
# Qt 5.14 will not work on OS X 10.12
# replace qt5-XYZ port with qt5{6,7,8,9,11,13}-XYZ port
if { ${os.platform} eq "darwin" && ${os.major} < 17} {

    if { ${os.major} < 12 } {
        set qt_replace_version 5.6
    } elseif { ${os.major} < 13 } {
        set qt_replace_version 5.7
    } elseif { ${os.major} < 14 } {
        set qt_replace_version 5.8
    } elseif { ${os.major} < 15 } {
        set qt_replace_version 5.9
    } elseif { ${os.major} < 16 } {
        set qt_replace_version 5.11
    } else {
        set qt_replace_version 5.13
    }

    set qt_replace_name qt[join [split ${qt_replace_version} .] ""]
    set qt_replace_port ${qt_replace_name}[string range ${subport} 3 end]

    set suffix [string range ${subport} [string last - ${subport}] end]
    if { ${suffix} eq "-docs" } {
        set component [string range ${subport} 4 end-5]
    } else {
        set component [string range ${subport} 4 end]
    }

    if { ${component} eq "" } {
        replaced_by      ${qt_replace_port}
        conflicts-delete ${qt_replace_port}
    } else {
        set component_info $qt5pg::qt5_component_lib(${component})
        set qt_version_introduced [lindex ${component_info} 0]
        set qt_version_removed    [lindex ${component_info} 1]

        if { [vercmp ${qt_version_introduced} ${qt_replace_version}] <= 0 && [vercmp ${qt_version_removed} ${qt_replace_version}] > 0 } {
            replaced_by      ${qt_replace_port}
            conflicts-delete ${qt_replace_port}
        } elseif {[vercmp ${qt_version_introduced} ${qt_replace_version}] > 0 && ![catch {set installed [lindex [registry_active ${subport}] 0]}]} {
            # this should not really be possible
            # Qt 5.14.0 built on macOS Sierra, but Qt 5.14.1 did not
            # so the upgrade path could be qt5 (5.14.0) --> qt513 (5.13.2)
            # this is a problem for qtquick3d and qtquicktimeline because they were introduced in 5.14.0
            # see https://trac.macports.org/ticket/60045
            replaced_by      ${qt_replace_name}-qtbase
            conflicts-delete ${qt_replace_name}-qtbase
        }
    }
}
