# -*- 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
# $Id: Portfile 120334 2014-05-23 15:19:30Z michaelld@macports.org $

PortSystem          1.0
PortGroup           cmake 1.0
PortGroup           github 1.0

name                uhd
categories          science comms
maintainers         michaelld openmaintainer
description         USRP Hardware Driver for Ettus Research Products
license             GPL-3+

homepage            https://ettus-apps.sourcerepo.com/redmine/ettus/projects/uhd/wiki
platforms           darwin

dist_subdir         uhd
set base_version    3.7.1

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

    long_description ${description}: \
Provides the release version, which is typically updated every month or so.

    github.setup    EttusResearch uhd \
        release_00[join [split ${base_version} "."] "_00"]
    version         ${base_version}
    conflicts       uhd-devel
    checksums       rmd160 e8919fb82d2695861d8a7d10075e01a801b2d0f7 \
                    sha256 114f85e89de17dd972642fdaee86a1205a607a01fd9fe38c3ecb9281244ae03b

}

subport uhd-devel {

    long_description ${description}: \
Provides the GIT master version, which is typically updated every few days to week.

    github.setup    EttusResearch uhd c4b1f8103343b9932ca1e74128de3afbbcb81928
    version         ${base_version}_20140522
    name            uhd-devel
    conflicts       uhd
    checksums       rmd160 ab45c62a7f4f4b2ae7ae3a6fbd03b6c0b7a08018 \
                    sha256 870aec0d6223f4cdf4f379facacfe1cdcca9ff3fa7de52f8eb8fa8a29b0f3e2e

}

depends_lib-append	 port:boost port:ncurses

depends_build-append port:pkgconfig

# do VPATH build

post-extract        { file mkdir ${workpath}/build }
configure.dir       ${workpath}/build
build.dir           ${workpath}/build

# 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

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

configure.post_args ../${worksrcdir}/host

# explicitly set configure args -D cmake flags to get MacPorts'
# curses; these are used by some variants, but they don't hurt.

configure.args-append \
    -DCURSES_CURSES_H_PATH=${prefix}/include \
    -DCURSES_CURSES_LIBRARY=${prefix}/lib/libcurses.dylib \
    -DCURSES_FORM_LIBRARY=${prefix}/lib/libform.dylib \
    -DCURSES_HAVE_CURSES_H=${prefix}/include/curses.h \
    -DCURSES_INCLUDE_PATH=${prefix}/include \
    -DCURSES_LIBRARY=${prefix}/lib/libcurses.dylib \
    -DCURSES_NCURSES_LIBRARY=${prefix}/lib/libncurses.dylib \
    -DPKG_LIB_DIR=${prefix}/share/uhd

# be verbose when building, for debugging purposes

build.post_args VERBOSE=1

# 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

default_variants +docs +examples +libusb +manual +test +orc

# 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 UHD for Python ${v}" \
            conflicts ${c} {

            set chosen_python_suffix ${s}
            depends_lib-append \
                port:${p} \
                port:py${s}-cheetah

            # specify that Python version to use
            configure.args-append \
                -DPYTHON_EXECUTABLE=${prefix}/bin/python${v}

            # after destroot, have uhd fetch its images into the
            # destroot area, so that they are also tracked by MacPorts

            post-destroot {
                ui_debug "${prefix}/bin/python${v} ${destroot}${prefix}/share/uhd/utils/uhd_images_downloader.py --install-location=${destroot}${prefix}/share/uhd/images"
                system "${prefix}/bin/python${v} ${destroot}${prefix}/share/uhd/utils/uhd_images_downloader.py --install-location=${destroot}${prefix}/share/uhd/images"
            }
        }
    }]
}

# if no python variant is set, default to 2.7
if {![variant_isset python26] &&
    ![variant_isset python27]} {
    default_variants +python27
}

# make sure -python27 was not used alone
if {![variant_isset python26] &&
    ![variant_isset python27]} {
    return -code error "Exactly one Python variant must be selected."
}

variant test description {enable tests} {
    configure.args-append -DENABLE_TESTS=ON
    test.run yes
}

if {![variant_isset test]} {
    configure.args-append -DENABLE_TESTS=OFF
}

variant examples description {enable examples} {
    configure.args-append -DENABLE_EXAMPLES=ON
}

if {![variant_isset examples]} {
    configure.args-append -DENABLE_EXAMPLES=OFF
}

variant libusb description {enable USB support via libusb version 1.0} {
    depends_lib-append port:libusb
    configure.args-append \
        -DENABLE_USB=ON \
        -DLIBUSB_INCLUDE_DIRS=${prefix}/include/libusb-1.0
}

if {![variant_isset libusb]} {
    configure.args-append -DENABLE_USB=OFF
}

variant docs description {build documentation} {
    depends_lib-append port:doxygen
    configure.args-append -DENABLE_DOXYGEN=ON
}

if {![variant_isset docs]} {
    configure.args-append -DENABLE_DOXYGEN=OFF
}

variant manual description {build manual} {
    depends_lib-append \
        port:py${chosen_python_suffix}-docutils
    pre-configure {
        set v [join [split ${chosen_python_suffix} ""] "."]
        configure.args-append -DENABLE_MANUAL=ON \
            -DRST2HTML_EXECUTABLE=${prefix}/bin/rst2html-${v}.py
    }
}

if {![variant_isset manual]} {
    configure.args-append -DENABLE_MANUAL=OFF
}

variant orc description {enable ORC support} {
    depends_lib-append port:orc
    configure.args-append -DENABLE_ORC=ON
}

if {![variant_isset orc]} {
    configure.args-append -DENABLE_ORC=OFF
}
