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

PortSystem          1.0
PortGroup           github 1.0
PortGroup           app 1.0
PortGroup           active_variants 1.1
PortGroup           conflicts_build 1.0
PortGroup           gnuradio 1.0

name                gnuradio
maintainers         {michaelld @michaelld} {ra1nb0w @ra1nb0w}
description         GNU Radio is Software Defined Radio (SDR)
homepage            https://www.gnuradio.org/
license             GPL-3

if {${subport} eq ${name}} {

    github.setup    gnuradio gnuradio 3.8.5.0 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    checksums       rmd160  098825a19d8962bac72f2f2e14fcdfee8500f307 \
                    sha256  dd336400034b57b7bce7b1fec6c5cd99f1e9dde7b78faa2cd28105877c213b14 \
                    size    3399901
    revision        3

    long_description    {*}${description}: \
        This port is kept up with the GNU Radio release, \
        currently ${version}, which is typically updated twice per year.

    conflicts           gnuradio37 gnuradio-next

    # temporary patch to fix and allow external setting for
    # CMAKE_C/CXX_STANDARD
    patchfiles-append \
        patch-C_CXX_STANDARD.38.diff
    # temporary patch to fix usage of GNURadioControlPortClient
    patchfiles-append \
        patch-fix-GNURadioControlPortClient-usage.38.diff
    # fix Quartz behaviour
    # TODO verify https://github.com/gnuradio/gnuradio/issues/2726
    patchfiles-append \
        patch-quartz_drop_crash.38.diff
    # fix https://github.com/gnuradio/gnuradio/pull/3981
    patchfiles-append \
        patch-fix-pkg-config.38.diff
    # fix SWIG41 and Python3.6+ usage
    # https://trac.macports.org/ticket/66389
    patchfiles-append \
        patch-Py3-swig41-fixes.38.diff

    set GR_VERSION_INFO "Release 3.8"

    github.livecheck.regex  {(3.8.[0-9.]+)}
}

subport gnuradio37 {

    name            gnuradio37
    github.setup    gnuradio gnuradio 3.7.14.0 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    checksums       rmd160  0165a52a4770a02b4843544e4ba6bc59d84fa7d1 \
                    sha256  3d895e29417efd4ef8ab4053bed2ea9117b4476c0ae729f065088242f0f61f9f \
                    size    4460817
    revision        6

    long_description    {*}${description}: \
        This port is kept up with the GNU Radio 3.7 release, \
        currently ${version}, which is a legacy branch that \
        will not see much maintenance\; place consider updating \
        to the \'gnuradio\' port.

    # force expansion of CMake-based template files
    patchfiles-append \
        patch-cmake-expand.37.diff
    # temporary patch to fix CTRLPORT testing
    patchfiles-append \
        patch-fix_ctrlport_test.37.diff
    # temporary patch to fix and allow external setting for
    # CMAKE_C/CXX_STANDARD
    patchfiles-append \
        patch-C_CXX_STANDARD.37.diff
    # temporary patch to fix use of GNUCXX to instead be for both GNU
    # and Clang compilers
    patchfiles-append \
        patch-fix-GNUCXX.37.diff

    set GR_VERSION_INFO "Release 3.7"

    conflicts_build gnuradio gnuradio-next

    github.livecheck.regex  {(3.7.[0-9.]+)}

}

subport gnuradio-next {

    name            gnuradio-next
    github.setup    gnuradio gnuradio 03d92ac19a11356175533aae4ad5804a354be075
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    version         20200419-[string range ${github.version} 0 7]
    checksums       rmd160  0a9e1294036e786116c96d68f9116e3f2798d325 \
                    sha256  bb2dab14e159a4b58ccf317ece3b4132959739f4b5b217d8e0add3c5ce581f5f \
                    size    3950832
    revision        7

    long_description    {*}${description}: \
        This port is kept up with the GNU Radio GIT 'master' \
        branch (as of 2018-09-20), which is typically updated \
        daily to weekly.  This version of GNU Radio represents \
        the next major release of GNU Radio, and hence its API \
        is likely to be different than that provided by gnuradio. \
        This port may or not compile \
        or function correctly, as it represents a work in progress. \
        If it does not work, check back in a few days.  Or try \
        deactivating the currently active gnuradio port, \
        cleaning any current builds, and trying again.

    conflicts       gnuradio gnuradio37

    # fix Quartz behaviour
    # TODO verify https://github.com/gnuradio/gnuradio/issues/2726
    patchfiles-append \
        patch-quartz_drag_n_drop.next.diff \
        patch-quartz_drop_crash.next.diff

    # set the version override string
    set GR_VERSION_INFO "next-git-[string range ${github.version} 0 7](${version})"

}

