You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
8.6KB

  1. # Set minimum CMake required version for this project.
  2. cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
  3. # Define a C++ project.
  4. project(RtAudio LANGUAGES CXX)
  5. # Check for Jack (any OS)
  6. find_library(JACK_LIB jack)
  7. find_package(PkgConfig)
  8. pkg_check_modules(jack jack)
  9. if(JACK_LIB OR jack_FOUND)
  10. set(HAVE_JACK TRUE)
  11. endif()
  12. # Check for Pulse (any OS)
  13. pkg_check_modules(pulse libpulse-simple)
  14. # Check for known non-Linux unix-likes
  15. if (CMAKE_SYSTEM_NAME MATCHES "kNetBSD.*|NetBSD.*")
  16. message(STATUS "NetBSD detected, using OSS")
  17. set(xBSD ON)
  18. elseif(UNIX AND NOT APPLE)
  19. set(LINUX ON)
  20. endif()
  21. # Necessary for Windows
  22. if(WIN32)
  23. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  24. set(CMAKE_DEBUG_POSTFIX "d")
  25. endif()
  26. # Build Options
  27. option(BUILD_SHARED_LIBS "Compile library shared lib." TRUE)
  28. option(BUILD_STATIC_LIBS "Compile library static lib." TRUE)
  29. option(BUILD_TESTING "Compile test programs." TRUE)
  30. option(BUILD_PYTHON "Build PyRtAudio python bindings" OFF)
  31. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (Release,Debug)")
  32. # API Options
  33. option(RTAUDIO_API_DS "Build DirectSound API" OFF)
  34. option(RTAUDIO_API_ASIO "Build ASIO API" OFF)
  35. option(RTAUDIO_API_WASAPI "Build WASAPI API" ${WIN32})
  36. option(RTAUDIO_API_OSS "Build OSS4 API" ${xBSD})
  37. option(RTAUDIO_API_ALSA "Build ALSA API" ${LINUX})
  38. option(RTAUDIO_API_PULSE "Build PulseAudio API" ${pulse_FOUND})
  39. option(RTAUDIO_API_JACK "Build JACK audio server API" ${HAVE_JACK})
  40. option(RTAUDIO_API_CORE "Build CoreAudio API" ${APPLE})
  41. # Check for functions
  42. include(CheckFunctionExists)
  43. check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
  44. if (HAVE_GETTIMEOFDAY)
  45. add_definitions(-DHAVE_GETTIMEOFDAY)
  46. endif ()
  47. # Add -Wall if possible
  48. if (CMAKE_COMPILER_IS_GNUCXX)
  49. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  50. endif (CMAKE_COMPILER_IS_GNUCXX)
  51. # Add debug flags
  52. if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  53. add_definitions(-D__RTAUDIO_DEBUG__)
  54. if (CMAKE_COMPILER_IS_GNUCXX)
  55. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
  56. endif (CMAKE_COMPILER_IS_GNUCXX)
  57. endif ()
  58. # Read libtool version info from configure.ac
  59. set(R "m4_define\\(\\[lt_([a-z]+)\\], ([0-9]+)\\)")
  60. file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/configure.ac" CONFIGAC
  61. REGEX ${R})
  62. foreach(_S ${CONFIGAC})
  63. string(REGEX REPLACE ${R} "\\1" k ${_S})
  64. string(REGEX REPLACE ${R} "\\2" v ${_S})
  65. set(SO_${k} ${v})
  66. endforeach()
  67. math(EXPR SO_current_minus_age "${SO_current} - ${SO_age}")
  68. set(SO_VER "${SO_current_minus_age}")
  69. set(FULL_VER "${SO_current_minus_age}.${SO_revision}.${SO_age}")
  70. # Read package version info from configure.ac
  71. set(R "AC_INIT\\(RtAudio, ([0-9\\.]+),.*\\)")
  72. file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/configure.ac" CONFIGAC
  73. REGEX ${R})
  74. string(REGEX REPLACE ${R} "\\1" PACKAGE_VERSION ${CONFIGAC})
  75. # Init variables
  76. set(rtaudio_SOURCES RtAudio.cpp RtAudio.h rtaudio_c.cpp rtaudio_c.h)
  77. set(LINKLIBS)
  78. set(PKGCONFIG_REQUIRES)
  79. set(API_DEFS)
  80. set(API_LIST)
  81. # Tweak API-specific configuration.
  82. # Jack
  83. if (RTAUDIO_API_JACK AND jack_FOUND)
  84. set(NEED_PTHREAD ON)
  85. list(APPEND PKGCONFIG_REQUIRES "jack")
  86. list(APPEND API_DEFS "-D__UNIX_JACK__")
  87. list(APPEND API_LIST "jack")
  88. if(jack_FOUND)
  89. list(APPEND LINKLIBS ${jack_LIBRARIES})
  90. list(APPEND INCDIRS ${jack_INCLUDEDIR})
  91. else()
  92. list(APPEND LINKLIBS ${JACK_LIB})
  93. endif()
  94. endif()
  95. # ALSA
  96. if (RTAUDIO_API_ALSA)
  97. set(NEED_PTHREAD ON)
  98. find_package(ALSA)
  99. if (NOT ALSA_FOUND)
  100. message(FATAL_ERROR "ALSA API requested but no ALSA dev libraries found")
  101. endif()
  102. list(APPEND INCDIRS ${ALSA_INCLUDE_DIR})
  103. list(APPEND LINKLIBS ${ALSA_LIBRARY})
  104. list(APPEND PKGCONFIG_REQUIRES "alsa")
  105. list(APPEND API_DEFS "-D__LINUX_ALSA__")
  106. list(APPEND API_LIST "alsa")
  107. endif()
  108. # OSS
  109. if (RTAUDIO_OSS)
  110. set(NEED_PTHREAD ON)
  111. find_library(OSSAUDIO_LIB ossaudio)
  112. if (OSSAUDIO_LIB)
  113. list(APPEND LINKLIBS ossaudio)
  114. # Note: not an error on some systems
  115. endif()
  116. list(APPEND API_DEFS "-D__LINUX_OSS__")
  117. list(APPEND API_LIST "oss")
  118. endif()
  119. # Pulse
  120. if (RTAUDIO_API_PULSE)
  121. set(NEED_PTHREAD ON)
  122. find_library(PULSE_LIB pulse)
  123. find_library(PULSESIMPLE_LIB pulse-simple)
  124. list(APPEND LINKLIBS ${PULSE_LIB} ${PULSESIMPLE_LIB})
  125. list(APPEND PKGCONFIG_REQUIRES "libpulse-simple")
  126. list(APPEND API_DEFS "-D__LINUX_PULSE__")
  127. list(APPEND API_LIST "pulse")
  128. endif()
  129. # CoreAudio
  130. if (RTAUDIO_API_CORE)
  131. find_library(COREAUDIO_LIB CoreAudio)
  132. find_library(COREFOUNDATION_LIB CoreFoundation)
  133. list(APPEND LINKLIBS ${COREAUDIO_LIB} ${COREFOUNDATION_LIB})
  134. list(APPEND API_DEFS "-D__MACOSX_CORE__")
  135. list(APPEND API_LIST "core")
  136. endif()
  137. # ASIO
  138. if (RTAUDIO_API_ASIO)
  139. set(NEED_WIN32LIBS ON)
  140. include_directories(include)
  141. list(APPEND rtaudio_SOURCES
  142. include/asio.cpp
  143. include/asiodrivers.cpp
  144. include/asiolist.cpp
  145. include/iasiothiscallresolver.cpp)
  146. list(APPEND API_DEFS "-D__WINDOWS_ASIO__")
  147. list(APPEND API_LIST "asio")
  148. endif()
  149. # DSound
  150. if (RTAUDIO_API_DS)
  151. set(NEED_WIN32LIBS ON)
  152. list(APPEND LINKLIBS dsound)
  153. list(APPEND API_DEFS "-D__WINDOWS_DS__")
  154. list(APPEND API_LIST "ds")
  155. endif()
  156. # WASAPI
  157. if (RTAUDIO_API_WASAPI)
  158. set(NEED_WIN32LIBS ON)
  159. list(APPEND LINKLIBS uuid ksuser)
  160. list(APPEND API_DEFS "-D__WINDOWS_WASAPI__")
  161. list(APPEND API_LIST "wasapi")
  162. endif()
  163. # Windows libs
  164. if (NEED_WIN32LIBS)
  165. list(APPEND LINKLIBS winmm ole32)
  166. endif()
  167. # pthread
  168. if (NEED_PTHREAD)
  169. find_package(Threads REQUIRED
  170. CMAKE_THREAD_PREFER_PTHREAD
  171. THREADS_PREFER_PTHREAD_FLAG)
  172. list(APPEND LINKLIBS Threads::Threads)
  173. endif()
  174. # Create library targets.
  175. cmake_policy(SET CMP0042 OLD)
  176. set(LIB_TARGETS)
  177. if(BUILD_SHARED_LIBS)
  178. add_library(rtaudio SHARED ${rtaudio_SOURCES})
  179. list(APPEND LIB_TARGETS rtaudio)
  180. # Add headers destination for install rule.
  181. set_target_properties(rtaudio PROPERTIES PUBLIC_HEADER RtAudio.h
  182. SOVERSION ${SO_VER}
  183. VERSION ${FULL_VER})
  184. # Set include paths, populate target interface.
  185. target_include_directories(rtaudio PRIVATE
  186. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  187. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  188. ${INCDIRS})
  189. # Set compile-time definitions
  190. target_compile_definitions(rtaudio PRIVATE ${API_DEFS})
  191. target_link_libraries(rtaudio ${LINKLIBS})
  192. endif()
  193. if(BUILD_STATIC_LIBS)
  194. add_library(rtaudio_static STATIC ${rtaudio_SOURCES})
  195. list(APPEND LIB_TARGETS rtaudio_static)
  196. # Add headers destination for install rule.
  197. set_target_properties(rtaudio_static PROPERTIES PUBLIC_HEADER RtAudio.h
  198. SOVERSION ${SO_VER}
  199. VERSION ${FULL_VER})
  200. # Set include paths, populate target interface.
  201. target_include_directories(rtaudio_static PRIVATE
  202. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  203. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  204. ${INCDIRS})
  205. # Set compile-time definitions
  206. target_compile_definitions(rtaudio_static PRIVATE ${API_DEFS})
  207. target_link_libraries(rtaudio_static ${LINKLIBS})
  208. endif()
  209. # Set standard installation directories.
  210. include(GNUInstallDirs)
  211. # Subdirs
  212. if (BUILD_TESTING)
  213. include(CTest)
  214. add_subdirectory(tests)
  215. endif (BUILD_TESTING)
  216. # Message
  217. string(REPLACE ";" " " apilist "${API_LIST}")
  218. message(STATUS "Compiling with support for: ${apilist}")
  219. # PkgConfig file
  220. string(REPLACE ";" " " req "${PKGCONFIG_REQUIRES}")
  221. string(REPLACE ";" " " api "${API_DEFS}")
  222. set(prefix ${CMAKE_INSTALL_PREFIX})
  223. configure_file("rtaudio.pc.in" "rtaudio.pc" @ONLY)
  224. # Add install rule.
  225. install(TARGETS ${LIB_TARGETS}
  226. EXPORT RtAudioTargets
  227. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  228. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  229. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  230. PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  231. # Store the package in the user registry.
  232. export(PACKAGE RtAudio)
  233. # Set installation path for CMake files.
  234. if(WIN32)
  235. set(RTAUDIO_CMAKE_DESTINATION cmake)
  236. else()
  237. set(RTAUDIO_CMAKE_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/RtAudio)
  238. endif()
  239. # Create CMake configuration export file.
  240. file(WRITE ${CMAKE_BINARY_DIR}/RtAudioConfig.cmake "include(\${CMAKE_CURRENT_LIST_DIR}/RtAudioTargets.cmake)")
  241. # Install CMake configuration export file.
  242. install(FILES ${CMAKE_BINARY_DIR}/RtAudioConfig.cmake
  243. DESTINATION ${RTAUDIO_CMAKE_DESTINATION})
  244. # Export library target (build-tree).
  245. export(EXPORT RtAudioTargets
  246. NAMESPACE RtAudio::)
  247. # Export library target (install-tree).
  248. install(EXPORT RtAudioTargets
  249. DESTINATION ${RTAUDIO_CMAKE_DESTINATION}
  250. NAMESPACE RtAudio::)
  251. # Configure uninstall target.
  252. configure_file(
  253. "${CMAKE_SOURCE_DIR}/cmake/RtAudioConfigUninstall.cmake.in"
  254. "${CMAKE_BINARY_DIR}/RtAudioConfigUninstall.cmake" @ONLY)
  255. # Create uninstall target.
  256. add_custom_target(uninstall
  257. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/RtAudioConfigUninstall.cmake)
  258. install(
  259. FILES ${CMAKE_CURRENT_BINARY_DIR}/rtaudio.pc
  260. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)