2
0

CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. cmake_minimum_required(VERSION 2.8.12)
  2. cmake_policy(SET CMP0077 NEW)
  3. project(bell)
  4. # Configurable options
  5. option(BELL_DISABLE_CODECS "Disable the entire audio codec wrapper" OFF)
  6. option(BELL_CODEC_AAC "Support libhelix-aac codec" ON)
  7. option(BELL_CODEC_MP3 "Support libhelix-mp3 codec" ON)
  8. option(BELL_CODEC_VORBIS "Support tremor Vorbis codec" ON)
  9. option(BELL_CODEC_ALAC "Support Apple ALAC codec" ON)
  10. option(BELL_CODEC_OPUS "Support Opus codec" ON)
  11. option(BELL_DISABLE_SINKS "Disable all built-in audio sink implementations" OFF)
  12. # These are default OFF, as they're OS-dependent (ESP32 sinks are always enabled - no external deps)
  13. option(BELL_SINK_ALSA "Enable ALSA audio sink" OFF)
  14. option(BELL_SINK_PORTAUDIO "Enable PortAudio sink" OFF)
  15. # cJSON wrapper
  16. option(BELL_DISABLE_CJSON "Disable cJSON and JSONObject completely" OFF)
  17. set(BELL_EXTERNAL_CJSON "" CACHE STRING "External cJSON library target name, optional")
  18. if(BELL_EXTERNAL_MBEDTLS)
  19. set(MbedTLS_DIR ${BELL_EXTERNAL_MBEDTLS})
  20. message(STATUS "Setting local mbedtls ${MbedTLS_DIR}")
  21. endif()
  22. # Backwards compatibility with deprecated options
  23. if(BELL_USE_ALSA)
  24. message(WARNING "Deprecated Bell options used, replace BELL_USE_ALSA with BELL_SINK_ALSA")
  25. set(BELL_SINK_ALSA ${BELL_USE_ALSA})
  26. endif()
  27. if(BELL_USE_PORTAUDIO)
  28. message(WARNING "Deprecated Bell options used, replace BELL_USE_PORTAUDIO with BELL_SINK_PORTAUDIO")
  29. set(BELL_SINK_PORTAUDIO ${BELL_USE_PORTAUDIO})
  30. endif()
  31. message(STATUS "Bell options:")
  32. message(STATUS " Disable all codecs: ${BELL_DISABLE_CODECS}")
  33. if(NOT BELL_DISABLE_CODECS)
  34. message(STATUS " - AAC audio codec: ${BELL_CODEC_AAC}")
  35. message(STATUS " - MP3 audio codec: ${BELL_CODEC_MP3}")
  36. message(STATUS " - Vorbis audio codec: ${BELL_CODEC_VORBIS}")
  37. message(STATUS " - Opus audio codec: ${BELL_CODEC_OPUS}")
  38. message(STATUS " - ALAC audio codec: ${BELL_CODEC_ALAC}")
  39. endif()
  40. message(STATUS " Disable built-in audio sinks: ${BELL_DISABLE_SINKS}")
  41. if(NOT BELL_DISABLE_SINKS)
  42. message(STATUS " - ALSA sink: ${BELL_SINK_ALSA}")
  43. message(STATUS " - PortAudio sink: ${BELL_SINK_PORTAUDIO}")
  44. endif()
  45. message(STATUS " Disable cJSON and JSONObject: ${BELL_DISABLE_CJSON}")
  46. # Include nanoPB library
  47. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/nanopb/extra")
  48. find_package(Nanopb REQUIRED)
  49. list(APPEND EXTRA_INCLUDES ${NANOPB_INCLUDE_DIRS})
  50. # CMake options
  51. set(CMAKE_CXX_STANDARD 20)
  52. set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  53. set(AUDIO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/audio")
  54. add_definitions("-DUSE_DEFAULT_STDLIB=1")
  55. # Main library sources
  56. file(GLOB SOURCES "src/*.cpp" "src/*.c" "nanopb/*.c")
  57. list(APPEND EXTRA_INCLUDES "include/platform")
  58. list(APPEND EXTRA_INCLUDES "include/audio/container")
  59. # Add platform specific sources
  60. if(ESP_PLATFORM)
  61. file(GLOB ESP_PLATFORM_SOURCES "src/platform/esp/*.cpp" "src/platform/esp/*.c" "src/asm/biquad_f32_ae32.S")
  62. list(APPEND SOURCES ${ESP_PLATFORM_SOURCES})
  63. endif()
  64. if(UNIX)
  65. file(GLOB UNIX_PLATFORM_SOURCES "src/platform/unix/*.cpp" "src/platform/unix/*.c")
  66. list(APPEND SOURCES ${UNIX_PLATFORM_SOURCES})
  67. endif()
  68. if(APPLE)
  69. file(GLOB APPLE_PLATFORM_SOURCES "src/platform/apple/*.cpp" "src/platform/apple/*.c")
  70. list(APPEND SOURCES ${APPLE_PLATFORM_SOURCES})
  71. list(APPEND EXTRA_INCLUDES "/usr/local/opt/mbedtls@3/include")
  72. endif()
  73. if(UNIX AND NOT APPLE)
  74. file(GLOB LINUX_PLATFORM_SOURCES "src/platform/linux/*.cpp" "src/platform/linux/*.c")
  75. list(APPEND SOURCES ${LINUX_PLATFORM_SOURCES})
  76. endif()
  77. if(WIN32)
  78. file(GLOB WIN32_PLATFORM_SOURCES "src/platform/win32/*.cpp" "src/platform/win32/*.c")
  79. list(APPEND SOURCES ${WIN32_PLATFORM_SOURCES})
  80. list(APPEND EXTRA_INCLUDES "include/platform/win32")
  81. endif()
  82. # A hack to make Opus keep quiet
  83. function(message)
  84. if(NOT MESSAGE_QUIET)
  85. _message(${ARGN})
  86. endif()
  87. endfunction()
  88. if(ESP_PLATFORM)
  89. list(APPEND EXTRA_LIBS idf::mbedtls idf::pthread idf::mdns)
  90. add_definitions(-Wunused-const-variable -Wchar-subscripts -Wunused-label -Wmaybe-uninitialized -Wmisleading-indentation)
  91. else()
  92. find_package(Threads REQUIRED)
  93. set(THREADS_PREFER_PTHREAD_FLAG ON)
  94. list(APPEND EXTRA_LIBS Threads::Threads)
  95. find_package(MbedTLS REQUIRED)
  96. get_target_property(MBEDTLS_INFO MbedTLS::mbedtls INTERFACE_INCLUDE_DIRECTORIES)
  97. list(APPEND EXTRA_INCLUDES ${MBEDTLS_INFO})
  98. # try to handle mbedtls when not system-wide installed
  99. if(BELL_EXTERNAL_MBEDTLS)
  100. if(MSVC)
  101. set(MBEDTLS_RELEASE "RELEASE" CACHE STRING "local mbedtls version")
  102. else()
  103. set(MBEDTLS_RELEASE "NOCONFIG" CACHE STRING "local mbedtls version")
  104. endif()
  105. message(STATUS "using local mbedtls version ${MBEDTLS_RELEASE}")
  106. get_target_property(MBEDTLS_INFO MbedTLS::mbedtls IMPORTED_LOCATION_${MBEDTLS_RELEASE})
  107. list(APPEND EXTRA_LIBS ${MBEDTLS_INFO})
  108. get_target_property(MBEDTLS_INFO MbedTLS::mbedx509 IMPORTED_LOCATION_${MBEDTLS_RELEASE})
  109. list(APPEND EXTRA_LIBS ${MBEDTLS_INFO})
  110. get_target_property(MBEDTLS_INFO MbedTLS::mbedcrypto IMPORTED_LOCATION_${MBEDTLS_RELEASE})
  111. list(APPEND EXTRA_LIBS ${MBEDTLS_INFO})
  112. else()
  113. list(APPEND EXTRA_LIBS mbedtls mbedcrypto mbedx509)
  114. endif()
  115. if(MSVC)
  116. add_compile_definitions(NOMINMAX _CRT_SECURE_NO_WARNINGS)
  117. add_definitions(/wd4068 /wd4244 /wd4018 /wd4101 /wd4102 /wd4142)
  118. endif()
  119. endif()
  120. if(NOT BELL_DISABLE_CODECS)
  121. file(GLOB EXTRA_SOURCES "src/audio/container/*.cpp")
  122. list(APPEND SOURCES "${EXTRA_SOURCES}")
  123. list(APPEND SOURCES "${AUDIO_DIR}/codec/DecoderGlobals.cpp")
  124. list(APPEND SOURCES "${AUDIO_DIR}/codec/BaseCodec.cpp")
  125. list(APPEND SOURCES "${AUDIO_DIR}/codec/AudioCodecs.cpp")
  126. list(APPEND EXTRA_INCLUDES "include/audio/codec")
  127. # AAC-LC codec
  128. if(BELL_CODEC_AAC)
  129. file(GLOB LIBHELIX_AAC_SOURCES "libhelix-aac/*.c")
  130. list(APPEND LIBHELIX_SOURCES ${LIBHELIX_AAC_SOURCES})
  131. list(APPEND EXTRA_INCLUDES "libhelix-aac")
  132. list(APPEND SOURCES "${AUDIO_DIR}/codec/AACDecoder.cpp")
  133. list(APPEND CODEC_FLAGS "-DBELL_CODEC_AAC")
  134. endif()
  135. # MP3 codec
  136. if(BELL_CODEC_MP3)
  137. file(GLOB LIBHELIX_MP3_SOURCES "libhelix-mp3/*.c")
  138. list(APPEND LIBHELIX_SOURCES ${LIBHELIX_MP3_SOURCES})
  139. list(APPEND EXTRA_INCLUDES "libhelix-mp3")
  140. list(APPEND SOURCES "${AUDIO_DIR}/codec/MP3Decoder.cpp")
  141. list(APPEND CODEC_FLAGS "-DBELL_CODEC_MP3")
  142. endif()
  143. # MP3 codec
  144. if(BELL_CODEC_ALAC)
  145. file(GLOB ALAC_SOURCES "alac/*.c" "alac/*.cpp")
  146. list(APPEND ALAC_SOURCES ${ALAC_SOURCES})
  147. list(APPEND EXTRA_INCLUDES "alac")
  148. # list(APPEND SOURCES "${AUDIO_DIR}/codec/ALACDecoder.cpp")
  149. list(APPEND CODEC_FLAGS "-DBELL_CODEC_ALAC")
  150. endif()
  151. # libhelix Cygwin workaround
  152. if(CYGWIN)
  153. # Both Cygwin and ESP are Unix-like so this seems to work (or, at least, compile)
  154. set_source_files_properties("${AUDIO_DIR}/codec/DecoderGlobals.cpp" ${LIBHELIX_SOURCES} PROPERTIES COMPILE_FLAGS "-DESP_PLATFORM")
  155. endif()
  156. list(APPEND SOURCES ${LIBHELIX_SOURCES})
  157. list(APPEND SOURCES ${ALAC_SOURCES})
  158. # Vorbis codec
  159. if(BELL_CODEC_VORBIS)
  160. file(GLOB TREMOR_SOURCES "tremor/*.c")
  161. list(REMOVE_ITEM TREMOR_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tremor/ivorbisfile_example.c")
  162. list(APPEND SOURCES ${TREMOR_SOURCES})
  163. list(APPEND EXTRA_INCLUDES "tremor")
  164. list(APPEND SOURCES "${AUDIO_DIR}/codec/VorbisDecoder.cpp")
  165. list(APPEND CODEC_FLAGS "-DBELL_CODEC_VORBIS")
  166. endif()
  167. # Opus codec
  168. if(BELL_CODEC_OPUS)
  169. set(OPUS_INSTALL_CMAKE_CONFIG_MODULE OFF CACHE BOOL "")
  170. set(OPUS_INSTALL_CMAKE_CONFIG_MODULE OFF)
  171. set(OPUS_INSTALL_PKG_CONFIG_MODULE OFF CACHE BOOL "")
  172. set(OPUS_INSTALL_PKG_CONFIG_MODULE OFF)
  173. set(MESSAGE_QUIET ON)
  174. add_subdirectory("opus")
  175. unset(MESSAGE_QUIET)
  176. target_compile_options(opus PRIVATE "-O3")
  177. list(APPEND EXTRA_LIBS Opus::opus)
  178. list(APPEND SOURCES "${AUDIO_DIR}/codec/OPUSDecoder.cpp")
  179. list(APPEND CODEC_FLAGS -DBELL_CODEC_OPUS)
  180. endif()
  181. # Enable global codecs
  182. string(REPLACE ";" " " CODEC_FLAGS "${CODEC_FLAGS}")
  183. set_source_files_properties("${AUDIO_DIR}/codec/AudioCodecs.cpp" PROPERTIES COMPILE_FLAGS "${CODEC_FLAGS}")
  184. elseif(BELL_EXTERNAL_TREMOR)
  185. list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_TREMOR})
  186. endif()
  187. if(NOT BELL_DISABLE_SINKS)
  188. set(PLATFORM "unix")
  189. if(ESP_PLATFORM)
  190. set(PLATFORM "esp")
  191. endif()
  192. # Add all built-in audio sinks
  193. file(GLOB SINK_SOURCES "${AUDIO_DIR}/sinks/${PLATFORM}/*.cpp" "${AUDIO_DIR}/sinks/${PLATFORM}/*.c")
  194. list(APPEND EXTRA_INCLUDES "include/audio/sinks/${PLATFORM}")
  195. # Find ALSA if required, else remove the sink
  196. if(BELL_SINK_ALSA)
  197. find_package(ALSA REQUIRED)
  198. list(APPEND EXTRA_INCLUDES ${ALSA_INCLUDE_DIRS})
  199. list(APPEND EXTRA_LIBS ${ALSA_LIBRARIES})
  200. else()
  201. list(REMOVE_ITEM SINK_SOURCES "${AUDIO_DIR}/sinks/unix/ALSAAudioSink.cpp")
  202. endif()
  203. # Find PortAudio if required, else remove the sink
  204. if(BELL_SINK_PORTAUDIO)
  205. if(WIN32)
  206. list(APPEND EXTRA_INCLUDES "portaudio/include")
  207. if(NOT "${CMAKE_GENERATOR}" MATCHES "(Win64|IA64)")
  208. list(APPEND EXTRA_LIBS "${CMAKE_CURRENT_SOURCE_DIR}/portaudio/portaudio_win32.lib")
  209. else()
  210. list(APPEND EXTRA_LIBS "${CMAKE_CURRENT_SOURCE_DIR}/portaudio/portaudio_x64.lib")
  211. endif()
  212. else()
  213. find_package(portaudio REQUIRED)
  214. list(APPEND EXTRA_INCLUDES ${PORTAUDIO_INCLUDE_DIRS})
  215. list(APPEND EXTRA_LIBS ${PORTAUDIO_LIBRARIES})
  216. endif()
  217. else()
  218. list(REMOVE_ITEM SINK_SOURCES "${AUDIO_DIR}/sinks/unix/PortAudioSink.cpp")
  219. endif()
  220. list(APPEND SOURCES ${SINK_SOURCES})
  221. endif()
  222. if(BELL_DISABLE_CJSON)
  223. list(REMOVE_ITEM SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/JSONObject.cpp")
  224. else()
  225. if(BELL_EXTERNAL_CJSON)
  226. list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_CJSON})
  227. else()
  228. list(APPEND SOURCES "cJSON/cJSON.c")
  229. list(APPEND EXTRA_INCLUDES "cJSON")
  230. endif()
  231. endif()
  232. add_library(bell STATIC ${SOURCES})
  233. # PUBLIC to propagate esp-idf includes to bell dependents
  234. target_link_libraries(bell PUBLIC ${EXTRA_LIBS})
  235. target_include_directories(bell PUBLIC "include" ${EXTRA_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
  236. target_compile_definitions(bell PUBLIC PB_ENABLE_MALLOC)
  237. if(WIN32)
  238. target_compile_definitions(bell PUBLIC PB_NO_STATIC_ASSERT)
  239. endif()