# disable all components by default;
# enable them only if the variant is enabled
configure.args-append \
    -DENABLE_GRC=OFF \
    -DENABLE_GR_QTGUI=OFF \
    -DENABLE_GR_WXGUI=OFF \
    -DQT_QMAKE_EXECUTABLE= \
    -DENABLE_GR_UHD=OFF \
    -DUHD_INCLUDE_DIRS= \
    -DUHD_LIBRARIES= \
    -DENABLE_GR_WAVELET=OFF \
    -DJACK_INCLUDE_DIR= \
    -DJACK_LIBRARY= \
    -DPORTAUDIO_INCLUDE_DIRS= \
    -DPORTAUDIO_LIBRARIES= \
    -DENABLE_GR_VIDEO_SDL=OFF \
    -DSDLMAIN_LIBRARY= \
    -DSDL_INCLUDE_DIR= \
    -DENABLE_GR_CTRLPORT=OFF \
    -DENABLE_CTRLPORT_THRIFT=OFF \
    -DENABLE_PERFORMANCE_COUNTERS=OFF \
    -DENABLE_GR_ZEROMQ=OFF \
    -DENABLE_GR_LOG=OFF \
    -DLOG4CPP_FOUND=OFF \
    -DLOG4CPP_INCLUDE_DIRS= \
    -DLOG4CPP_LIBRARIES=

# override default version string to be MacPorts-specific
configure.args-append \
    -DGR_GIT_COUNT="MacPorts" \
    -DGR_GIT_HASH="${GR_VERSION_INFO}"

# disable using internal VOLK
configure.args-append \
    -DENABLE_INTERNAL_VOLK=OFF

# default: don't create the application bundle
app.create no

variant grc description "Install GNU Radio Companion" {

    # these are checked for at configure, then required
    # for runtime; so use depends_lib to get both.
    if {${subport} eq "gnuradio37"} {

        depends_lib-append \
            port:py${active_python_version_no_dot}-pygtk \
            port:py${active_python_version_no_dot}-lxml

    } else {

        depends_lib-append \
            port:py${active_python_version_no_dot}-gobject3 \
            port:py${active_python_version_no_dot}-mako \
            port:py${active_python_version_no_dot}-lxml \
            port:py${active_python_version_no_dot}-yaml \
            path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
            path:lib/pkgconfig/cairo.pc:cairo \
            path:lib/pkgconfig/pango.pc:pango

    }

    # icons needed by GUI
    depends_run-append \
        port:adwaita-icon-theme

    configure.args-replace \
        -DENABLE_GRC=OFF \
        -DENABLE_GRC=ON

    # create the app to simplify usage
    app.create yes
    app.name GNURadio
    app.executable gnuradio-companion
    app.icon grc/scripts/freedesktop/grc-icon-256.png
    app.retina yes

}

variant qtgui description "Install GNU Radio with support for Qt GUI" {

    if {${subport} eq "gnuradio37"} {

        PortGroup qt4 1.0

        depends_lib-append \
            port:qwtplot3d

        # pyqt4 is checked for at configure, then required for
        # runtime; so use depends_lib to get both.
        depends_lib-append \
            port:py${active_python_version_no_dot}-pyqt4

        # Requires qwt >= 5.2, so not the "qwt" port.  Also requires
        # py*-pyqwt which does not compile with qwt >= 6.0; so -have- to
        # use qwt52 for now.
        depends_lib-append port:qwt52
        require_active_variants qwt52 qt4

        # pyqwt is not checked for at configure, but is
        # required for runtime; so use depends_run.
        depends_run-append \
            port:py${active_python_version_no_dot}-pyqwt

        configure.args-append \
            -DQWT_INCLUDE_DIRS=${prefix}/include/qwt \
            -DQWT_LIBRARIES=${prefix}/lib/libqwt.dylib

    } else {

        PortGroup qt5 1.0

        # pyqt5 is checked for at configure, then required for
        # runtime; so use depends_lib to get both.  Can use
        # either py*-pyqt5 or py*-pyqt5-devel.
        depends_lib-append \
            path:share/py${active_python_version_no_dot}-sip/PyQt5:py${active_python_version_no_dot}-pyqt5

        # Requires qwt >= 6.0, installed for qt5.
        # just use the new port that does this.
        depends_lib-append \
            port:qwt-qt5

        # require PyQtGraph for runtime only
        depends_run-append \
            port:py${active_python_version_no_dot}-pyqtgraph

        configure.args-append \
            -DQWT_INCLUDE_DIRS=${qt_dir}/lib/qwt.framework/Versions/6/Headers \
            -DQWT_LIBRARIES=${qt_dir}/lib/qwt.framework/qwt \
            -DQT_HEADERS_DIR=${qt_includes_dir}

    }

    configure.args-delete \
        -DENABLE_GR_QTGUI=OFF \
        -DQT_QMAKE_EXECUTABLE=

    configure.args-append \
        -DENABLE_GR_QTGUI=ON \
        -DQT_QMAKE_EXECUTABLE=${qt_qmake_cmd}

}



