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

name                bearssl
version             0.6
revision            1
categories          devel crypto
license             MIT
maintainers         nomaintainer
description         BearSSL is an implementation of the SSL/TLS protocol (RFC 5246) written in C
long_description    {*}${description}
homepage            https://bearssl.org
master_sites        ${homepage}
distname            ${name}-${version}
checksums           rmd160  15cbb065fcccfac434e097a98e054d3b0498e2ea \
                    sha256  6705bba1714961b41a728dfc5debbe348d2966c117649392f8c8139efc83ff14 \
                    size    765094

# We want to tidy up installation of headers, but not to disturb the build which uses existing structure.
post-extract {
    copy ${worksrcpath}/inc ${worksrcpath}/${name}
}

patchfiles-append   patch-conf.diff \
                    patch-includes.diff

universal_variant   no

post-patch {
    reinplace "s|@CC@|${configure.cc}|g" ${worksrcpath}/conf/Unix.mk
    reinplace "s|@ARCHFLAGS@|[get_canonical_archflags cc]|g" ${worksrcpath}/conf/Unix.mk
    if {${configure.build_arch} in [list i386 ppc]} {
        reinplace "s|@DEFS@|-DBR_LOMUL|" ${worksrcpath}/conf/Unix.mk
    } else {
        reinplace "s|@DEFS@||" ${worksrcpath}/conf/Unix.mk
    }
}

set builddir ${worksrcpath}/build

# aes_x86ni.c: error: assigning to '__m128i' (vector of 2 'long long' values) from incompatible type 'int'
compiler.blacklist-append {clang < 800}

destroot {
    copy ${builddir}/brssl ${destroot}${prefix}/bin
    copy ${builddir}/libbearssl.a ${destroot}${prefix}/lib
    copy ${builddir}/libbearssl.dylib ${destroot}${prefix}/lib
    system "install_name_tool -id ${prefix}/lib/libbearssl.dylib ${destroot}${prefix}/lib/libbearssl.dylib"
    # The following is intended: the main header is placed into include directly:
    move ${worksrcpath}/${name}/bearssl.h ${destroot}${prefix}/include/bearssl.h
    copy ${worksrcpath}/${name} ${destroot}${prefix}/include
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} LICENSE.txt README.txt ${destroot}${docdir}
}

test.run            yes
test.dir            ${builddir}
test.cmd            ./testcrypto
test.target         all
