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

github.setup            ngtcp2 ngtcp2 1.26.0 v
revision                0
categories              net devel
maintainers             {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer
license                 MIT
description             ${name} project is an effort to implement RFC9000 QUIC protocol
long_description        {*}${description}
homepage                https://nghttp2.org/ngtcp2
checksums               rmd160  43c80a89cb6524064dd98cb876d4d859c01778c7 \
                        sha256  7804aa0fa1c39942ba322a90805824c37231f7675b08d0c3cc4332858a7b085e \
                        size    733792
github.tarball_from     releases

use_xz                  yes

# Other dependencies are only used for examples:
# https://github.com/ngtcp2/ngtcp2/issues/1129
depends_build-append    path:bin/pkg-config:pkgconfig

compiler.cxx_standard   2017

configure.args-append   --disable-silent-rules \
                        --enable-lib-only=yes \
                        --enable-werror=no \
                        --with-gnutls=no \
                        --with-openssl=no \
                        --with-picotls=no

subport ${name}-gnutls {
    description ${name} GnuTLS QUIC crypto backend

    depends_lib-append  port:${name} \
                        path:lib/pkgconfig/gnutls.pc:gnutls

    configure.args-replace \
                        --with-gnutls=no --with-gnutls=yes

    post-destroot {
        system -W ${destroot}${prefix} \
            "find . ! -type d ! -name '*crypto_gnutls*' -delete"
    }
}

subport ${name}-openssl {
    description ${name} OpenSSL QUIC crypto backend

    # The backend needs SSL_set_quic_tls_cbs, added in OpenSSL 3.5. configure
    # fails outright when the selected OpenSSL lacks it, rather than quietly
    # dropping the library.
    depends_lib-append  port:${name} \
                        path:lib/libssl.dylib:openssl

    configure.args-replace \
                        --with-openssl=no --with-openssl=yes

    post-destroot {
        system -W ${destroot}${prefix} \
            "find . ! -type d ! -name '*crypto_ossl*' -delete"
    }
}

subport ${name}-picotls {
    description ${name} picotls QUIC crypto backend

    # configure probes picotls through its OpenSSL backend, using the OpenSSL
    # pkg-config flags, so OpenSSL is needed here too.
    depends_lib-append  port:${name} \
                        path:lib/libssl.dylib:openssl \
                        port:picotls
    configure.args-replace \
                        --with-picotls=no --with-picotls=yes
    configure.cppflags-append \
                        -I${prefix}/include/picotls
    configure.ldflags-append \
                        -lpicotls-core -lpicotls-minicrypto -lpicotls-openssl

    post-destroot {
        system -W ${destroot}${prefix} \
            "find . ! -type d ! -name '*crypto_picotls*' -delete"
    }
}

if {${subport} eq ${name}} {
    post-destroot {
        # Have the main port install ngtcp2_crypto.h because it
        # would be shared between all the crypto subports.
        xinstall -m 644 \
            ${worksrcpath}/crypto/includes/ngtcp2/ngtcp2_crypto.h \
            ${destroot}${prefix}/include/ngtcp2/
    }
} else {
    long_description ${description}
}

test.run                yes
test.target             check
