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

epoch                                   1
name                                    php-swoole
categories-append                       net devel
maintainers                             {ryandesign @ryandesign} {mathiesen.info:macintosh @BjarneDMat} @ArtKoKo openmaintainer
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

description                             event-driven asynchronous & concurrent & coroutine networking engine
long_description                        ${name} is an ${description} with high performance for PHP.

if {[vercmp ${php.branch} >= 8.2]} {
    version                             6.2.3
    revision                            0
    checksums                           rmd160  991c9a5da017d30046fca486ef98b41f41859342 \
                                        sha256  dde8d2a4a6b5c5cd418aedd8561760baad59767a2f8a963b2c0e9eb9c86f4c8d \
                                        size    2195688
    patchfiles-append                   patch-src_core_misc.cc.diff
} elseif {[vercmp ${php.branch} >= 8.1]} {
    version                             6.1.10
    revision                            0
    checksums                           rmd160  fe027c3abc997e8e3875b02cbe3f1472f0228bdd \
                                        sha256  173ee1762c68027ef2f6aa30d326a552414bdfa5e53d182305351d54983e53cb \
                                        size    2014269
    patchfiles-append                   patch-src_core_misc.cc.diff
} elseif {[vercmp ${php.branch} >= 8.0]} {
    version                             5.1.8
    revision                            1
    checksums                           rmd160  43bc122ea59e54022c631b59dced60680de83982 \
                                        sha256  ed4dd6767e087b54f61eb43d48865c7197e67ab52b22b04fe7f8c6cce059394a \
                                        size    1902928
} elseif {[vercmp ${php.branch} >= 7.2]} {
    version                             4.8.13
    revision                            2
    checksums                           rmd160  2e624ad168a7e4de2e3ab9602ffa48e10aa8e46a \
                                        sha256  5d8352521ee31ddbd23b46eccffb5c99f03af99c6d058857ecaf964c2ef433d6 \
                                        size    1767137
    patchfiles-append                   arm.patch
    patchfiles-append                   timer.patch
} elseif {[vercmp ${php.branch} >= 7.1]} {
    version                             4.5.11
    revision                            4
    checksums                           rmd160  e68af13a465f5be8a43c6bade04ea6aecbb62e9f \
                                        sha256  11286d74f3a3ddc6f1e59f037dcdc6188242ebd5f0eeb7fe3c25e3d7b4c7a311 \
                                        size    1454976
} elseif {[vercmp ${php.branch} >= 7.0]} {
    version                             4.3.6
    revision                            4
    checksums                           rmd160  adefe2b1737a97a4b9d1685e47b05c2d9c964f11 \
                                        sha256  c6e91f877346afff17041b91ac0ba6f36f3d27a8f97937cba61ee9d86c195ba9 \
                                        size    1313085
} elseif {[vercmp ${php.branch} >= 5.5]} {
    # https://github.com/swoole/swoole-src/issues/1783
    version                             2.0.11
    revision                            6
    checksums                           rmd160  c6200de1b82017425e1c26154c66f0499e87e605 \
                                        sha256  d6a4e7541dad345aabd88811df9140b94393e7bc49769e53007dbba550766e31 \
                                        size    810669
} else {
    version                             1.10.5
    revision                            6
    checksums                           rmd160  ffc99b1a8b0dff8b268f99dc0d715d46c99ca1e3 \
                                        sha256  558e26c75853fc3e97038faf26de91bdefe912093f67178874d225e0d8b3ec25 \
                                        size    764436
}

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

    # remove w/ next update
    dist_subdir                         ${name}/${version}_1

    github.tarball_from                 archive
    github.setup                        swoole  swoole-src  ${version}  v

    # 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_build-append                path:bin/pkg-config:pkgconfig

    depends_lib-append                  port:hiredis \
                                        port:nghttp2 \
                                        path:lib/libssl.dylib:openssl \
                                        port:zlib
    if {[vercmp ${version} >= 4.1]} {
        depends_lib-append              port:brotli
    }

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

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