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

epoch                   1
name                    php-swoole
categories-append       net devel
maintainers             {ryandesign @ryandesign} {mathiesen.info:macintosh @BjarneDMat} @ArtKoKo
license                 Apache-2

php.branches            5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4 8.5
php.pecl                yes

if {[vercmp ${php.branch} >= 8.2]} {
    version             6.2.0
    revision            0
    checksums           rmd160  b8479770bc100c16e815d75346b641d1180dba4a \
                        sha256  b66bf449440c1ba2727314e1e9c4c078978e7def21a02b729bbfec35601ae2ee \
                        size    2598797
} elseif {[vercmp ${php.branch} >= 8.1]} {
    version             6.1.7
    revision            0
    checksums           rmd160  098d8248139e2c741a2c16670a4d13917e950399 \
                        sha256  7835a9983ab4837be35e8c54c1787fc465de06c14282c28581cf4716f518b9c0 \
                        size    2413131
} elseif {[vercmp ${php.branch} >= 8.0]} {
    version             5.1.8
    revision            0
    checksums           rmd160  dbb5bddd7df188a56d23338606182986c31ef8dc \
                        sha256  c365ccd37b1804527064325a0d2641d4bc33fda23c5b4660dd840ac24f333aa8 \
                        size    2267867
} elseif {[vercmp ${php.branch} >= 7.2]} {
    version             4.8.13
    revision            1
    checksums           rmd160  0725d0bc7feb986ca41180d015e48445c8f70d6d \
                        sha256  f4b7b856c112b1058985e0b1fca320ec1211299607b363073de9a709628ad32a \
                        size    2106874
    patchfiles-append   arm.patch
    patchfiles-append   timer.patch
} elseif {[vercmp ${php.branch} >= 7.1]} {
    version             4.5.11
    revision            3
    checksums           rmd160  787880685a821869f19c1b6f4645857b75a048fe \
                        sha256  2d85752ee2b0944399c2901bb22832f196ba3f7ba4262c239d12ed494e2460dd \
                        size    1554142
} elseif {[vercmp ${php.branch} >= 7.0]} {
    version             4.3.6
    revision            3
    checksums           rmd160  e77d9a43dbbc6c133e42bf564af9d975e8874838 \
                        sha256  9253d1cb3ae109e18473efc51d14a99067417c28f4c66b1b0a0cdae5141253f9 \
                        size    1349407
} elseif {[vercmp ${php.branch} >= 5.5]} {
    # https://github.com/swoole/swoole-src/issues/1783
    version             2.0.11
    revision            6
    checksums           rmd160  7238015b141faf59d9662d4ab21c73bb66b58e35 \
                        sha256  07df75ede4bf0833d2c2443b666a5c3054bc498a78f5251babc1b0e7cc8cd573 \
                        size    769029
} else {
    version             1.10.5
    revision            6
    checksums           rmd160  48b8c3bf5999e5365b3e804440464114500120ed \
                        sha256  5c1dc0b82772ca1c352de4ddf20deeb35f06e4c5a01beba5a446d78a1f747bd2 \
                        size    730797
}

description             an event-driven asynchronous & concurrent & coroutine networking engine with high performance for PHP

long_description        ${name} is ${description}.

if {${name} ne ${subport}} {
    if {[vercmp ${php.branch} >= 7.2]} {
        conflicts           ${php}-openswoole
    }

    # macOS pthread compatibility (all PHP versions)
    # Disable Linux-specific features not available on macOS
    if {[vercmp ${version} >= 6.1.5]} {
        post-patch {
            reinplace "s|AC_CHECK_LIB(pthread, pthread_spin_lock|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_spin_lock|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_barrier_init|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_barrier_init|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_mutex_timedlock|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_mutex_timedlock|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_mutexattr_setrobust|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_mutexattr_setrobust|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_mutex_consistent|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_mutex_consistent|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, getrandom|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, getrandom|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, accept4|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, accept4|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, signalfd|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, signalfd|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, malloc_trim|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, malloc_trim|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, epoll_create|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, epoll_create|g" ${worksrcpath}/config.m4
        }
    }

    # Not sure what version
    if {[vercmp ${version} >= 4.0.0]} {
        PortGroup       legacysupport 1.1
        # strndup
        legacysupport.newest_darwin_requires_legacy \
                        10

        if {[vercmp ${version} >= 4.0.4]} {
            compiler.cxx_standard 2011

            if {[vercmp ${version} >= 4.5.0]} {
                compiler.thread_local_storage yes
            }

            if {[vercmp ${version} < 4.6.2]} {
                patchfiles-append \
                        arm64.patch
            }
        }
    }

    depends_lib-append  port:hiredis \
                        port:nghttp2 \
                        path:lib/libssl.dylib:openssl \
                        port:zlib


    compiler.blacklist-append \
                        *gcc-4.0 *gcc-4.2

    configure.args      --enable-async-redis \
                        --enable-http2 \
                        --enable-openssl \
                        --enable-swoole \
                        --with-openssl-dir=${prefix}

    if {[vercmp ${version} >= 4.1]} {
        depends_lib-append \
                        port:brotli
    }

    use_parallel_build  yes
}
