# -*- 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            0
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.2
    revision            0
    checksums           rmd160  fa924688d68fd7b097b39b1acac4167092aefeb3 \
                        sha256  cba47efbaad1bedd529725cbc14e21f156c7a19366f07b3edfbb076ffd7afdf8 \
                        size    18053248

    set clang_version   22
    # Same clang version as for qt6-qtbase
} else {
    version             6.7.3
    revision            2
    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}} {
    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"
    set python.branch [string index ${python.version} 0].[string range ${python.version} 1 end]
    set internal_install_dir \
        ${worksrcpath}/build/qfp-py${python.branch}-qt${version}-64bit-release/install

    build.args-append \
        --qtpaths=${prefix}/libexec/qt6/bin/qtpaths6 \
        --cmake=${prefix}/bin/cmake \
        --parallel=${build.jobs} \
        --shiboken-target-path=${internal_install_dir} \
        --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

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

    variant extracmakepcfiles description "Install extra .cmake and .pc files to ${prefix}/lib/cmake and ${prefix}/lib/pkgconfig" {
        post-destroot {
            # Upstream installs wheel-aware CMake packages in site-packages.
            # Add forwarding packages where CMake will find them automatically.
            foreach {cmake_package cmake_package_dir cmake_targets} [list \
                PySide6 \
                    ${python.pkgd}/PySide6/lib/cmake/PySide6 \
                    {PySide6::pyside6 PySide6::pyside6qml} \
                Shiboken6 \
                    ${python.pkgd}/shiboken6/lib/cmake/Shiboken6 \
                    {Shiboken6::libshiboken} \
                Shiboken6Tools \
                    ${python.pkgd}/shiboken6_generator/lib/cmake/Shiboken6Tools \
                    {Shiboken6::shiboken6} \
            ] {
                set global_cmake_dir ${destroot}${prefix}/lib/cmake/${cmake_package}
                xinstall -d ${global_cmake_dir}

                set config_fd [open ${global_cmake_dir}/${cmake_package}Config.cmake w]
                puts ${config_fd} "include(\"${cmake_package_dir}/${cmake_package}Config.cmake\")"
                foreach cmake_target ${cmake_targets} {
                    puts ${config_fd} "get_target_property(_imported_location ${cmake_target} IMPORTED_LOCATION_RELEASE)"
                    puts ${config_fd} "if(_imported_location)"
                    puts ${config_fd} "    set_property(TARGET ${cmake_target} PROPERTY IMPORTED_LOCATION \"\${_imported_location}\")"
                    puts ${config_fd} "endif()"
                }
                puts ${config_fd} "unset(_imported_location)"
                close ${config_fd}

                xinstall -m 0644 \
                    ${destroot}${cmake_package_dir}/${cmake_package}ConfigVersion.cmake \
                    ${global_cmake_dir}
            }

            # Upstream does not package the pkg-config files. Install them next
            # to their libraries and link them into pkg-config's global path,
            # following the layout used by qt6-qtbase.
            set build_pkgconfig_dir [lindex [glob -types d \
                ${worksrcpath}/build/qfp-py${python.branch}-qt${qt6.version}*/install/lib/pkgconfig] 0]
            set pyside_pc ${python.pkgd}/PySide6/lib/pkgconfig/pyside6.pc
            set shiboken_pc ${python.pkgd}/shiboken6/lib/pkgconfig/shiboken6.pc

            xinstall -d \
                ${destroot}${python.pkgd}/PySide6/lib/pkgconfig \
                ${destroot}${python.pkgd}/shiboken6/lib/pkgconfig \
                ${destroot}${prefix}/lib/pkgconfig
            xinstall -m 0644 ${build_pkgconfig_dir}/pyside6.pc \
                ${destroot}${pyside_pc}
            xinstall -m 0644 ${build_pkgconfig_dir}/shiboken6.pc \
                ${destroot}${shiboken_pc}

            ln -s ${pyside_pc} ${destroot}${prefix}/lib/pkgconfig
            ln -s ${shiboken_pc} ${destroot}${prefix}/lib/pkgconfig

            reinplace "s|prefix=.*|prefix=${python.prefix}|g" \
                ${destroot}${pyside_pc} \
                ${destroot}${shiboken_pc}
            # pyside6.pc
            reinplace "s|includedir=.*|includedir=${python.pkgd}/PySide6/include|g" \
                ${destroot}${pyside_pc}
            reinplace "s|typesystemdir=.*|typesystemdir=${python.pkgd}/PySide6/typesystems|g" \
                ${destroot}${pyside_pc}
            reinplace "s|gluedir=.*|gluedir=${python.pkgd}/PySide6/glue|g" \
                ${destroot}${pyside_pc}
            reinplace "s|libdir=.*|libdir=${python.pkgd}/PySide6/lib|g" \
                ${destroot}${pyside_pc}
            reinplace "s|pythonpath=.*|pythonpath=${python.pkgd}|g" \
                ${destroot}${pyside_pc}
            # shiboken6.pc
            reinplace "s|libdir=.*|libdir=${python.pkgd}/shiboken6/lib|g" \
                ${destroot}${shiboken_pc}
            reinplace "s|includedir=.*|includedir=${python.pkgd}/shiboken6/include|g" \
                ${destroot}${shiboken_pc}
        }
    }

    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
    }

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