CMakeLists.txt 10 KB

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