# -*- 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           python 1.0
PortGroup           qt6 1.0

name                py-pyside6
revision            1
categories-append   devel aqua

maintainers         {@johankytt gmail.com:johan.kytt} openmaintainer
license             LGPL
supported_archs     arm64 x86_64

python.pep517       no
universal_variant   no
use_xz              yes

description         Python bindings for the Qt cross-platform application and UI framework
long_description    PySide6 is the official Python module from the Qt for Python project, \
                    which provides access to the complete Qt 6.0+ framework.\
                    \nFor module details see https://doc.qt.io/qtforpython-6/package_details.html
homepage            https://pyside.org/

# Qt 6.8 is not supported on macOS 13 (Darwin 22)
if {[vercmp ${qt6.version} 6.8] >= 0} {
    version 6.11.1
    checksums           rmd160  7017462608f9edb3cc5da47a2e088ef93d236c0d \
                        sha256  6ffd9835bb0dd2c56f061d62f1616bb1707cfc0202b80e3165d6be087f3965e2 \
                        size    17963432

    set clang_version   22
    # Same clang version as for qt6-qtbase
} else {
    version             6.7.3
    checksums           rmd160  38dea392d656b16f4c0ad6b8e0971740f2080805 \
                        sha256  a4c414be013d5051a2d10a9a1151e686488a3172c08a57461ea04b0a0ab74e09 \
                        size    14591496

    set clang_version   18
}
python.versions     310 311 312 313 314

distname            pyside-setup-everywhere-src-${version}
master_sites        https://download.qt.io/official_releases/QtForPython/pyside6/PySide6-${version}-src

