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

PortSystem  1.0
PortGroup   cmake 1.1
PortGroup   github 1.0
PortGroup   muniversal 1.0
PortGroup   active_variants 1.1
PortGroup   boost 1.0

name        uhd
categories  science comms
maintainers {michaelld @michaelld}
description USRP Hardware Driver for Ettus Research Products
license     GPL-3+
platforms   darwin

compiler.c_standard   2011
compiler.cxx_standard 2011

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

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

    github.setup EttusResearch uhd 3.15.0.0 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    checksums rmd160 ac9ef6473cd5b034650e3979236d2535f398dec2 \
              sha256 fe385e826b8fbf4e869b4b919b7b7b8f01fba283bc0b8ffb954947f819b9c07b \
              size   12896898
    revision  7

    boost.version 1.71

    conflicts uhd-devel uhd-39lts

    # set the version override string

    set UHD_VERSION_INFO "Release"

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

    github.livecheck.regex {(3\.15\.[0-9.]+)}

    # temporary patch to disable Boost DIRS before internal DIRS

    patchfiles-append patch-host_CMakeLists.txt.release.diff
    patch.pre_args-replace  -p0 -p1

    # temporary patch to reduce optimization from -Os to -O1 because 1
    # file takes 1000 times longer to compile using the former than
    # the latter on some Clang compilers.
    patchfiles-append patch-host_lib_usrp_dboart_CMakeLists.txt_twinrx_gain_tables.cpp-fix-optim.release.diff

    # temporary patch to move uhd images downloader to HTTPS from
    # HTTP, to avoid accessibility issues on some networks; this is a
    # backport of UHD commit b7216212 .
    patchfiles-append patch-host-utils-uhd_images_downloader-use_https.diff

}

subport uhd-39lts {

    long_description ${description}: Provides UHD 3.9 Long-Term Service \(LTS\) branch. \
                     This branch is not updated except for compatibility with OSs released \
                     after it was finalized.

    name         uhd-39lts
    conflicts    uhd uhd-devel
    github.setup EttusResearch uhd UHD-3.9.LTS
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    version      3.9-LTS
    checksums    rmd160 0765543ffdc36df568012640422fda8c70809f62 \
                 sha256 a6b4055c5d2d007c978c6577d87151ce9d505d6f5c116f0d20f176497424147e \
                 size   2463897
    revision     6

    boost.version 1.71

    # set the version override string

    set UHD_VERSION_INFO "${version}"

    # temporary patch to disable Boost DIRS before internal DIRS

    patchfiles-append patch-host_CMakeLists.txt.39lts.diff
    patch.pre_args-replace  -p0 -p1

    # temporary patch to fix the API usage for "gpsd_read" for the 3.18.0
    # release. works on all non-devel UHD ports; devel contains this patch.
    patchfiles-append patch-fix-gpsd_read-api.diff

    # temporary patch to fix gpsdata API: .separation -> .fix.geoid_sep
    patchfiles-append patch-fix-gpsdata_separation_api.39lts.diff

    # temporary patch to add double quoting around UHD_VERSION, such
    # that if specified externally it can be any string. devel already
    # contains this patch.
    patchfiles-append patch-fix-UHD_VERSION_quotes.39lts.diff

    # temporary patch to fix gpsdata API: .fix.altitude -> .fix.altHAE
    patchfiles-append patch-fix-gpsdata_altitude_api.39lts.diff

    # gpsd has been removed from release and devel
    default_variants +gpsd

    variant gpsd description {enable gpsd support} {
        depends_lib-append path:lib/libgps.dylib:gpsd
        configure.args-append -DENABLE_GPSD=ON \
            -DLIBGPS_LIBRARY=${prefix}/lib/libgps.dylib \
            -DLIBGPS_INCLUDE_DIR=${prefix}/include
    }

    if {![variant_isset gpsd]} {
        configure.args-append -DENABLE_GPSD=OFF
    }
}

