# -*- 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} {reneeotten @reneeotten} 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.17
version             ${qt_version}
set middle_name     everywhere-opensource
set worksrcdir_middle_name everywhere

# qt5-qtbase is broken on macOS 15+, so fix needs to be done at the top level as all subports depends on it.
# because of `error: 'CGDisplayCreateImageForRect' is unavailable: obsoleted in macOS 15.0 - Please use ScreenCaptureKit instead.`
if {${os.platform} eq "darwin" && [vercmp ${macosx_deployment_target} >= 15.0]} {
    macosx_deployment_target 14.0
}

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.19
    # 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 6d29e9cfcfffa7632cc3858ceaf8940677ba9c91

    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  6e1a94af2bc76ed012d98ce1fabe7fcb00bcd172 \
                    sha256  33cd20f4a87874f34ef3f9c8d767e006498acc3e6cff89a9c64a9e3387bbfaa5 \
                    size    3778796 \
                    ${qtwebengine_chromium_distfile} \
                    rmd160  a0def2100f9cc956d172c5e77d98450b52a5d272 \
                    sha256  05c17d2facae807577b0b1eeb31bbbb27a02256e7c058f364ede5b67677f3974 \
                    size    469042157

    # Backport fix for https://bugreports.qt.io/browse/QTBUG-138486
    patchfiles-append   patch-qtwebengine-patch-zlib.diff

    # Backport fix for bundled libpng
    patchfiles-append   patch-qtwebengine-libpng.diff

    # fixes for newer LLVM/Clang; Sequoia or newer
    if {${os.platform} eq "darwin" && ${os.major} >= 24} {
        patchfiles-append \
                        patch-qtwebengine-libc++19.diff \
                        patch-qtwebengine-src_3rdparty_chromium_third__party_perfetto_include_perfetto_tracing_internal_track__event__data__source.h.diff \
                        patch-qtwebengine-src_3rdparty_chromium_third__party_blink_renderer_platform_wtf_hash__table.h.diff
    }

    # 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 19

