# -*- 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           active_variants 1.1
PortGroup           github 1.0
PortGroup           gnuradio 1.0

name                gr-osmosdr
maintainers         {michaelld @michaelld} openmaintainer
homepage            http://sdr.osmocom.org/trac/wiki/GrOsmoSDR
license             GPL-3
description         gr-osmosdr provides support for OsmoSDR hardware within GNU Radio
long_description    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, FunCube Dongle Pro+, HackRF, RFSpace, Ettus UHD, and rtl-sdr radios \
    and software.  By using gr-osmosdr source you can take advantage of a common software API \
    in your applications independent of the underlying radio hardware.

if {${name} eq ${subport}} {
    github.setup osmocom gr-osmosdr cffef690f29e0793cd2d6c5d028c0c929115f0ac
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    version      20210117-[string range ${github.version} 0 7]
    checksums    rmd160  d80040fa6a40280dfff5e010d5f9b2575280f5e6 \
                 sha256  ce916d4fd04af6cde67e137f1ffc4ac69c12284c49f412312ace13622b1a1d07 \
                 size    252770
    revision     3

    conflicts gr37-osmosdr

    # add out-of-tree backend
    patchfiles-append \
        add_airspy-spyserver_support.patch \
        add_gr-iio_support.patch \
        patch-fix-SWIG41-use.diff

    github.livecheck.branch gr3.8
}

subport gr37-osmosdr {

    name         gr37-osmosdr

    github.setup osmocom gr-osmosdr a95bbd74181de7df55cb14d728ee313314f6c092
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    github.livecheck.branch gr3.7
    version      20191201-[string range ${github.version} 0 7]
    checksums    rmd160  1d4c0d1738a2c975caab615e89900d6ec958817b \
                 sha256  baf4ce1cc0531c25e33c6b8b3c5eed12d2af1903c3c9a6c364a9b8360199fd48 \
                 size    278344
    revision     5

    conflicts gr-osmosdr

    github.livecheck.branch gr3.7
}

# default
configure.args-append \
    -DENABLE_IQBALANCE=OFF \
    -DENABLE_OSMOSDR=OFF \
    -DENABLE_FILE=ON \
    -DENABLE_MIRI=OFF