if {${name} ne ${subport}} {
    # 6.10 cannot be built if 6.9 is active. If this continues in the next version,
    # consider uncommenting this block
    #conflicts_build \
    #    ${subport}

    if {${python.version} >= 313} {
        platforms   {darwin >= 23}
    }

    depends_lib-append \
        port:clang-${clang_version} \
        port:libxml2 \
        port:libxslt \
        port:py${python.version}-numpy

    depends_build-append \
        path:bin/cmake:cmake \
        path:bin/ninja:ninja \
        port:py${python.version}-packaging \
        port:py${python.version}-setuptools

    set essential_modules "Core,Gui,Widgets,Help,Network,Concurrent,DBus,Designer,OpenGL,OpenGLWidgets,PrintSupport,Qml,Quick,QuickControls2,QuickTest,QuickWidgets,Xml,Test,Sql,Svg,SvgWidgets,UiTools"

    build.args-append \
        --qtpaths=${prefix}/libexec/qt6/bin/qtpaths6 \
        --cmake=${prefix}/bin/cmake \
        --parallel=${build.jobs} \
        --module-subset=${essential_modules}

    build.env-append \
        LLVM_INSTALL_DIR=${prefix}/libexec/llvm-${clang_version}

    if {${configure.sdkroot} != ""} {
        build.args-append \
            --macos-sysroot=${configure.sdkroot}
    }

    destroot.args-append \
        --qtpaths=${prefix}/libexec/qt6/bin/qtpaths6 \
        --cmake=${prefix}/bin/cmake \
        --skip-cmake \
        --reuse-build \
        --module-subset=${essential_modules}

    qt6.depends_lib-append \
        qttools


    variant addonmodules description "Install the addon modules. See https://doc.qt.io/qtforpython-6/package_details.html" {
        build.args-delete --module-subset=${essential_modules}
        destroot.args-delete --module-subset=${essential_modules}

        qt6.depends_lib-append \
            qtlocation \
            qtsensors \
            qtconnectivity \
            qtserialbus \
            qt3d \
            qtwebview \
            qtcharts \
            qtdatavis3d \
            qtvirtualkeyboard \
            qtscxml \
            qtspeech \
            qtnetworkauth \
            qtremoteobjects \
            qtquicktimeline \
            qtquick3dphysics \
            qthttpserver \
            qtquickeffectmaker \
            qtgraphs
    }

    post-destroot {
        # Python 3.14 has trouble extracting IMPORTED_LOCATION even with extra cmake files
        # This is the workaround
        ln -s ../[file tail {*}[glob ${destroot}${python.pkgd}/PySide6/libpyside6.abi3*.dylib]] ${destroot}${python.pkgd}/PySide6/lib/libpyside6.abi3.dylib
        ln -s ../[file tail {*}[glob ${destroot}${python.pkgd}/PySide6/libpyside6.abi3*.dylib]] ${destroot}${python.pkgd}/PySide6/lib/libpyside6qml.abi3.dylib
        ln -s ../[file tail {*}[glob ${destroot}${python.pkgd}/shiboken6/libshiboken6.abi3*.dylib]] ${destroot}${python.pkgd}/shiboken6/lib/libshiboken6.abi3.dylib
    }

    # Global soversion for cmake/pc files variant
    # SOVERSION is major.minor, see ${worksrcpath}/sources/shiboken6/cmake/ShibokenSetup.cmake and search _library_so_version
    set soversion [join [lrange [split $version .] 0 1] .]

    variant extracmakepcfiles description "Install extra .cmake and .pc files to ${prefix}/lib/cmake and ${prefix}/lib/pkgconfig" {
        set python.branch [string index ${python.version} 0].[string range ${python.version} 1 end ]

        post-destroot {
            # manually install and patch .cmake and pkgconfig .pc files
            xinstall -d ${destroot}${prefix}/lib/cmake/
            copy {*}[glob ${worksrcpath}/build/qfp-py${python.branch}-qt${qt6.version}*/install/lib/cmake/*] ${destroot}${prefix}/lib/cmake/
            copy {*}[glob ${worksrcpath}/build/qfp-py${python.branch}-qt${qt6.version}*/install/lib/pkgconfig/*.pc] ${destroot}${prefix}/lib/pkgconfig/
            # All .pc files
            reinplace "s|prefix=.*|prefix=${python.prefix}|g" \
                {*}[glob ${destroot}${prefix}/lib/pkgconfig/*.pc]
            # pyside6.pc
            reinplace "s|includedir=.*|includedir=${python.pkgd}/PySide6/include|g" \
                ${destroot}${prefix}/lib/pkgconfig/pyside6.pc
            reinplace "s|typesystemdir=.*|typesystemdir=${python.pkgd}/PySide6/typesystems|g" \
                ${destroot}${prefix}/lib/pkgconfig/pyside6.pc
            reinplace "s|gluedir=.*|gluedir=${python.pkgd}/PySide6/glue|g" \
                ${destroot}${prefix}/lib/pkgconfig/pyside6.pc
            reinplace "s|libdir=.*|libdir=${python.pkgd}/PySide6|g" \
                ${destroot}${prefix}/lib/pkgconfig/pyside6.pc
            reinplace "s|pythonpath=.*|pythonpath=${python.pkgd}|g" \
                ${destroot}${prefix}/lib/pkgconfig/pyside6.pc
            # shiboken6.pc
            reinplace "s|libdir=.*|libdir=${python.pkgd}/shiboken6|g" \
                ${destroot}${prefix}/lib/pkgconfig/shiboken6.pc
            reinplace "s|includedir=.*|includedir=${python.pkgd}/shiboken6/include|g" \
                ${destroot}${prefix}/lib/pkgconfig/shiboken6.pc
            # All .cmake files
            reinplace "s|${version}\\.dylib|${soversion}.dylib|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/*/*Targets*.cmake]
            reinplace "s|IMPORTED_LOCATION_RELEASE|IMPORTED_LOCATION|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/*/*Targets*.cmake]
            # PySide6 .cmake files
            reinplace "s|\${_IMPORT_PREFIX}/lib|${python.pkgd}/PySide6|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/PySide6*/*Targets*.cmake]
            reinplace "s|\${_IMPORT_PREFIX}/\[a-zA-Z0-9\]\*/include|${python.pkgd}/PySide6/include|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/PySide6*/*Targets*.cmake]
            reinplace "s|PYSIDE_PYTHONPATH \".*\"|PYSIDE_PYTHONPATH \"${python.pkgd}/PySide6\"|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/PySide6*/*Config*.cmake]
            reinplace "s|PYSIDE_TYPESYSTEMS \".*\"|PYSIDE_TYPESYSTEMS \"${python.pkgd}/PySide6/typesystems\"|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/PySide6*/*Config*.cmake]
            reinplace "s|PYSIDE_GLUE \".*\"|PYSIDE_GLUE \"${python.pkgd}/PySide6/glue\"|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/PySide6*/*Config*.cmake]
            # Shiboken6 .cmake files
            reinplace "s|\${_IMPORT_PREFIX}/lib|${python.pkgd}/shiboken6|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/Shiboken6*/*Targets*.cmake]
            reinplace "s|\${_IMPORT_PREFIX}/bin|${python.prefix}/bin|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/Shiboken6*/*Targets*.cmake]
            reinplace "s|\${_IMPORT_PREFIX}/\[a-zA-Z0-9\]\*/include|${python.pkgd}/shiboken6/include|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/Shiboken6*/*Targets*.cmake]
            reinplace "s|SHIBOKEN_PYTHON_MODULE_DIR \".*\"|SHIBOKEN_PYTHON_MODULE_DIR \"${python.pkgd}/shiboken6\"|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/Shiboken6*/*Config*.cmake]
            reinplace "s|SHIBOKEN_SHARED_LIBRARY_DIR \".*\"|SHIBOKEN_SHARED_LIBRARY_DIR \"${python.pkgd}/shiboken6\"|g" \
                {*}[glob ${destroot}${prefix}/lib/cmake/Shiboken6*/*Config*.cmake]
        }
    }

    variant qtlottie description "Install the Qt Lottie Animation module used with the Bodymovin plugin for Adobe After Effects" {
        qt6.depends_lib-append \
            qtlottie
    }

    variant qt5compat description "Install the Qt5Compat module" {
        qt6.depends_lib-append \
            qt5compat
    }

    default_variants    +addonmodules +extracmakepcfiles
    livecheck.type      none
} else {
    livecheck.type      regex
    livecheck.url       https://download.qt.io/official_releases/QtForPython/pyside6/
    livecheck.regex     (\\d+\\.\\d+\\.\\d+)
}
