# -*- 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.4
revision            0

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/
master_sites        pypi:w/wxpython
distname            wxpython-${version}


checksums           rmd160  66265daa26366a13a888bfaed26150e6d9d10624 \
                    sha256  2eb123979c87bcb329e8a2452269d60ff8f9f651e9bf25c67579e53c4ebbae3c \
                    size    58583054

compiler.cxx_standard     2011

worksrcdir          ${python.rootname}-${version}

python.versions     310 311 312 313 314

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

    patchfiles-append \
                    buildtools_config_py.patch \
                    build_py.patch \
                    dd3f2509499e0e6aac4ebf8dab72b5d496c5bfbc.patch \

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

    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} "DOXYGEN=${prefix}/bin/doxygen ${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
}
