# -*- 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
PortGroup                   active_variants 1.1
PortGroup                   qt6_info 1.0

name                        qt6

categories                  aqua
platforms                   {macosx >= 18} ; # upon change, please update qt6 PG
maintainers                 {mcalhoun @MarcusCalhoun-Lopez} openmaintainer

# for OpenSSLException, see source and header files in src/network/ssl of qtbase
# see also https://doc.qt.io/qt-6.4/qtnetwork-index.html#licenses-and-attributions
license                     {LGPL-3 GPL-3 OpenSSLException}

homepage                    https://www.qt.io

version                     6.4.3

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

# see https://www.qt.io/blog/qt-6.0-released
compiler.cxx_standard       2017

master_sites                https://download.qt.io/official_releases/qt/${branch}/${version}/submodules
# file sizes are significantly smaller using xz
use_xz                      yes

supported_archs             arm64 x86_64

# conflict with all other versions of qt6
foreach {qt_base qt_info} ${qt6::available_versions} {
    if { ${qt_base} ne ${name} } {
        conflicts-append    port:${qt_base}-qtbase
    }
}

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

# MacPorts Python branch & version to use when required
set python_branch           3.11
set python_version          [join [split ${python_branch} .] ""]

############################################################################### Modules Not Considered
#
# No qtactiveqt      (Windows Only)
# No qtwayland       (Linux Only)
#
###############################################################################

############################################################################### Qt Dependencies
#
# https://code.qt.io/cgit/qt/qt5.git/tree/.gitmodules?h=6.4.3
#
###############################################################################

