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.

315 lines
9.0KB

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