Debian Codegraph @

CMakeLists.txt

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # TODO
2 # - backend selection via command line, rather than simply detecting headers.
3
4 cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
5 cmake_policy(SET CMP0091 NEW)
6 project(cubeb
7 VERSION 0.0.0)
8
9 option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
10 option(BUILD_TESTS "Build tests" ON)
11 option(BUILD_RUST_LIBS "Build rust backends" OFF)
12 option(BUILD_TOOLS "Build tools" ON)
13 option(BUNDLE_SPEEX "Bundle the speex library" OFF)
14 option(LAZY_LOAD_LIBS "Lazily load shared libraries" ON)
15 option(USE_SANITIZERS "Use sanitizers" ON)
16 option(USE_STATIC_MSVC_RUNTIME "Use /MT instead of /MD in MSVC" OFF)
17 if(USE_STATIC_MSVC_RUNTIME)
18 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
19 endif()
20
21 if(NOT CMAKE_BUILD_TYPE)
22 set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
23 "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
24 endif()
25
26 set(CMAKE_C_STANDARD 99)
27 set(CMAKE_CXX_STANDARD 17)
28 set(CMAKE_CXX_STANDARD_REQUIRED ON)
29 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
30
31 if(USE_SANITIZERS)
32 if(NOT COMMAND add_sanitizers)
33 list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/sanitizers-cmake/cmake")
34 find_package(Sanitizers)
35 if(NOT COMMAND add_sanitizers)
36 message(FATAL_ERROR "Could not find sanitizers-cmake: run\n\tgit submodule update --init --recursive\nin base git checkout")
37 endif()
38 endif()
39 else()
40 macro(add_sanitizers UNUSED)
41 endmacro()
42 endif()
43
44 if(BUILD_TESTS)
45 find_package(GTest QUIET)
46 if(TARGET GTest::Main)
47 add_library(gtest_main ALIAS GTest::Main)
48 endif()
49 if(NOT TARGET gtest_main)
50 if(NOT EXISTS "${PROJECT_SOURCE_DIR}/googletest/CMakeLists.txt")
51 message(FATAL_ERROR "Could not find googletest: run\n\tgit submodule update --init --recursive\nin base git checkout")
52 endif()
53 add_definitions(-DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0)
54 set(gtest_force_shared_crt ON CACHE BOOL "")
55 add_subdirectory(googletest)
56 endif()
57 endif()
58
59 if (BUILD_RUST_LIBS)
60 if(EXISTS "${PROJECT_SOURCE_DIR}/src/cubeb-pulse-rs")
61 set(USE_PULSE_RUST 1)
62 endif()
63 if(EXISTS "${PROJECT_SOURCE_DIR}/src/cubeb-coreaudio-rs")
64 set(USE_AUDIOUNIT_RUST 1)
65 endif()
66 endif()
67
68 # On OS/2, visibility attribute is not supported.
69 if(NOT OS2)
70 set(CMAKE_C_VISIBILITY_PRESET hidden)
71 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
72 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
73 endif()
74
75 set(CMAKE_CXX_WARNING_LEVEL 4)
76 if(NOT MSVC)
77 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter")
78 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -fno-exceptions -fno-rtti")
79 else()
80 string(REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Disable RTTI
81 string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Disable Exceptions
82 endif()
83
84 add_library(cubeb
85 src/cubeb.c
86 src/cubeb_audio_dump.cpp
87 src/cubeb_mixer.cpp
88 src/cubeb_resampler.cpp
89 src/cubeb_log.cpp
90 src/cubeb_strings.c
91 src/cubeb_utils.cpp
92 )
93 target_include_directories(cubeb
94 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
95 )
96 set_target_properties(cubeb PROPERTIES
97 VERSION ${cubeb_VERSION}
98 SOVERSION ${cubeb_VERSION_MAJOR}
99 )
100
101 add_sanitizers(cubeb)
102
103 include(GenerateExportHeader)
104 generate_export_header(cubeb EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/exports/cubeb_export.h)
105 target_include_directories(cubeb
106 PUBLIC $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/exports>
107 )
108
109 include(GNUInstallDirs)
110
111 install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME} TYPE INCLUDE)
112 install(DIRECTORY ${CMAKE_BINARY_DIR}/exports/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
113
114 include(CMakePackageConfigHelpers)
115 write_basic_package_version_file(
116 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
117 COMPATIBILITY SameMajorVersion
118 )
119
120 configure_package_config_file(
121 "Config.cmake.in"
122 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
123 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
124 )
125
126 install(
127 FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
128 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
129 )
130
131 install(TARGETS cubeb EXPORT "${PROJECT_NAME}Targets")
132 install(
133 EXPORT "${PROJECT_NAME}Targets"
134 NAMESPACE "${PROJECT_NAME}::"
135 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
136 )
137
138 if(NOT BUNDLE_SPEEX)
139 find_package(PkgConfig)
140 if(PKG_CONFIG_FOUND)
141 pkg_check_modules(speexdsp IMPORTED_TARGET speexdsp)
142 if(speexdsp_FOUND)
143 add_library(speex ALIAS PkgConfig::speexdsp)
144 endif()
145 endif()
146 endif()
147
148 if(NOT TARGET speex)
149 add_library(speex OBJECT subprojects/speex/resample.c)
150 set_target_properties(speex PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
151 target_include_directories(speex INTERFACE subprojects)
152 target_compile_definitions(speex PUBLIC
153 OUTSIDE_SPEEX
154 FLOATING_POINT
155 EXPORT=
156 RANDOM_PREFIX=speex
157 )
158 endif()
159
160 # $<BUILD_INTERFACE:> required because of https://gitlab.kitware.com/cmake/cmake/-/issues/15415
161 target_link_libraries(cubeb PRIVATE $<BUILD_INTERFACE:speex>)
162
163 include(CheckIncludeFiles)
164
165 # Threads needed by cubeb_log, _pulse, _alsa, _jack, _sndio, _oss and _sun
166 set(THREADS_PREFER_PTHREAD_FLAG ON)
167 find_package(Threads)
168 target_link_libraries(cubeb PRIVATE Threads::Threads)
169
170 if(LAZY_LOAD_LIBS)
171 check_include_files(pulse/pulseaudio.h USE_PULSE)
172 check_include_files(alsa/asoundlib.h USE_ALSA)
173 check_include_files(jack/jack.h USE_JACK)
174 check_include_files(sndio.h USE_SNDIO)
175 check_include_files(aaudio/AAudio.h USE_AAUDIO)
176
177 if(USE_PULSE OR USE_ALSA OR USE_JACK OR USE_SNDIO OR USE_AAUDIO)
178 target_link_libraries(cubeb PRIVATE ${CMAKE_DL_LIBS})
179
180 if(ANDROID)
181 target_compile_definitions(cubeb PRIVATE __ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__)
182 endif()
183 endif()
184
185 else()
186
187 find_package(PkgConfig REQUIRED)
188
189 pkg_check_modules(libpulse IMPORTED_TARGET libpulse)
190 if(libpulse_FOUND)
191 set(USE_PULSE ON)
192 target_compile_definitions(cubeb PRIVATE DISABLE_LIBPULSE_DLOPEN)
193 target_link_libraries(cubeb PRIVATE PkgConfig::libpulse)
194 endif()
195
196 pkg_check_modules(alsa IMPORTED_TARGET alsa)
197 if(alsa_FOUND)
198 set(USE_ALSA ON)
199 target_compile_definitions(cubeb PRIVATE DISABLE_LIBASOUND_DLOPEN)
200 target_link_libraries(cubeb PRIVATE PkgConfig::alsa)
201 endif()
202
203 pkg_check_modules(jack IMPORTED_TARGET jack)
204 if(jack_FOUND)
205 set(USE_JACK ON)
206 target_compile_definitions(cubeb PRIVATE DISABLE_LIBJACK_DLOPEN)
207 target_link_libraries(cubeb PRIVATE PkgConfig::jack)
208 endif()
209
210 check_include_files(sndio.h USE_SNDIO)
211 if(USE_SNDIO)
212 target_compile_definitions(cubeb PRIVATE DISABLE_LIBSNDIO_DLOPEN)
213 target_link_libraries(cubeb PRIVATE sndio)
214 endif()
215
216 check_include_files(aaudio/AAudio.h USE_AAUDIO)
217 if(USE_AAUDIO)
218 target_compile_definitions(cubeb PRIVATE DISABLE_LIBAAUDIO_DLOPEN)
219 target_link_libraries(cubeb PRIVATE aaudio)
220 endif()
221 endif()
222
223 if(USE_PULSE)
224 target_sources(cubeb PRIVATE src/cubeb_pulse.c)
225 target_compile_definitions(cubeb PRIVATE USE_PULSE)
226 endif()
227
228 if(USE_ALSA)
229 target_sources(cubeb PRIVATE src/cubeb_alsa.c)
230 target_compile_definitions(cubeb PRIVATE USE_ALSA)
231 endif()
232
233 if(USE_JACK)
234 target_sources(cubeb PRIVATE src/cubeb_jack.cpp)
235 target_compile_definitions(cubeb PRIVATE USE_JACK)
236 endif()
237
238 if(USE_SNDIO)
239 target_sources(cubeb PRIVATE src/cubeb_sndio.c)
240 target_compile_definitions(cubeb PRIVATE USE_SNDIO)
241 endif()
242
243 if(USE_AAUDIO)
244 target_sources(cubeb PRIVATE src/cubeb_aaudio.cpp)
245 target_compile_definitions(cubeb PRIVATE USE_AAUDIO)
246
247 # set this definition to enable low latency mode. Possibly bad for battery
248 target_compile_definitions(cubeb PRIVATE CUBEB_AAUDIO_LOW_LATENCY)
249
250 # set this definition to enable power saving mode. Possibly resulting
251 # in high latency
252 # target_compile_definitions(cubeb PRIVATE CUBEB_AAUDIO_LOW_POWER_SAVING)
253
254 # set this mode to make the backend use an exclusive stream.
255 # will decrease latency.
256 # target_compile_definitions(cubeb PRIVATE CUBEB_AAUDIO_EXCLUSIVE_STREAM)
257 endif()
258
259 check_include_files(AudioUnit/AudioUnit.h USE_AUDIOUNIT)
260 if(USE_AUDIOUNIT)
261 target_sources(cubeb PRIVATE
262 src/cubeb_audiounit.cpp
263 src/cubeb_osx_run_loop.cpp)
264 target_compile_definitions(cubeb PRIVATE USE_AUDIOUNIT)
265 target_link_libraries(cubeb PRIVATE "-framework AudioUnit" "-framework CoreAudio" "-framework CoreServices")
266 endif()
267
268 check_include_files(audioclient.h USE_WASAPI)
269 if(USE_WASAPI)
270 target_sources(cubeb PRIVATE
271 src/cubeb_wasapi.cpp)
272 target_compile_definitions(cubeb PRIVATE USE_WASAPI)
273 target_link_libraries(cubeb PRIVATE avrt ole32 ksuser)
274 endif()
275
276 check_include_files("windows.h;mmsystem.h" USE_WINMM)
277 if(USE_WINMM)
278 target_sources(cubeb PRIVATE
279 src/cubeb_winmm.c)
280 target_compile_definitions(cubeb PRIVATE USE_WINMM)
281 target_link_libraries(cubeb PRIVATE winmm)
282 endif()
283
284 check_include_files(SLES/OpenSLES.h USE_OPENSL)
285 if(USE_OPENSL)
286 target_sources(cubeb PRIVATE
287 src/cubeb_opensl.cpp
288 src/cubeb-jni.cpp)
289 target_compile_definitions(cubeb PRIVATE USE_OPENSL)
290 target_link_libraries(cubeb PRIVATE OpenSLES)
291 endif()
292
293 check_include_files(sys/soundcard.h HAVE_SYS_SOUNDCARD_H)
294 if(HAVE_SYS_SOUNDCARD_H)
295 try_compile(USE_OSS "${PROJECT_BINARY_DIR}/compile_tests"
296 ${PROJECT_SOURCE_DIR}/cmake/compile_tests/oss_is_v4.c)
297 if(USE_OSS)
298 # strlcpy is not available on BSD systems that use glibc,
299 # like Debian kfreebsd, so try using libbsd if available
300 include(CheckSymbolExists)
301 check_symbol_exists(strlcpy string.h HAVE_STRLCPY)
302 if(NOT HAVE_STRLCPY)
303 pkg_check_modules(libbsd-overlay IMPORTED_TARGET libbsd-overlay)
304 if(libbsd-overlay_FOUND)
305 target_link_libraries(cubeb PRIVATE PkgConfig::libbsd-overlay)
306 set(HAVE_STRLCPY true)
307 endif()
308 endif()
309 if (HAVE_STRLCPY)
310 target_sources(cubeb PRIVATE
311 src/cubeb_oss.c)
312 target_compile_definitions(cubeb PRIVATE USE_OSS)
313 endif()
314 endif()
315 endif()
316
317 check_include_files(android/log.h USE_AUDIOTRACK)
318 if(USE_AUDIOTRACK)
319 target_sources(cubeb PRIVATE
320 src/cubeb_audiotrack.c)
321 target_compile_definitions(cubeb PRIVATE USE_AUDIOTRACK)
322 target_link_libraries(cubeb PRIVATE log)
323 endif()
324
325 check_include_files(sys/audioio.h USE_SUN)
326 if(USE_SUN)
327 target_sources(cubeb PRIVATE
328 src/cubeb_sun.c)
329 target_compile_definitions(cubeb PRIVATE USE_SUN)
330 endif()
331
332 check_include_files(kai.h USE_KAI)
333 if(USE_KAI)
334 target_sources(cubeb PRIVATE
335 src/cubeb_kai.c)
336 target_compile_definitions(cubeb PRIVATE USE_KAI)
337 target_link_libraries(cubeb PRIVATE kai)
338 endif()
339
340 if(USE_PULSE AND USE_PULSE_RUST)
341 include(ExternalProject)
342 set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/rust)
343 ExternalProject_Add(
344 cubeb_pulse_rs
345 DOWNLOAD_COMMAND ""
346 CONFIGURE_COMMAND ""
347 BUILD_COMMAND cargo build --features=gecko-in-tree "$<IF:$<CONFIG:Release,RelWithDebInfo,MinSizeRel>,--release,>"
348 BUILD_ALWAYS ON
349 BINARY_DIR "${PROJECT_SOURCE_DIR}/src/cubeb-pulse-rs"
350 INSTALL_COMMAND ""
351 LOG_BUILD ON
352 LOG_OUTPUT_ON_FAILURE ON)
353 add_dependencies(cubeb cubeb_pulse_rs)
354 target_compile_definitions(cubeb PRIVATE USE_PULSE_RUST)
355 target_link_libraries(cubeb PRIVATE
356 debug "${PROJECT_SOURCE_DIR}/src/cubeb-pulse-rs/target/debug/libcubeb_pulse.a"
357 optimized "${PROJECT_SOURCE_DIR}/src/cubeb-pulse-rs/target/release/libcubeb_pulse.a" pulse)
358 endif()
359
360 if(USE_AUDIOUNIT AND USE_AUDIOUNIT_RUST)
361 include(ExternalProject)
362 set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/rust)
363 ExternalProject_Add(
364 cubeb_coreaudio_rs
365 DOWNLOAD_COMMAND ""
366 CONFIGURE_COMMAND ""
367 BUILD_COMMAND cargo build --features=gecko-in-tree "$<IF:$<CONFIG:Release,RelWithDebInfo,MinSizeRel>,--release,>"
368 BUILD_ALWAYS ON
369 BINARY_DIR "${PROJECT_SOURCE_DIR}/src/cubeb-coreaudio-rs"
370 INSTALL_COMMAND ""
371 LOG_BUILD ON
372 LOG_OUTPUT_ON_FAILURE ON)
373 add_dependencies(cubeb cubeb_coreaudio_rs)
374 target_compile_definitions(cubeb PRIVATE USE_AUDIOUNIT_RUST)
375 target_link_libraries(cubeb PRIVATE
376 debug "${PROJECT_SOURCE_DIR}/src/cubeb-coreaudio-rs/target/debug/libcubeb_coreaudio.a"
377 optimized "${PROJECT_SOURCE_DIR}/src/cubeb-coreaudio-rs/target/release/libcubeb_coreaudio.a")
378 endif()
379
380 find_package(Doxygen)
381 if(DOXYGEN_FOUND)
382 set(DOXYGEN_JAVADOC_AUTOBRIEF YES)
383 set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES)
384 set(DOXYGEN_CASE_SENSE_NAMES NO)
385 set(DOXYGEN_SORT_MEMBER_DOCS NO)
386 set(DOXYGEN_QUIET YES)
387 set(DOXYGEN_WARN_NO_PARAMDOC YES)
388 set(DOXYGEN_GENERATE_HTML YES)
389
390 doxygen_add_docs(doc "include/cubeb/" ALL
391 COMMENT "Generating API documentation with Doxygen"
392 )
393
394 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/ TYPE DOC COMPONENT Documentation)
395 endif()
396
397 if(BUILD_TESTS)
398 enable_testing()
399
400 macro(cubeb_add_test NAME)
401 add_executable(test_${NAME} test/test_${NAME}.cpp)
402 target_include_directories(test_${NAME} PRIVATE ${gtest_SOURCE_DIR}/include src)
403 target_link_libraries(test_${NAME} PRIVATE cubeb gtest_main)
404 add_test(${NAME} test_${NAME} --gtest_death_test_style=threadsafe)
405 add_sanitizers(test_${NAME})
406 endmacro(cubeb_add_test)
407
408 cubeb_add_test(sanity)
409 cubeb_add_test(tone)
410 cubeb_add_test(audio)
411 cubeb_add_test(record)
412 cubeb_add_test(devices)
413 cubeb_add_test(callback_ret)
414
415 add_executable(test_resampler test/test_resampler.cpp src/cubeb_resampler.cpp src/cubeb_log.cpp)
416 target_include_directories(test_resampler PRIVATE ${gtest_SOURCE_DIR}/include src)
417 target_link_libraries(test_resampler PRIVATE cubeb gtest_main speex)
418 add_test(resampler test_resampler)
419 add_sanitizers(test_resampler)
420
421 cubeb_add_test(duplex)
422 cubeb_add_test(logging)
423 cubeb_add_test(triple_buffer)
424 cubeb_add_test(audio_dump)
425
426 if (USE_WASAPI)
427 cubeb_add_test(overload_callback)
428 cubeb_add_test(loopback)
429 endif()
430
431 cubeb_add_test(latency test_latency)
432 cubeb_add_test(ring_array)
433
434 cubeb_add_test(utils)
435 cubeb_add_test(ring_buffer)
436 cubeb_add_test(device_changed_callback)
437 endif()
438
439 if(BUILD_TOOLS)
440 add_executable(cubeb-test tools/cubeb-test.cpp)
441 target_include_directories(cubeb-test PRIVATE src)
442 target_link_libraries(cubeb-test PRIVATE cubeb)
443 add_sanitizers(cubeb-test)
444 install(TARGETS cubeb-test)
445 endif()
446
447 if(NOT CLANG_FORMAT_BINARY)
448 set(CLANG_FORMAT_BINARY clang-format)
449 endif()
450
451 add_custom_target(clang-format-check
452 find
453 ${CMAKE_CURRENT_SOURCE_DIR}/src
454 ${CMAKE_CURRENT_SOURCE_DIR}/include
455 ${CMAKE_CURRENT_SOURCE_DIR}/test
456 -type f (-name "*.cpp" -o -name "*.c" -o -name "*.h")
457 -not -path "*/subprojects/speex/*"
458 -print0
459 | xargs -0 ${CLANG_FORMAT_BINARY} -Werror -n
460 COMMENT "Check formatting with clang-format"
461 VERBATIM)