Skip to content

Commit

Permalink
[cmake] build examples w/ a specified driver
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Jan 24, 2022
1 parent 2c42989 commit dcb895b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
56 changes: 56 additions & 0 deletions cmake/AutoConfig_RF24_DRIVER.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
set(RF24_LINKED_DRIVER "")
set(RF24_DRIVER "UNKNOWN" CACHE STRING "override automatic configuration of RF24's utility driver.
Specify 1 of the following supported drivers (ie -DRF24_DRIVER=SPIDEV):
wiringPi
RPi
SPIDEV
MRAA
LittleWire
pigpio"
)

###########################
# detect pre-existing (locally installed) 3rd party libraries
###########################

# detect installed libraries despite what RF24_DRIVER is set to
# this is always done for cross-compiling purposes
find_library(LibMRAA mraa)
find_library(LibWiringPi wiringPi)
find_library(LibLittleWire littlewire-spi)
find_library(LibPIGPIO pigpio)
if(EXISTS /dev/spidev0.0)
set(SPIDEV_EXISTS TRUE)
else()
set(SPIDEV_EXISTS FALSE)
endif()


if(${RF24_DRIVER} STREQUAL "UNKNOWN") # invokes automatic configuration
if("${SOC}" STREQUAL "BCM2708" OR "${SOC}" STREQUAL "BCM2709" OR "${SOC}" STREQUAL "BCM2835")
set(RF24_DRIVER RPi CACHE STRING "using folder /utility/RPi" FORCE)
elseif(NOT "${LibPIGPIO}" STREQUAL "LibPIGPIO-NOTFOUND")
message(STATUS "Found pigpio library: ${LibPIGPIO}")
set(RF24_DRIVER pigpio CACHE STRING "using folder /utility/pigpio" FORCE)
elseif(NOT "${LibWiringPi}" STREQUAL "LibWiringPi-NOTFOUND")
message(STATUS "Found wiringPi library: ${LibWiringPi}")
set(RF24_DRIVER wiringPi CACHE STRING "using folder /utility/wiringPi" FORCE)
elseif(NOT "${LibLittleWire}" STREQUAL "LibLittleWire-NOTFOUND")
message(STATUS "Found LittleWire library: ${LibLittleWire}")
set(RF24_DRIVER LittleWire CACHE STRING "using folder /utility/LittleWire" FORCE)
elseif(NOT "${LibMRAA}" STREQUAL "LibMRAA-NOTFOUND")
message(STATUS "Found MRAA library: ${LibMRAA}")
set(RF24_DRIVER MRAA CACHE STRING "using folder /utility/MRAA" FORCE)
elseif(SPIDEV_EXISTS) # should be a non-empty string if SPI is enabled
message(STATUS "detected that SPIDEV is enabled: ${SPIDEV_EXISTS}")
set(RF24_DRIVER SPIDEV CACHE STRING "using folder /utility/SPIDEV" FORCE)
endif()
endif()

# override the auto-detect if RF24_DRIVER is defined in an env var
if(DEFINED ENV{RF24_DRIVER})
message(STATUS "RF24_DRIVER (set from env var) = $ENV{RF24_DRIVER}")
set(RF24_DRIVER $ENV{RF24_DRIVER} CACHE STRING "" FORCE)
endif()

message(STATUS "Using driver: ${RF24_DRIVER}")
42 changes: 33 additions & 9 deletions examples_RPi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,48 @@ add_compile_options(-Ofast -Wall) # passing the compiler a `-pthread` flag doesn
# detect the CPU make and type
include(../cmake/detectCPU.cmake) # sets the variable SOC accordingly

# detect if any additional libs need be linked to executable (ei RF24_DRIVER)
include(../cmake/AutoConfig_RF24_DRIVER.cmake)

find_library(RF24 rf24 REQUIRED)
message(STATUS "using RF24 library: ${RF24}")

find_library(RF24Network rf24network REQUIRED)
message(STATUS "using RF24Network library: ${RF24Network}")

find_library(LibPIGPIO pigpio)
if (NOT "${LibPIGPIO}" STREQUAL "LibPIGPIO-NOTFOUND")
message(STATUS "linking to ${LibPIGPIO} for interrupt support")
set(linked_libs
${RF24}
pthread # Notice we specify pthread as a linked lib here
${RF24Network}
)

# append additional libs for linking to the executable
if("${RF24_DRIVER}" STREQUAL "MRAA")
if(NOT "${LibMRAA}" STREQUAL "LibMRAA-NOTFOUND")
message(STATUS "linking to ${LibMRAA}")
list(APPEND linked_libs ${LibMRAA})
else()
message(FATAL "Lib ${RF24_DRIVER} not found.")
endif()
elseif("${RF24_DRIVER}" STREQUAL "wiringPi")
if(NOT "${LibWiringPi}" STREQUAL "LibWiringPi-NOTFOUND")
message(STATUS "linking to ${LibWiringPi}")
list(APPEND linked_libs ${LibWiringPi})
else()
message(FATAL "Lib ${RF24_DRIVER} not found.")
endif()
elseif(NOT "${LibPIGPIO}" STREQUAL "LibPIGPIO-NOTFOUND")
if(NOT "${RF24_DRIVER}" STREQUAL "pigpio")
message(STATUS "linking to ${LibPIGPIO} for interrupt support")
else()
message(STATUS "linking to ${LibPIGPIO}")
endif()
list(APPEND linked_libs ${LibPIGPIO})
endif()

foreach(example ${EXAMPLES_LIST})
# make a target
add_executable(${example} ${example}.cpp)
# link the RF24 lib to the target. Notice we specify pthread as a linked lib here
if ("${LibPIGPIO}" STREQUAL "LibPIGPIO-NOTFOUND")
target_link_libraries(${example} PUBLIC ${RF24} pthread ${RF24Network})
else()
target_link_libraries(${example} PUBLIC ${RF24} pthread ${RF24Network} ${LibPIGPIO})
endif()
# link the RF24 lib to the target.
target_link_libraries(${example} PUBLIC ${linked_libs})
endforeach()

0 comments on commit dcb895b

Please sign in to comment.