if {${subport} eq "gnuradio37"} {

    PortGroup wxWidgets 1.0

    variant wxgui description "Install GNU Radio with support for Wx GUI" {

        wxWidgets.use wxWidgets-3.0

        depends_lib-append \
            port:${wxWidgets.port}

        # wxpython is checked for at configure, then required
        # for runtime; so use depends_lib to get both.
        depends_lib-append \
            port:py${active_python_version_no_dot}-wxpython-3.0

        configure.args-delete \
            -DENABLE_GR_WXGUI=OFF

        configure.args-append \
            -DENABLE_GR_WXGUI=ON \
            -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig}

    }
} else {
    # gr-vocoder removed in-tree libgsm, libcodec2, use system-wide libs
    depends_lib-append \
        port:codec2 \
        port:libgsm

    # gr_modtool requires click and click-plugins, and these are
    # checked for at configure time, so have to use 'lib' here
    depends_lib-append \
        port:py${active_python_version_no_dot}-click \
        port:py${active_python_version_no_dot}-click-plugins \
        port:py${active_python_version_no_dot}-six
}

variant uhd description "Install GNU Radio with support for UHD" {

    # allow for uhd or uhd-devel
    depends_lib-append \
        path:lib/libuhd.dylib:uhd

    configure.args-delete \
        -DENABLE_GR_UHD=OFF \
        -DUHD_INCLUDE_DIRS= \
        -DUHD_LIBRARIES=

    configure.args-append \
        -DENABLE_GR_UHD=ON \
        -DUHD_INCLUDE_DIRS=${prefix}/include \
        -DUHD_LIBRARIES=${prefix}/lib/libuhd.dylib

}


variant wavelet description "Install GNU Radio Wavelet component" {

    depends_lib-append \
        port:gsl

    configure.args-replace \
        -DENABLE_GR_WAVELET=OFF \
        -DENABLE_GR_WAVELET=ON

}

variant jack description "Install GNU Radio with support for JACK audio" {

    depends_lib-append \
        port:jack

    configure.args-delete \
        -DJACK_INCLUDE_DIR= \
        -DJACK_LIBRARY=

    configure.args-append \
        -DJACK_INCLUDE_DIR=${prefix}/include \
        -DJACK_LIBRARY=${prefix}/lib/libjack.dylib

}

variant portaudio description "Install GNU Radio with support for portaudio audio" {

    depends_lib-append \
        port:portaudio

    configure.args-delete \
        -DPORTAUDIO_INCLUDE_DIRS= \
        -DPORTAUDIO_LIBRARIES=

    configure.args-append \
        -DPORTAUDIO_INCLUDE_DIRS=${prefix}/include \
        -DPORTAUDIO_LIBRARIES=${prefix}/lib/libportaudio.dylib

}

variant sdl description "Install GNU Radio with support for SDL-based video" {

    depends_lib-append \
        port:libsdl

    configure.args-delete \
        -DENABLE_GR_VIDEO_SDL=OFF \
        -DSDLMAIN_LIBRARY= \
        -DSDL_INCLUDE_DIR=

    configure.args-append \
        -DENABLE_GR_VIDEO_SDL=ON \
        -DSDLMAIN_LIBRARY=${prefix}/lib/libSDLmain.a \
        -DSDL_INCLUDE_DIR=${prefix}/include/SDL

}

variant ctrlport description {Install GNU Radio with support for control port (CTRLPORT) enhancements (EXPERIMENTAL)} {

    # A ctrlport utility uses graphviz for display purposes. This
    # dependency is not checked for during configuration nor used
    # during building or testing. It is just a runtime dependency.
    depends_run-append \
        path:bin/dot:graphviz

    # enable configuration arguments for ctrlport
    configure.args-replace \
        -DENABLE_GR_CTRLPORT=OFF \
        -DENABLE_GR_CTRLPORT=ON

}

