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

boost.version       1.90
# clock_gettime, getentropy
legacysupport.newest_darwin_requires_legacy 15

name                botan3
version             3.12.0
revision            1
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  7505b86524f368fe38e57a439e4eaed72d9a036e \
                    sha256  5370f98dc15f8c222ee1ce52cd61c8756a53be0dc57cc4c1b0714d5a09ad74fb \
                    size    9041764

set py_ver          3.14
set py_ver_nodot    [string map {. {}} ${py_ver}]

depends_lib-append  port:bzip2 \
                    path:share/curl/curl-ca-bundle.crt:curl-ca-bundle \
                    port:lzma \
                    port:python${py_ver_nodot} \
                    port:sqlite3 \
                    port:zlib

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

# https://github.com/randombit/botan/pull/4892
patchfiles-append   0001-cpuid_ppc.cpp-fix-for-macOS.patch
# sys/random.h:37:32: error: 'u_int' has not been declared
patchfiles-append   0002-getentropy-add-missing-header-for-macOS.patch

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

compiler.cxx_standard   2020
compiler.thread_local_storage   yes

# Build fails if ccache is enabled but inactive.
configure.ccache    no

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

configure.cmd         ${prefix}/bin/python${py_ver} ./configure.py
configure.args-append --docdir=share/doc \
                      --program-suffix=3 \
                      --system-cert-bundle=${prefix}/share/curl/curl-ca-bundle.crt \
                      --with-boost \
                      --with-bzip2 \
                      --with-lzma \
                      --with-sqlite3 \
                      --with-zlib

# See comments in https://github.com/randombit/botan/pull/4892
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    configure.args-append \
                      --without-os-features=ccrandom \
                      --without-os-features=sandbox_proc
}

# 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
    vsx
    neon
    armv8crypto
    powercrypto
}

# Disable all the intrinsics.
# 3.9.0 changed the way this is done:
# https://github.com/randombit/botan/commit/7fe6e0b499736c711266547702ac778f6f030868
foreach elem $intrinsics {
    configure.args-append --disable-modules=$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-modules=$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/${py_ver}/lib
    move ${destroot}${prefix}/lib/python${py_ver} ${destroot}${frameworks_dir}/Python.framework/Versions/${py_ver}/lib/python${py_ver}
}
