# -*- 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             3.12.0
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  7505b86524f368fe38e57a439e4eaed72d9a036e \
                    sha256  5370f98dc15f8c222ee1ce52cd61c8756a53be0dc57cc4c1b0714d5a09ad74fb \
                    size    9041764

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

platform powerpc {
    # https://github.com/macos-powerpc/powerpc-ports/blob/main/security/botan3/files/0001-cpuid_ppc.cpp-fix-for-macOS.patch
    # https://github.com/macos-powerpc/powerpc-ports/blob/main/security/botan3/files/0002-getentropy-add-missing-header-for-macOS.patch
    patchfiles-append \
                    0001-cpuid_ppc.cpp-fix-for-macOS.patch \
                    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
}

# ${prefix}/var/macports/sources/rsync.macports.org/macports/release/tarballs/ports/_resources/port1.0/group/python-1.0.tcl 
set python_version  314
set python_branch   [string index ${python_version} 0].[string range ${python_version} 1 end]
                                                              
depends_build-append \
                    port:python${python_version} \
                    port:py${python_version}-standard-distutils

depends_lib-append  port:bzip2 \
                    port:curl-ca-bundle \
                    port:lzma \
                    port:sqlite3 \
                    port:zlib

compiler.cxx_standard   2020

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

# CLT clang build issues, including:
# https://github.com/randombit/botan/issues/4348
set os_major_version_clang_issue_arm64  23
set os_major_version_clang_issue        21
platform darwin {
    if {${build_arch} eq {arm64}
        && ${os.major} >= ${os_major_version_clang_issue_arm64}
        || ${os.major} == ${os_major_version_clang_issue}} {
        compiler.blacklist      clang
    }
}

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${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

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>'
platform darwin {
    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 }

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}

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}
}

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