# -*- 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 108390 2013-07-22 14:36:17Z michaelld@macports.org $

PortSystem          1.0
PortGroup           cmake 1.0
PortGroup           active_variants 1.1

name                gr-osmosdr
maintainers         michaelld openmaintainer

description         gr-osmosdr provides support for OsmoSDR hardware within GNU Radio

categories          science comms
homepage            http://sdr.osmocom.org/trac/wiki/GrOsmoSDR
license             GPL-3
platforms           darwin macosx

set description_common {Includes OSMO SDR support GNU Radio source and sink blocks in C++, Python, and GNU Radio Companion (grc).  This port also offers a wrapper functionality for the FunCube Dongle, Ettus UHD, and rtl-sdr radios.  By using gr-osmosdr source you can take advantage of a common software API in your applications independent of the underlying radio hardware.}

if {${subport} == ${name}} {

    long_description    ${description}.  \
        This port is kept up with the gr-osmosdr GIT 'master' branch, which is typically updated weekly to monthly, providing compatibility with GNU Radio release 3.7 API: the gnuradio and gnuradio-devel ports.  ${description_common}

    version             20130720

    fetch.type          git
    git.url             git://git.osmocom.org/gr-osmosdr
    git.branch          e5f7b28093c10f05d272bcf12c6c4b6583af7021

    conflicts           gr-osmosdr-legacy

    # allow gr-osmosdr to work with both gnuradio and gnuradio-devel ...

    depends_lib-append  path:lib/libgnuradio-audio.dylib:gnuradio

    # ... but not with gnuradio-legacy or gnuradio-next

    pre-fetch {
        if {![catch {set installed [lindex [registry_active gnuradio-legacy] 0]}]} {
            # gnuradio-legacy is installed; this version of gr-osmosdr does not work with gnuradio-legacy
            ui_msg "\nError: ${name} requires the gnuradio or gnuradio-devel port, and will not work with the gnuradio-legacy port.  deactivate gnuradio-legacy, and then install or activate gnuradio or gnuradio-devel.\n"
            return -code error "Invalid port dependency: gnuradio-legacy"
        }
        if {![catch {set installed [lindex [registry_active gnuradio-next] 0]}]} {
            # gnuradio-next is installed; this version of gr-osmosdr does not work with gnuradio-next
            ui_msg "\nError: ${name} requires the gnuradio or gnuradio-devel port, and will not work with the gnuradio-next port.  deactivate gnuradio-next, and then install or activate gnuradio or gnuradio-devel.\n"
            return -code error "Invalid port dependency: gnuradio-next"
        }
    }

    livecheck.url       http://cgit.osmocom.org/gr-osmosdr/log/?h=master
    livecheck.version   ${git.branch}
    livecheck.regex     id=(\[a-f0-9\]+).*>HEAD<

}

subport gr-osmosdr-legacy {

    long_description    ${description}.  \
        This port is for the gr-osmosdr gr3.6 branch, providing compatibility with GNU Radio release 3.6 API: the gnuradio-legacy port.  ${description_common}

    version             20130612

    fetch.type          git
    git.url             git://git.osmocom.org/gr-osmosdr
    git.branch          ffd2bbf424963ae2c4249fbeb9dbca9b2c3870c4

    conflicts           gr-osmosdr

    # gr-osmosdr-legacy works with gnuradio-legacy only

    depends_lib-append  port:gnuradio-legacy

    livecheck.url       http://cgit.osmocom.org/gr-osmosdr/log/?h=gr3.6
    livecheck.version   ${git.branch}
    livecheck.regex     id=(\[a-f0-9\]+).*>gr3.6<

}

subport gr-osmosdr-next {

    replaced_by gr-osmosdr

    version             20130610
    revision            1

    distfiles

    pre-configure {
        ui_error "${name} has been renamed to ${replaced_by}. Please install ${replaced_by} instead."
        return -code error "obsolete port"
    }

    livecheck.type      none

}

depends_lib-append  port:rtl-sdr

# do VPATH build

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

# 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

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

            # require gnuradio to also have this variant
            require_active_variants \
                path:lib/libgnuradio-audio.dylib:gnuradio ${p}

            # specify the Python dependencies

            depends_lib-append \
                port:${p}

            # 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

        }
    }]
}

# 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 "${name} requires a Python to be selected; you cannot use -python27 as a variant alone."
    return -code error "Invalid variant selection"
}

# shortcut to installing all variants except Python

variant full requires docs uhd swig hackrf \
    description {Enable all variants except +debug and +universal} {}

variant docs description "Install ${name} documentation" {

    depends_build-append \
        port:doxygen \
        path:bin/dot:graphviz

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

}

if {![variant_isset docs]} {

    configure.args-append \
        -DDOXYGEN_DOT_EXECUTABLE= \
        -DDOXYGEN_EXECUTABLE=

}

variant uhd description "Install ${name} with support for UHD" {

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

    # require gnuradio to also have this variant

    require_active_variants \
        path:lib/libgnuradio-audio.dylib:gnuradio uhd

    configure.args-append \
        -DENABLE_UHD=ON \
        -DGNURADIO_UHD_INCLUDE_DIRS=${prefix}/include \
        -DGNURADIO_UHD_LIBRARIES=${prefix}/lib/libgnuradio-uhd.dylib \
        -DUHD_INCLUDE_DIRS=${prefix}/include/uhd \
        -DUHD_LIBRARIES=${prefix}/lib/libuhd.dylib

}

if {![variant_isset uhd]} {

    configure.args-append \
        -DENABLE_UHD=OFF \
        -DGNURADIO_UHD_INCLUDE_DIRS= \
        -DGNURADIO_UHD_LIBRARIES= \
        -DUHD_INCLUDE_DIRS= \
        -DUHD_LIBRARIES=

}

variant swig description "Install ${name} with support for SWIG-base Python bindings" {

    depends_lib-append \
        port:swig-python

    # require gnuradio to also have this variant

    require_active_variants \
        path:lib/libgnuradio-audio.dylib:gnuradio swig

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

}

if {![variant_isset swig]} {

    configure.args-append \
        -DSWIG_EXECUTABLE=

}

variant hackrf description "Install ${name} with support for hackrf" {

    depends_lib-append port:hackrf
    configure.args-append -DENABLE_HACKRF=ON

}

if {![variant_isset hackrf]} {

    configure.args-append -DENABLE_HACKRF=OFF

}