subport uhd-devel {

    long_description ${description}: Provides the UHD GIT maint or master branch, depending \
                     on which is more advanced, and is typically updated every few days to week.

    github.setup    EttusResearch uhd 5fac246bc18ab04cb4870026a630e46d0fd87b17
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    version         20230127-4.4.0.0-[string range ${github.version} 0 7]
    checksums       rmd160  8e99ae1688803e9be0df23d7f52356b0d00d8f31 \
                    sha256  2f34bfa88ee6f7f339d18f7ef0a8a3cc772472f18a36ead8a6e00347f3e9a62b \
                    size    38202865
    revision        0

    boost.version   1.81

    conflicts       uhd uhd-39lts

    # set the version override string

    set UHD_VERSION_INFO "${version}"

    # temporary patch to disable Boost DIRS before internal DIRS

    patchfiles-append patch-host_CMakeLists.txt.devel.diff
    patch.pre_args-replace  -p0 -p1

    # set the variable UHD_VERSION_DEVEL to TRUE, no matter its
    # current setting (so, quietly)
    post-patch {
        reinplace -q "s@^set\(UHD_VERSION_DEVEL\[^\)\]*@set(UHD_VERSION_DEVEL FALSE@1" \
            ${worksrcpath}/host/cmake/Modules/UHDVersion.cmake
    }

    configure.args-append -DENABLE_SIM=ON
}

# see https://github.com/macports/macports-ports/pull/7805

license_noconflict-append openssl

# override the github Portgroup's homepage setting

homepage https://kb.ettus.com/UHD

if {${subport} ne "uhd-39lts"} {

    # require a compiler that supports thread_local storage,
    # which was introduced in 3.11.0.0.
    # see also < https://trac.macports.org/ticket/55980 >
    compiler.thread_local_storage yes

}

depends_lib-append port:ncurses

depends_build-append port:pkgconfig

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

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

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

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

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

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

# explicitly set CMake flags to get MacPorts' curses;
# these are used by some variants, and won't hurt the others.

configure.args-append \
    -DCURSES_CURSES_LIBRARY=${prefix}/lib/libcurses.dylib \
    -DCURSES_FORM_LIBRARY=${prefix}/lib/libform.dylib \
    -DCURSES_INCLUDE_PATH=${prefix}/include \
    -DCURSES_LIBRARY=${prefix}/lib/libcurses.dylib \
    -DCURSES_NCURSES_LIBRARY=${prefix}/lib/libncurses.dylib

# override default version string to be MacPorts-specific

configure.args-append \
    -DUHD_GIT_COUNT="MacPorts" \
    -DUHD_GIT_HASH="${UHD_VERSION_INFO}"

# directory to install shared UHD files
# (examples, images, modules, tests, utilities)

configure.args-append \
    -DPKG_LIB_DIR=${prefix}/share/uhd

# directory to install all CMake files; internal appends "uhd"

configure.args-append \
    -DCMAKE_MODULES_DIR=${prefix}/share/cmake

# on much older installs ...

if {${configure.cxx_stdlib} eq "macports-libstdc++" ||
    ${configure.cxx_stdlib} eq "libstdc++"} {

    # for some reason cmake doesn't recognize libstdc++ compilers as
    # being c++11 capable, even though some are. set this and hope for
    # the best.

    configure.cxxflags-append -std=gnu++11

    # when using GCC on older OSX, certain math functions will not be
    # part of std:: without this macro defined. This macro required
    # c++11 to be enabled, which is addressed in the next item.

    configure.cxxflags-append -D_GLIBCXX_USE_C99_MATH_TR1

    # enable C/CXX standard extensions (e.g., "gnu++11" and "gnu11"
    # instead of than "c++11" and "c11"). UHD itself does not require
    # these extensions, but when compiling with GCC on older OSX they
    # allow for various various math functions to be in std:: ... so,
    # enable them.

    configure.args-append \
        -DCMAKE_CXX_EXTENSIONS=ON \
        -DCMAKE_C_EXTENSIONS=ON

} else {

    # disable C/CXX standard extensions (e.g., "gnu++11" and "gnu11"
    # rather than "c++11" and "c11"). UHD itself does not require
    # these extensions, and when compiling with Clang they don't
    # really do anything anyway; so, just disable them.

    configure.args-append \
        -DCMAKE_CXX_EXTENSIONS=OFF \
        -DCMAKE_C_EXTENSIONS=OFF

}

# per user concensus: enable all variants except +debug and +universal

default_variants +docs +examples +libusb +manual +test +manpages

# specify the Python dependencies; these are checked for at configure,
# then used for building, but not at runtime.