############################################################################### 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 {
        {
            5776fb4f0149a6343f636451be0e40e2b5a61132 \
            434e1620b5cfe7979d57f178962e38b21c1d6e9541ef4d53a8f3d10ffa51708e \
            118273116
        }
        ""
        "port:assimp"
        "qtbase qtdeclarative qtimageformats qtgamepad"
        {"Qt 3D"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtbase {
        {
            8ee231400deeb913066fc283cacd36ad370b7857 \
            db1513cbb3f4a5bd2229f759c0839436f7fe681a800ff2bc34c4960b09e756ff \
            51429880
        }
        ""
        "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 0"
        "License: "
    }
    qtcharts {
        {
            ea634edea9364f5f2e055c2932ae63ad6bd3180c \
            843d7c9f2f2325d83589ad2f07a1d9f14a044446b1a19b23382eb94650ad5133 \
            4248064
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Charts"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtconnectivity {
        {
            ec95561b49781a17b3bfee12d7eb573ee1deb02d \
            87f5fcdc9cc832cdac857a953d161b79b81b1dda92b379db31533963f3a5502b \
            2822488
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Bluetooth" "Qt NFC"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtdatavis3d {
        {
            e50050d7fc66af0b0d59bb71a71e9802e3812189 \
            422bd104bca6c180a9ae9f28f1763f39893491d59661216c33d6034a9c83bc27 \
            5186440
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Data Visualization"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtdeclarative {
        {
            0af7dd105aa65e3142ca363e43be1c711a2b8cbd \
            e1cd2d5d7e8de7b83156caae9b41f2be8b42d93414fe8d705212cda10c64d5da \
            21603892
        }
        "port:python312"
        ""
        "qtbase qtsvg"
        {"Qt QML" "Qt Quick" "Qt Quick Layouts" "Qt Quick Widgets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtdoc {
        {
            64ba96d5993c94e92c63a0259068bc824713582a \
            c9f2ca18ca64b656d6e61baeace7e6fd510212449429559cae907039dfcf5f66 \
            5782072
        }
        ""
        ""
        "qtdeclarative qttools qtmultimedia qtquickcontrols qtquickcontrols2 sqlite-plugin"
        {"Qt Reference Documentation"}
        "requires all documentation"
        "variant overrides: ~examples ~tests ~debug noarch ~docs"
        "revision 0"
        "License: "
    }
    qtgamepad {
        {
            e49bfac503d1fba5aa1e05b2870ee4f3dfda2a1d \
            2551831332e2010bb3deb55a1787fd8d4625d4845189028f1e7787340ebd05b4 \
            382732
        }
        ""
        "port:libsdl2"
        "qtbase qtdeclarative"
        {"Qt Gamepad"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtgraphicaleffects {
        {
            e9463f9a12a5c80919f17a1dd343e80b8fcaeb69 \
            aaa2d04daa9c0feb9931c31ea4a1ff6811d51c56fc82d79f1f219a8a0b2fa21e \
            14037028
        }
        ""
        ""
        "qtdeclarative"
        {"Qt Graphical Effects"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtimageformats {
        {
            dc0ce483bffcc937e23856bfbdb67a1c3fdf4565 \
            4318e249887894725df96fdf0a8fa267d00dd09d10e1ef7c26842e118436ced1 \
            1902916
        }
        ""
        "port:jasper port:libmng port:tiff port:webp"
        "qtbase"
        {"Qt Image Formats"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtlocation {
        {
            16be42d13b96472c95d893cede38930df3bbc83b \
            f3e609b56730d302204e727578b2a768c7a51aff095af0c88aae89aec69414cf \
            6549632
        }
        ""
        "path:lib/pkgconfig/icu-uc.pc:icu port:zlib"
        "qtbase qtdeclarative qtquickcontrols qtquickcontrols2 qtserialport"
        {"Qt Location" "Qt Positioning"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtlottie  {
        {
            aed690043450b9b70cc981c695719db0ee6a24eb \
            d6c6872b3395ddc97b11f513e40f98cebf145043336a7633be77d3ddbbc588ee \
            78176
        }
        ""
        ""
        "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 {
        {
            eefbd49a67fa689ee81b73f9922c93ddc7362bd4 \
            5af08d49ac037e3fcccbbfd4bc6ba9b462c69d867a22dcd5d70e5ada94ce46a4 \
            65324
        }
        ""
        ""
        "qtbase"
        {"Qt Mac Extras"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtmultimedia {
        {
            8ab773e1c8c1328758b962579c92ccfe8caf59e1 \
            a65cc6cf764af197c047a130f08b5d5af19be76d9dfb12e325f7cb805f1b3d93 \
            3843304
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Multimedia" "Qt Multimedia Widgets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtnetworkauth {
        {
            d5ec745a8fcc9ae558b78b531792fe561685baf7 \
            f3ac3d6283a3e8b6e04af9ed95bf184b76a294a1e1a0f122ff13217ada325789 \
            137840
        }
        ""
        ""
        "qtbase"
        {"Qt Network Authorization"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtpurchasing {
        {
            91e7db0a8e8601e7da3b8738afce2388cce31bc1 \
            2cbd86111684d6a630dcd11b30e9acfd48e8931e98931876b5901c545b4f5cc4 \
            516640
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Purchasing"}
        "in-app purchases is of limited value on desktop"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtquick3d {
        {
            aeb674f005077891baa045f5878738df712ad33e \
            6fb53fb0a1d5a45b301793b504de4ddfbb2f61ea70c44e241bdefeb3a41776f4 \
            55772320
        }
        ""
        "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 {
        {
            e01beb589d97e7ea8179306eb934e7cdc21d2b7d \
            8347edc4bb2a0a7c363f626ce7c2aba7a0f8ae8dc7e9f76b0f3b1e1a94f94234 \
            5976476
        }
        ""
        ""
        "qtdeclarative qtgraphicaleffects"
        {"Qt Quick Controls" "Qt Quick Dialogs" "Qt Quick Extras"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtquickcontrols2 {
        {
            3987364237f9cb0f28fe5b79b47dba86bcc5cf1c \
            a0d9d6cdcf6b8a2b6c82f05d32dd665843ac7dc5e985f045709c09c2e0e7f56c \
            8287820
        }
        ""
        ""
        "qtgraphicaleffects qtimageformats"
        {"Qt Quick Controls 2"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtquicktimeline {
        {
            2870d53088fddd11ed71025e743726d15f87959c \
            5b80752e397efa12c98ae5c71b0bbb348414da79ce7c8a7e1d5ac06d034dd783 \
            98388
        }
        ""
        ""
        "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 {
        {
            c261363da36306cec6d54f012cd8e2b1e71954dd \
            2f0acee1b8c6db63febd8157f86a95d6e197b6f39d64ae9eecb1361428353ac9 \
            372392
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Remote Objects"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtscript {
        {
            bab69c4712c1d03adafe74ffcafced4a7f383d7f \
            622db1a47f918d970507125c7245267546c45c0bc58e8e7a5133d27f7a4f5ae1 \
            2659128
        }
        ""
        ""
        "qtbase"
        {"Qt Script" "Qt Script Tools"}
        "deprecated in favor QJS* in Qt QML"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtscxml {
        {
            8f7000462a522fc792eb5048f52277eb774313c2 \
            d389edf819b81734180ccbbe720a5192cc838f69dfc72881d16e42373554f9ef \
            431292
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt SCXML"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtsensors {
        {
            5e60068aa1a2474ddd275083a7a4e257f9eab0f5 \
            90ac9b4e2e9aa3b177c52c504d5b545e863b520d779f35b0295e62f04cc50002 \
            2052732
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Sensors"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialbus {
        {
            1d6a845cc1c41082d1a13d12ba8f6a491fb53c44 \
            ee80a8c9c467a0c72f8ff004d7c1e4eee40dfa13ab5a0d0cf42764fad2bd0810 \
            352568
        }
        ""
        ""
        "qtbase qtserialport"
        {"Qt Serial Bus"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialport {
        {
            c5c5aab613aabfb52104ab6436c017e4572686b6 \
            22c4b6508cb86222cc3d08d7e6fd3115d754ab43a7317a04b4ac7b0744dcaae4 \
            316592
        }
        ""
        ""
        "qtbase"
        {"Qt Serial Port"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtspeech {
        {
            c9e6b0ef8196063d4746665fb875c846666613c6 \
            5a374222dff48630b1a930234c65ed0396554844dbd55b397747db2319b44587 \
            97644
        }
        ""
        "port:flite"
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Speech"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtsvg {
        {
            0763c519dd7a31bb21f69f990034a49a523e1133 \
            750ad4cd70c0e739d2cdfb9b18ea57211fcb2af1116771131858f35956a31e6c \
            1882604
        }
        ""
        ""
        "qtbase"
        {"Qt SVG"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttools {
        {
            ca7d1c6e071af9fdf002cc8b7d81fdd5444b8278 \
            433006eb6732bb7f546f63e0d1890477a9dd2f889228f30aa881aed5dfc9bfc6 \
            8890508
        }
        ""
        "port:clang-${llvm_version}"
        "qtbase qtdeclarative"
        {"Qt Designer" "Qt Help" "Qt UI Tools"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttranslations {
        {
            6dce6cc4d7acf7469e31759f4d9771b58cdd7edf \
            309ddea3d2696042001c5d0ef1ea86cec8d0323bc3a0b942b65aaaf5a5d483c9 \
            1628836
        }
        "port:qt5-qttools"
        ""
        ""
        {"translation files"}
        ""
        "variant overrides: ~examples ~tests ~debug noarch ~docs"
        "revision 0"
        "License: "
    }
    qtvirtualkeyboard {
        {
            2089333777ae31b945f6d6a4086697819fc5c08c \
            06ea900885ae57c3bf3f030c20ffa600b7d553ade52cbb72e6380c2206a51d0f \
            10954680
        }
        ""
        "port:hunspell"
        "qtbase qtdeclarative qtsvg qtmultimedia qtquickcontrols"
        {"Qt Virtual Keyboard"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtwebchannel {
        {
            e44d0cb49bdbe213501ca5dad022c932f911d9e1 \
            ecfd3c1cf70c48f9e7f84016d22081bfffb56a57218230ab6a289e37d652f694 \
            204848
        }
        ""
        ""
        "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 port:ffmpeg4 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 0"
        "License: "
    }
    qtwebglplugin {
        {
            fea3d8c3a5ae6db52c553cae55c69966de76ab2a \
            0bd16674afdb1ab066cc2b6c4cda6be65fdec22a454e6acfb6eabc12e1134f57 \
            69972
        }
        ""
        ""
        "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 9"
        "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 {
        {
            73f092616593f287d3fc9cb9f715c5f2d9d2fddc \
            ccc4d0012bad39787592e8a8665bda8d46057fb5008fe63995b0cda5f632cb85 \
            254836
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt WebSockets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebview {
        {
            69f0352926afd7e42db9f0ae725007f0564467c1 \
            b3aadc3a928193ffaf7a728698eb7e23035183f072894ac1fc9174a2e91a9c4b \
            129988
        }
        ""
        ""
        "qtdeclarative qtwebengine"
        {"Qt WebView"}
        "new in 5.6.0; requires qtwebengine"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtxmlpatterns {
        {
            67efa299420d336e2dcea44a1dc2e66e8fccec33 \
            912c62847269ee579eeeb069f0f76cefcd43abef5e6388eab142ff93eb32f8a6 \
            5182264
        }
        ""
        ""
        "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"
            ""
        }
        {
            "postgresql15"
            "port:postgresql15"
            "${prefix}/include/postgresql15"
            "${prefix}/lib/postgresql15"
            "-lpq"
            ""
        }
        {
            "postgresql14"
            "port:postgresql14"
            "${prefix}/include/postgresql14"
            "${prefix}/lib/postgresql14"
            "-lpq"
            ""
        }
        {
            "postgresql13"
            "port:postgresql13"
            "${prefix}/include/postgresql13"
            "${prefix}/lib/postgresql13"
            "-lpq"
            ""
        }
    }
    {mysql MySQL "revision 0"} {
        {
            "mariadb55"
            "port:mariadb"
            "${prefix}/include/mariadb/mysql"
            "${prefix}/lib/mariadb/mysql"
            "-lmysqlclient_r"
            ""
        }
        {
            "mariadb10_11"
            "port:mariadb-10.11"
            "${prefix}/include/mariadb-10.11/mysql"
            "${prefix}/lib/mariadb-10.11/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"
            ""
        }
        {
            "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"
                }
            }


            # patches for CVEs, see: https://wiki.qt.io/List_of_known_vulnerabilities_in_Qt_products

            # TODO: remove when updating to 5.15.19
            patchfiles-append   CVE-2024-39936-qtbase-5.15.patch
            patchfiles-append   CVE-2025-4211-qtbase-5.15.diff
            patchfiles-append   CVE-2025-5455-qtbase-5.15.patch
            patchfiles-append   CVE-2025-30348-qtbase-5.15.diff

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

            # 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

            # macOS Tahoe removed AGL.framework
            # https://trac.macports.org/ticket/72729
            patchfiles-append patch-qtbase-tahoe-no-agl-framework.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
            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        \
                -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
            patchfiles-append       patch-qtbase-icu-cxx11.diff

            # 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
                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   boost 1.0

                # use MacPorts' default version of boost
                # remove the includes of headers of vendored version, and since nothing links with boost set it as build dependency
                patchfiles-append patch-qt5location-boost.diff
                boost.depends_type build

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

                # 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

                # https://trac.macports.org/ticket/73016
                patchfiles-append   patch-qtlocation-narrowing-const-reference.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
            }

            # 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

                # fix compiling with C++17
                patchfiles-append patch-qtwebkit-cxx17.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
                    }
                }

                # 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

                # 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

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

                # Fix missing header files
                # from fix recommended in https://bugreports.qt.io/browse/QTBUG-124375
                patchfiles-append patch-qtwebengine-browser-build.gn.diff

                # Suppress following warnings to avoid Warnings-as-errors:
                # -Wenum-constexpr-conversion
                # -Wunused-but-set-variable
                patchfiles-append patch-qtwebengine_v8_build.gn_error.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

                # Fix build on macOS 15+
                patchfiles-append patch-qtwebengine-chromium-freetype-gzip.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

                configure.pkg_config_path   ${prefix}/libexec/ffmpeg4/lib/pkgconfig

                # 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 "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 "qtspeech" } {
                # see https://trac.macports.org/ticket/67593
                # error: library not found for -lasound
                configure.post_args-append  \
                    --                      \
                    -no-flite-alsa
            }
        }
    }

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

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

            supported_archs   noarch
            installs_libs     no
            platforms         any

            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
            } elseif { ${module} eq "qtconnectivity" } {
                # TODO: remove when updating to 5.15.19
                patchfiles-append CVE-2025-23050-qtconnectivity-5.15.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
    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 { [string first ":python312" ${deps}] >= 0 } {
        # If Qt components use Python 3, ensure that MacPorts python312 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.12
    }
}
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/official_releases/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
        }
    }
}
