# -*- 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               cmake 1.1
PortGroup               github 1.0
PortGroup               legacysupport 1.1

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

# libuv version here should match the main port.
github.setup            skypjack uvw 3.0.0 v _libuv_v1.44
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
revision                2
categories              devel
maintainers             nomaintainer
license                 MIT
description             Header-only, event based, tiny and easy to use libuv wrapper in modern C++
long_description        {*}${description}
checksums               sha256  b93d7cc841ace369f5930953f1b00a008597682a8690950f586db5fab4434da1 \
                        rmd160  4e1ce2497a5c4eb30c46697a65595004b7d21c55 \
                        size    106022

# uvw upstream may not match the libuv version used by Macports, and some ports may require to be up-to-date with upstream.
# Having a single uvw port leads into trouble due to potential, and likely, incompatibilities.
# So we offer header-only and static lib variants which are intended to be up-to-date, while letting the main variant to lag behind,
# tracking Macports libuv. Notice, while subports mutually conflict, the main port can co-exist with either of the former.
# In addition, there is a uvw2 port: its API is incompatible with v. 3.x: : https://github.com/oxen-io/lokinet/issues/2197
set upstream_vvw        3.2.0
set upstream_luv        1.46

subport uvw-static {
    github.setup        skypjack uvw ${upstream_vvw} v _libuv_v${upstream_luv}
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision            1
    conflicts           uvw-headers
    fetch.type          git
}

subport uvw-headers {
    github.setup        skypjack uvw ${upstream_vvw} v _libuv_v${upstream_luv}
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision            1
    conflicts           uvw-static
    checksums           rmd160  d6579c2b1fc6bdf43aadd20ee2ce9f999225c16e \
                        sha256  67b1c002f51750264db7686ee99e3330e188d9337dc7ebc0e2e2d41210973c31 \
                        size    106942
    platforms           any
    supported_archs     noarch
}

subport uvw2 {
    github.setup        skypjack uvw 2.12.1 v _libuv_v1.44
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision            0
    checksums           rmd160  ab836964c230520bfb970df5b44a4cba9a6a8654 \
                        sha256  740cb917673cdcbe579ff9a14b92efda7f0190f02902bc63e6f4a0ac98d31042 \
                        size    107951
}

depends_build-append    port:pkgconfig

compiler.cxx_standard   2017

configure.pre_args-replace \
                        -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                        -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF

configure.args-append   -DBUILD_DOCS:BOOL=OFF

if {${subport} eq ${name} || ${subport} eq "uvw2"} {
    depends_lib-append  port:libuv
    configure.args-append \
                        -DBUILD_TESTING:BOOL=ON \
                        -DBUILD_UVW_SHARED_LIB:BOOL=ON \
                        -DFIND_LIBUV:BOOL=ON

    # FIXME: two tests fail on ppc: https://github.com/skypjack/uvw/issues/286
    test.run            yes
} elseif {${subport} eq "uvw-static"} {
    configure.args-append \
                        -DBUILD_UVW_LIBS:BOOL=ON \
                        -DFETCH_LIBUV:BOOL=ON
    post-configure {
        # Sources of libuv are fetched during configure, so magic is needed.
        system -W ${cmake.build_dir}/_deps/libuv-src "patch -p0 < [shellescape ${filespath}/patch-libuv-${upstream_luv}.diff]"
    }
} elseif {${subport} eq "uvw-headers"} {
    configure.args-append \
                        -DBUILD_UVW_LIBS:BOOL=OFF \
                        -DFETCH_LIBUV:BOOL=OFF
}

# Install everything into sub-prefices:
if {${subport} eq ${name}} {
    set uvw_libexec     ${prefix}/libexec/uvw/
} elseif {${subport} eq "uvw2"} {
    set uvw_libexec     ${prefix}/libexec/uvw2/
} elseif {${subport} eq "uvw-static" || ${subport} eq "uvw-headers"} {
    set uvw_libexec     ${prefix}/libexec/uvw-upstream/
    configure.args-append \
                        -DBUILD_TESTING:BOOL=OFF \
                        -DBUILD_UVW_SHARED_LIB:BOOL=OFF \
                        -DFIND_LIBUV:BOOL=OFF
}

configure.pre_args-append \
                        -DCMAKE_INSTALL_INCLUDEDIR:STRING=${uvw_libexec}include \
                        -DCMAKE_INSTALL_LIBDIR:STRING=${uvw_libexec}lib

platform powerpc {
    configure.args-append \
                        -DUSE_LIBCPP:BOOL=OFF
}

pre-destroot {
    xinstall -d ${destroot}${uvw_libexec}
}