############################################################################### 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-6.4/qtmodules.html
#
###############################################################################
array set modules {
    qtbase {
        {
            3cadb6bd4cefcb2db1f47e780520d3f3d863fa1d
            5087c9e5b0165e7bc3c1a4ab176b35d0cd8f52636aea903fa377bdba00891a60
            48078536
        }
        ""
        "port:brotli path:bin/dbus-daemon:dbus port:double-conversion port:freetype
            path:lib/pkgconfig/glib-2.0.pc:glib2 path:lib/pkgconfig/harfbuzz.pc:harfbuzz
            path:lib/pkgconfig/icu-uc.pc:icu path:include/turbojpeg.h:libjpeg-turbo port:libb2
            port:libpng port:md4c path:lib/pkgconfig/libpcre2-posix.pc:pcre2
            port:zlib path:lib/pkgconfig/libzstd.pc: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: "
    }
    qtsvg {
        {
            2a021623559aca5a8d23298544b3d32fd8597e42
            88315f886cf81898705e487cedba6e6160724359d23c518c92c333c098879a4a
            1726464
        }
        ""
        "port:zlib"
        "qtbase"
        {"Qt SVG"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtdeclarative {
        {
            91fafeb58501d02b49a9d97ff39759cb82ccaad9
            67ded1b3b988135e3600916b34bb156fe3986c88420c6f87a6d0fd53521ba795
            30556972
        }
        ""
        ""
        "qtbase qtimageformats qtshadertools qtsvg qtlanguageserver"
        {"Qt QML" "Qt Quick" "Qt Quick Layouts" "Qt Quick Widgets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtmultimedia {
        {
            c085075a84da5e9d9fa8b613dcdc318138ef10da
            a4a72469e12da1e59ee032ab2e6677d620a5663c354d0ba88906d2bec1567d00
            5941312
        }
        ""
        "path:lib/libavcodec.dylib:ffmpeg port:pulseaudio"
        "qtbase qtshadertools qtdeclarative qtquick3d"
        {"Qt Multimedia"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttools {
        {
            047a22392a4f500c6bf70c50432e16543e7505f0
            867df829cd5cd3ae8efe62e825503123542764b13c96953511e567df70c5a091
            8738280
        }
        ""
        "port:clang-${llvm_version}"
        "qtbase qtdeclarative"
        {"Qt Designer" "Qt Help" "Qt UI Tools"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qttranslations {
        {
            47f6a8448cc96dc28119aa2748ac42130a12c2c8
            79e56b7800d49649a8a8010818538c367a829e0b7a09d5f60bd3aecf5abe972c
            1466828
        }
        ""
        ""
        "qttools"
        {"translation files"}
        ""
        "variant overrides: ~examples ~tests ~debug noarch ~docs"
        "revision 0"
        "License: "
    }
    qtdoc {
        {
            b39c76dbcb6159b392ad9348146d39befda2da59
            73eac34dd629f629ca8cf5827c165b476e834731bb8262c937bc57eef4128183
            8633284
        }
        ""
        ""
        "qtdeclarative qttools qtmultimedia"
        {"documentation and examples"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtpositioning {
        {
            11046a934c31c916822b01d60b4e5975153ffcdd
            b218b1e1d37c12e2935ae90600d8b42ef68f90617fe8677ec89f0b0f05bcd70c
            1494352
        }
        ""
        "port:gconf"
        "qtbase qtdeclarative qtserialport"
        {"Qt Positioning"}
        ""
        "variant overrides: ~examples ~tests"
        "revision 0"
        "License: "
    }
    qtsensors {
        {
            15373f978e5dd1f3b2b09e53646ae8a734b26fcb
            5f3964fa2889456736a0bdf07221105fd67853539496e2d3ba2f380a47babc48
            2106700
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Sensors"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtconnectivity {
        {
            df9e16e8b99e00f7261587a3acf582d65f8f1bc9
            f98c2014cc4ab7ed69e762ecddd2433b95468942bb1612f648a7e9c94ba44c9d
            1108992
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Bluetooth" "Qt NFC"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qt3d {
        {
            819f7dd13b6e6d8ceb0ae96399d36aa43a970698
            a88225f997253d1605afced4eaac5c2c4b368fd90cd5e076a90bd7d1b2ae3d71
            104002896
        }
        ""
        "port:assimp"
        "qtbase qtdeclarative qtshadertools"
        {"Qt 3D"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtimageformats {
        {
            a95630b7d6b920d22a3726b8607690359a724e83
            0aff58062e74b84617c5da8325d8cdad5368d8f4d2a11ceafcd58329fe99b798
            1962440
        }
        ""
        "path:lib/pkgconfig/jasper.pc:jasper port:libmng port:tiff port:webp"
        "qtbase"
        {"Qt Image Formats"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialbus {
        {
            0f962f4b8152df25f8d66eb24b4c41c7bb7f8c10
            b6446a7516d1f04e561c00f9c50ce4d39dad72150f067722ba759f00b4b34366
            381036
        }
        ""
        ""
        "qtbase qtserialport"
        {"Qt Serial Bus"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtserialport {
        {
            9dcb75b488458cc4a07999ce5768839e00018300
            5f97ad9067efa39a8a2a39fbbc1e03d2191f305733d9c2f3060f8017ecfc95de
            319400
        }
        ""
        ""
        "qtbase"
        {"Qt Serial Port"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebsockets {
        {
            7ac6871f10d33b08abd745d5c0cebaac45a745bd
            de27357eb55c5e83ae560371d7f76b36b5b2ba85caf8965c527f1d5d4b8d458f
            267624
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt WebSockets"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebchannel {
        {
            5cc690a6fa4e1449b2e496a868522a922023bb5f
            e955044587f0e74a64c88bbbe4d6be9fd869ac89d66a96cce4752756bcbef3a8
            213316
        }
        ""
        ""
        "qtbase qtdeclarative qtwebsockets"
        {"Qt WebChannel"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebengine {
        {
            80e059ab1380234fa324888d9b1c3c4009c908c8
            55f85af736a1dc79a41b8d95014ba27d8fce0be44293a69e64fece7fa12b2925
            440562844
        }
        "port:python${python_version} port:py${python_version}-html5lib"
        "path:bin/node:nodejs16 port:re2 path:lib/pkgconfig/icu-uc.pc:icu port:webp \
            port:libopus path:lib/libavcodec.dylib:ffmpeg path:lib/pkgconfig/vpx.pc:libvpx \
            port:snappy path:lib/pkgconfig/glib-2.0.pc:glib2 port:zlib port:minizip port:libevent \
            port:libxml2 port:libxslt port:lcms2 port:libpng path:include/turbojpeg.h:libjpeg-turbo \
            path:lib/pkgconfig/harfbuzz.pc:harfbuzz port:freetype port:pciutils port:pulseaudio"
        "qtdeclarative qtwebchannel qttools qtpositioning"
        {"Qt WebEngine Qt" "Qt PDF"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtwebview {
        {
            7d53b1a1189db57431daf83e21d6a33eca0945f7
            51951770495e142d8952827b4e1d28565d2cb00fce652dae6eddd9425cf3e843
            139820
        }
        ""
        ""
        "qtdeclarative qtwebengine"
        {"Qt WebView"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtcharts {
        {
            991ea524f2d8b66ce5bee6570cb186c573d963a7
            4a61e4f5ffb55df69fc58f88255aabca44fb51935b180c03ab81e098d437c346
            4386856
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Charts"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtdatavis3d {
        {
            2f8650abed7a1c7bd984275856c5fd6ea85b137a
            c978ac5db663560b8071ddf62e7d7e82e0c01d109d9292171f5589ce5e787995
            5216376
        }
        ""
        ""
        "qtbase qtdeclarative qtmultimedia"
        {"Qt Data Visualization"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtvirtualkeyboard {
        {
            53c7929d232eeee86ba89d28381e80401c2022b1
            3f4685226f5b00e65c8b304a47dd96c7d9b843649fba6e75421d055825de0ee5
            3723096
        }
        ""
        "port:hunspell"
        "qtbase qtdeclarative qtsvg qtmultimedia"
        {"Qt Virtual Keyboard"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtscxml {
        {
            25d44a78d1eb9e3efa4492823d3c126e85b51c1f
            fe722c016d434c02e713e3c7d5d4a845998efe69d8b645923e83f1f4f7b72f3a
            1783744
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt SCXML"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtspeech {
        {
            365368ee5fe0e50a8fe374d39a8a6323dbfbb995
            79e0d0195e0a061d384a46eef96ca604e3814043d1a9903f63b3cfc985254ae2
            219444
        }
        ""
        "port:flite"
        "qtbase qtdeclarative qtmultimedia"
        {"Qt TextToSpeech"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtnetworkauth {
        {
            88985827d6cab812578506e59cb085717f80d79a
            4d115ba572f4b8c8fbb64f4c26a40723556d252bd337a643f2266f19c7716ae5
            145000
        }
        ""
        ""
        "qtbase"
        {"Qt Network Authorization"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtremoteobjects {
        {
            abb63a30db6d355274412817c565305d852ff567
            9c9447df79cba249191d8628b6a6910658a7882d14114977f5d69c69440c186a
            366136
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Remote Objects"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtlottie {
        {
            b2a3b0a303a549388c838720fb93bc4c356ece4f
            ff6c52e04566ecadee351eb3b803237bf07d417e4bb27d2122a5879618f1693d
            91844
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Lottie Animation"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtquicktimeline {
        {
            387c316629a5c555f1e3719959a70619d6f8e5c6
            3808c447c3a3af98c797f12f38d3c371c628c35d34123ce7a7e1f580e55e3da5
            113348
        }
        ""
        ""
        "qtbase qtdeclarative"
        {"Qt Quick Timeline"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtquick3d {
        {
            17030605c5065bd1a481f8495f11f50599a47cb4
            78a38d565d449b939ffd1b30cacd5ba3e1b97c7a8c0a02fefc810ed5d4dc8050
            44048520
        }
        ""
        "port:assimp"
        "qtbase qtdeclarative qtshadertools"
        {"Qt Quick 3D"}
        "GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtshadertools {
        {
            488113b3273e2ec486abba0a5495a5916ff80575
            c648b74ce3e769a20bd62792a589c540bceacfedf2f38fc7273d2c39782768f8
            1001780
        }
        ""
        ""
        "qtbase"
        {"Qt Shader Tools"}
        ""
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qt5compat {
        {
            53ae2a93d8568314c696776311650901e563b3c6
            d4b249abb823d575eee9045c24d924ba8d1276e6be7735b287689991d998aa7a
            14638428
        }
        ""
        "path:lib/pkgconfig/icu-uc.pc:icu port:libiconv"
        "qtbase qtdeclarative"
        {"Qt 5 Core Compatibility APIs"}
        "status = deprecated"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qtlanguageserver {
        {
            f03552565c952d8c97fc77760966e4b9d6059ef7
            0e881006442e19c7c4a6d229c342cc685142568e2b41f843e73cddf4c3d37975
            153856
        }
        ""
        ""
        "qtbase"
        {"Qt Language Server"}
        "status = preview"
        "variant overrides: "
        "revision 0"
        "License: "
    }
    qthttpserver {
        {
            1fbd0194e5be04caa30d2c04cec81e719eda0c58
            c37adcadc779b972ceecd739717c4cc3b90487c3387c61eaf2aa94daaf30c179
            121772
        }
        ""
        ""
        "qtbase"
        {"Qt HTTP Server"}
        "status = preview; GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
    qtquick3dphysics {
        {
            312ff38521a1ce290aaa0c1f9bc44799ea2ecd4e
            ab86e8a3c44c31082022259375e7984f9955f81e9de2c846cef563c7562ddb15
            2965840
        }
        ""
        ""
        "qtbase qtdeclarative qtquick3d qtshadertools"
        {"Qt Quick 3D Physics"}
        "status = preview; GPLv3 license only"
        "variant overrides: "
        "revision 0"
        "License: {GPL-3 OpenSSLException}"
    }
}

############################################################################### SQL Plugin Format
#
# {
#     module name
#     {
#         SQL name
#         revision string
#         {list of feature names}
#         {
#             variant name
#             {
#                 variant name
#                 library dependencies
#                 {list of CMake options to find library}
#             }
#         }
#     }
# }
#
###############################################################################
array set sql_plugins {
    sqlite {
        SQLite
        "revision 0"
        {sql-sqlite system-sqlite}
        {
            sqlite3 {
                "port:sqlite3"
                {
                    -DSQLite3_INCLUDE_DIR=${prefix}/include
                    -DSQLite3_LIBRARY=${prefix}/lib/libsqlite3.dylib
                }
            }
        }
    }
    psql {
        PostgreSQL
        "revision 0"
        {sql-psql}
        {
            postgresql15 {
                "port:postgresql15"
                {
                    -DPostgreSQL_INCLUDE_DIR=${prefix}/include/postgresql15
                    -DPostgreSQL_TYPE_INCLUDE_DIR=${prefix}/include/postgresql15
                    -DCMAKE_LIBRARY_PATH=${prefix}/lib/postgresql15
                }
            }
            postgresql14 {
                "port:postgresql14"
                {
                    -DPostgreSQL_INCLUDE_DIR=${prefix}/include/postgresql14
                    -DPostgreSQL_TYPE_INCLUDE_DIR=${prefix}/include/postgresql14
                    -DCMAKE_LIBRARY_PATH=${prefix}/lib/postgresql14
                }
            }
            postgresql13 {
                "port:postgresql13"
                {
                    -DPostgreSQL_INCLUDE_DIR=${prefix}/include/postgresql13
                    -DPostgreSQL_TYPE_INCLUDE_DIR=${prefix}/include/postgresql13
                    -DCMAKE_LIBRARY_PATH=${prefix}/lib/postgresql13
                }
            }
            postgresql12 {
                "port:postgresql12"
                {
                    -DPostgreSQL_INCLUDE_DIR=${prefix}/include/postgresql12
                    -DPostgreSQL_TYPE_INCLUDE_DIR=${prefix}/include/postgresql12
                    -DCMAKE_LIBRARY_PATH=${prefix}/lib/postgresql12
                }
            }
            postgresql11 {
                "port:postgresql11"
                {
                    -DPostgreSQL_INCLUDE_DIR=${prefix}/include/postgresql11
                    -DPostgreSQL_TYPE_INCLUDE_DIR=${prefix}/include/postgresql11
                    -DCMAKE_LIBRARY_PATH=${prefix}/lib/postgresql11
                }
            }
        }
    }
    mysql {
        MySQL
        "revision 0"
        {sql-mysql}
        {
            mariadb10_11 {
                "port:mariadb-10.11"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.11/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.11/mysql/libmariadb.dylib
                }
            }
            mariadb10_10 {
                "port:mariadb-10.10"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.10/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.10/mysql/libmariadb.dylib
                }
            }
            mariadb10_9 {
                "port:mariadb-10.9"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.9/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.9/mysql/libmariadb.dylib
                }
            }
            mariadb10_8 {
                "port:mariadb-10.8"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.8/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.8/mysql/libmariadb.dylib
                }
            }
            mariadb10_9 {
                "port:mariadb-10.7"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.7/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.7/mysql/libmariadb.dylib
                }
            }
            mariadb10_6 {
                "port:mariadb-10.6"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.6/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.6/mysql/libmariadb.dylib
                }
            }
            mariadb10_5 {
                "port:mariadb-10.5"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.5/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.5/mysql/libmariadb.dylib
                }
            }
            mariadb10_4 {
                "port:mariadb-10.6"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mariadb-10.4/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mariadb-10.4/mysql/libmariadb.dylib
                }
            }
            mysql8 {
                "port:mysql8"
                {
                    -DMySQL_INCLUDE_DIR=${prefix}/include/mysql8/mysql
                    -DMySQL_LIBRARY=${prefix}/lib/mysql8/mysql/libmysqlclient.dylib
                }
            }
        }
    }
}

foreach {module module_info} [array get modules] {
    subport ${name}-${module} {
        distname                            ${module}-everywhere-src-${version}

        revision                            [regexp -inline {[0-9]+} [lindex ${module_info} 7]]

        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] {
            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 { [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 } {
            # debugging seems to be turned on if and only if it is turned on in the base
            # CMAKE_CONFIGURATION_TYPES is set with the FORCE option
            # see https://code.qt.io/cgit/qt/qtbase.git/tree/cmake/QtPostProcessHelpers.cmake
            variant debug description {Build both release and debug libraries} {
                require_active_variants     qt6-qtbase debug ""
            }
            if {![variant_isset debug]} {
                require_active_variants     qt6-qtbase "" debug
            }
        }

        if { [lsearch -exact [lindex ${module_info} 6] "~examples"] == -1 } {
            variant examples description {Build examples} {}
        }

        if { [lsearch -exact [lindex ${module_info} 6] "~tests"] == -1 } {
            variant tests description {Enable tests} {}
        }
    }
}

foreach {module module_info} [array get modules] {
    if { [lsearch -exact [lindex ${module_info} 6] "~docs"] != -1 } { continue }

    subport ${name}-${module}-docs {
        distname                            ${module}-everywhere-src-${version}

        revision                            [regexp -inline {[0-9]+} [lindex ${module_info} 7]]

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

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

        description                         Documentation 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                    Documentation for Qt Tool Kit ${qt_major}: ${modules_provided_join}

        supported_archs                     noarch
        universal_variant                   no
    }
}

# see https://doc.qt.io/qt-6/sql-driver.html for info on building SQL Database Drivers
foreach {driver driver_info} [array get sql_plugins] {
    set dbms                                [lindex ${driver_info} 0]

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

    set sql_variants                        [lindex ${driver_info} 3]

    subport ${name}-${driver}-plugin {
        distname                            qtbase-everywhere-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]

        depends_lib-append                  port:${name}-qtbase

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

        foreach feature {sql-db2 sql-ibase sql-mysql sql-oci sql-odbc sql-psql sql-sqlite system-sqlite} {
            if { ${feature} in [lindex ${driver_info} 2] } {
                configure.args-append       -feature-${feature}
            } else {
                configure.args-append       -no-feature-${feature}
            }
        }

        set worksrcpath                     ${worksrcpath}/src/plugins/sqldrivers

        set any_sql                         no
        foreach {variant_name variant_info} ${sql_variants} {
            set conflict_list ""

            foreach {variant_name_other variant_info_other} ${sql_variants} {
                if { ${variant_name_other} ne ${variant_name} } {
                    lappend conflict_list   ${variant_name_other}
                }
            }

            if { [llength ${conflict_list}] > 0 } {
                variant ${variant_name} conflicts {*}${conflict_list} description "use version [regexp -inline {[0-9].} ${variant_name}] of ${dbms}" {}
            }
            if { ([variant_exists ${variant_name}] && [variant_isset ${variant_name}]) || [llength ${conflict_list}]==0 } {
                set any_sql                 yes
            }
        }

        if { !${any_sql} } {
            default_variants-append         +[lindex ${sql_variants} 0]
            if { ![variant_isset [lindex ${sql_variants} 0]] } {
                known_fail                  yes
                pre-extract {
                    ui_error                "At least one SQL variant must be selected."
                    return -code error      "No SQL variant selected."
                }
            }
        }

        foreach {variant_name variant_info} ${sql_variants} {
            if { ([variant_exists ${variant_name}] && [variant_isset ${variant_name}]) || [llength ${conflict_list}]==0 } {
                depends_lib-append          {*}[lindex ${variant_info} 0]
                configure.post_args-append  {*}[subst [lindex ${variant_info} 1]]
            }
        }
    }
}

###############################################################################
# Special Cases
###############################################################################
subport ${name}-qtdeclarative {
    PortGroup                       compiler_blacklist_versions 1.0

    # Xcode 10.3 compiler segfaults
    compiler.blacklist-append       {clang < 1100}
}

subport ${name}-qttools {
    configure.env-append            LLVM_INSTALL_DIR=${prefix}/libexec/llvm-${llvm_version}

    post-destroot {
        # avoid having to add `-Wl,-rpath ${prefix}/libexec/llvm-${llvm_version}/lib` or something like it
        foreach bin {lupdate qdoc} {
            foreach lib {libclang.dylib libclang-cpp.dylib libLLVM.dylib} {
                system              "install_name_tool -change @rpath/${lib} ${prefix}/libexec/llvm-${llvm_version}/lib/${lib} ${destroot}${qt6.dir}/bin/${bin}"
            }
        }
    }
}

subport ${name}-qtwebengine {
    configure.env-append            PYTHON3_PATH=${prefix}/Library/Frameworks/Python.framework/Versions/${python_branch}/bin
    # in ${worksrcpath}, `${qt6.dir}/bin/qt-configure-module . -help` and `${qt6.dir}/bin/qt-configure-module . -list-features`
    # it is not clear why, but icu and ffmpeg support must be added manually
    # native-spellchecker support is off by default
    # support for proprietary codecs could by added via `-webengine-proprietary-codecs`
    configure.args-append           -webengine-icu \
                                    -webengine-ffmpeg \
                                    -webengine-native-spellchecker

    # avoid
    #     xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer
    #     directory '/Library/Developer/CommandLineTools' is a command line tools
    #     instance
    use_xcode                       yes
}

subport ${name}-qtmultimedia {
    # GStreamer will be found if gstreamer1 and gstreamer1-gst-plugins-base are installed
    # however, an error will ensue since the GStreamer support requires "Linux DMA buffer support"
    # see
    #     https://code.qt.io/cgit/qt/qtmultimedia.git/tree/src/plugins/multimedia/gstreamer/CMakeLists.txt
    #     https://code.qt.io/cgit/qt/qtmultimedia.git/tree/src/multimedia/configure.cmake
    configure.args-append           -no-gstreamer
}

subport ${name}-qt5compat {
    patchfiles-append               patch-qt5compat-find_libs.diff
}

subport ${name}-qtspeech {
    # ALSA is Linux only (https://www.alsa-project.org/wiki/Main_Page)
    # Speech Dispatcher *might* be made to work on macOS (https://freebsoft.org/speechd)
    configure.args-append           -no-flite-alsa \
                                    -no-speechd
}

if { ${subport} eq "${name}-qtbase" || ${subport} eq "${name}-qtbase-docs" } {
    # allow building with macOS 10.14 SDK
    # see https://trac.macports.org/ticket/64345
    patchfiles-append               patch-qtbase-macos_10.14_sdk.diff

    configure.pre_args-replace      --prefix=${prefix} \
                                    "-prefix ${qt6.dir}"

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

    # SQL Options
    foreach driver {db2 ibase mysql oci odbc psql sqlite} {
        configure.args-append       -no-sql-${driver}
    }

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

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

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

    # additional options:
    configure.args-append           {-make tools} \
                                    {-nomake examples} \
                                    {-nomake tests} \
                                    -gui \
                                    -widgets \
                                    -cups \
                                    -no-evdev \
                                    -no-tslib \
                                    -icu \
                                    -no-fontconfig \
                                    -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-system-proxies \
                                    -no-libudev \
                                    -no-egl

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

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

    # attempt to avoid @rpath
    configure.args-append           -no-rpath \
                                    -no-feature-relocatable
}

subport ${name}-qtbase {
    # this subport uses configure script
    PortGroup                       openssl 1.0

    build.cmd                       ninja
    build.post_args-append          -v
    destroot.target                 install

    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

        # configure has issues locating openssl files
        configure.pre_args-append   -DOPENSSL_ROOT_DIR=[openssl::install_area] \
                                    -DOPENSSL_INCLUDE_DIR=[openssl::include_dir] \
                                    -DOPENSSL_LIBRARIES=[openssl::install_area]/lib \
                                    -DOPENSSL_LIBS='-L[openssl::install_area]/lib -lssl -lcrypto'
    }
    default_variants-append         +openssl

    post-destroot {
        # do not record  __qt_initial_c_compiler and __qt_initial_cxx_compiler
        reinplace                   "s|${configure.cc}|/usr/bin/clang|g" \
                                    ${destroot}${qt6.dir}/lib/cmake/Qt6/qt.toolchain.cmake
        reinplace                   "s|${configure.cxx}|/usr/bin/clang++|g" \
                                    ${destroot}${qt6.dir}/lib/cmake/Qt6/qt.toolchain.cmake

        # return to default value of CMAKE_OSX_DEPLOYMENT_TARGET
        #reinplace                   "s|CMAKE_OSX_DEPLOYMENT_TARGET \"${macosx_deployment_target}\"|CMAKE_OSX_DEPLOYMENT_TARGET \"10.14\"|g" \
        #                            ${destroot}${qt6.dir}/lib/cmake/Qt6/qt.toolchain.cmake

        # do not record QT_SOURCE_TREE
        reinplace                   "s|${worksrcpath}|${qt6.dir}/src|g" \
                                    ${destroot}${qt6.dir}/lib/cmake/Qt6BuildInternals/QtBuildInternalsExtra.cmake

        # do not record original_cmake_path
        reinplace                   "s|${prefix}/bin/cmake|/Applications/CMake.app/Contents/bin/cmake|g" \
                                    ${destroot}${qt6.dir}/bin/qt-cmake
        reinplace                   "s|${prefix}/bin/cmake|/Applications/CMake.app/Contents/bin/cmake|g" \
                                    ${destroot}${qt6.dir}/bin/qt-cmake-private
    }

    # 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 false }
    configure.sdkroot
    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
}

subport ${name}-qtbase-docs {
    # see https://wiki.qt.io/Building_Qt_Documentation
    build.cmd                      cmake --build ${build.dir}
    destroot.target
    build.target                   --target docs
    destroot.cmd                   cmake --build ${build.dir} --target install_docs
}

###############################################################################
# Applies to allmost all modules
###############################################################################
if { ${subport} ne "${name}" && ${subport} ne "${name}-qtbase" && ${subport} ne "${name}-qtbase-docs" } {
    # attempting to match module configure instructions from here: https://www.qt.io/blog/qt-6-build-system
    configure.cmd                   ${qt6.dir}/bin/qt-configure-module
    configure.dir                   ${workpath}/build
    configure.pre_args              ${worksrcpath}
    configure.args-append           -verbose

    build.dir                       ${workpath}/build
    build.cmd                       cmake --build ${build.dir}

    destroot.target

    if { [lindex [split ${subport} -] end] ne "docs" } {
        build.target
        destroot.cmd               cmake --install ${build.dir}
    } else {
        build.target               --target docs
        destroot.cmd               cmake --build ${build.dir} --target install_docs
    }

    if { [variant_exists examples] } {
        configure.post_args-append  -DQT_BUILD_EXAMPLES=[expr {[variant_isset examples] ? ON : OFF}]
    }
    if { [variant_exists tests] } {
        configure.post_args-append  -DQT_BUILD_TESTS=[expr {[variant_isset tests] ? ON : OFF}]
    }
}

###############################################################################
# Applies to all subports except ${name}
#     this code may depend on the subport specific code (e.g., configure.cc)
###############################################################################
depends_build-append                path:bin/cmake:cmake \
                                    port:ninja \
                                    port:pkgconfig

# 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

configure.universal_args

test.cmd                            ctest
test.args                           -V
test.target

# ninja needs the DESTDIR argument in the environment
destroot.destdir
destroot.env-append                 DESTDIR=${destroot}

# set CMake variables (similar to what cmake PortGroup does)
# to allow using ccache and controlling compiler selection
configure.post_args-prepend         --
configure.post_args-append          -DCMAKE_C_COMPILER=[option configure.cc] \
                                    -DCMAKE_CXX_COMPILER=[option configure.cxx] \
                                    -DCMAKE_OBJC_COMPILER=[option configure.objc] \
                                    -DCMAKE_OBJCXX_COMPILER=[option configure.objcxx] \
                                    -DCMAKE_OSX_DEPLOYMENT_TARGET=${macosx_deployment_target}
if { ${configure.ccache} } {
    if { ${subport} ne "${name}-qtbase" } {
        configure.post_args-append  -DQT_USE_CCACHE=ON
    } else {
        # do not use `configure.args-append -ccache`
        # or `configure.post_args-append -DQT_USE_CCACHE=1`
        # since that affects installed files.
        configure.post_args-append  -DCMAKE_C_COMPILER_LAUNCHER=${prefix}/bin/ccache \
                                    -DCMAKE_CXX_COMPILER_LAUNCHER=${prefix}/bin/ccache \
                                    -DCMAKE_OBJC_COMPILER_LAUNCHER=${prefix}/bin/ccache \
                                    -DCMAKE_OBJCXX_COMPILER_LAUNCHER=${prefix}/bin/ccache
    }
}

test.run                            [expr {[variant_exists tests] && [variant_isset tests] ? yes : no}]

if { ${universal_possible} && [variant_isset universal] } {
    configure.post_args-append      -DCMAKE_OSX_ARCHITECTURES="[join ${configure.universal_archs} \;]"
} elseif { ${configure.build_arch} ne ""  } {
    configure.post_args-append      -DCMAKE_OSX_ARCHITECTURES="${configure.build_arch}"
}

# attempt to avoid @rpath
configure.post_args-append          -DCMAKE_INSTALL_NAME_DIR=${qt6.dir}/lib

post-destroot {
    fs-traverse f ${destroot} {
        if { [file isfile ${f}] && [file extension ${f}] eq ".prl" } {
            # do not record build directory
            reinplace -q            "/^QMAKE_PRL_BUILD_DIR = /d" \
                                    ${f}
        }
    }

    # .app and non-.app programs are both put in ${qt6.dir}/bin
    # put a link of any .app programs in the ${applications_dir}/Qt6
    xinstall -d -m 0755             ${destroot}${applications_dir}/Qt6
    foreach app [glob -nocomplain -tails -directory ${destroot}${qt6.dir}/bin *.app] {
        ln -s                       ${qt6.dir}/bin/${app} \
                                    ${destroot}${applications_dir}/Qt6
    }
}

livecheck.type                      none

###############################################################################
# Special Case: must be near the end since it undoes other code
###############################################################################
subport ${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
    test.run                        no
    depends_extract
    depends_build
    depends_lib
    depends_test

    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

    livecheck.type                  regex
    livecheck.url                   https://download.qt.io/archive/qt/${branch}/
    livecheck.regex                 (\\d+(\\.\\d+)+)
}
