# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           boost 1.0
PortGroup           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1

boost.version       1.81
# strnlen
legacysupport.newest_darwin_requires_legacy 10

github.setup        VectorCamp vectorscan 5.4.12 vectorscan/
revision            0
categories          textproc
license             BSD
maintainers         nomaintainer

description         Portable fork of the high-performance regular \
                    expression matching library

long_description    {*}${description}. It follows the regular \
                    expression syntax of the commonly-used libpcre \
                    library, but is a standalone library with its own \
                    C API. Vectorscan uses hybrid automata techniques \
                    to allow simultaneous matching of large numbers \
                    (up to tens of thousands) of regular expressions \
                    and for the matching of regular expressions across \
                    streams of data. Vectorscan is typically used \
                    in a DPI library stack.

homepage            https://www.vectorcamp.gr/project/vectorscan

conflicts           hyperscan

github.tarball_from archive

# What upstream uses, as of v. 5.4.12:
set simde_hash      416091ebdb9e901b29d026633e73167d6353a0b0

master_sites-append https://github.com/simd-everywhere/simde/archive/${simde_hash}/:simde
distfiles-append    simde-${simde_hash}.tar.gz:simde

checksums           ${distname}${extract.suffix} \
                    rmd160  ab56f60d25d4dd630a2d289f57207302242c358b \
                    sha256  1ac4f3c038ac163973f107ac4423a6b246b181ffd97fdd371696b2517ec9b3ed \
                    size    20849791 \
                    simde-${simde_hash}.tar.gz \
                    rmd160  7eebe37d56d0cf145ccb4103a1415abca783bbd9 \
                    sha256  2362ee3d2a6160094b2663b102ab08b376b1ea0dbd8e3c79ee9cab81775c7ee1 \
                    size    7480659

extract.only        ${distname}${extract.suffix}

post-extract {
    set tar [findBinary tar ${portutil::autoconf::tar_command}]
    system -W ${workpath} "${tar} -zxf ${distpath}/simde-${simde_hash}.tar.gz"
    delete ${worksrcpath}/simde
    move ${workpath}/simde-${simde_hash} ${worksrcpath}/simde
}

# Upstreamed patches from simde port:
patchfiles-append   patch-simde.diff

# https://github.com/VectorCamp/vectorscan/pull/350
patchfiles-append   0001-CMakeLists-fix-for-powerpc.patch

# Use ${python.default_version}
# name consistency with ${prefix}/var/macports/sources/rsync.macports.org/macports/release/tarballs/ports/_resources/port1.0/group/python-1.0.tcl
set python_default_version 313
set python_version  ${python_default_version}
set python_branch   [string index ${python_version} 0].[string range ${python_version} 1 end]

depends_build-append \
                    path:bin/pkg-config:pkgconfig \
                    port:python${python_version} \
                    port:ragel

depends_lib-append  port:pcre \
                    port:sqlite3

compiler.cxx_standard   2023

# The flag `-DCMAKE_BUILD_TYPE=MacPorts` adds -Werror, which causes compile errors with -Wshadow
# Reference: https://github.com/intel/hyperscan/blob/master/CMakeLists.txt
cmake.build_type    Release

switch ${build_arch} {
    arm64 {
        configure.args-append \
                    -DARCH_AARCH64=On
    }
    x86_64 {
        configure.args-append \
                    -DARCH_X86_64=On \
                    -DBUILD_AVX512VBMI=On
    }
    ppc {
        configure.args-append \
                    -DBUILD_SSE2_SIMDE=Off \
                    -DSIMDE_BACKEND=On
    }
    # ppc64 should work, but is not tried at the moment.
    # Adding it will need a trivial adjustment to CMake checks.
}

configure.args-append \
                    -DBUILD_DOC=Off \
                    -DBUILD_SHARED_LIBS=On \
                    -DBUILD_STATIC_LIBS=Off \
                    -DPYTHON_EXECUTABLE=${prefix}/bin/python${python_branch}

# Benchmarks fails to link on ppc/ppc64 and are not needed anyway.
# https://github.com/VectorCamp/vectorscan/issues/343
configure.args-append \
                    -DBUILD_BENCHMARKS=Off

# https://trac.macports.org/ticket/73067
post-destroot {
    foreach tool {hscheck hsbench patbench pcapscan simplegrep} {
        xinstall -m 755 ${cmake.build_dir}/bin/${tool} ${destroot}${prefix}/bin/
    }
}

# A number of tests fail atm: https://github.com/VectorCamp/vectorscan/issues/351
test.run            yes
test.env-append     DYLD_LIBRARY_PATH=${cmake.build_dir}/lib:\$DYLD_LIBRARY_PATH
test.cmd            ${cmake.build_dir}/bin/unit-hyperscan
