# -*- 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.\
                    \nBy default, installs both the essential and addon modules. \
                    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.9.0
    checksums           rmd160  a93de1f959f333e4b7af85e09e87000a8e079e46 \
                        sha256  315b73bb7570d5b9e6793a8d3fafd1d2dd7f43d35eebb01d2b554ea206aad88e \
                        size    16652756

    python.versions     310 311 312 313
    set clang_version   19
    # Same clang version as for qt6-qtbase
} else {
    version             6.7.3
    checksums           rmd160  38dea392d656b16f4c0ad6b8e0971740f2080805 \
                        sha256  a4c414be013d5051a2d10a9a1151e686488a3172c08a57461ea04b0a0ab74e09 \
                        size    14591496

    python.versions     39 310 311 312
    set clang_version   18
}

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

if {${name} ne ${subport}} {
    depends_lib-append \
        port:py${python.version}-numpy

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

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

    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

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

    set python.branch [string index ${python.version} 0].[string range ${python.version} 1 end ]
    # SOVERSION is major.minor, see ${worksrcpath}/sources/shiboken6/cmake/ShibokenSetup.cmake and search _library_so_version
    set soversion [join [lrange [split $version .] 0 1] .]
    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_generator/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}/include/\[a-zA-Z0-9\]\*|${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}/include/\[a-zA-Z0-9\]\*|${python.pkgd}/shiboken6_generator/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 essentialsonly description "Installs only the essential modules. See https://doc.qt.io/qtforpython-6/package_details.html" {
        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 \
            --module-subset=${essential_modules}
        destroot.args-append \
            --module-subset=${essential_modules}

        qt6.depends_lib \
            qttools
    }

    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
    }

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