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

name                py-wxpython-4.0
python.rootname     wxPython
# check kicad's version before updating
version             4.2.2
revision            1

categories-append   graphics devel
license             wxwidgets-3.1
maintainers         nomaintainer

description         Python interface to the wxWidgets 3.2 cross platform GUI
long_description    wxPython is a GUI toolkit for the Python programming \
                    language. It allows Python programmers to create \
                    programs with a robust, highly functional graphical \
                    user interface, simply and easily. It is implemented \
                    as a Python extension module (native code) that wraps \
                    the wxWidgets cross platform GUI library, written in C++.

homepage            https://www.wxpython.org/

checksums           rmd160  37f6feb94e4aacc7e41a377ce19278a26a39cb4a \
                    sha256  5dbcb0650f67fdc2c5965795a255ffaa3d7b09fb149aa8da2d0d9aa44e38e2ba \
                    size    57358880

compiler.cxx_standard     2011

python.versions     39 310 311 312 313

if {${name} ne ${subport}} {
    if {${os.platform} eq "darwin" && [vercmp ${macosx_deployment_target} >= 15.0]} {
        macosx_deployment_target 14.0
    }

    patchfiles-append \
                    buildtools_build_wxwidgets_py.patch \
                    buildtools_config_py.patch \
                    buildtools_config_sip.patch \
                    build_py.patch

    depends_build-append \
        port:py${python.version}-cython \
        port:py${python.version}-requests \
        port:py${python.version}-setuptools \
        port:py${python.version}-sip
        

    depends_lib-append \
        port:tiff

    depends_run-append \
        port:py${python.version}-numpy \
        port:py${python.version}-Pillow

    set wxpython_options "--no_magic --release --python ${python.bin} --osx_cocoa --nodoc --prefix=${python.prefix}"
    build {
        system -W ${worksrcpath} "${python.bin} build.py build_wx ${wxpython_options}"
        system -W ${worksrcpath} "${python.bin} build.py dox etg sip ${wxpython_options}"
        system -W ${worksrcpath} "${python.bin} build.py build_py ${wxpython_options}"
    }
    build.env-append SIP=${python.prefix}/bin/sip
    build.env-append PATH=${python.prefix}/bin:$env(PATH)

    destroot.cmd ${python.bin} build.py
    destroot.target install
    destroot.destdir --destdir=${destroot} ${wxpython_options}

    post-destroot {
        # relink rpaths of wxWidgets' binaries
        foreach wxbin [split [exec find ${destroot}${python.prefix}/bin/ -type f] \n] {
            if {[string match *text* [exec file ${wxbin}]] == 0} {
                foreach line [lrange [split [exec otool -L ${wxbin}] \n] 1 end] {
                    if {[regexp {\t(.*)(libwx_.*dylib)} $line matched path lib] == 1} {
                        system "install_name_tool -change ${path}${lib} ${python.prefix}/lib/${lib} ${wxbin}"
                    }
                }
            }
        }
    }

    post-patch {
        reinplace "s|MACOSX_DEPLOYMENT_TARGET = \"10.10\"|MACOSX_DEPLOYMENT_TARGET = \"${macosx_deployment_target}\"|" \
            ${worksrcpath}/wscript
        reinplace "s|--with-macosx-version-min=10.10|--with-macosx-version-min=${macosx_deployment_target}|" \
            ${worksrcpath}/buildtools/build_wxwidgets.py
    }

    test.run yes
    test.cmd "${python.bin} -c 'import wx ; print(wx.__version__)'"
    test.target
}