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

name                libiio
categories          science comms
maintainers         {michaelld @michaelld} openmaintainer
description         ${name} is used to interface to the Industrial Input/Output (IIO) Subsystem
long_description    ${description}. The IIO subsystem is intended to provide support for devices that in some sense are analog to digital or digital to analog converters (ADCs, DACs). This includes, but is not limited to ADCs, Accelerometers, Gyros, IMUs, Capacitance to Digital Converters (CDCs), Pressure Sensors, Color, Light and Proximity Sensors, Temperature Sensors, Magnetometers, DACs, DDS (Direct Digital Synthesis), PLLs (Phase Locked Loops), Variable/Programmable Gain Amplifiers (VGA, PGA), and RF transceivers. You can use libiio natively on an embedded Linux target (local mode), or use libiio to communicate remotely to that same target from a host Linux, Windows, or MAC over USB or Ethernet or Serial.
license             LGPL-2+

# release
github.setup        analogdevicesinc libiio 0.24 v
checksums           rmd160  5c214e103c2527ace2b8e4ff5cf46d5ff0210115 \
                    sha256  3680d53aa129ca2ebaac009fecc88b9f4e79d1c3ad03067c16321d25b91ee779 \
                    size    483053
revision            2

long_description ${long_description} Provides the release, which is typically updated every few months to yearly.
# because I moved from a "devel" style 20190503 to a release style
# "0.18" and the latter is "less than" the former when comparing.
epoch               1

# patch to fix install directory of the framework

patchfiles-append   patch-fix_build.diff

# patch to disable checking for libiio during install

patchfiles-append   patch-disable-install-libiio-check.diff

if {${os.platform} eq "darwin" && ${os.major} < 15} {
    PortGroup       legacysupport 1.1

    # strnlen
    legacysupport.newest_darwin_requires_legacy 10

    # This is the last commit which builds.
    github.setup    analogdevicesinc libiio 027919e318aacbe56c4a4a64803b6dead068180c
    version         0.19
    revision        1
    checksums       rmd160  1ace55fd89616d3a79d4ef70b99e517f17e6addd \
                    sha256  b0efca3544cd106d9e3ef3957cff658fdd525af0a2f98aef2c00e8cc24277d7e \
                    size    236403
    github.tarball_from archive

    patchfiles-replace \
                    patch-fix_build.diff patch-fix_build-0.19.diff

    if {${os.major} < 15} {
        patchfiles-append \
                    patch-fix-headers.diff
    }

    # Uses endianness macros and built-ins requiring newer gcc.
    compiler.blacklist-append \
                    *gcc-4.0 *gcc-4.2
}

# additional arguments
configure.args-append \
    -DCMAKE_INSTALL_DOCDIR=${prefix}/share/doc/${github.project} \
    -DOSX_INSTALL_FRAMEWORKSDIR=${prefix}/Library/Frameworks

depends_lib-append \
    port:libxml2 \
    path:lib/pkgconfig/libusb-1.0.pc:libusb \
    port:avahi \
    port:xz \
    port:zlib \
    port:libiconv \
    path:lib/pkgconfig/icu-uc.pc:icu

depends_build-append \
    path:bin/pkg-config:pkgconfig \
    path:bin/doxygen:doxygen

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

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

configure.args-append \
    -DAVAHI_CLIENT_LIBRARIES=${prefix}/lib/libavahi-client.dylib \
    -DAVAHI_COMMON_LIBRARIES=${prefix}/lib/libavahi-common.dylib \
    -DAVAHI_SERVICE_INSTALL_DIR=${prefix}/etc/avahi/services \
    -DLIBLZMA_INCLUDE_DIR:PATH=${prefix}/include \
    -DLIBLZMA_LIBRARY:FILEPATH=${prefix}/lib/liblzma.dylib \
    -DLIBUSB_INCLUDE_DIR:PATH=${prefix}/include/libusb-1.0 \
    -DLIBUSB_LIBRARIES:FILEPATH=${prefix}/lib/libusb-1.0.dylib \
    -DZLIB_INCLUDE_DIR:PATH=${prefix}/include \
    -DZLIB_LIBRARY_RELEASE:FILEPATH=${prefix}/lib/libz.dylib \
    -DOSX_PACKAGE=OFF \
    -DPYTHON_BINDINGS=ON

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

