modern cmake

Refactor HackRF build system with a more modern CMake style.
Install files to allow library users using CMake to
find_project(HackRF CONFIG) and obtain HackRF::hackrf
and/or HackRF::hackrf_static library targets.

Add options ENABLE_STATIC_LIB and ENABLE_SHARED_LIB
to allow shared or static library to be disabled.
(Default builds both shared and static libraries.)

Add option ENABLE_HACKRF_SWEEP, when disabled allows
building without FFT library. (Default enabled.)

Add option DISABLE_USB_DEVICE_DISCOVERY for a compile definition
which could be used to alter libusb usage as needed on Android.
This commit is contained in:
A. Maitland Bottoms
2025-08-17 16:03:36 -04:00
committed by Martin Ling
parent adc537331c
commit 506c8cb292
12 changed files with 456 additions and 664 deletions

View File

@@ -1,23 +1,25 @@
#top dir cmake project for libhackrf + tools
# top dir cmake project for libhackrf + tools
cmake_minimum_required(VERSION 3.10.0)
project (HackRF C)
project(HackRF C)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_FLAGS "$ENV{CFLAGS}" CACHE STRING "C Flags")
set(CMAKE_C_FLAGS
"$ENV{CFLAGS}"
CACHE STRING "C Flags")
add_subdirectory(libhackrf)
add_subdirectory(hackrf-tools)
########################################################################
# ##############################################################################
# Create uninstall target
########################################################################
# ##############################################################################
configure_file(
${PROJECT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
@ONLY)
configure_file(${PROJECT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake @ONLY)
add_custom_target(uninstall
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)
add_custom_target(
uninstall
COMMENT "Provide uninstall target"
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

View File

@@ -1,30 +0,0 @@
# - Find FFTW
# Find the native FFTW includes and library
#
# FFTW_INCLUDES - where to find fftw3.h
# FFTW_LIBRARIES - List of libraries when using FFTW.
# FFTW_FOUND - True if FFTW found.
if (FFTW_INCLUDES)
# Already in cache, be silent
set (FFTW_FIND_QUIETLY TRUE)
endif (FFTW_INCLUDES)
find_path (FFTW_INCLUDES fftw3.h)
IF (WIN32)
include_directories(${FFTW_INCLUDES})
find_library (FFTW_LIBRARIES NAMES ${FFTW_LIBRARIES})
ELSE(WIN32)
find_library (FFTW_LIBRARIES NAMES fftw3f)
ENDIF(WIN32)
# handle the QUIETLY and REQUIRED arguments and set FFTW_FOUND to TRUE if
# all listed variables are TRUE
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (FFTW DEFAULT_MSG FFTW_LIBRARIES FFTW_INCLUDES)
mark_as_advanced (FFTW_LIBRARIES FFTW_INCLUDES)

View File

@@ -0,0 +1,49 @@
# http://tim.klingt.org/code/projects/supernova/repository/revisions/d336dd6f400e381bcfd720e96139656de0c53b6a/entry/cmake_modules/FindFFTW3f.cmake
# Modified to use pkg config and use standard var names
# Find single-precision (float) version of FFTW3
find_package(PkgConfig)
pkg_check_modules(PC_FFTW3f "fftw3f >= 3.0")
find_path(
FFTW3f_INCLUDE_DIRS
NAMES fftw3.h
HINTS $ENV{FFTW3_DIR}/include ${PC_FFTW3f_INCLUDE_DIRS}
PATHS /usr/local/include /usr/include)
find_library(
FFTW3f_LIBRARIES
NAMES fftw3f libfftw3f
HINTS $ENV{FFTW3_DIR}/lib ${PC_FFTW3f_LIBDIR}
PATHS /usr/local/lib /usr/lib /usr/lib64)
find_library(
FFTW3f_THREADS_LIBRARIES
NAMES fftw3f_threads libfftw3f_threads
HINTS $ENV{FFTW3_DIR}/lib ${PC_FFTW3f_LIBDIR}
PATHS /usr/local/lib /usr/lib /usr/lib64)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FFTW3f DEFAULT_MSG FFTW3f_LIBRARIES
FFTW3f_INCLUDE_DIRS)
mark_as_advanced(FFTW3f_LIBRARIES FFTW3f_INCLUDE_DIRS FFTW3f_THREADS_LIBRARIES)
if(FFTW3f_FOUND AND NOT TARGET fftw3f::fftw3f)
add_library(fftw3f::fftw3f INTERFACE IMPORTED)
if(NOT WIN32)
list(APPEND FFTW3f_LIBRARIES m)
endif()
set_target_properties(
fftw3f::fftw3f
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFTW3f_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${FFTW3f_LIBRARIES}")
if(FFTW3f_THREADS_LIBRARIES)
set_property(
TARGET fftw3f::fftw3f
APPEND
PROPERTY INTERFACE_LINK_LIBRARIES "${FFTW3f_THREADS_LIBRARIES}")
set_target_properties(
fftw3f::fftw3f PROPERTIES INTERFACE_COMPILE_DEFINITIONS "FFTW3F_THREADS")
endif()
endif()

View File

@@ -1,55 +0,0 @@
# - Try to find the libhackrf library
# Once done this defines
#
# LIBHACKRF_FOUND - system has libhackrf
# LIBHACKRF_INCLUDE_DIR - the libhackrf include directory
# LIBHACKRF_LIBRARIES - Link these to use libhackrf
# Copyright (c) 2013 Benjamin Vernoux
#
if (LIBHACKRF_INCLUDE_DIR AND LIBHACKRF_LIBRARIES)
# in cache already
set(LIBHACKRF_FOUND TRUE)
else (LIBHACKRF_INCLUDE_DIR AND LIBHACKRF_LIBRARIES)
IF (NOT WIN32)
# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
find_package(PkgConfig)
pkg_check_modules(PC_LIBHACKRF QUIET libhackrf)
ENDIF(NOT WIN32)
FIND_PATH(LIBHACKRF_INCLUDE_DIR
NAMES hackrf.h
HINTS $ENV{LIBHACKRF_DIR}/include ${PC_LIBHACKRF_INCLUDEDIR}
PATHS /usr/local/include/libhackrf /usr/include/libhackrf /usr/local/include
/usr/include ${CMAKE_SOURCE_DIR}/../libhackrf/src
/opt/local/include/libhackrf
${LIBHACKRF_INCLUDE_DIR}
)
set(libhackrf_library_names hackrf)
FIND_LIBRARY(LIBHACKRF_LIBRARIES
NAMES ${libhackrf_library_names}
HINTS $ENV{LIBHACKRF_DIR}/lib ${PC_LIBHACKRF_LIBDIR}
PATHS /usr/local/lib /usr/lib /opt/local/lib ${PC_LIBHACKRF_LIBDIR} ${PC_LIBHACKRF_LIBRARY_DIRS} ${CMAKE_SOURCE_DIR}/../libhackrf/src
)
if(LIBHACKRF_INCLUDE_DIR)
set(CMAKE_REQUIRED_INCLUDES ${LIBHACKRF_INCLUDE_DIR})
endif()
if(LIBHACKRF_LIBRARIES)
set(CMAKE_REQUIRED_LIBRARIES ${LIBHACKRF_LIBRARIES})
endif()
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBHACKRF DEFAULT_MSG LIBHACKRF_LIBRARIES LIBHACKRF_INCLUDE_DIR)
MARK_AS_ADVANCED(LIBHACKRF_INCLUDE_DIR LIBHACKRF_LIBRARIES)
endif (LIBHACKRF_INCLUDE_DIR AND LIBHACKRF_LIBRARIES)

View File

@@ -0,0 +1,59 @@
# Try to find the libusb library
# Once done this defines
#
# LIBUSB_FOUND - system has libusb
# LIBUSB_INCLUDE_DIR - the libusb include directory
# LIBUSB_LIBRARIES - Link these to use libusb
# LIBUSB::LIBUSB - CMake imported library target
# Copyright (c) 2006, 2008 Laurent Montel, <montel@kde.org>
# Copyright (c) 2023 Jamie Smith <jsmith@crackofdawn.onmicrosoft.com>
# Copyright (c) 2025 A. Maitland Bottoms <bottoms@debian.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
if(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
# in cache already
set(LIBUSB_FOUND TRUE)
else(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
if(NOT WIN32)
# use pkg-config to get the directories and then use these values in the
# FIND_PATH() and FIND_LIBRARY() calls
find_package(PkgConfig)
pkg_check_modules(PC_LIBUSB libusb-1.0)
endif(NOT WIN32)
set(LIBUSB_LIBRARY_NAME usb-1.0)
if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(LIBUSB_LIBRARY_NAME usb)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# vcpkg's libusb-1.0 has a "lib" prefix, but on Windows MVSC, CMake doesn't
# search for static libraries with lib prefixes automatically.
list(APPEND LIBUSB_LIBRARY_NAMES libusb-1.0)
endif()
find_path(LIBUSB_INCLUDE_DIR libusb.h PATHS ${PC_LIBUSB_INCLUDEDIR}
${PC_LIBUSB_INCLUDE_DIRS})
find_library(
LIBUSB_LIBRARIES
NAMES ${LIBUSB_LIBRARY_NAME}
PATHS ${PC_LIBUSB_LIBDIR} ${PC_LIBUSB_LIBRARY_DIRS})
endif(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LIBUSB DEFAULT_MSG LIBUSB_LIBRARIES
LIBUSB_INCLUDE_DIR)
mark_as_advanced(LIBUSB_INCLUDE_DIR LIBUSB_LIBRARIES)
if(LIBUSB_FOUND AND NOT TARGET LIBUSB::LIBUSB)
add_library(LIBUSB::LIBUSB INTERFACE IMPORTED)
set_target_properties(
LIBUSB::LIBUSB
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBUSB_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${LIBUSB_LIBRARIES}")
endif()

View File

@@ -1,246 +0,0 @@
# Updated FindThreads.cmake that supports pthread-win32
# Downloaded from http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=6399
# - This module determines the thread library of the system.
#
# The following variables are set
# CMAKE_THREAD_LIBS_INIT - the thread library
# CMAKE_USE_SPROC_INIT - are we using sproc?
# CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads?
# CMAKE_USE_PTHREADS_INIT - are we using pthreads
# CMAKE_HP_PTHREADS_INIT - are we using hp pthreads
#
# If use of pthreads-win32 is desired, the following variables
# can be set.
#
# THREADS_USE_PTHREADS_WIN32 -
# Setting this to true searches for the pthreads-win32
# port (since CMake 2.8.0)
#
# THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME
# C = no exceptions (default)
# (NOTE: This is the default scheme on most POSIX thread
# implementations and what you should probably be using)
# CE = C++ Exception Handling
# SE = Structure Exception Handling (MSVC only)
# (NOTE: Changing this option from the default may affect
# the portability of your application. See pthreads-win32
# documentation for more details.)
#
#======================================================
# Example usage where threading library
# is provided by the system:
#
# find_package(Threads REQUIRED)
# add_executable(foo foo.cc)
# target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT})
#
# Example usage if pthreads-win32 is desired on Windows
# or a system provided thread library:
#
# set(THREADS_USE_PTHREADS_WIN32 true)
# find_package(Threads REQUIRED)
# include_directories(${THREADS_PTHREADS_INCLUDE_DIR})
#
# add_executable(foo foo.cc)
# target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT})
#
INCLUDE (CheckIncludeFiles)
INCLUDE (CheckLibraryExists)
SET(Threads_FOUND FALSE)
IF(WIN32 AND NOT CYGWIN AND THREADS_USE_PTHREADS_WIN32)
SET(_Threads_ptwin32 true)
ENDIF()
# Do we have sproc?
IF(CMAKE_SYSTEM MATCHES IRIX)
CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H)
ENDIF()
IF(CMAKE_HAVE_SPROC_H)
# We have sproc
SET(CMAKE_USE_SPROC_INIT 1)
ELSEIF(_Threads_ptwin32)
IF(NOT DEFINED THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME)
# Assign the default scheme
SET(THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME "C")
ELSE()
# Validate the scheme specified by the user
IF(NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "C" AND
NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "CE" AND
NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE")
MESSAGE(FATAL_ERROR "See documentation for FindPthreads.cmake, only C, CE, and SE modes are allowed")
ENDIF()
IF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE")
MESSAGE(FATAL_ERROR "Structured Exception Handling is only allowed for MSVC")
ENDIF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE")
ENDIF()
FIND_PATH(THREADS_PTHREADS_INCLUDE_DIR pthread.h)
# Determine the library filename
IF(MSVC)
SET(_Threads_pthreads_libname
pthreadV${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2)
ELSEIF(MINGW)
SET(_Threads_pthreads_libname
pthreadG${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2)
ELSE()
MESSAGE(FATAL_ERROR "This should never happen")
ENDIF()
# Use the include path to help find the library if possible
SET(_Threads_lib_paths "")
IF(THREADS_PTHREADS_INCLUDE_DIR)
GET_FILENAME_COMPONENT(_Threads_root_dir
${THREADS_PTHREADS_INCLUDE_DIR} PATH)
SET(_Threads_lib_paths ${_Threads_root_dir}/lib)
ENDIF()
FIND_LIBRARY(THREADS_PTHREADS_WIN32_LIBRARY
NAMES ${_Threads_pthreads_libname}
PATHS ${_Threads_lib_paths}
DOC "The Portable Threads Library for Win32"
NO_SYSTEM_PATH
)
IF(THREADS_PTHREADS_INCLUDE_DIR AND THREADS_PTHREADS_WIN32_LIBRARY)
MARK_AS_ADVANCED(THREADS_PTHREADS_INCLUDE_DIR)
SET(CMAKE_THREAD_LIBS_INIT ${THREADS_PTHREADS_WIN32_LIBRARY})
SET(CMAKE_HAVE_THREADS_LIBRARY 1)
SET(Threads_FOUND TRUE)
ENDIF()
MARK_AS_ADVANCED(THREADS_PTHREADS_WIN32_LIBRARY)
ELSE()
# Do we have pthreads?
CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
IF(CMAKE_HAVE_PTHREAD_H)
#
# We have pthread.h
# Let's check for the library now.
#
SET(CMAKE_HAVE_THREADS_LIBRARY)
IF(NOT THREADS_HAVE_PTHREAD_ARG)
# Do we have -lpthreads
CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
IF(CMAKE_HAVE_PTHREADS_CREATE)
SET(CMAKE_THREAD_LIBS_INIT "-lpthreads")
SET(CMAKE_HAVE_THREADS_LIBRARY 1)
SET(Threads_FOUND TRUE)
ENDIF()
# Ok, how about -lpthread
CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
IF(CMAKE_HAVE_PTHREAD_CREATE)
SET(CMAKE_THREAD_LIBS_INIT "-lpthread")
SET(Threads_FOUND TRUE)
SET(CMAKE_HAVE_THREADS_LIBRARY 1)
ENDIF()
IF(CMAKE_SYSTEM MATCHES "SunOS.*")
# On sun also check for -lthread
CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
IF(CMAKE_HAVE_THR_CREATE)
SET(CMAKE_THREAD_LIBS_INIT "-lthread")
SET(CMAKE_HAVE_THREADS_LIBRARY 1)
SET(Threads_FOUND TRUE)
ENDIF()
ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*")
ENDIF(NOT THREADS_HAVE_PTHREAD_ARG)
IF(NOT CMAKE_HAVE_THREADS_LIBRARY)
# If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
MESSAGE(STATUS "Check if compiler accepts -pthread")
TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
${CMAKE_BINARY_DIR}
${CMAKE_ROOT}/Modules/CheckForPthreads.c
CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
COMPILE_OUTPUT_VARIABLE OUTPUT)
IF(THREADS_HAVE_PTHREAD_ARG)
IF(THREADS_PTHREAD_ARG MATCHES "^2$")
SET(Threads_FOUND TRUE)
MESSAGE(STATUS "Check if compiler accepts -pthread - yes")
ELSE()
MESSAGE(STATUS "Check if compiler accepts -pthread - no")
FILE(APPEND
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
ENDIF()
ELSE()
MESSAGE(STATUS "Check if compiler accepts -pthread - no")
FILE(APPEND
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
ENDIF()
ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
IF(THREADS_HAVE_PTHREAD_ARG)
SET(Threads_FOUND TRUE)
SET(CMAKE_THREAD_LIBS_INIT "-pthread")
ENDIF()
ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY)
ENDIF(CMAKE_HAVE_PTHREAD_H)
ENDIF()
IF(CMAKE_THREAD_LIBS_INIT)
SET(CMAKE_USE_PTHREADS_INIT 1)
SET(Threads_FOUND TRUE)
ENDIF()
IF(CMAKE_SYSTEM MATCHES "Windows"
AND NOT THREADS_USE_PTHREADS_WIN32)
SET(CMAKE_USE_WIN32_THREADS_INIT 1)
SET(Threads_FOUND TRUE)
ENDIF()
IF(CMAKE_USE_PTHREADS_INIT)
IF(CMAKE_SYSTEM MATCHES "HP-UX-*")
# Use libcma if it exists and can be used. It provides more
# symbols than the plain pthread library. CMA threads
# have actually been deprecated:
# http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395
# http://docs.hp.com/en/947/d8.html
# but we need to maintain compatibility here.
# The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
# are available.
CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
IF(CMAKE_HAVE_HP_CMA)
SET(CMAKE_THREAD_LIBS_INIT "-lcma")
SET(CMAKE_HP_PTHREADS_INIT 1)
SET(Threads_FOUND TRUE)
ENDIF(CMAKE_HAVE_HP_CMA)
SET(CMAKE_USE_PTHREADS_INIT 1)
ENDIF()
IF(CMAKE_SYSTEM MATCHES "OSF1-V*")
SET(CMAKE_USE_PTHREADS_INIT 0)
SET(CMAKE_THREAD_LIBS_INIT )
ENDIF()
IF(CMAKE_SYSTEM MATCHES "CYGWIN_NT*")
SET(CMAKE_USE_PTHREADS_INIT 1)
SET(Threads_FOUND TRUE)
SET(CMAKE_THREAD_LIBS_INIT )
SET(CMAKE_USE_WIN32_THREADS_INIT 0)
ENDIF()
ENDIF(CMAKE_USE_PTHREADS_INIT)
INCLUDE(FindPackageHandleStandardArgs)
IF(_Threads_ptwin32)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG
THREADS_PTHREADS_WIN32_LIBRARY THREADS_PTHREADS_INCLUDE_DIR)
ELSE()
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND)
ENDIF()

View File

@@ -1,43 +0,0 @@
# - Try to find the freetype library
# Once done this defines
#
# LIBUSB_FOUND - system has libusb
# LIBUSB_INCLUDE_DIR - the libusb include directory
# LIBUSB_LIBRARIES - Link these to use libusb
# Copyright (c) 2006, 2008 Laurent Montel, <montel@kde.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
if (LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
# in cache already
set(LIBUSB_FOUND TRUE)
else (LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
IF (NOT WIN32)
# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
find_package(PkgConfig)
pkg_check_modules(PC_LIBUSB libusb-1.0)
ENDIF(NOT WIN32)
set(LIBUSB_LIBRARY_NAME usb-1.0)
IF(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(LIBUSB_LIBRARY_NAME usb)
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
FIND_PATH(LIBUSB_INCLUDE_DIR libusb.h
PATHS ${PC_LIBUSB_INCLUDEDIR} ${PC_LIBUSB_INCLUDE_DIRS})
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES ${LIBUSB_LIBRARY_NAME}
PATHS ${PC_LIBUSB_LIBDIR} ${PC_LIBUSB_LIBRARY_DIRS})
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBUSB DEFAULT_MSG LIBUSB_LIBRARIES LIBUSB_INCLUDE_DIR)
MARK_AS_ADVANCED(LIBUSB_INCLUDE_DIR LIBUSB_LIBRARIES)
endif (LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)

View File

@@ -3,44 +3,46 @@
#
# This file is part of HackRF.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
# Based heavily upon the libftdi cmake setup.
# You should have received a copy of the GNU General Public License along with
# this program; see the file COPYING. If not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Boston, MA 02110-1301, USA.
cmake_minimum_required(VERSION 3.10.0)
project(hackrf-tools C)
set(PACKAGE hackrf-tools)
project(hackrf-tools LANGUAGES C)
include(GNUInstallDirs)
include(${PROJECT_SOURCE_DIR}/../cmake/set_release.cmake)
add_definitions(-DTOOL_RELEASE="${RELEASE}")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake/modules)
if(MSVC)
include_directories(getopt)
add_definitions(/D _CRT_SECURE_NO_WARNINGS)
include_directories(getopt)
add_definitions(/D _CRT_SECURE_NO_WARNINGS)
else()
add_definitions(-Wall)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu90")
add_definitions(-Wall)
endif()
if(NOT libhackrf_SOURCE_DIR)
find_package(LIBHACKRF REQUIRED)
include_directories(${LIBHACKRF_INCLUDE_DIR})
else()
include_directories(${libhackrf_SOURCE_DIR}/src)
if(LIBHACKRF_INCLUDE_DIR
AND LIBHACKRF_LIBRARIES
AND NOT TARGET HackRF::hackrf)
add_library(HackRF::hackrf INTERFACE IMPORTED)
set_target_properties(
HackRF::hackrf
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBHACKRF_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${LIBHACKRF_LIBRARIES}")
else()
find_package(HackRF REQUIRED)
endif()
endif()
add_subdirectory(src)
@@ -50,12 +52,12 @@ add_subdirectory(src)
########################################################################
if(NOT HackRF_SOURCE_DIR)
configure_file(
${PROJECT_SOURCE_DIR}/../cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
@ONLY)
configure_file(${PROJECT_SOURCE_DIR}/../cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake @ONLY)
add_custom_target(uninstall
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)
add_custom_target(
uninstall
COMMENT "Provide hackrf-tools uninstall target"
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

View File

@@ -1,74 +1,76 @@
# Copyright 2012 Jared Boone
# Copyright 2013 Benjamin Vernoux
# Copyright 2025 A. Maitland Bottoms <bottoms@debian.org>
#
# This file is part of HackRF.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
# You should have received a copy of the GNU General Public License along with
# this program; see the file COPYING. If not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Boston, MA 02110-1301, USA.
#
# Based heavily upon the libftdi cmake setup.
option(ENABLE_HACKRF_SWEEP
"Build and Install hackrf_sweep tool (Requires FFTW3f)" ON)
find_package(FFTW3f)
set(INSTALL_DEFAULT_BINDIR "bin" CACHE STRING "Appended to CMAKE_INSTALL_PREFIX")
set(TOOLS
hackrf_transfer
hackrf_spiflash
hackrf_cpldjtag
hackrf_info
hackrf_debug
hackrf_clock
hackrf_operacake
hackrf_biast)
find_package(FFTW REQUIRED)
include_directories(${FFTW_INCLUDES})
get_filename_component(FFTW_LIBRARY_DIRS ${FFTW_LIBRARIES} DIRECTORY)
link_directories(${FFTW_LIBRARY_DIRS})
SET(TOOLS
hackrf_transfer
hackrf_spiflash
hackrf_cpldjtag
hackrf_info
hackrf_debug
hackrf_clock
hackrf_sweep
hackrf_operacake
hackrf_biast
)
if(MSVC)
add_library(libgetopt_static STATIC
../getopt/getopt.c
)
LIST(APPEND TOOLS_LINK_LIBS ${FFTW_LIBRARIES})
if(TARGET HackRF::hackrf)
list(APPEND TOOLS_LINK_LIBS HackRF::hackrf)
else()
LIST(APPEND TOOLS_LINK_LIBS m fftw3f)
endif()
if(NOT libhackrf_SOURCE_DIR)
include_directories(${LIBHACKRF_INCLUDE_DIR})
LIST(APPEND TOOLS_LINK_LIBS ${LIBHACKRF_LIBRARIES})
else()
LIST(APPEND TOOLS_LINK_LIBS hackrf)
list(APPEND TOOLS_LINK_LIBS HackRF::hackrf_static)
endif()
if(MSVC)
LIST(APPEND TOOLS_LINK_LIBS libgetopt_static)
add_library(libgetopt_static STATIC ../getopt/getopt.c)
list(APPEND TOOLS_LINK_LIBS libgetopt_static)
endif()
include(CheckLibraryExists)
check_library_exists(m log10 "" LIBM)
if(LIBM)
list(APPEND TOOLS_LINK_LIBS m)
endif()
foreach(tool ${TOOLS})
add_executable(${tool} ${tool}.c)
target_link_libraries(${tool} ${TOOLS_LINK_LIBS})
install(TARGETS ${tool} RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
add_executable(${tool} ${tool}.c)
target_compile_features(${tool} PRIVATE c_std_90)
target_link_libraries(${tool} ${TOOLS_LINK_LIBS})
install(TARGETS ${tool} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endforeach(tool)
if(PC_FFTW3f_FOUND AND ENABLE_HACKRF_SWEEP)
add_executable(hackrf_sweep hackrf_sweep.c)
target_compile_features(hackrf_sweep PRIVATE c_std_90)
target_link_libraries(hackrf_sweep fftw3f::fftw3f ${TOOLS_LINK_LIBS})
install(TARGETS hackrf_sweep RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
else()
message(
STATUS
"Not building hackrf_sweep tool. (Install FFTW, set ENABLE_HACKRF_SWEEP)"
)
endif()
if( ${WIN32} )
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/"
DESTINATION ${INSTALL_DEFAULT_BINDIR}
FILES_MATCHING
PATTERN "fftw*.dll")
endif( ${WIN32} )
if(WIN32)
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/"
DESTINATION ${CMAKE_INSTALL_BINDIR}
FILES_MATCHING
PATTERN "fftw*.dll")
endif()

View File

@@ -3,71 +3,88 @@
#
# This file is part of HackRF.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
# Based heavily upon the libftdi cmake setup.
# You should have received a copy of the GNU General Public License along with
# this program; see the file COPYING. If not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Boston, MA 02110-1301, USA.
cmake_minimum_required(VERSION 3.10.0)
project(libhackrf C)
set(MAJOR_VERSION 0)
set(MINOR_VERSION 9)
set(PACKAGE libhackrf)
set(VERSION_STRING ${MAJOR_VERSION}.${MINOR_VERSION})
set(VERSION ${VERSION_STRING})
add_definitions(-DLIBRARY_VERSION="${VERSION_STRING}")
project(
libhackrf
LANGUAGES C
VERSION 0.9)
include(GNUInstallDirs)
include(${PROJECT_SOURCE_DIR}/../cmake/set_release.cmake)
add_definitions(-DLIBRARY_RELEASE="${RELEASE}")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake/modules)
if(MSVC)
set(THREADS_USE_PTHREADS_WIN32 true)
set(THREADS_USE_PTHREADS_WIN32 true)
else()
add_definitions(-Wall)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu90")
INCLUDE(TestBigEndian)
TEST_BIG_ENDIAN(BIGENDIAN)
if(${BIGENDIAN})
add_definitions(-DHACKRF_BIG_ENDIAN)
endif(${BIGENDIAN})
include(TestBigEndian)
test_big_endian(BIGENDIAN)
if(${BIGENDIAN})
add_definitions(-DHACKRF_BIG_ENDIAN)
endif(${BIGENDIAN})
endif()
find_package(USB1 REQUIRED)
find_package(Threads REQUIRED)
include_directories(${LIBUSB_INCLUDE_DIR} ${THREADS_PTHREADS_INCLUDE_DIR})
find_package(LIBUSB REQUIRED)
find_package(Threads REQUIRED)
find_package(PThreads4W QUIET)
add_subdirectory(src)
########################################################################
# ##############################################################################
# Create Cmake Config-file package interface
# ##############################################################################
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/HackRF/HackRFConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion)
export(
EXPORT HackRFTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/HackRF/HackRFTargets.cmake"
NAMESPACE HackRF::)
configure_file(HackRFConfig.cmake
"${CMAKE_CURRENT_BINARY_DIR}/HackRF/HackRFConfig.cmake" COPYONLY)
set(CONFIG_PACKAGE_LOCATION ${CMAKE_INSTALL_LIBDIR}/cmake/HackRF)
install(
EXPORT HackRFTargets
FILE HackRFTargets.cmake
NAMESPACE HackRF::
DESTINATION ${CONFIG_PACKAGE_LOCATION})
install(
FILES HackRFConfig.cmake ../cmake/modules/FindLIBUSB.cmake
"${CMAKE_CURRENT_BINARY_DIR}/HackRF/HackRFConfigVersion.cmake"
DESTINATION ${CONFIG_PACKAGE_LOCATION}
COMPONENT Devel)
# ##############################################################################
# Create Pkg Config File
########################################################################
FOREACH(inc ${LIBUSB_INCLUDE_DIR})
LIST(APPEND HACKRF_PC_CFLAGS "-I${inc}")
ENDFOREACH(inc)
# ##############################################################################
foreach(inc ${LIBUSB_INCLUDE_DIR})
list(APPEND HACKRF_PC_CFLAGS "-I${inc}")
endforeach(inc)
# use space-separation format for the pc file
STRING(REPLACE ";" " " HACKRF_PC_CFLAGS "${HACKRF_PC_CFLAGS}")
STRING(REPLACE ";" " " HACKRF_PC_LIBS "${HACKRF_PC_LIBS}")
string(REPLACE ";" " " HACKRF_PC_CFLAGS "${HACKRF_PC_CFLAGS}")
string(REPLACE ";" " " HACKRF_PC_LIBS "${HACKRF_PC_LIBS}")
# unset these vars to avoid hard-coded paths to cross environment
IF(CMAKE_CROSSCOMPILING)
UNSET(HACKRF_PC_CFLAGS)
UNSET(HACKRF_PC_LIBS)
ENDIF(CMAKE_CROSSCOMPILING)
if(CMAKE_CROSSCOMPILING)
unset(HACKRF_PC_CFLAGS)
unset(HACKRF_PC_LIBS)
endif(CMAKE_CROSSCOMPILING)
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix \${prefix})
@@ -76,93 +93,91 @@ set(includedir \${prefix}/include)
set(libpkgdata lib${LIB_SUFFIX})
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
set(libpkgdata "libdata")
set(libpkgdata "libdata")
endif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
CONFIGURE_FILE(
${CMAKE_CURRENT_SOURCE_DIR}/libhackrf.pc.in
${CMAKE_CURRENT_BINARY_DIR}/libhackrf.pc
@ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libhackrf.pc.in
${CMAKE_CURRENT_BINARY_DIR}/libhackrf.pc @ONLY)
INSTALL(
FILES ${CMAKE_CURRENT_BINARY_DIR}/libhackrf.pc
DESTINATION ${libpkgdata}/pkgconfig
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libhackrf.pc
DESTINATION ${libpkgdata}/pkgconfig)
########################################################################
# Create Pkg Config File
########################################################################
# ##############################################################################
# Handle udev rules file
# ##############################################################################
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
SET(SYSTEM_IS_LINUX TRUE)
SET(UDEV_OPTION_DEFAULT ON)
set(SYSTEM_IS_LINUX TRUE)
set(UDEV_OPTION_DEFAULT ON)
else()
SET(SYSTEM_IS_LINUX FALSE)
SET(UDEV_OPTION_DEFAULT OFF)
set(SYSTEM_IS_LINUX FALSE)
set(UDEV_OPTION_DEFAULT OFF)
endif()
option(INSTALL_UDEV_RULES
"Install udev rules for the HackRF"
${UDEV_OPTION_DEFAULT}
)
option(INSTALL_UDEV_RULES "Install udev rules for the HackRF"
${UDEV_OPTION_DEFAULT})
set(UDEV_RULES_PATH
"/etc/udev/rules.d"
CACHE STRING
"Target directory for udev rule installation. Ensure you have permissions to write to this directory."
)
CACHE STRING "Target directory for udev rule installation.
Ensure you have permissions to write to this directory.")
if(SYSTEM_IS_LINUX)
if(INSTALL_UDEV_RULES)
if(NOT DEFINED UDEV_RULES_GROUP)
foreach(group usb plugdev)
execute_process(COMMAND "getent" group "${group}"
RESULT_VARIABLE _GETENT_RESULT
OUTPUT_QUIET
ERROR_QUIET)
if(NOT _GETENT_RESULT)
message(STATUS "Setting udev rule group to - ${group}")
set(UDEV_RULES_GROUP ${group})
break()
endif(NOT _GETENT_RESULT)
endforeach(group)
endif(NOT DEFINED UDEV_RULES_GROUP)
if(DEFINED UDEV_RULES_GROUP)
set(HACKRF_GROUP "${UDEV_RULES_GROUP}"
CACHE STRING "Group to associate HackRF devices with in udev rules")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/53-hackrf.rules.in
${CMAKE_CURRENT_BINARY_DIR}/53-hackrf.rules
@ONLY
)
message(STATUS "HackRF udev rules will be installed to '${UDEV_RULES_PATH}' upon running 'make install'")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/53-hackrf.rules
DESTINATION ${UDEV_RULES_PATH}
COMPONENT "udev_rules")
else(UDEV_RULES_GROUP)
message(STATUS "HackRF udev rules will not be installed because no suitable group was found")
message(STATUS "A group can be specified with -DUDEV_RULES_GROUP=<group>")
endif(DEFINED UDEV_RULES_GROUP)
else(INSTALL_UDEV_RULES)
message(STATUS
"HackRF udev rules will not be installed because INSTALL_UDEV_RULES=OFF"
)
endif(INSTALL_UDEV_RULES)
if(INSTALL_UDEV_RULES)
if(NOT DEFINED UDEV_RULES_GROUP)
foreach(group usb plugdev)
execute_process(
COMMAND "getent" group "${group}"
RESULT_VARIABLE _GETENT_RESULT
OUTPUT_QUIET ERROR_QUIET)
if(NOT _GETENT_RESULT)
message(STATUS "Setting udev rule group to - ${group}")
set(UDEV_RULES_GROUP ${group})
break()
endif(NOT _GETENT_RESULT)
endforeach(group)
endif(NOT DEFINED UDEV_RULES_GROUP)
if(DEFINED UDEV_RULES_GROUP)
set(HACKRF_GROUP
"${UDEV_RULES_GROUP}"
CACHE STRING "Group to associate HackRF devices with in udev rules")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/53-hackrf.rules.in
${CMAKE_CURRENT_BINARY_DIR}/53-hackrf.rules @ONLY)
message(
STATUS "HackRF udev rules will be installed to '${UDEV_RULES_PATH}'")
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/53-hackrf.rules
DESTINATION ${UDEV_RULES_PATH}
COMPONENT "udev_rules")
else(UDEV_RULES_GROUP)
message(
STATUS
"HackRF udev rules will not be installed because no group was found")
message(STATUS "A group can be specified with -DUDEV_RULES_GROUP=<group>")
endif(DEFINED UDEV_RULES_GROUP)
else(INSTALL_UDEV_RULES)
message(
STATUS
"HackRF udev rules will not be installed because INSTALL_UDEV_RULES=OFF"
)
endif(INSTALL_UDEV_RULES)
else(SYSTEM_IS_LINUX)
if(INSTALL_UDEV_RULES)
message(STATUS "udev rules not supported on this platform. Hide this message via -DINSTALL_UDEV_RULES=Off")
endif(INSTALL_UDEV_RULES)
if(INSTALL_UDEV_RULES)
message(STATUS "udev rules not supported on this platform.")
message(STATUS "Hide this message via -DINSTALL_UDEV_RULES=Off")
endif(INSTALL_UDEV_RULES)
endif(SYSTEM_IS_LINUX)
########################################################################
# ##############################################################################
# Create uninstall target
########################################################################
# ##############################################################################
if(NOT HackRF_SOURCE_DIR)
configure_file(
${PROJECT_SOURCE_DIR}/../cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
@ONLY)
configure_file(${PROJECT_SOURCE_DIR}/../cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake @ONLY)
add_custom_target(uninstall
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)
add_custom_target(
uninstall
COMMENT "Provide libhackrf uninstall target"
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

View File

@@ -0,0 +1,5 @@
include(CMakeFindDependencyMacro)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
find_dependency(LIBUSB REQUIRED)
find_dependency(Threads REQUIRED)
include("${CMAKE_CURRENT_LIST_DIR}/HackRFTargets.cmake")

View File

@@ -1,87 +1,119 @@
#
# Copyright (c) 2012, Jared Boone <jared@sharebrained.com>
# Copyright (c) 2013, Benjamin Vernoux <titanmkd@gmail.com>
# Copyright (c) 2013, Michael Ossmann <mike@ossmann.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# Neither the name of Great Scott Gadgets nor the names of its contributors may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Copyright (c) 2025, A. Maitland Bottoms <bottoms@debian.org>
#
# Based heavily upon the libftdi cmake setup.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer. Redistributions in binary
# form must reproduce the above copyright notice, this list of conditions and
# the following disclaimer in the documentation and/or other materials provided
# with the distribution. Neither the name of Great Scott Gadgets nor the names
# of its contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Targets
set(c_sources ${CMAKE_CURRENT_SOURCE_DIR}/hackrf.c CACHE INTERNAL "List of C sources")
set(c_headers ${CMAKE_CURRENT_SOURCE_DIR}/hackrf.h CACHE INTERNAL "List of C headers")
option(ENABLE_STATIC_LIB "Build and Install libhackrf.a static library" ON)
option(ENABLE_SHARED_LIB "Build and Install libhackrf.so shared library" ON)
option(
DISABLE_USB_DEVICE_DISCOVERY
"Prevent libusb from trying to enumerate devices. Useful on non-root android"
ANDROID)
# For cygwin just force UNIX OFF and WIN32 ON
if(${CYGWIN})
set(UNIX OFF)
set(WIN32 ON)
endif(${CYGWIN})
# Settings common for shared and static libraries
function(libhackrf_common_settings libtarget)
target_compile_features(${libtarget} PRIVATE c_std_90)
set_target_properties(hackrf PROPERTIES CLEAN_DIRECT_OUTPUT 1)
target_compile_definitions(
${libtarget} PRIVATE -DLIBRARY_VERSION="${PROJECT_VERSION}"
-DLIBRARY_RELEASE="${RELEASE}")
if(DISABLE_USB_DEVICE_DISCOVERY)
target_compile_definitions(${libtarget}
PRIVATE -DDISABLE_USB_DEVICE_DISCOVERY)
endif()
target_include_directories(
${libtarget}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libhackrf>)
target_link_libraries(${libtarget} PRIVATE LIBUSB::LIBUSB)
if(TARGET PThreads4W::PThreads4W)
target_link_libraries(${libtarget} PRIVATE PThreads4W::PThreads4W)
else()
target_link_libraries(${libtarget} PRIVATE Threads::Threads)
endif()
if(${UNIX})
install(
TARGETS ${libtarget}
EXPORT HackRFTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT sharedlibs
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif(${UNIX})
if(${WIN32})
install(
TARGETS ${libtarget}
EXPORT HackRFTargets
DESTINATION bin
COMPONENT sharedlibs)
endif(${WIN32})
endfunction()
# Dynamic library
add_library(hackrf SHARED ${c_sources})
set_target_properties(hackrf PROPERTIES VERSION ${MAJOR_VERSION}.${MINOR_VERSION}.0 SOVERSION 0)
# Static library
add_library(hackrf-static STATIC ${c_sources})
if(MSVC)
set_target_properties(hackrf-static PROPERTIES OUTPUT_NAME "hackrf_static")
else()
set_target_properties(hackrf-static PROPERTIES OUTPUT_NAME "hackrf")
if(ENABLE_SHARED_LIB)
add_library(hackrf SHARED hackrf.c)
set_target_properties(${libtarget} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})
libhackrf_common_settings(hackrf)
add_library(HackRF::hackrf ALIAS hackrf)
endif()
set_target_properties(hackrf PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(hackrf-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
# Static library
if(ENABLE_STATIC_LIB)
add_library(hackrf_static STATIC hackrf.c)
if(MSVC)
set_target_properties(hackrf_static PROPERTIES OUTPUT_NAME "hackrf_static")
else()
set_target_properties(hackrf_static PROPERTIES OUTPUT_NAME "hackrf")
endif()
libhackrf_common_settings(hackrf_static)
endif()
# Dependencies
target_link_libraries(hackrf ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
# For cygwin just force UNIX OFF and WIN32 ON
if( ${CYGWIN} )
SET(UNIX OFF)
SET(WIN32 ON)
endif( ${CYGWIN} )
if(${WIN32})
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/"
DESTINATION bin
FILES_MATCHING
PATTERN "libusb*.dll"
PATTERN "pthread*.dll")
endif(${WIN32})
if( ${UNIX} )
install(TARGETS hackrf
LIBRARY DESTINATION lib${LIB_SUFFIX}
COMPONENT sharedlibs
)
install(TARGETS hackrf-static
ARCHIVE DESTINATION lib${LIB_SUFFIX}
COMPONENT staticlibs
)
install(FILES ${c_headers}
DESTINATION include/${PROJECT_NAME}
COMPONENT headers
)
endif( ${UNIX} )
if( ${WIN32} )
install(TARGETS hackrf
DESTINATION bin
COMPONENT sharedlibs
)
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/"
DESTINATION bin
FILES_MATCHING
PATTERN "libusb*.dll"
PATTERN "pthread*.dll"
)
install(TARGETS hackrf-static
DESTINATION bin
COMPONENT staticlibs
)
install(FILES ${c_headers}
DESTINATION include/${PROJECT_NAME}
COMPONENT headers
)
endif( ${WIN32} )
install(
FILES hackrf.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
COMPONENT headers)