# -*- 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
# $Id: Portfile 120308 2014-05-22 19:22:53Z michaelld@macports.org $

PortSystem          1.0
PortGroup           cmake 1.0
PortGroup           wxWidgets 1.0
PortGroup           active_variants 1.1
PortGroup           github 1.0

wxWidgets.use       wxPython-3.0

name                gnuradio
maintainers         michaelld openmaintainer

description         GNU Radio is Software Defined Radio (SDR)

categories          science comms
license             GPL-3
platforms           darwin macosx

dist_subdir         gnuradio

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

    github.setup    gnuradio gnuradio 3.7.3 v
    revision        2

    long_description    ${description}: \
        This port is kept up with the GNU Radio release, currently ${version}, which is typically updated every few months.

    conflicts       gnuradio-legacy gnuradio-next gnuradio-devel

    checksums \
        rmd160 5d1b3d03c96e0afd193407346f4f24ed4ed3907b \
        sha256 bec2eee034a525b268e14b2b2529d419313d36844f794f730f7176f75a52262f

    patchfiles-append \
        patch-cmake-expand.release.diff

    # overload the github livecheck regex to look for versions that
    # are just numbers and '.', no letters (e.g., "3.7.3git").

    livecheck.regex archive/[join ${github.tag_prefix} ""](\[0-9\.\]+)${extract.suffix}

}

subport gnuradio-legacy {

    github.setup    gnuradio gnuradio 3.6.5.1 v
    name            gnuradio-legacy
    revision        8

    long_description    ${description}: \
        This port is for GNU Radio ${version}, reflecting the final release in the 3.6 API series.  It is for legacy purposes only\; please consider updating your code to the GNU Radio 3.7 API.

    conflicts       gnuradio gnuradio-next gnuradio-devel

    checksums \
        rmd160 f5d5890fca81e5c379d32b3504976bc24b7ac12f \
        sha256 1af7840b9b46aaa85786bf58274da4334b84192e04990b4f3132c1a84b84956f

    # fix INCLUDE_DIRECTORIES path order such that internal
    # directories are searched before ${prefix}/include or system
    # ones.

    patchfiles-append patch-path-order.diff

    # add checks for xgetbv instruction and cvtpi32_ps intrinsic

    patchfiles-append patch-volk_lib_CMakeLists.txt.legacy.diff

    # fix VOLK arch generators to include clang

    patchfiles-append patch-volk_gen_archs.xml.legacy.diff

    # fix SWIG include of std_string to come before anything else,
    # otherwise it will not be properly defined later on.

    patchfiles-append patch-gnuradio-core_swig_include-std_string.i.diff

    # fix friend function API declaration

    patchfiles-append patch-gnuradio-core_src_lib_io_ppio_ppdev.h.diff

    # 3.6.5.1 is the last of the 3.6 API development

    livecheck.type  none

}

subport gnuradio-devel {

    long_description    ${description}: \
        This port is kept up with the GNU Radio GIT 'master' branch, which is typically updated daily to weekly.  This version of GNU Radio generally contains fixes to, and its API is compatible with, the current GNU Radio release, and will be incorporated in an upcoming release.  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.

    github.setup        gnuradio gnuradio cba2d184e2abb8a52353929a34b562ea4f9bd031
    version             3.7.4_20140520
    revision            1
    name                gnuradio-devel

    conflicts           gnuradio-legacy gnuradio gnuradio-next

    checksums \
        rmd160 673aa98e4a5cc9b2340c56bfacddafacca7755bc \
        sha256 0d8e006c086a9421798aa7d2b481e147d3b5919e44645689ceda50620610c9c2

}

subport gnuradio-next {

    long_description    ${description}: \
        This port is kept up with the GNU Radio GIT 'next' branch, 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 either gnuradio or gnuradio-devel.  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.

    github.setup        gnuradio gnuradio c38322bea953f6c33b530ea3b171eb3bcc39d916
    version             3.8.0_20140520
    revision            1
    name                gnuradio-next

    conflicts           gnuradio-legacy gnuradio gnuradio-devel

    checksums \
        rmd160 c0ef14a81db958e22743a78d2d4ff1352b4327e8 \
        sha256 01cb26c48b1f95152d2eb4c49881148db6ec03b13be499f558a0a729e99f4775

    # overload the github livecheck URL with the correct branch

    livecheck.url       ${github.homepage}/commits/next.atom

}

