# -*- 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           cmake 1.1
PortGroup           github 1.0
PortGroup           muniversal 1.0
PortGroup           boost 1.0

# Volk requires C/C++11 as of release 2.0.0
compiler.c_standard   2011
compiler.cxx_standard 2011

# some older Clang say they support SIMD when they don't
compiler.blacklist-append {clang < 900}

name                volk
categories          science comms
maintainers         {michaelld @michaelld}
description         Vector-Optimized Library of Kernels
long_description    VOLK is the Vector-Optimized Library of Kernels, a library that contains kernels of hand-written single-instruction multiple data (SIMD) code for different mathematical operations, providing portable SIMD code that is optimized for a variety of platforms.
long_description {*}${long_description} ${subport} \
    provides the release version, which is typically updated every month or so.
license             GPL-3
homepage            https://libvolk.org/

github.setup gnuradio volk 2.5.0 v
github.tarball_from releases
github.setup gnuradio volk 3.1.2 v
revision            0

epoch               2
# 1: bump the epoch because I moved the version from 20150707 to 1.0.1
#
# 2: because 2.5.1 requires std::filesystem, which requires updated libc++,
# which is not easily doable on 10.[7-14]; see also
# https://trac.macports.org/ticket/65377 and
# https://github.com/macports/macports-ports/pull/15205

# TODO: perhaps consider updating, finally.
# legacysupport provides filesystem for older clangs,
# and gcc supports it out of the box.

# Volk requires the submodule "cpu_features" as found here: <
# https://github.com/google/cpu_features >.  Because we're not
# cloning into a GIT module in the first place, we can't use the
# git submodule feature, and instead have to download separately
# and install into the correct location to get this port to work!
# Which means we need a cpu_features branch and so forth
# ... basically replicate what the github PG does but without
# using it since it is not designed to be used for multiple
# downloads.

set cpu_features_github_author  google
set cpu_features_github_project cpu_features
set cpu_features_github_branch  ba4bffa86cbb5456bdb34426ad22b9551278e2c0

set main_distname ${distname}
set main_distfile ${distfiles}

set cpu_features_distname ${cpu_features_github_project}-${cpu_features_github_branch}
set cpu_features_distfile ${cpu_features_distname}${extract.suffix}
    
distfiles    ${main_distfile}:tagone \
             ${cpu_features_distfile}:tagtwo

master_sites ${github.homepage}/archive/${git.branch}:tagone \
             https://github.com/${cpu_features_github_author}/${cpu_features_github_project}/archive/${cpu_features_github_branch}:tagtwo

checksums    ${main_distfile} \
             rmd160  b49e02fcac2b98c226b804bb0659468dfb1f3727 \
             sha256  90082bf68d76d00aa6c35f8bff0e93891b4204d817e1a3ba829486b445f715dc \
             size    382769 \
             ${cpu_features_distfile} \
             rmd160  8a398e527b7f095c2f6f8c5633528085687cc03e \
             sha256  1f7ef67bdb0797f55e9d69340bcb16685de75fbd22a0c92f4443f09f171c0e2c \
             size    109281

post-extract {
    # put the cpu_features_github_project directory into position
    # remove the original directory
    ui_debug "delete ${worksrcpath}/cpu_features"
    delete ${worksrcpath}/cpu_features
    ui_debug "move ${workpath}/${cpu_features_distname} ${worksrcpath}/cpu_features"
    move ${workpath}/${cpu_features_distname} ${worksrcpath}/cpu_features
}

# We can add a proper support for ppc into cpu_features library,
# however for volk it seems inconsequential presently, since it
# does not have dedicated code for ppc; cpu_features are used
# for x86/arm64 SIMD. We need to fix one silly macro, however:
# https://trac.macports.org/ticket/70202

patchfiles-append 0001-cpu_features_macros.h-fix-PowerPC-macros.patch

# done with special git command replication

depends_build-append path:bin/pkg-config:pkgconfig

# Volk doesn't understand build type "MacPorts"; use release unless +debug is set

if {[variant_isset debug]} {
    cmake.build_type Debug
} else {
    cmake.build_type Release
}

configure.checks.implicit_function_declaration.whitelist-append vst1_u8

# remove use of CPATH, as it is not needed

compiler.cpath

# remove top-level library path, such that internal libraries are used
# instead of any already-installed ones.

configure.ldflags-delete -L${prefix}/lib

# remove top-level include path, such that internal headers are used
# instead of any already-installed ones.

configure.cppflags-delete -I${prefix}/include

# specify the Python dependencies; these are checked for at configure,
# then used for building, but not at runtime.

set pythons_suffixes {39 310 311 312}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

proc python_dir {} {
    global pythons_suffixes
    foreach s ${pythons_suffixes} {
        if {[variant_isset python${s}]} {
            set p python[string index ${s} 0].[string range ${s} 1 end]
            return [file normalize [exec ${p} -c "import sys; print(sys.prefix)"]/lib/${p}/site-packages]
        }
    }
    error "Python support not enabled."
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string range ${s} 1 end]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build Volk using Python ${v}" conflicts {*}${c} "

    # required Python

    depends_lib-append \
        port:${p}

    depends_build-append \
        port:py${s}-six \
        port:py${s}-mako

    # specify the Python version to use

    configure.args-append \
        -DPYTHON_EXECUTABLE=${frameworks_dir}/Python.framework/Versions/${v}/bin/python${v} \
        -DVOLK_PYTHON_DIR=${frameworks_dir}/Python.framework/Versions/${v}/lib/python${v}/site-packages
    "
}

if {![variant_isset debug]} {
    set selected_python python310
    foreach s ${pythons_suffixes} {
        if {[variant_isset python${s}]} {
            set selected_python python${s}
        }
    }
    default_variants +${selected_python}
}

# override default version string to be MacPorts-specific
# git / "devel" only for now

set VOLK_VERSION_INFO "git-[string range ${github.version} 0 7](${version})"

configure.args-append \
    -DVOLK_GIT_COUNT="MacPorts" \
    -DVOLK_GIT_HASH="${VOLK_VERSION_INFO}"

# install CMake files into this directory

configure.args-append \
    -DCMAKE_MODULES_DIR=${prefix}/share/cmake

default_variants +docs +orc

variant docs description "Install Volk documentation" {

    depends_build-append \
        path:bin/doxygen:doxygen

    configure.args-append \
        -DDOXYGEN_EXECUTABLE=${prefix}/bin/doxygen
}

if {![variant_isset docs]} {

    configure.args-append \
        -DDOXYGEN_EXECUTABLE=

}

variant orc description "Install Volk with support for ORC" {

    depends_lib-append \
        port:orc

    configure.args-append \
        -DORCC_EXECUTABLE=${prefix}/bin/orcc \
        -DORC_INCLUDE_DIR=${prefix}/include/orc-0.4 \
        -DORC_LIBRARY_DIR=${prefix}/lib

}

if {![variant_isset orc]} {

    configure.args-append \
        -DORCC_EXECUTABLE= \
        -DORC_INCLUDE_DIR= \
        -DORC_LIBRARY_DIR=

}
