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

# Pegged version for old systems.
# https://github.com/ronaldoussoren/pyobjc/issues/587
# The port is not going to be updated beyond maintenance and fix-ups.
github.setup        ronaldoussoren pyobjc 6.2.2 v
revision            1

checksums           rmd160  7387bc8c6ec744ca2cf6328036dde9826bcb2763 \
                    sha256  0730edcdc5ca0bbc1e10983d3a7ef143ea80dfec9db8b02eb81d64e4fc18635f \
                    size    13008288
github.tarball_from archive

name                py-pyobjc6
categories-append   devel
license             MIT
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
platforms           {darwin < 11}
description         Bidirectional bridge between Python and Objective C
long_description    The PyObjC project aims to provide a bridge between \
                    the Python and Objective-C programming languages. \
                    The bridge is intended to be fully bidirectional, \
                    allowing the Python programmer to take full advantage \
                    of the power provided by various Objective-C-based \
                    toolkits and the Objective-C programmer transparent \
                    access to Python based functionality.
homepage            https://pyobjc.readthedocs.io

# It fails to build with Python 3.11+
# Extension error (examples): Handler <function build_examples at 0x4e29168>
# for event 'builder-inited' threw an exception (exception: invalid mode: 'rU')
# hello <sphinx.application.Sphinx object at 0x4420870>
python.versions     38 39 310
# Please refrain from dropping Python versions support here.

# https://trac.macports.org/ticket/69728
python.pep517       no

# FIXME: optional modules do not build at the moment.

if {${name} ne ${subport}} {
    depends_lib-append \
                    port:libffi \
                    port:py${python.version}-setuptools
    depends_build   port:py${python.version}-sphinx

    # The patches below suppress cleaning on incremental builds,
    # as MacPorts handles that itself, and disable a Sphinx extension
    # not present in MacPorts.
    patchfiles-append \
                    patch-docs-conf.py.diff \
                    patch-install.py.diff

    # Backport newer code from upstream.
    # Based on https://github.com/ronaldoussoren/pyobjc/commit/db2cdc8b2df25c1ef871534edd1ed97b1c89aa6e
    # and https://github.com/ronaldoussoren/pyobjc/commit/2ae69be6c9b185417fa472f7a961771ae2600405
    patchfiles-append \
                    0001-Fix-obsolete-Python-API.patch

    platform darwin powerpc {
        # Exclude this for ppc:
        patchfiles-append \
                    0002-module.m-no-AssociatedObject-on-ppc.patch
    }

    if {[string match *gcc* ${configure.compiler}]} {
        patchfiles-append \
                    0003-properties.m-disable-test-that-fails-to-compile-with.patch
    }

    if {${python.version} > 39} {
        patchfiles-append \
                    0004-struct-wrapper-fix-for-python-3.10.patch
    }

    post-patch {
        reinplace "s|use-system-libffi = 0|\\
use-system-libffi = 1\\
deployment-target = ${macosx_deployment_target}\\
|" ${worksrcpath}/pyobjc-core/setup.cfg

        reinplace \
            "s/sphinx-build /sphinx-build-${python.branch} /g" \
            ${worksrcpath}/docs/Makefile
    }

    if {${configure.sdkroot} eq ""} {
        set sdkroot "/"
    } else {
        set sdkroot ${configure.sdkroot}
    }

    build.env-append    SDKROOT=${sdkroot}
    destroot.env-append SDKROOT=${sdkroot}

    # The pyobjc build system is vaguely horrible,
    # so do everything in the destroot target.
    build {
        system -W ${worksrcpath}/docs "make html"
    }

    # Don't pass --no-user-cfg, as the build system subclasses
    # the install command from distutils, not setuptools;
    # in fact, doing so breaks the build...
    destroot.cmd "${python.bin} install.py"

    destroot.destdir    --prefix=${prefix}/lib/${subport} --root=${destroot}

    post-destroot {
        xinstall -m 644 -W ${worksrcpath}/pyobjc-core HISTORIC.txt Install.txt \
            License.txt README.txt \
            ${destroot}${prefix}/share/doc/${subport}
        copy ${worksrcpath}/docs/_build/html ${destroot}${prefix}/share/doc/${subport}/html
    }

}

livecheck.type      none
