# -*- 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
PortGroup           python 1.0

github.setup        ciphey cipheycore 0.3.2 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                py-cipheycore
revision            3
python.versions     39

description         Some cryptanalysis tidbits written in a proper language
long_description    {*}${description}. This core provides many crackers, encryption \
                    methods & decryption methods as well as cryptanalysis tidbitis to \
                    Ciphey. Written in C++ for the speed.

license             MIT
categories-append   security
maintainers         {@harens gmail.com:harensdeveloper} \
                    openmaintainer

checksums           rmd160  a917b9b820119d3b2662b185e48d8eee7b95cc11 \
                    sha256  524452c94739ab0ae4ac2d0fbaef0138508286640b6ce94acee1c15ab0926970 \
                    size    40892

if {${name} ne ${subport}} {

    # To break this down there are two stages

    # Use the configure stage (and build cmd) of cmake PG
    # This generates the python package

    # Use the build and destroot stage of python PG
    # This installs the generated python package

    PortGroup           cmake 1.1
    PortGroup           boost 1.0

    depends_build-append \
                        port:py${python.version}-setuptools \
                        port:poetry \
                        port:swig-python \
                        port:swig

    # cipheycore requires C++20 standard
    # This acts as a baseline (with additional blacklisting below)
    compiler.cxx_standard 2017

    # See https://en.cppreference.com/w/cpp/compiler_support/20
    # Couldn't get gcc to play nicely with cipheycore
    # For clang < 1000: target_compile_features The compiler feature "cxx_std_20"
    # is not known to CXX compiler "AppleClang" version ...
    # clang < 5 is blocked by cxx_standard 2017
    compiler.blacklist-append *gcc* {clang < 1000} {macports-clang-[5-9].0}
    compiler.fallback-append  macports-clang-10 macports-clang-11

    # Build the generated python package
    build.cmd           ${python.bin} setup.py

    # Generate the python package during the configure stage
    use_configure       yes

    # Remove dependency on gtest
    # Set the build dep python's headers
    # Fixes segmentation fault if different python version also installed
    configure.args-append \
                        -DCIPHEY_CORE_TEST=OFF \
                        -DCIPHEY_CORE_PYTHON=${frameworks_dir}/Python.framework/Versions/${python.branch}

    # We use make for configure, not the setup.py
    # (since python package hasn't been generated yet)
    configure.pre_args-replace \
                        -DCMAKE_MAKE_PROGRAM=${build.cmd} \
                        -DCMAKE_MAKE_PROGRAM=make

    # The extracted python package
    build.dir           ${cmake.build_dir}/dist/${python.rootname}-${version}

    # Build the python package
    pre-build {
        # Standard cmake build
        system -W ${cmake.build_dir} "make -j4 -w all VERBOSE=ON"

        # Build sdist only (wheel not required)
        system -W ${cmake.build_dir} "poetry build -f sdist --verbose"
        system -W ${cmake.build_dir}/dist "tar xf ${python.rootname}-${version}.tar.gz"
    }

    # Remove invalid args from cmake PG
    # (cmake only required in pre-build and configure)
    build.post_args
    destroot.target     install

    livecheck.type      none
}