set pythons_versions {2.7 3.9 3.10}

set pythons_ports {}
foreach s ${pythons_versions} {
    lappend pythons_ports python[string map {. {}} ${s}]
}

foreach v ${pythons_versions} {
    set s [string map {. {}} ${v}]
    set p python${s}
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build ${name} using Python ${v}" conflicts {*}${c} ""
}

set selected_python_no_dot 310
foreach v ${pythons_versions} {
    set s [string map {. {}} ${v}]
    if {[variant_isset python${s}]} {
        set selected_python_no_dot ${s}
    }
}
default_variants +python${selected_python_no_dot}

# make sure the default python variant wasn't
# deselected without selecting another one
set selected_python_no_dot ""
foreach v ${pythons_versions} {
    set s [string map {. {}} ${v}]
    if {[variant_isset python${s}]} {
        set selected_python_no_dot ${s}
    }
}

if {${selected_python_no_dot} eq ""} {
    set py_vars ""
    set final_s [string map {. {}} [lindex ${pythons_versions} end]]
    foreach s ${pythons_suffixes} {
        if {${s} eq ${final_s}} {
            set py_vars "${py_vars}or "
        }
        set py_vars "${py_vars}+python${s}"
        if {${s} ne ${final_s}} {
            set py_vars "${py_vars}, "
        }
    }
    ui_error "\n\nYou must select one of the variants ${py_vars}.\n"
    return -code error "Invalid variant selection"
}

set PythonVersionNoDot ${selected_python_no_dot}

set PythonVersionWithDot [string index ${selected_python_no_dot} 0].[string range ${selected_python_no_dot} 1 end]

set PythonTopDir "${frameworks_dir}/Python.framework/Versions/${PythonVersionWithDot}"

set PythonName python${PythonVersionWithDot}

# required Python

depends_lib-append \
    port:python${PythonVersionNoDot} \
    port:py${PythonVersionNoDot}-mako \
    port:py${PythonVersionNoDot}-requests \
    port:py${PythonVersionNoDot}-six

# specify the Python version to use

set PythonExe "${PythonTopDir}/bin/${PythonName}"

configure.args-append \
    -DPYTHON_EXECUTABLE=${PythonExe}

if {${PythonVersionNoDot} eq "27"} {
    configure.args-append -DENABLE_PYTHON3=OFF
} else {
    configure.args-append -DENABLE_PYTHON3=ON
}

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

post-destroot {

    ui_debug "Fetching images:"
    ui_debug "${PythonExe} ${destroot}${prefix}/share/uhd/utils/uhd_images_downloader.py --install-location=${destroot}${prefix}/share/uhd/images"
    system "${PythonExe} ${destroot}${prefix}/share/uhd/utils/uhd_images_downloader.py -v -v -v -v  --install-location=${destroot}${prefix}/share/uhd/images"

}

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 path:lib/pkgconfig/libusb-1.0.pc: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
} else {
    # if using +libusb, enable E300 too
    configure.args-append -DENABLE_E300=ON
}

variant docs description {build documentation} {
    depends_build-append path:bin/doxygen: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${PythonVersionNoDot}-docutils
    configure.args-append -DENABLE_MANUAL=ON
}

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

variant manpages description {build man pages} {
    depends_lib-append port:gzip
    configure.args-append -DENABLE_MAN_PAGES=ON
}

if {![variant_isset manpages]} {
    configure.args-append -DENABLE_MAN_PAGES=OFF
}

if {${subport} ne "uhd-39lts"} {

    variant python_api \
        description {enable Python API support (experimental)} {
        depends_lib-append \
            port:py${PythonVersionNoDot}-numpy \
            port:py${PythonVersionNoDot}-pybind11
        configure.args-append \
            -DENABLE_PYTHON_API=ON \
            -DPYTHON_INCLUDE_DIR=${PythonTopDir}/Headers \
            -DPYTHON_LIBRARY=${PythonTopDir}/Python \
            -DUHD_PYTHON_DIR=${PythonTopDir}/lib/${PythonName}/site-packages \
            -DUSRP_MPM_PYTHON_DIR=${PythonTopDir}/lib/${PythonName}/site-packages
    }

    if {![variant_isset python_api]} {
        configure.args-append -DENABLE_PYTHON_API=OFF
    }

    default_variants +python_api

}
