# -*- 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           muniversal 1.1
PortGroup           openssl 1.0
PortGroup           legacysupport 1.1

# botan needs clock_gettime & getentropy which are missing until osx 10.11 (=darwin 15)
legacysupport.newest_darwin_requires_legacy 15

name                botan
version             2.19.5
revision            0
set branch          [join [lrange [split ${version} .] 0 1] .]
categories          security devel
maintainers         nomaintainer
license             BSD
description         cryptographic algorithms and formats library in C++
long_description    Botan is a C++ library implementing a variety of \
                    cryptographic algorithms and formats. At this time, it is \
                    quite stable, and is suitable for use in a wide variety of \
                    programs.

homepage            https://botan.randombit.net
master_sites        ${homepage}/releases/
distname            Botan-${version}
use_xz              yes

checksums           rmd160  b82daea6a81d29aeb425bfceeb80826ff1ee1fb3 \
                    sha256  dfeea0e0a6f26d6724c4af01da9a7b88487adb2d81ba7c72fcaf52db522c9ad4 \
                    size    6140148

depends_build       port:python312
depends_lib         port:bzip2 \
                    port:lzma \
                    port:sqlite3 \
                    port:zlib

# respect MacPorts configure values
patchfiles-append   patch-compiler_flags.diff

post-patch {
    reinplace -W ${worksrcpath}/src/build-data/cc \
        "s|__MACPORTS_CXX_STDLIB__|${configure.cxx_stdlib}|g" \
        clang.txt
}

compiler.cxx_standard   2011
# botan uses thread_local, which is not supported in Xcode < 8
compiler.thread_local_storage   yes

if {[tbool configure.ccache]} {
    configure.args-append   --cc-bin="${prefix}/bin/ccache ${configure.cxx}"
} else {
    configure.args-append   --cc-bin=${configure.cxx}
}

set python_branch     3.12
configure.cmd         ${prefix}/bin/python${python_branch} ./configure.py
configure.args-append --docdir=share/doc \
                      --system-cert-bundle=${prefix}/share/curl/curl-ca-bundle.crt \
                      --with-bzip2 \
                      --with-lzma \
                      --with-sqlite3 \
                      --with-zlib

# List of all intrinsics that can be disabled
set intrinsics {
    sse2
    ssse3
    sse4.1
    sse4.2
    avx2
    bmi2
    rdrand
    rdseed
    aes-ni
    sha-ni
    altivec
    neon
    armv8crypto
    powercrypto
}

# Disable all the intrinsics
foreach elem $intrinsics {
    configure.args-append --disable-$elem
}

if {[string match *clang* ${configure.compiler}]} {
    configure.args-append --cc=clang
} elseif {[string match *gcc* ${configure.compiler}]} {
    configure.args-append --cc=gcc
}

# no matching constructor for initialization of 'std::vector<CFStringRef>'
if {${os.major} <= 14} {
    configure.args-append --disable-modules=certstor_system_macos
}

# Upstream default for both GCC and Clang
configure.optflags  -O3

platform darwin { configure.args-append --os=darwin }

# Variant to re-enable use of intrinsics
variant native description {Enable all intrinsics} {
    foreach elem $intrinsics {
        configure.args-delete --disable-$elem
    }
}

configure.args.ppc     "--cpu=ppc   --cc-abi-flags='-arch ppc'"
configure.args.i386    "--cpu=ia32  --cc-abi-flags='-arch i386'"
configure.args.ppc64   "--cpu=ppc64 --cc-abi-flags='-arch ppc64'"
configure.args.x86_64  "--cpu=amd64 --cc-abi-flags='-arch x86_64'"
configure.args.arm64   "--cpu=arm64 --cc-abi-flags='-arch arm64'"

# configure.py rejects --host
triplet.add_host    {none}

livecheck.regex     Botan-(${branch}\\.\[0-9.\]+)${extract.suffix}

post-destroot {
    xinstall -d ${destroot}${frameworks_dir}/Python.framework/Versions/${python_branch}/lib
    move ${destroot}${prefix}/lib/python${python_branch} ${destroot}${frameworks_dir}/Python.framework/Versions/${python_branch}/lib/python${python_branch}
}