if {${subport} ne "gnuradio-legacy"} {

    # GNU Radio 3.7 has requirements that 3.6 does not, and provides
    # features that 3.6 does not have.

    depends_lib-append port:log4cpp

    variant ctrlport description {Enable control port enhancements (EXPERIMENTAL)} {}

    if {[variant_isset ctrlport]} {

        depends_lib-append port:zeroc-ice35
        configure.args-append -DENABLE_GR_CTRLPORT=ON

    } else {

        configure.args-append -DENABLE_GR_CTRLPORT=OFF

    }

    variant performance_counters description {Enable support for performance counters (EXPERIMENTAL)} {}

    if {[variant_isset performance_counters]} {

        configure.args-append \
            -DENABLE_PERFORMANCE_COUNTERS=ON

    } else {

        configure.args-append \
            -DENABLE_PERFORMANCE_COUNTERS=OFF

    }

    if {${subport} ne "gnuradio"} {

        # patches for devel and next (which are kept aligned for now)

        # temporary patch to fix header creation to be before dependencies
        # are determined, such that local (to the build) headers are found
        # before already-installed ones.

        patchfiles-append   patch-cmake-expand.devel.diff

        # the zeromq variant is for devel and next only
        # until the next release

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

        if {[variant_isset zeromq]} {

            depends_lib-append \
                port:cppzmq

            configure.args-append \
                -DENABLE_GR_ZEROMQ=ON

        } else {

            configure.args-append \
                -DENABLE_GR_ZEROMQ=OFF

        }
    }
}

# override githib PortGroup homepage setting

homepage            http://gnuradio.org/

use_parallel_build  yes

depends_lib-append  \
    port:boost \
    port:cppunit \
    port:fftw-3-single

depends_build-append \
    port:pkgconfig

# do VPATH build

set vpath           ${workpath}/build
pre-configure       { file mkdir ${vpath} }
configure.dir       ${vpath}
build.dir           ${vpath}

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

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

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

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

# always enable VOLK, with or without orc

configure.args-append -DENABLE_VOLK=ON

# set last configure argument to the reletive path
# to the top-level cmake source

configure.post_args ../${worksrcdir}

# be verbose when building, for debugging purposes

build.post_args VERBOSE=1

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

# shortcut to installing all variants except Python

variant full description {Deprecated legacy variant} {}
default_variants -full

# per user concensus: enable all variants except +debug and +universal
# (and, for next, except +ctrlport, +performance_counters, and +zeromq);

default_variants +docs +grc +qtgui +wxgui +uhd +orc +wavelet \
    +jack +portaudio +swig +sdl

# set Python variants

set pythons_suffixes {26 27}
global chosen_python_suffix
set chosen_python_suffix ""

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

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [join [split ${s} ""] "."]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    eval [subst {
        variant ${p} description "Build GNU Radio using Python ${v}" \
            conflicts ${c} {

            set chosen_python_suffix ${s}

            # specify the Python dependencies

            depends_lib-append \
                port:${p}

            # these are checked for at configure, then required for
            # runtime; so use depends_lib to get both.

            depends_lib-append \
                port:py${s}-numpy \
                port:py${s}-cheetah

            # these are not checked for at configure, but are required
            # for runtime; so use depends_run.

            depends_run-append \
                port:py${s}-opengl \
                port:py${s}-scipy

            # specify the Python version to use

            configure.args-append \
                -DPYTHON_EXECUTABLE=${prefix}/bin/python${v} \
                -DPYTHON_INCLUDE_DIR=${prefix}/Library/Frameworks/Python.framework/Versions/${v}/Headers \
                -DPYTHON_LIBRARY=${prefix}/Library/Frameworks/Python.framework/Versions/${v}/Python \
                -DGR_PYTHON_DIR=${frameworks_dir}/Python.framework/Versions/${v}/lib/python${v}/site-packages

            # check for variants that require Python

            if {[variant_isset docs]} {

                depends_build-append \
                    port:py${s}-sphinx

                configure.args-append \
                    -DSPHINX_EXECUTABLE=${prefix}/bin/sphinx-build-${v}

            }

            if {[variant_isset grc]} {

                # these are checked for at configure, then required
                # for runtime; so use depends_lib to get both.

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

            }

            if {[variant_isset qtgui]} {

                # pyqt4 is checked for at configure, then required for
                # runtime; so use depends_lib to get both.  Can use
                # either py*-pyqt4 or py*-pyqt4-devel.

                depends_lib-append \
                    path:share/py${s}-sip/PyQt4:py${s}-pyqt4

                # pyqwt is not checked for at configure, but is
                # required for runtime; so use depends_run.

                depends_run-append \
                    port:py${s}-pyqwt

            }

            if {[variant_isset wxgui]} {

                # wxpython is checked for at configure, then required
                # for runtime; so use depends_lib to get both.

                depends_lib-append \
                    port:py${s}-wxpython-3.0

            }

            if {[variant_exists ctrlport] && \
                [variant_isset ctrlport]} {

                # pyice is not checked for at configure, but is
                # required for runtime; so use depends_run.

                depends_run-append \
                    port:py${s}-zeroc-ice35

            }

            if {[variant_exists performance_counters] && \
                [variant_isset performance_counters]} {

                # these are not checked for at configure, but are
                # required for runtime; so use depends_run.

                depends_run-append \
                    port:py${s}-pygraphviz \
                    port:py${s}-networkx

            }

            if {[variant_exists zeromq] && \
                [variant_isset zeromq]} {

                # pyzmq is not checked for at configure, but is
                # required for runtime; so use depends_run.

                depends_run-append \
                    port:py${s}-zmq

            }
        }
    }]
}

