Skip to content

Commit 07c352c

Browse files
Tachi107kinetiknz
authored andcommitted
build: use system speex when possible
Following #658 (comment), the speex library is now handled like a normal dependency: cubeb will link against the system version if available, and fall back to the bundled one if not. I've also added a BUNDLE_SPEEX option, so that you can force the use of the bundled library if needed (e.g. creating a standalone libcubeb on a system where libspeex is available). I also had to move the bundled library to a separate folder. As `src` is always added as an include path, the headers in `src/speex` would conflict with system headers. And it also clears the relationship between cubeb and speex. I choose the "subprojects" name to follow the Meson convention, since CMake does not have one. A bit OT, but if you're curious you can see their rationale here: https://mesonbuild.com/Subprojects.html#why-must-all-subprojects-be-inside-a-single-directory Lastly, I added cubeb_log.cpp to the list of sources of test_resampler, as I was getting linking errors when building with BUILD_SHARED_LIBS=true Fixes #658
1 parent 5a64bc8 commit 07c352c

9 files changed

+31
-24
lines changed

CMakeLists.txt

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
99
option(BUILD_TESTS "Build tests" ON)
1010
option(BUILD_RUST_LIBS "Build rust backends" OFF)
1111
option(BUILD_TOOLS "Build tools" ON)
12+
option(BUNDLE_SPEEX "Bundle the speex library" OFF)
1213
option(LAZY_LOAD_LIBS "Lazily load shared libraries" ON)
1314
option(USE_SANITIZERS "Use sanitizers" ON)
1415

@@ -81,15 +82,10 @@ add_library(cubeb
8182
src/cubeb_log.cpp
8283
src/cubeb_strings.c
8384
src/cubeb_utils.cpp
84-
$<TARGET_OBJECTS:speex>)
85+
)
8586
target_include_directories(cubeb
8687
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
8788
)
88-
target_include_directories(cubeb PRIVATE src)
89-
target_compile_definitions(cubeb PRIVATE OUTSIDE_SPEEX)
90-
target_compile_definitions(cubeb PRIVATE FLOATING_POINT)
91-
target_compile_definitions(cubeb PRIVATE EXPORT=)
92-
target_compile_definitions(cubeb PRIVATE RANDOM_PREFIX=speex)
9389
set_target_properties(cubeb PROPERTIES
9490
VERSION ${cubeb_VERSION}
9591
SOVERSION ${cubeb_VERSION_MAJOR}
@@ -146,13 +142,30 @@ install(
146142
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
147143
)
148144

149-
add_library(speex OBJECT
150-
src/speex/resample.c)
151-
set_target_properties(speex PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
152-
target_compile_definitions(speex PRIVATE OUTSIDE_SPEEX)
153-
target_compile_definitions(speex PRIVATE FLOATING_POINT)
154-
target_compile_definitions(speex PRIVATE EXPORT=)
155-
target_compile_definitions(speex PRIVATE RANDOM_PREFIX=speex)
145+
if(NOT BUNDLE_SPEEX)
146+
find_package(PkgConfig)
147+
if(PKG_CONFIG_FOUND)
148+
pkg_check_modules(speexdsp IMPORTED_TARGET speexdsp)
149+
if(speexdsp_FOUND)
150+
add_library(speex ALIAS PkgConfig::speexdsp)
151+
endif()
152+
endif()
153+
endif()
154+
155+
if(NOT TARGET speex)
156+
add_library(speex STATIC subprojects/speex/resample.c)
157+
set_target_properties(speex PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
158+
target_include_directories(speex INTERFACE subprojects)
159+
target_compile_definitions(speex PUBLIC
160+
OUTSIDE_SPEEX
161+
FLOATING_POINT
162+
EXPORT=
163+
RANDOM_PREFIX=speex
164+
)
165+
endif()
166+
167+
# $<BUILD_INTERFACE:> required because of https://gitlab.kitware.com/cmake/cmake/-/issues/15415
168+
target_link_libraries(cubeb PRIVATE $<BUILD_INTERFACE:speex>)
156169

157170
include(CheckIncludeFiles)
158171

@@ -366,8 +379,7 @@ if(BUILD_TESTS)
366379

367380
macro(cubeb_add_test NAME)
368381
add_executable(test_${NAME} test/test_${NAME}.cpp)
369-
target_include_directories(test_${NAME} PRIVATE ${gtest_SOURCE_DIR}/include)
370-
target_include_directories(test_${NAME} PRIVATE src)
382+
target_include_directories(test_${NAME} PRIVATE ${gtest_SOURCE_DIR}/include src)
371383
target_link_libraries(test_${NAME} PRIVATE cubeb gtest_main)
372384
add_test(${NAME} test_${NAME})
373385
add_sanitizers(test_${NAME})
@@ -381,14 +393,9 @@ if(BUILD_TESTS)
381393
cubeb_add_test(devices)
382394
cubeb_add_test(callback_ret)
383395

384-
add_executable(test_resampler test/test_resampler.cpp src/cubeb_resampler.cpp $<TARGET_OBJECTS:speex>)
385-
target_include_directories(test_resampler PRIVATE ${gtest_SOURCE_DIR}/include)
386-
target_include_directories(test_resampler PRIVATE src)
387-
target_compile_definitions(test_resampler PRIVATE OUTSIDE_SPEEX)
388-
target_compile_definitions(test_resampler PRIVATE FLOATING_POINT)
389-
target_compile_definitions(test_resampler PRIVATE EXPORT=)
390-
target_compile_definitions(test_resampler PRIVATE RANDOM_PREFIX=speex)
391-
target_link_libraries(test_resampler PRIVATE cubeb gtest_main)
396+
add_executable(test_resampler test/test_resampler.cpp src/cubeb_resampler.cpp src/cubeb_log.cpp)
397+
target_include_directories(test_resampler PRIVATE ${gtest_SOURCE_DIR}/include src)
398+
target_link_libraries(test_resampler PRIVATE cubeb gtest_main speex)
392399
add_test(resampler test_resampler)
393400
add_sanitizers(test_resampler)
394401
install(TARGETS test_resampler DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR})
@@ -421,7 +428,7 @@ add_custom_target(clang-format-check
421428
${CMAKE_CURRENT_SOURCE_DIR}/src
422429
${CMAKE_CURRENT_SOURCE_DIR}/include
423430
-type f (-name "*.cpp" -o -name "*.c" -o -name "*.h")
424-
-not -path "*/src/speex/*"
431+
-not -path "*/subprojects/speex/*"
425432
-print0
426433
| xargs -0 clang-format -Werror -n
427434
COMMENT "Check formatting with clang-format"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)