set pythons_suffixes {27 35 36 37 38 39 310 311}

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

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string range ${s} 1 end]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build ${name} using Python ${v}" conflicts {*}${c} ""
}

# default to Py310 if not selected by the user
set selected_python_no_dot 310
foreach s ${pythons_suffixes} {
    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 s ${pythons_suffixes} {
    if {[variant_isset python${s}]} {
        set selected_python_no_dot ${s}
    }
}

if {${selected_python_no_dot} eq ""} {
    set py_vars ""
    set final_s [lindex ${pythons_suffixes} 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 selected_python_with_dot [string index ${selected_python_no_dot} 0].[string range ${selected_python_no_dot} 1 end]

set python_top_dir ${frameworks_dir}/Python.framework/Versions/${selected_python_with_dot}

set python_name python${selected_python_with_dot}

# required Python

depends_lib-append port:python${selected_python_no_dot} \
    port:py${selected_python_no_dot}-setuptools
configure.args-append \
    -DPython_EXECUTABLE=${python_top_dir}/bin/${python_name}

post-destroot {
    # move Python install to frameworks_dir for Python
    set py_install_dir ${python_top_dir}/lib
    xinstall -m 755 -d ${destroot}${py_install_dir}
    move ${destroot}${prefix}/lib/${python_name} ${destroot}${py_install_dir}

    # link from iio.framework to normal
    set iio_framework_version ${version}
    set iio_framework_top ${frameworks_dir}/iio.framework
    set iio_framework ${iio_framework_top}/Versions/${iio_framework_version}/iio
    set iio_dr_framework_top ${destroot}${iio_framework_top}

    # fix iio library & link it into .../lib
    xinstall -m 755 -d ${destroot}${prefix}/lib
    ln -s ${iio_framework_top}/Versions/${iio_framework_version}/iio ${destroot}${prefix}/lib/libiio.dylib
    system "install_name_tool -id ${iio_framework} ${destroot}${iio_framework}"

    if {[variant_isset docs]} {
        # fix Tools, and link them into .../bin
        xinstall -m 755 -d ${destroot}${prefix}/bin
        foreach app [glob ${iio_dr_framework_top}/Tools/*] {
            set non_dr_app [strsed ${app} "s@${destroot}@@"]
            set lib_iio_framework [strsed ${iio_framework} "s@Library/Frameworks@lib@"]
            system "install_name_tool -change ${lib_iio_framework} ${iio_framework} ${app}"
            ln -s ${non_dr_app} ${destroot}${prefix}/bin
        }
    }

    # link includes into .../include
    xinstall -m 755 -d ${destroot}${prefix}/include
    foreach header [glob ${iio_dr_framework_top}/Versions/${iio_framework_version}/Headers/*] {
        ln -s [strsed ${header} "s@${destroot}@@"] ${destroot}${prefix}/include
    }
}

variant docs description {build documentation} {

    depends_build-append \
        path:bin/doxygen:doxygen \
        port:man \
        port:py${selected_python_no_dot}-sphinx \
        port:py${selected_python_no_dot}-sphinx_rtd_theme

    configure.args-append \
        -DWITH_MAN=ON \
        -DWITH_DOC=ON \
        -DWITH_TESTS=ON \
        -DSPHINX_EXECUTABLE=${prefix}/bin/sphinx-build-${selected_python_with_dot}

}

if {![variant_isset docs]} {
    configure.args-append \
        -DWITH_MAN=OFF \
        -DWITH_DOC=OFF \
        -DWITH_TESTS=OFF
}

# 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 {([0-9.]+)}