variant uhd description "Install ${name} with support for UHD" {
    # allow uhd or uhd-devel
    depends_lib-append \
        path:lib/libuhd.dylib:uhd

    # require gnuradio to also have this variant
    require_active_variants \
        path:lib/libgnuradio-runtime.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 iio description "Install ${name} with support for IIO devices like PlutoSDR" {
    depends_lib-append \
        path:lib/libiio.dylib:libiio \
        port:gr-iio

    configure.args-append \
        -DENABLE_IIO=ON \
        -DGNURADIO_IIO_INCLUDE_DIRS=${prefix}/include \
        -DGNURADIO_IIO_LIBRARIES=${prefix}/lib/libgnuradio-iio.dylib \
        -DIIO_INCLUDE_DIRS=${prefix}/include \
        -DIIO_LIBRARIES=${prefix}/lib/libiio.dylib
}

if {![variant_isset iio]} {
    configure.args-append \
        -DENABLE_IIO=OFF \
        -DGNURADIO_IIO_INCLUDE_DIRS= \
        -DGNURADIO_IIO_LIBRARIES= \
        -DIIO_INCLUDE_DIRS= \
        -DIIO_LIBRARIES=
}

variant hackrf description "Install ${name} with support for hackrf" {
    # allow for release or devel
    depends_lib-append path:lib/libhackrf.dylib:hackrf
    configure.args-append -DENABLE_HACKRF=ON
}

if {![variant_isset hackrf]} {
    configure.args-append -DENABLE_HACKRF=OFF
}

variant fcdproplus description "Install ${name} with support for FunCube Dongle Pro+ (fcdproplus)" {
    if {${subport} eq "gr37-osmosdr"} {
        depends_lib-append \
            port:gr37-fcdproplus
    } else {
        depends_lib-append \
            port:gr-fcdproplus
    }

   configure.args-append -DENABLE_FCDPP=ON
}

if {![variant_isset fcdproplus]} {
    configure.args-append -DENABLE_FCDPP=OFF
}

variant rtlsdr description "Install ${name} with support for rtl-sdr" {
    depends_lib-append port:rtl-sdr
    configure.args-append -DENABLE_RTL=ON -DENABLE_RTL_TCP=ON
}

if {![variant_isset rtlsdr]} {
    configure.args-append -DENABLE_RTL=OFF -DENABLE_RTL_TCP=OFF
}

variant bladeRF description "Install ${name} with support for bladeRF" {
    depends_lib-append port:bladeRF
    configure.args-append -DENABLE_BLADERF=ON
}

if {![variant_isset bladeRF]} {
    configure.args-append -DENABLE_BLADERF=OFF
}

variant airspy description "Install ${name} with support for airspy" {
    depends_lib-append port:airspy
    configure.args-append -DENABLE_AIRSPY=ON
}

if {![variant_isset airspy]} {
    configure.args-append -DENABLE_AIRSPY=OFF
}

variant spyserver description "Install ${name} with support for airspy spyserver" {
    configure.args-append -DENABLE_SPYSERVER=ON
}

if {![variant_isset spyserver]} {
    configure.args-append -DENABLE_SPYSERVER=OFF
}

variant redpitaya description "Install ${name} with support for Red Pitaya" {
    configure.args-append -DENABLE_REDPITAYA=ON
}

if {![variant_isset redpitaya]} {
    configure.args-append -DENABLE_REDPITAYA=OFF
}

variant rfspace description "Install ${name} with support for RFSpace" {
    configure.args-append -DENABLE_RFSPACE=ON
}

if {![variant_isset rfspace]} {
    configure.args-append -DENABLE_RFSPACE=OFF
}

# SoapySDR and SDRPlay work on 10.9 and newer only until fixed
platform darwin {
    if {${os.major} > 12} {

        variant soapysdr description "Install ${name} with support for SoapySDR" {
            depends_lib-append port:SoapySDR
            configure.args-append -DENABLE_SOAPY=ON
        }

        if {![variant_isset soapysdr]} {
            configure.args-append -DENABLE_SOAPY=OFF
        }

        variant sdrplay description "Install ${name} with support for SDRplay" {
            depends_lib-append port:SDRplay
            configure.args-append -DENABLE_NONFREE=ON -DENABLE_SDRPLAY=ON
        }

        if {![variant_isset sdrplay]} {
            configure.args-append -DENABLE_NONFREE=OFF -DENABLE_SDRPLAY=OFF
        }

    } else {
        # for any Mac OS X <= 10.8 (major <= 12), explicitly disable
        # SOAPYSDR and SDRPLAY. Just in case they might be installed
        # somehow, we don't want to link with those installs.
        configure.args-append \
            -DENABLE_SOAPY=OFF \
            -DENABLE_NONFREE=OFF \
            -DENABLE_SDRPLAY=OFF
    }
}

# available only on >GR3.7 versions
if {${subport} eq "gr-osmosdr"} {
    variant airspyhf description "Install ${name} with support for airspyhf" {
        depends_lib-append port:airspyhf
        configure.args-append -DENABLE_AIRSPYHF=ON
    }

    if {![variant_isset airspyhf]} {
        configure.args-append -DENABLE_AIRSPYHF=OFF
    }
}

# per user concensus: enable all variants except +debug and +universal
default_variants +uhd +hackrf +rtlsdr +bladeRF +airspy +redpitaya +rfspace +fcdproplus +spyserver

# SoapySDR and SDRPlay work on 10.9 and newer only until fixed
platform darwin {
    if {${os.major} > 12} {
        default_variants +soapysdr
        if {${build_arch} eq "x86_64"} {
            default_variants +sdrplay
        }
    }
}

# available only on >GR3.7 versions
if {${subport} eq "gr-osmosdr"} {
    default_variants +airspyhf
}
