# -*- 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-sip4
categories-append   devel
maintainers         {michaelld @michaelld} openmaintainer
license             {GPL-2 GPL-3 PSF}
platforms           macosx

description         create Python bindings for C and C++ libraries
long_description \
    SIP is a tool that makes it very easy to create \
    Python bindings for C and C++ libraries. \
    It was originally developed to create PyQt, \
    the Python bindings for the Qt toolkit, \
    but can be used to create bindings for any \
    C or C++ library.

homepage            https://www.riverbankcomputing.com/software/sip/intro

version             4.19.25

# from siplib/sip.h[.in]:
#
# Define the current API version number.  SIP must handle modules with
# the same major number and with the same or earlier minor number.
# Whenever data structure elements are added they must be appended and
# the minor number incremented.  Whenever data structure elements are
# removed or the order changed then the major number must be
# incremented and the minor number set to 0.
#
# SIP 4.19.[19-25] provides SIP API 12.7
# SIP 4.19.[14-18] provides SIP API 12.6
# SIP 4.19.[12-13] provides SIP API 12.5
# SIP 4.19.[9-11] do not work with MacPorts
# -> see the "epoch" note above for more info
# SIP 4.19.8 provides SIP API 12.4
# SIP 4.19.[4-7] provides SIP API 12.3
# SIP 4.19.3 provides SIP API 12.2
# SIP 4.19.[1-2] provides SIP API 12.1
# SIP 4.19.0 provides SIP API 12.0
# SIP 4.18.[0-1] provides SIP API 11.3
# SIP 4.17.0 provides SIP API 11.2
# SIP 4.16.[8-9] provides SIP API 11.2
#  -> BUT: the change is to the private API, not the public one.
#  -> SO: no rev-bumps are be needed.
# SIP 4.16.[1-7] provides SIP API 11.1.
# SIP 4.15.5 provided SIP API 11.0.

python.versions     27 35 36 37 38 39 310

if {${name} ne ${subport}} {
    master_sites        https://www.riverbankcomputing.com/static/Downloads/sip/${version}

    set branch          [join [lrange [split ${version} .] 0 1] .]
    set patch           [lindex [split ${version} .] 2]

    if {${patch} == 0} {
        distname        sip-${branch}
    } else {
        distname        sip-${version}
    }

    checksums           rmd160 9709eda79d7e0a73eca82e25cbbfd0af6da074c8 \
                        sha256 b39d93e937647807bac23579edbff25fe46d16213f708370072574ab1f1b4211 \
                        size   1056384
    revision            2

    patchfiles          patch-siputils.py.diff \
                        patch-specs_macx-g++.diff \
                        patch-configure.py.diff \
                        patch-add_ppc64.diff

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

    use_configure       yes

    pre-configure {
        # Ensure correct compilers are used
        reinplace "s|@CC@|${configure.cc}|" \
            ${worksrcpath}/specs/macx-g++
        reinplace "s|@CXX@|${configure.cxx}|" \
            ${worksrcpath}/specs/macx-g++
    }

    configure.cmd       ${python.bin} configure.py
    configure.pre_args  -b ${python.prefix}/bin \
                        -d ${python.pkgd} \
                        -e ${python.include} \
                        -v ${python.prefix}/share/sip \
                        -p macx-g++ \
                        --no-dist-info

    # always add some arch flags, since the user might have overridden
    # the default arch build (e.g., ticket #44086).
    python.add_archflags    no
    if {${universal_possible} && [variant_isset universal]} {

        foreach arch ${configure.universal_archs} {
            configure.args-append   "--arch=${arch}"
        }
        configure.universal_args
        configure.post_args LFLAGS="-F${frameworks_dir} -L${prefix}/lib \
                                    ${configure.universal_ldflags}"
        configure.post_args-append  CFLAGS="${configure.universal_cflags}"
        configure.post_args-append  CXXFLAGS="${configure.universal_cxxflags}"

    } else {

        configure.args-append       "--arch=${configure.build_arch}"
        configure.post_args LFLAGS="-F${frameworks_dir} -L${prefix}/lib \
                                    ${configure.ld_archflags}"
        configure.post_args-append  CFLAGS="${configure.cc_archflags}"
        configure.post_args-append  CXXFLAGS="${configure.cxx_archflags}"

    }

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

    configure.args-append --sdk=${configure.sdkroot}

    build.cmd           make
    build.target        all

    test.run            yes
    test.cmd            cd siplib && ${python.bin} -c 'import sip'

    destroot.cmd        make
    destroot.destdir    DESTDIR=${destroot}

    post-destroot {
        xinstall -m 755 -d ${destroot}${prefix}/share/doc
        file copy ${worksrcpath}/doc ${destroot}${prefix}/share/doc/${subport}
        xinstall -m 644 -W ${worksrcpath} LICENSE LICENSE-GPL2 LICENSE-GPL3 NEWS README \
            ${destroot}${prefix}/share/doc/${subport}
        system "${python.bin} ${python.libdir}/compileall.py ${destroot}${prefix}"
        system "${python.bin} -O ${python.libdir}/compileall.py ${destroot}${prefix}"
    }

    notes "${subport} is available under a PSF license with one addition: \n
    4. Licensee may not use SIP to generate Python bindings for any C or
       C++ library for which bindings are already provided by Riverbank. \n
GPL-2 or GPL-3 licenses are also available. For details see
${prefix}/share/doc/${subport}/LICENSE \[-GPL2 -GPL3\]\n\n"

    livecheck.type   none

    # deactivate hack to have a smooth transition from the original py-pip
    # (providing version 4.x) to the legacy py-pip4 port
    pre-activate {
        if {![catch {set installed [lindex [registry_active py${python.version}-sip] 0]}]} {
            set _version [lindex $installed 1]
            if {[vercmp $_version ${version}] >= 0} {
                registry_deactivate_composite py${python.version}-sip "" [list ports_nodepcheck 1]
                }
            }
        }
    } else {
    livecheck.type   regex
    livecheck.url    https://www.riverbankcomputing.com/software/sip/download
    livecheck.regex  sip-(4(\\.\\d+)+)${extract.suffix}
}