variant ctrlport_thrift description {Install GNU Radio with support for Thrift for +ctrlport (EXPERIMENTAL)} requires ctrlport {

    # require Thrift and Python Thrift
    depends_lib-append \
        port:py${active_python_version_no_dot}-thrift \
        port:thrift

    # enable configuration arguments for ctrlport and thrift
    configure.args-replace \
        -DENABLE_CTRLPORT_THRIFT=OFF \
        -DENABLE_CTRLPORT_THRIFT=ON

}

variant performance_counters description {Install GNU Radio with support for performance counters (EXPERIMENTAL)} {

    # these are not checked for at configure, but are
    # required for runtime; so use depends_run.
    depends_run-append \
        port:py${active_python_version_no_dot}-pygraphviz \
        port:py${active_python_version_no_dot}-networkx

    configure.args-replace \
        -DENABLE_PERFORMANCE_COUNTERS=OFF \
        -DENABLE_PERFORMANCE_COUNTERS=ON

}

variant zeromq description {Install GNU Radio with support for the ZeroMQ lightweight messaging kernel (EXPERIMENTAL)} {

    # can use cppzmq or cppzmq-devel
    depends_lib-append \
        path:include/zmq.hpp:cppzmq

    # pyzmq is not checked for at configure, but is
    # required for runtime; so use depends_run.
    depends_run-append \
        port:py${active_python_version_no_dot}-zmq

    configure.args-replace \
        -DENABLE_GR_ZEROMQ=OFF \
        -DENABLE_GR_ZEROMQ=ON

}

if {${subport} eq "gnuradio37"} {

    # logging is an option on GR release and devel (3.7).
    variant logging description {Install GNU Radio with support for logging} {
        configure.args-replace \
            -DENABLE_GR_LOG=OFF \
            -DENABLE_GR_LOG=ON
    }

    variant log4cpp requires logging description {Install GNU Radio with support for logging via log4cpp} {
        depends_lib-append  \
            port:log4cpp

        configure.args-delete \
            -DLOG4CPP_FOUND= \
            -DLOG4CPP_INCLUDE_DIRS= \
            -DLOG4CPP_LIBRARIES=

        configure.args-append \
            -DLOG4CPP_FOUND=ON \
            -DLOG4CPP_INCLUDE_DIRS=${prefix}/include \
            -DLOG4CPP_LIBRARIES=${prefix}/lib/liblog4cpp.dylib
    }

    # enabled by default
    default_variants +logging +log4cpp

} else {

    # logging using log4cpp is always enabled on GR 3.8+
    depends_lib-append  \
        port:log4cpp

    configure.args-delete \
        -DLOG4CPP_FOUND=OFF \
        -DLOG4CPP_INCLUDE_DIRS= \
        -DLOG4CPP_LIBRARIES=

    configure.args-append \
        -DLOG4CPP_FOUND=ON \
        -DLOG4CPP_INCLUDE_DIRS=${prefix}/include \
        -DLOG4CPP_LIBRARIES=${prefix}/lib/liblog4cpp.dylib

}

variant quartz conflicts x11 description {Enable native macOS graphics support} {
    require_active_variants gtk3 quartz
    require_active_variants path:lib/pkgconfig/cairo.pc:cairo quartz
    require_active_variants path:lib/pkgconfig/pango.pc:pango quartz
}

variant x11 conflicts quartz description {Enable X11 support} {
    require_active_variants gtk3 x11
    require_active_variants path:lib/pkgconfig/cairo.pc:cairo x11
    require_active_variants path:lib/pkgconfig/pango.pc:pango x11
}

# per user consensus: enable all variants except +jack, +portaudio,
# +ctrlport, +ctrlport_thrift, +performance_counters,+debug, and
# +universal.
default_variants +grc +qtgui +uhd +wavelet \
    +sdl +zeromq

post-destroot {
    # fix residual cmake module install location when
    # CMAKE_MODULES_DIR does not do the whole trick

    # GNU Radio installs cmake files as "Config*.cmake", which has the
    # odd behavior of requiring the files to be in a top-level shared
    # cmake directory; for MacPorts' cmake, these are, apparently:
    # ${prefix}/lib:${prefix}/share/cmake .  I don't know why ... but
    # I do not want cmake files installed into the former, so move
    # them to the latter.

    if {[file exists ${destroot}${prefix}/lib/cmake]} {
        xinstall -m 755 -d ${destroot}${prefix}/share
        move ${destroot}${prefix}/lib/cmake \
            ${destroot}${prefix}/share
    }
}

# TODO test; at first sight, require CMAKE_BUILD_TYPE=Coverage