# Python is required; default to 2.7

if {![variant_isset python26] && ![variant_isset python27]} {
    default_variants +python27
}

# Make sure -python27 was not specified alone

if {![variant_isset python26] && ![variant_isset python27]} {
    ui_error "GNU Radio requires a Python to be selected; you cannot use -python27 as a variant alone."
    return -code error "Invalid variant selection"
}

variant docs description "Install GNU Radio documentation" {

    depends_lib-append \
        port:doxygen \
        path:bin/dot:graphviz \
        port:xmlto

    configure.args-append \
        -DENABLE_DOXYGEN=ON \
        -DENABLE_SPHINX=ON

}

if {![variant_isset docs]} {

    configure.args-append \
        -DENABLE_DOXYGEN=OFF \
        -DENABLE_SPHINX=OFF

}

variant grc requires swig description "Install GNU Radio Companion" {

    configure.args-append \
        -DENABLE_GRC=ON

}

if {![variant_isset grc]} {

    configure.args-append \
        -DENABLE_GRC=OFF

}

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

    depends_lib-append \
        port:qt4-mac \
        port:qwtplot3d

    configure.args-append \
        -DENABLE_GR_QTGUI=ON \
        -DQT_QMAKE_EXECUTABLE=${prefix}/bin/qmake

    # 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

}

if {![variant_isset qtgui]} {

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

}

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

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

    configure.args-append \
        -DENABLE_GR_WXGUI=ON

}

if {![variant_isset wxgui]} {

    configure.args-append \
        -DENABLE_GR_WXGUI=OFF

}

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

    depends_lib-append \
        path:lib/libuhd.dylib:uhd

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

}

if {![variant_isset uhd]} {

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

}

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

    depends_lib-append \
        port:orc

    configure.args-append \
        -DORCC_EXECUTABLE=${prefix}/bin/orcc

}

if {![variant_isset orc]} {

    configure.args-append \
        -DORCC_EXECUTABLE=

}

variant wavelet description "Install GNU Radio Wavelet component" {

    depends_lib-append \
        port:gsl

    configure.args-append \
        -DENABLE_GR_WAVELET=ON

}

if {![variant_isset wavelet]} {

    configure.args-append \
        -DENABLE_GR_WAVELET=OFF

}

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

    depends_lib-append \
        port:jack

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

}

if {![variant_isset jack]} {

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

}

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

    depends_lib-append \
        port:portaudio

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

}

if {![variant_isset portaudio]} {

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

}

variant swig description "Install GNU Radio with support for SWIG-base Python bindings" {

    depends_build-append \
        port:swig-python

    configure.args-append \
        -DSWIG_EXECUTABLE=${prefix}/bin/swig

}

if {![variant_isset swig]} {

    configure.args-append \
        -DSWIG_EXECUTABLE=

}

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

    depends_lib-append \
        port:libsdl

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

}

if {![variant_isset sdl]} {

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

}
