diff --git a/CMakeLists.txt b/CMakeLists.txt index 187be1f8f..eacb14259 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,7 @@ option(ENABLE_BLADERF "Enable bladeRF support" ON) option(ENABLE_FUNCUBE "Enable FUNcube support" ON) option(ENABLE_HACKRF "Enable HackRF support" ON) option(ENABLE_IIO "Enable liniio support like PlutoSDR" ON) +option(ENABLE_LIBIIO_V1 "Build PlutoSDR support against the libiio 1.x API (default: 0.x)" OFF) option(ENABLE_LIMESUITE "Enable limesuite support" ON) option(ENABLE_METIS "Enable Metis support" ON) option(ENABLE_MIRISDR "Enable LibMiri for old SDRPlay" ON) diff --git a/cmake/Modules/FindLibIIO.cmake b/cmake/Modules/FindLibIIO.cmake index b6afa651f..182e0ad5b 100644 --- a/cmake/Modules/FindLibIIO.cmake +++ b/cmake/Modules/FindLibIIO.cmake @@ -1,53 +1,104 @@ +# Finds libiio for PlutoSDR support. +# +# The libiio 0.x API is used by default. Set ENABLE_LIBIIO_V1=ON to build +# against libiio 1.x instead. The header and the library always come from the +# same install: IIO_DIR if set, else the one pkg-config reports, else the +# system paths. An install with the other API is skipped. +# +# Sets LIBIIO_FOUND, LIBIIO_INCLUDE_DIR, LIBIIO_LIBRARIES and LIBIIO_V1. + +if(ENABLE_LIBIIO_V1) + set(_LIBIIO_WANT_V1 TRUE) +else() + set(_LIBIIO_WANT_V1 FALSE) +endif() + +# Detect again when ENABLE_LIBIIO_V1 changes in an existing build directory +if(LIBIIO_FOUND AND NOT LIBIIO_EXTERNAL AND NOT (LIBIIO_V1 STREQUAL _LIBIIO_WANT_V1)) + unset(LIBIIO_FOUND CACHE) + unset(LIBIIO_INCLUDE_DIR CACHE) + unset(LIBIIO_LIBRARIES CACHE) +endif() + +# Checks one install for the requested API: header and library must both be found there. +# Sets _LIBIIO_INSTALL_FOUND in the caller's scope. +function(libiio_check_install label include_dirs lib_dirs) + unset(_LIBIIO_INC_V1 CACHE) + unset(_LIBIIO_INC_V0 CACHE) + unset(_LIBIIO_LIB CACHE) + find_path(_LIBIIO_INC_V1 NAMES iio/iio.h PATHS ${include_dirs} NO_DEFAULT_PATH) + find_path(_LIBIIO_INC_V0 NAMES iio.h PATHS ${include_dirs} NO_DEFAULT_PATH) + find_library(_LIBIIO_LIB NAMES iio PATHS ${lib_dirs} + PATH_SUFFIXES ${CMAKE_LIBRARY_ARCHITECTURE} NO_DEFAULT_PATH) + + if(_LIBIIO_WANT_V1) + set(include_dir ${_LIBIIO_INC_V1}) + set(other_include_dir ${_LIBIIO_INC_V0}) + set(other_api "0.x") + set(option_state "ON") + else() + set(include_dir ${_LIBIIO_INC_V0}) + set(other_include_dir ${_LIBIIO_INC_V1}) + set(other_api "1.x") + set(option_state "OFF") + endif() + + set(found FALSE) + + if(include_dir AND _LIBIIO_LIB) + set(LIBIIO_INCLUDE_DIR ${include_dir} CACHE PATH "libiio include directory" FORCE) + set(LIBIIO_LIBRARIES ${_LIBIIO_LIB} CACHE FILEPATH "libiio library" FORCE) + set(found TRUE) + elseif(other_include_dir AND _LIBIIO_LIB) + message(STATUS "Ignoring libiio ${other_api} in ${label}: ENABLE_LIBIIO_V1 is ${option_state}") + endif() + + unset(_LIBIIO_INC_V1 CACHE) + unset(_LIBIIO_INC_V0 CACHE) + unset(_LIBIIO_LIB CACHE) + set(_LIBIIO_INSTALL_FOUND ${found} PARENT_SCOPE) +endfunction() + if(NOT LIBIIO_FOUND) pkg_check_modules (LIBIIO_PKG libiio>=0.7) + set(LIBIIO_V1 FALSE CACHE INTERNAL "libiio 1.x API") if(LIBIIO_PKG_FOUND OR (DEFINED IIO_DIR)) - # libiio 1.x installs its header as /include/iio/iio.h - find_path(LIBIIO_V1_INCLUDE_DIR - NAMES iio/iio.h - HINTS ${IIO_DIR}/include - ${LIBIIO_PKG_INCLUDE_DIRS} - PATHS /usr/include - /usr/local/include - ) - - if(LIBIIO_V1_INCLUDE_DIR) - set(LIBIIO_INCLUDE_DIR ${LIBIIO_V1_INCLUDE_DIR} CACHE PATH "libiio include directory") - set(LIBIIO_V1 TRUE CACHE INTERNAL "libiio 1.x API") - else() - # libiio 0.x installs its header as /include/iio.h - find_path(LIBIIO_INCLUDE_DIR - NAMES iio.h - HINTS ${IIO_DIR}/include - ${LIBIIO_PKG_INCLUDE_DIRS} - PATHS /usr/include - /usr/local/include - ) - set(LIBIIO_V1 FALSE CACHE INTERNAL "libiio 1.x API") + set(_LIBIIO_INSTALL_FOUND FALSE) + + if(DEFINED IIO_DIR) + libiio_check_install("IIO_DIR (${IIO_DIR})" + "${IIO_DIR}/include" + "${IIO_DIR}/lib;${IIO_DIR}/lib64") endif() - find_library(LIBIIO_LIBRARIES - NAMES iio - HINTS ${IIO_DIR}/lib - ${LIBIIO_PKG_LIBRARY_DIRS} - PATHS /usr/lib - /usr/local/lib - ) + if(NOT _LIBIIO_INSTALL_FOUND AND LIBIIO_PKG_FOUND) + libiio_check_install("pkg-config (libiio ${LIBIIO_PKG_VERSION})" + "${LIBIIO_PKG_INCLUDEDIR};${LIBIIO_PKG_INCLUDE_DIRS}" + "${LIBIIO_PKG_LIBDIR};${LIBIIO_PKG_LIBRARY_DIRS}") + endif() - if(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + if(NOT _LIBIIO_INSTALL_FOUND) + libiio_check_install("system paths" + "/usr/include;/usr/local/include" + "/usr/lib;/usr/local/lib") + endif() + + if(_LIBIIO_INSTALL_FOUND) set(LIBIIO_FOUND TRUE CACHE INTERNAL "libiio found") + set(LIBIIO_V1 ${_LIBIIO_WANT_V1} CACHE INTERNAL "libiio 1.x API") if(LIBIIO_V1) message(STATUS "Found libiio (1.x API): ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") else() message(STATUS "Found libiio (0.x API): ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") endif() - else(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + else() set(LIBIIO_FOUND FALSE CACHE INTERNAL "libiio found") message(STATUS "libiio not found.") - endif(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + endif() - mark_as_advanced(LIBIIO_INCLUDE_DIR LIBIIO_V1_INCLUDE_DIR LIBIIO_LIBRARIES) + mark_as_advanced(LIBIIO_INCLUDE_DIR LIBIIO_LIBRARIES) endif(LIBIIO_PKG_FOUND OR (DEFINED IIO_DIR))