CMake: Clean up the handling of libopus and libvorbis.

This commit is contained in:
David Reid
2026-01-03 15:02:21 +10:00
parent 44b39fe097
commit 9128cbe35b
+22 -48
View File
@@ -470,24 +470,18 @@ if(NOT MINIAUDIO_NO_LIBVORBIS)
# Try pkg-config first # Try pkg-config first
find_package(PkgConfig QUIET) find_package(PkgConfig QUIET)
if(PkgConfig_FOUND) if(PkgConfig_FOUND)
pkg_check_modules(PC_VORBISFILE vorbisfile) pkg_check_modules(vorbisfile IMPORTED_TARGET vorbisfile)
endif() endif()
if(PC_VORBISFILE_FOUND) if(TARGET PkgConfig::vorbisfile)
message(STATUS "Found vorbisfile via pkg-config: ${PC_VORBISFILE_LIBRARIES}") add_library(vorbisfile ALIAS PkgConfig::vorbisfile)
set(HAS_LIBVORBIS TRUE)
else() else()
# Fallback to building from source. # Fallback to building from source.
add_libvorbis_subdirectory() add_libvorbis_subdirectory()
if(NOT TARGET vorbisfile) if(NOT TARGET vorbisfile)
message(STATUS "libvorbisfile not found. miniaudio_libvorbis will be excluded.") message(STATUS "libvorbisfile not found. miniaudio_libvorbis will be excluded.")
else()
set(HAS_LIBVORBIS TRUE)
endif() endif()
endif() endif()
else()
message(STATUS "libvorbisfile already found.")
set(HAS_LIBVORBIS TRUE)
endif() endif()
endif() endif()
@@ -499,24 +493,18 @@ if(NOT MINIAUDIO_NO_LIBOPUS)
# Try pkg-config first # Try pkg-config first
find_package(PkgConfig QUIET) find_package(PkgConfig QUIET)
if(PkgConfig_FOUND) if(PkgConfig_FOUND)
pkg_check_modules(PC_OPUSFILE opusfile) pkg_check_modules(opusfile IMPORTED_TARGET opusfile)
endif() endif()
if(PC_OPUSFILE_FOUND) if(TARGET PkgConfig::opusfile)
message(STATUS "Found opusfile via pkg-config: ${PC_OPUSFILE_LIBRARIES}") add_library(opusfile ALIAS PkgConfig::opusfile)
set(HAS_LIBOPUS TRUE)
else() else()
# Fallback to building from source. # Fallback to building from source.
add_libopusfile_subdirectory() add_libopusfile_subdirectory()
if(NOT TARGET opusfile) if(NOT TARGET opusfile)
message(STATUS "libopusfile not found. miniaudio_libopus will be excluded.") message(STATUS "libopusfile not found. miniaudio_libopus will be excluded.")
else()
set(HAS_LIBOPUS TRUE)
endif() endif()
endif() endif()
else()
message(STATUS "libopusfile already found.")
set(HAS_LIBOPUS TRUE)
endif() endif()
endif() endif()
@@ -667,19 +655,12 @@ endif()
# Extra Decoders # Extra Decoders
add_library(libvorbis_interface INTERFACE) if(TARGET vorbisfile)
if(HAS_LIBVORBIS) add_library (libvorbis_interface INTERFACE)
if(TARGET vorbisfile) target_link_libraries(libvorbis_interface INTERFACE vorbisfile)
target_link_libraries(libvorbis_interface INTERFACE vorbisfile)
elseif(PC_VORBISFILE_FOUND)
target_link_libraries (libvorbis_interface INTERFACE ${PC_VORBISFILE_LIBRARIES})
target_include_directories(libvorbis_interface INTERFACE ${PC_VORBISFILE_INCLUDE_DIRS})
target_link_directories (libvorbis_interface INTERFACE ${PC_VORBISFILE_LIBRARY_DIRS})
target_compile_options (libvorbis_interface INTERFACE ${PC_VORBISFILE_CFLAGS_OTHER})
endif()
endif() endif()
if(HAS_LIBVORBIS) if(TARGET libopus_interface)
add_library(miniaudio_libvorbis add_library(miniaudio_libvorbis
extras/decoders/libvorbis/miniaudio_libvorbis.c extras/decoders/libvorbis/miniaudio_libvorbis.c
extras/decoders/libvorbis/miniaudio_libvorbis.h extras/decoders/libvorbis/miniaudio_libvorbis.h
@@ -696,25 +677,18 @@ if(HAS_LIBVORBIS)
target_include_directories(miniaudio_libvorbis PUBLIC extras/decoders/libvorbis/) target_include_directories(miniaudio_libvorbis PUBLIC extras/decoders/libvorbis/)
endif() endif()
add_library(libopus_interface INTERFACE)
if(HAS_LIBOPUS) if(TARGET opusfile)
if(TARGET opusfile) add_library (libopus_interface INTERFACE)
target_link_libraries (libopus_interface INTERFACE opusfile) target_link_libraries(libopus_interface INTERFACE opusfile)
elseif(PC_OPUSFILE_FOUND)
target_link_libraries (libopus_interface INTERFACE ${PC_OPUSFILE_LIBRARIES})
target_include_directories(libopus_interface INTERFACE ${PC_OPUSFILE_INCLUDE_DIRS})
target_link_directories (libopus_interface INTERFACE ${PC_OPUSFILE_LIBRARY_DIRS})
target_compile_options (libopus_interface INTERFACE ${PC_OPUSFILE_CFLAGS_OTHER})
endif()
endif() endif()
if(HAS_LIBOPUS) if(TARGET libopus_interface)
add_library(miniaudio_libopus add_library(miniaudio_libopus
extras/decoders/libopus/miniaudio_libopus.c extras/decoders/libopus/miniaudio_libopus.c
extras/decoders/libopus/miniaudio_libopus.h extras/decoders/libopus/miniaudio_libopus.h
) )
list(APPEND LIBS_TO_INSTALL miniaudio_libopus) list(APPEND LIBS_TO_INSTALL miniaudio_libopus)
if(MINIAUDIO_INSTALL) if(MINIAUDIO_INSTALL)
install(FILES extras/decoders/libopus/miniaudio_libopus.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/miniaudio/extras/decoders/libopus) install(FILES extras/decoders/libopus/miniaudio_libopus.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/miniaudio/extras/decoders/libopus)
@@ -893,27 +867,27 @@ if (MINIAUDIO_BUILD_EXAMPLES)
add_miniaudio_example(miniaudio_custom_backend custom_backend.c) add_miniaudio_example(miniaudio_custom_backend custom_backend.c)
add_miniaudio_example(miniaudio_custom_decoder_engine custom_decoder_engine.c) add_miniaudio_example(miniaudio_custom_decoder_engine custom_decoder_engine.c)
if(HAS_LIBVORBIS) if(TARGET libvorbis_interface)
target_link_libraries(miniaudio_custom_decoder_engine PRIVATE libvorbis_interface) target_link_libraries(miniaudio_custom_decoder_engine PRIVATE libvorbis_interface)
else() else()
target_compile_definitions(miniaudio_custom_decoder_engine PRIVATE MA_NO_LIBVORBIS) target_compile_definitions(miniaudio_custom_decoder_engine PRIVATE MA_NO_LIBVORBIS)
message(STATUS "miniaudio_libvorbis is disabled. Vorbis support is disabled in miniaudio_custom_decoder_engine.") message(STATUS "miniaudio_libvorbis is disabled. Vorbis support is disabled in miniaudio_custom_decoder_engine.")
endif() endif()
if(HAS_LIBOPUS) if(TARGET libopus_interface)
target_link_libraries(miniaudio_custom_decoder_engine PRIVATE libopus_interface) target_link_libraries(miniaudio_custom_decoder_engine PRIVATE libopus_interface)
else() else()
target_compile_definitions(miniaudio_custom_decoder_engine PRIVATE MA_NO_LIBOPUS) target_compile_definitions(miniaudio_custom_decoder_engine PRIVATE MA_NO_LIBOPUS)
message(STATUS "miniaudio_libopus is disabled. Opus support is disabled in miniaudio_custom_decoder_engine.") message(STATUS "miniaudio_libopus is disabled. Opus support is disabled in miniaudio_custom_decoder_engine.")
endif() endif()
add_miniaudio_example(miniaudio_custom_decoder custom_decoder.c) add_miniaudio_example(miniaudio_custom_decoder custom_decoder.c)
if(HAS_LIBVORBIS) if(TARGET libvorbis_interface)
target_link_libraries(miniaudio_custom_decoder PRIVATE libvorbis_interface) target_link_libraries(miniaudio_custom_decoder PRIVATE libvorbis_interface)
else() else()
target_compile_definitions(miniaudio_custom_decoder PRIVATE MA_NO_LIBVORBIS) target_compile_definitions(miniaudio_custom_decoder PRIVATE MA_NO_LIBVORBIS)
message(STATUS "miniaudio_libvorbis is disabled. Vorbis support is disabled in miniaudio_custom_decoder.") message(STATUS "miniaudio_libvorbis is disabled. Vorbis support is disabled in miniaudio_custom_decoder.")
endif() endif()
if(HAS_LIBOPUS) if(TARGET libopus_interface)
target_link_libraries(miniaudio_custom_decoder PRIVATE libopus_interface) target_link_libraries(miniaudio_custom_decoder PRIVATE libopus_interface)
else() else()
target_compile_definitions(miniaudio_custom_decoder PRIVATE MA_NO_LIBOPUS) target_compile_definitions(miniaudio_custom_decoder PRIVATE MA_NO_LIBOPUS)
@@ -981,10 +955,10 @@ string(JOIN ", " MINIAUDIO_PC_REQUIRES_PRIVATE ${LINKED_LIBS})
# Add vorbisfile and opusfile to pkg-config dependencies if found via pkg-config # Add vorbisfile and opusfile to pkg-config dependencies if found via pkg-config
set(PC_REQUIRES_PRIVATE_LIST) set(PC_REQUIRES_PRIVATE_LIST)
if(PC_VORBISFILE_FOUND AND HAS_LIBVORBIS) if(TARGET vorbisfile)
list(APPEND PC_REQUIRES_PRIVATE_LIST "vorbisfile") list(APPEND PC_REQUIRES_PRIVATE_LIST "vorbisfile")
endif() endif()
if(PC_OPUSFILE_FOUND AND HAS_LIBOPUS) if(TARGET opusfile)
list(APPEND PC_REQUIRES_PRIVATE_LIST "opusfile") list(APPEND PC_REQUIRES_PRIVATE_LIST "opusfile")
endif() endif()
if(PC_REQUIRES_PRIVATE_LIST) if(PC_REQUIRES_PRIVATE_LIST)