CMakeLists.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ########################################################################
  2. # Project setup
  3. ########################################################################
  4. cmake_minimum_required(VERSION 2.6)
  5. # Fix behavior of CMAKE_C_STANDARD when targeting macOS.
  6. if(POLICY CMP0025)
  7. cmake_policy(SET CMP0025 NEW)
  8. endif()
  9. # Only interpret if() arguments as variables or keywords when unquoted.
  10. if(POLICY CMP0054)
  11. cmake_policy(SET CMP0054 NEW)
  12. endif()
  13. project(rtl433 C)
  14. #select the release build type by default to get optimization flags
  15. if(NOT CMAKE_BUILD_TYPE)
  16. set(CMAKE_BUILD_TYPE "Release")
  17. message(STATUS "Build type not specified: defaulting to release.")
  18. endif(NOT CMAKE_BUILD_TYPE)
  19. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "")
  20. list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
  21. ########################################################################
  22. # Get version info from Git
  23. ########################################################################
  24. include(GetGitRevisionDescription)
  25. get_git_head_revision(GIT_REFSPEC GIT_COMMIT)
  26. if(GIT_COMMIT) # is a git repo
  27. # shorten branch spec
  28. string(REGEX REPLACE ".*/" "" GIT_BRANCH "${GIT_REFSPEC}")
  29. # use lightweight (non-annotated) tags
  30. git_describe(GIT_VERSION "--tags")
  31. git_timestamp(GIT_TIMESTAMP)
  32. message(STATUS "Found Git version: ${GIT_REFSPEC} commit ${GIT_COMMIT} from ${GIT_TIMESTAMP_ISO}")
  33. message(STATUS "Using Git version tag: ${GIT_VERSION} on ${GIT_BRANCH} at ${GIT_TIMESTAMP}")
  34. ADD_DEFINITIONS(-DGIT_VERSION=${GIT_VERSION})
  35. ADD_DEFINITIONS(-DGIT_BRANCH=${GIT_BRANCH})
  36. ADD_DEFINITIONS(-DGIT_TIMESTAMP=${GIT_TIMESTAMP})
  37. endif()
  38. ########################################################################
  39. # Compiler specific setup
  40. ########################################################################
  41. set(CMAKE_C_EXTENSIONS OFF)
  42. set(CMAKE_C_STANDARD 99)
  43. if(("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" MATCHES "Clang") AND NOT WIN32)
  44. ADD_DEFINITIONS(-Wall)
  45. ADD_DEFINITIONS(-Wextra)
  46. ADD_DEFINITIONS(-Wsign-compare)
  47. ADD_DEFINITIONS(-Wno-unused)
  48. ADD_DEFINITIONS(-Wno-unused-parameter)
  49. ADD_DEFINITIONS(-Wno-missing-field-initializers)
  50. ADD_DEFINITIONS(-std=c99)
  51. ADD_DEFINITIONS(-pedantic)
  52. # for strdup, setenv, use either
  53. #ADD_DEFINITIONS(-D_POSIX_C_SOURCE=200809) # does not work with uClibc
  54. ADD_DEFINITIONS(-D_GNU_SOURCE)
  55. #http://gcc.gnu.org/wiki/Visibility
  56. add_definitions(-fvisibility=hidden)
  57. # CMake Release default for GCC/Clang is "-O3 -DNDEBUG"
  58. # set(CMAKE_C_FLAGS_RELEASE -O2)
  59. # CMake Debug default for GCC/Clang is "-g -DNDEBUG"
  60. # set(CMAKE_C_FLAGS_DEBUG -g3 -O0)
  61. # make use of ASAN
  62. set(CMAKE_C_FLAGS_DEBUG "-ggdb -fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer")
  63. endif()
  64. if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
  65. # make sure we don't accidentally copy more than an int
  66. ADD_DEFINITIONS(-Wlarge-by-value-copy=8)
  67. endif()
  68. # Shut MSVC up about strdup and strtok
  69. if(MSVC)
  70. ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
  71. ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
  72. ADD_DEFINITIONS(-DNOMINMAX)
  73. endif()
  74. # Fix printf %zu
  75. if(MINGW)
  76. add_definitions(-D__USE_MINGW_ANSI_STDIO)
  77. endif()
  78. ########################################################################
  79. # Find build dependencies
  80. ########################################################################
  81. set(ENABLE_RTLSDR ON CACHE STRING "Enable RTL-SDR (lbrtlsdr) driver support")
  82. set_property(CACHE ENABLE_RTLSDR PROPERTY STRINGS AUTO ON OFF)
  83. if(ENABLE_RTLSDR) # AUTO / ON
  84. find_package(PkgConfig)
  85. find_package(LibRTLSDR)
  86. find_package(LibUSB)
  87. if(LIBRTLSDR_FOUND AND LIBUSB_FOUND)
  88. message(STATUS "RTL-SDR device input will be compiled.")
  89. include_directories(${LIBRTLSDR_INCLUDE_DIRS} ${LIBUSB_INCLUDE_DIRS})
  90. list(APPEND SDR_LIBRARIES ${LIBRTLSDR_LIBRARIES} ${LIBUSB_LIBRARIES})
  91. ADD_DEFINITIONS(-DRTLSDR)
  92. elseif(ENABLE_RTLSDR STREQUAL "AUTO")
  93. message(STATUS "RTL-SDR development files not found, RTL-SDR device input won't be possible.")
  94. else()
  95. message(FATAL_ERROR "RTL-SDR development files not found.")
  96. endif()
  97. else()
  98. message(STATUS "RTL-SDR device input disabled.")
  99. endif()
  100. set(ENABLE_SOAPYSDR AUTO CACHE STRING "Enable SoapySDR driver support")
  101. set_property(CACHE ENABLE_SOAPYSDR PROPERTY STRINGS AUTO ON OFF)
  102. if(ENABLE_SOAPYSDR) # AUTO / ON
  103. find_package(SoapySDR "0.6" NO_MODULE)
  104. if(SoapySDR_FOUND)
  105. message(STATUS "SoapySDR device input will be compiled.")
  106. include_directories(${SoapySDR_INCLUDE_DIRS})
  107. list(APPEND SDR_LIBRARIES ${SoapySDR_LIBRARIES})
  108. ADD_DEFINITIONS(-DSOAPYSDR)
  109. elseif(ENABLE_SOAPYSDR STREQUAL "AUTO")
  110. message(STATUS "SoapySDR development files not found, SoapySDR device input won't be possible.")
  111. else()
  112. message(FATAL_ERROR "SoapySDR development files not found.")
  113. endif()
  114. else()
  115. message(STATUS "SoapySDR device input disabled.")
  116. endif()
  117. # cmake -DCMAKE_BUILD_TYPE=Profile ..
  118. # CPUPROFILE=prof.out ./src/rtl_433 ...
  119. # pprof -text ./src/rtl_433 prof.out
  120. if("${CMAKE_BUILD_TYPE}" STREQUAL "Profile")
  121. message(STATUS "Build type set to Profile. Linking GPerfTools.")
  122. find_package(Gperftools REQUIRED)
  123. include_directories(${GPERFTOOLS_INCLUDE_DIR})
  124. list(APPEND SDR_LIBRARIES ${GPERFTOOLS_LIBRARIES} -Wl,-no_pie)
  125. ADD_DEFINITIONS(-g)
  126. ADD_DEFINITIONS(-fno-builtin-malloc)
  127. ADD_DEFINITIONS(-fno-builtin-calloc)
  128. ADD_DEFINITIONS(-fno-builtin-realloc)
  129. ADD_DEFINITIONS(-fno-builtin-free)
  130. endif()
  131. if(MINGW OR MSVC)
  132. list(APPEND NET_LIBRARIES ws2_32 mswsock)
  133. endif()
  134. ########################################################################
  135. # Setup the include and linker paths
  136. ########################################################################
  137. include_directories(
  138. BEFORE
  139. ${PROJECT_SOURCE_DIR}/include
  140. )
  141. ########################################################################
  142. # Create uninstall target
  143. ########################################################################
  144. configure_file(
  145. ${PROJECT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
  146. ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
  147. @ONLY)
  148. add_custom_target(uninstall
  149. ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
  150. )
  151. ########################################################################
  152. # Build documentation with Doxygen
  153. ########################################################################
  154. option(BUILD_DOCUMENTATION "Create and install the HTML based API
  155. documentation (requires Doxygen)" OFF)
  156. find_package(Doxygen)
  157. if(BUILD_DOCUMENTATION)
  158. if(NOT DOXYGEN_FOUND)
  159. message(FATAL_ERROR "Doxygen is needed to build the documentation.")
  160. endif()
  161. set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
  162. set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
  163. configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
  164. message(STATUS "Doxygen build started")
  165. # note the option ALL which allows to build the docs together with the application
  166. add_custom_target(doc_doxygen ALL
  167. COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
  168. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  169. COMMENT "Generating API documentation with Doxygen"
  170. VERBATIM)
  171. # install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc/html DESTINATION share/doc)
  172. endif()
  173. ########################################################################
  174. # Build tests with analyzer
  175. ########################################################################
  176. option(BUILD_TESTING_ANALYZER "Build the testing tree with static
  177. analyzer (requires Clang)" OFF)
  178. ########################################################################
  179. # Build tests
  180. ########################################################################
  181. include(CTest) # note: this adds a BUILD_TESTING which defaults to ON
  182. ########################################################################
  183. # Configure CI deploy
  184. ########################################################################
  185. configure_file(${PROJECT_SOURCE_DIR}/.bintray.json ${CMAKE_CURRENT_BINARY_DIR}/bintray.json)
  186. ########################################################################
  187. # Add subdirectories
  188. ########################################################################
  189. add_subdirectory(include)
  190. add_subdirectory(src)
  191. if(BUILD_TESTING)
  192. add_subdirectory(tests)
  193. endif(BUILD_TESTING)
  194. add_subdirectory(conf)
  195. # use space-separation format for the pc file
  196. STRING(REPLACE ";" " " RTL433_PC_CFLAGS "${RTL433_PC_CFLAGS}")
  197. STRING(REPLACE ";" " " RTL433_PC_LIBS "${RTL433_PC_LIBS}")
  198. # unset these vars to avoid hard-coded paths to cross environment
  199. IF(CMAKE_CROSSCOMPILING)
  200. UNSET(RTL433_PC_CFLAGS)
  201. UNSET(RTL433_PC_LIBS)
  202. ENDIF(CMAKE_CROSSCOMPILING)
  203. set(prefix ${CMAKE_INSTALL_PREFIX})
  204. set(exec_prefix \${prefix})
  205. set(libdir \${exec_prefix}/lib)
  206. set(includedir \${prefix}/include)
  207. INSTALL(
  208. FILES
  209. DESTINATION lib/pkgconfig
  210. )
  211. install(DIRECTORY man
  212. DESTINATION share
  213. PATTERN ".md" EXCLUDE)