cmake: use FindALSA.cmake to find ALSA library

This commit is contained in:
nfarid 2022-12-11 22:23:45 +00:00 committed by GitHub
parent faa7e3cc2e
commit b79732b967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 86 additions and 27 deletions

6
.gitignore vendored
View File

@ -62,6 +62,11 @@ cmake-build-*
xcuserdata xcuserdata
*.xcworkspace *.xcworkspace
# for QtCreator
CMakeLists.txt.user
build*/
*.pro.user*
# for Visual C++ # for Visual C++
.vs .vs
Debug Debug
@ -173,6 +178,7 @@ test/testyuv
test/torturethread test/torturethread
builddir/ builddir/
!build-scripts/
debian/*.debhelper.log debian/*.debhelper.log
debian/*.substvars debian/*.substvars
debian/*.tar.gz debian/*.tar.gz

View File

@ -18,6 +18,9 @@ endif()
set(EXTRA_LIBS) set(EXTRA_LIBS)
set(EXTRA_LDFLAGS) set(EXTRA_LDFLAGS)
set(CMAKE_DEPENDS)
set(PKGCONFIG_DEPENDS)
# This is a virtual "library" that just exists to collect up compiler and # This is a virtual "library" that just exists to collect up compiler and
# linker options that used to be global to this CMake project. When you # linker options that used to be global to this CMake project. When you
# specify it as part of a real library's target_link_libraries(), that # specify it as part of a real library's target_link_libraries(), that
@ -27,7 +30,6 @@ set(EXTRA_LDFLAGS)
add_library(sdl-build-options INTERFACE) add_library(sdl-build-options INTERFACE)
if(WINDOWS_STORE) if(WINDOWS_STORE)
cmake_minimum_required(VERSION 3.11.0)
target_compile_definitions(sdl-build-options INTERFACE "-DSDL_BUILDING_WINRT=1") target_compile_definitions(sdl-build-options INTERFACE "-DSDL_BUILDING_WINRT=1")
target_compile_options(sdl-build-options INTERFACE "-ZW") target_compile_options(sdl-build-options INTERFACE "-ZW")
endif() endif()
@ -3074,6 +3076,7 @@ string(REGEX REPLACE "-lSDL2( |$)" "-l${sdl_static_libname} " SDL_STATIC_LIBS "$
if(NOT SDL_SHARED) if(NOT SDL_SHARED)
string(REGEX REPLACE "-lSDL2( |$)" "-l${sdl_static_libname} " SDL_LIBS "${SDL_LIBS}") string(REGEX REPLACE "-lSDL2( |$)" "-l${sdl_static_libname} " SDL_LIBS "${SDL_LIBS}")
endif() endif()
listtostr(PKGCONFIG_DEPENDS PKGCONFIG_DEPENDS)
if(SDL_STATIC AND SDL_SHARED AND NOT sdl_static_libname STREQUAL "SDL2") if(SDL_STATIC AND SDL_SHARED AND NOT sdl_static_libname STREQUAL "SDL2")
message(STATUS "\"pkg-config --static --libs sdl2\" will return invalid information") message(STATUS "\"pkg-config --static --libs sdl2\" will return invalid information")
@ -3154,7 +3157,7 @@ if (SDL_ASAN)
endif() endif()
endif() endif()
if(SDL_CCACHE) if(SDL_CCACHE AND NOT CMAKE_VERSION VERSION_LESS 3.4)
cmake_minimum_required(VERSION 3.4) cmake_minimum_required(VERSION 3.4)
find_program(CCACHE_BINARY ccache) find_program(CCACHE_BINARY ccache)
if(CCACHE_BINARY) if(CCACHE_BINARY)
@ -3165,6 +3168,8 @@ if(SDL_CCACHE)
else() else()
set(HAVE_CCACHE OFF) set(HAVE_CCACHE OFF)
endif() endif()
else()
set(HAVE_CCACHE OFF)
endif() endif()
if(SDL_TESTS) if(SDL_TESTS)
@ -3334,12 +3339,13 @@ if(SDL_SHARED)
set_target_properties(SDL2 PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB") set_target_properties(SDL2 PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
endif() endif()
# FIXME: if CMAKE_VERSION >= 3.13, use target_link_options for EXTRA_LDFLAGS # FIXME: if CMAKE_VERSION >= 3.13, use target_link_options for EXTRA_LDFLAGS
target_link_libraries(SDL2 PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS} ${EXTRA_LDFLAGS_BUILD}) target_link_libraries(SDL2 PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS} ${EXTRA_LDFLAGS_BUILD} ${CMAKE_DEPENDS})
target_include_directories(SDL2 PUBLIC target_include_directories(SDL2 PUBLIC
"$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include>" "$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>" "$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL2>") "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL2>"
)
# This picks up all the compiler options and such we've accumulated up to here. # This picks up all the compiler options and such we've accumulated up to here.
target_link_libraries(SDL2 PRIVATE $<BUILD_INTERFACE:sdl-build-options>) target_link_libraries(SDL2 PRIVATE $<BUILD_INTERFACE:sdl-build-options>)
if(MINGW OR CYGWIN) if(MINGW OR CYGWIN)
@ -3366,12 +3372,13 @@ if(SDL_STATIC)
target_compile_definitions(SDL2-static PRIVATE SDL_STATIC_LIB) target_compile_definitions(SDL2-static PRIVATE SDL_STATIC_LIB)
# TODO: Win32 platforms keep the same suffix .lib for import and static # TODO: Win32 platforms keep the same suffix .lib for import and static
# libraries - do we need to consider this? # libraries - do we need to consider this?
target_link_libraries(SDL2-static PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS}) target_link_libraries(SDL2-static PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS} ${CMAKE_DEPENDS})
target_include_directories(SDL2-static PUBLIC target_include_directories(SDL2-static PUBLIC
"$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include>" "$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>" "$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL2>") "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL2>"
)
# This picks up all the compiler options and such we've accumulated up to here. # This picks up all the compiler options and such we've accumulated up to here.
target_link_libraries(SDL2-static PRIVATE $<BUILD_INTERFACE:sdl-build-options>) target_link_libraries(SDL2-static PRIVATE $<BUILD_INTERFACE:sdl-build-options>)
if(NOT ANDROID) if(NOT ANDROID)
@ -3505,6 +3512,7 @@ if(NOT SDL2_DISABLE_INSTALL)
FILES FILES
${CMAKE_CURRENT_BINARY_DIR}/SDL2Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/SDL2Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/SDL2ConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/SDL2ConfigVersion.cmake
${SDL2_SOURCE_DIR}/cmake/sdlfind.cmake
DESTINATION "${SDL_INSTALL_CMAKEDIR}" DESTINATION "${SDL_INSTALL_CMAKEDIR}"
COMPONENT Devel COMPONENT Devel
) )

View File

@ -30,6 +30,18 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL2testTargets.cmake")
set(SDL2_SDL2test_FOUND TRUE) set(SDL2_SDL2test_FOUND TRUE)
endif() endif()
include("${CMAKE_CURRENT_LIST_DIR}/sdlfind.cmake")
set(SDL_ALSA @SDL_ALSA@)
set(SDL_ALSA_SHARED @SDL_ALSA_SHARED@)
if(SDL_ALSA AND NOT SDL_ALSA_SHARED AND TARGET SDL2::SDL2-static)
sdlFindALSA()
endif()
unset(SDL_ALSA)
unset(SDL_ALSA_SHARED)
check_required_components(SDL2) check_required_components(SDL2)
# Create SDL2::SDL2 alias for static-only builds # Create SDL2::SDL2 alias for static-only builds

View File

@ -29,7 +29,7 @@ ENDMACRO()
# Message Output # Message Output
macro(MESSAGE_WARN _TEXT) macro(MESSAGE_WARN _TEXT)
message(STATUS "*** WARNING: ${_TEXT}") message(WARNING "${_TEXT}")
endmacro() endmacro()
macro(MESSAGE_ERROR _TEXT) macro(MESSAGE_ERROR _TEXT)
@ -64,7 +64,7 @@ macro(MESSAGE_TESTED_OPTION _NAME)
message(STATUS " ${_NAME}${_PAD}(Wanted: ${_REQVALUE}): ${HAVE_${_STRIPPEDNAME}}") message(STATUS " ${_NAME}${_PAD}(Wanted: ${_REQVALUE}): ${HAVE_${_STRIPPEDNAME}}")
endmacro() endmacro()
macro(LISTTOSTR _LIST _OUTPUT) function(LISTTOSTR _LIST _OUTPUT)
if(${ARGC} EQUAL 3) if(${ARGC} EQUAL 3)
# prefix for each element # prefix for each element
set(_LPREFIX ${ARGV2}) set(_LPREFIX ${ARGV2})
@ -73,10 +73,12 @@ macro(LISTTOSTR _LIST _OUTPUT)
endif() endif()
# Do not use string(REPLACE ";" " ") here to avoid messing up list # Do not use string(REPLACE ";" " ") here to avoid messing up list
# entries # entries
set(res)
foreach(_ITEM ${${_LIST}}) foreach(_ITEM ${${_LIST}})
set(${_OUTPUT} "${${_OUTPUT}} ${_LPREFIX}${_ITEM}") set(res "${res} ${_LPREFIX}${_ITEM}")
endforeach() endforeach()
endmacro() set(${_OUTPUT} "${res}" PARENT_SCOPE)
endfunction()
macro(LISTTOSTRREV _LIST _OUTPUT) macro(LISTTOSTRREV _LIST _OUTPUT)
if(${ARGC} EQUAL 3) if(${ARGC} EQUAL 3)

View File

@ -1,4 +1,5 @@
include(CMakeParseArguments) include(CMakeParseArguments)
include(${SDL2_SOURCE_DIR}/cmake/sdlfind.cmake)
macro(FindLibraryAndSONAME _LIB) macro(FindLibraryAndSONAME _LIB)
cmake_parse_arguments(FLAS "" "" "LIBDIRS" ${ARGN}) cmake_parse_arguments(FLAS "" "" "LIBDIRS" ${ARGN})
@ -6,6 +7,13 @@ macro(FindLibraryAndSONAME _LIB)
string(REGEX REPLACE "\\-" "_" _LNAME "${_UPPERLNAME}") string(REGEX REPLACE "\\-" "_" _LNAME "${_UPPERLNAME}")
find_library(${_LNAME}_LIB ${_LIB} PATHS ${FLAS_LIBDIRS}) find_library(${_LNAME}_LIB ${_LIB} PATHS ${FLAS_LIBDIRS})
if(${_LNAME}_LIB MATCHES ".*\\${CMAKE_SHARED_LIBRARY_SUFFIX}.*" AND NOT ${_LNAME}_LIB MATCHES ".*\\${CMAKE_STATIC_LIBRARY_SUFFIX}.*")
set(${_LNAME}_SHARED TRUE)
else()
set(${_LNAME}_SHARED FALSE)
endif()
if(${_LNAME}_LIB) if(${_LNAME}_LIB)
# reduce the library name for shared linking # reduce the library name for shared linking
@ -83,26 +91,35 @@ endmacro()
# - HAVE_SDL_LOADSO opt # - HAVE_SDL_LOADSO opt
macro(CheckALSA) macro(CheckALSA)
if(SDL_ALSA) if(SDL_ALSA)
CHECK_INCLUDE_FILE(alsa/asoundlib.h HAVE_ASOUNDLIB_H) sdlFindALSA()
if(HAVE_ASOUNDLIB_H) if(ALSA_FOUND)
CHECK_LIBRARY_EXISTS(asound snd_pcm_recover "" HAVE_LIBASOUND) file(GLOB ALSA_SOURCES "${SDL2_SOURCE_DIR}/src/audio/alsa/*.c")
endif()
if(HAVE_LIBASOUND)
set(HAVE_ALSA TRUE)
file(GLOB ALSA_SOURCES ${SDL2_SOURCE_DIR}/src/audio/alsa/*.c)
list(APPEND SOURCE_FILES ${ALSA_SOURCES}) list(APPEND SOURCE_FILES ${ALSA_SOURCES})
set(SDL_AUDIO_DRIVER_ALSA 1) set(SDL_AUDIO_DRIVER_ALSA 1)
if(SDL_ALSA_SHARED AND NOT HAVE_SDL_LOADSO) set(HAVE_ALSA TRUE)
message_warn("You must have SDL_LoadObject() support for dynamic ALSA loading") set(HAVE_ALSA_SHARED FALSE)
endif() if(SDL_ALSA_SHARED)
if(HAVE_SDL_LOADSO)
FindLibraryAndSONAME("asound") FindLibraryAndSONAME("asound")
if(SDL_ALSA_SHARED AND ASOUND_LIB AND HAVE_SDL_LOADSO) if(ASOUND_LIB AND ASOUND_SHARED)
target_include_directories(sdl-build-options INTERFACE $<TARGET_PROPERTY:ALSA::ALSA,INTERFACE_INCLUDE_DIRECTORIES>)
set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${ASOUND_LIB_SONAME}\"") set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${ASOUND_LIB_SONAME}\"")
set(HAVE_ALSA_SHARED TRUE) set(HAVE_ALSA_SHARED TRUE)
else() else()
list(APPEND EXTRA_LIBS asound) message(WARNING "Unable to find asound shared object")
endif()
else()
message(WARNING "You must have SDL_LoadObject() support for dynamic ALSA loading")
endif()
endif()
if(NOT HAVE_ALSA_SHARED)
list(APPEND CMAKE_DEPENDS ALSA::ALSA)
list(APPEND PKGCONFIG_DEPENDS alsa)
endif() endif()
set(HAVE_SDL_AUDIO TRUE) set(HAVE_SDL_AUDIO TRUE)
else()
set(HAVE_ALSA FALSE)
message(WARNING "Unable to found the alsa development library")
endif() endif()
endif() endif()
endmacro() endmacro()

9
cmake/sdlfind.cmake Normal file
View File

@ -0,0 +1,9 @@
macro(sdlFindALSA)
find_package(ALSA MODULE)
if(ALSA_FOUND AND (NOT TARGET ALSA::ALSA) )
add_Library(ALSA::ALSA UNKNOWN IMPORTED)
set_property(TARGET ALSA::ALSA PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${ALSA_INCLUDE_DIRS})
set_property(TARGET ALSA::ALSA APPEND PROPERTY IMPORTED_LOCATION ${ALSA_LIBRARY})
endif()
endmacro()

3
configure vendored
View File

@ -677,6 +677,7 @@ ENABLE_STATIC_TRUE
ENABLE_SHARED_FALSE ENABLE_SHARED_FALSE
ENABLE_SHARED_TRUE ENABLE_SHARED_TRUE
PKGCONFIG_LIBS_PRIV PKGCONFIG_LIBS_PRIV
PKGCONFIG_DEPENDS
SDL_RLD_FLAGS SDL_RLD_FLAGS
SDL_STATIC_LIBS SDL_STATIC_LIBS
SDL_LIBS SDL_LIBS
@ -29873,6 +29874,8 @@ done
PKGCONFIG_DEPENDS=""
if test x$enable_shared = xyes; then if test x$enable_shared = xyes; then
PKGCONFIG_LIBS_PRIV=" PKGCONFIG_LIBS_PRIV="
Libs.private:" Libs.private:"

View File

@ -4861,6 +4861,8 @@ AC_SUBST(SDL_CFLAGS)
AC_SUBST(SDL_LIBS) AC_SUBST(SDL_LIBS)
AC_SUBST(SDL_STATIC_LIBS) AC_SUBST(SDL_STATIC_LIBS)
AC_SUBST(SDL_RLD_FLAGS) AC_SUBST(SDL_RLD_FLAGS)
PKGCONFIG_DEPENDS=""
AC_SUBST(PKGCONFIG_DEPENDS)
if test x$enable_shared = xyes; then if test x$enable_shared = xyes; then
PKGCONFIG_LIBS_PRIV=" PKGCONFIG_LIBS_PRIV="
Libs.private:" Libs.private:"

View File

@ -8,7 +8,7 @@ includedir=@includedir@
Name: sdl2 Name: sdl2
Description: Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. Description: Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer.
Version: @SDL_VERSION@ Version: @SDL_VERSION@
Requires: Requires.private: @PKGCONFIG_DEPENDS@
Conflicts: Conflicts:
Libs: -L${libdir} @SDL_RLD_FLAGS@ @SDL_LIBS@ @PKGCONFIG_LIBS_PRIV@ @SDL_STATIC_LIBS@ Libs: -L${libdir} @SDL_RLD_FLAGS@ @SDL_LIBS@ @PKGCONFIG_LIBS_PRIV@ @SDL_STATIC_LIBS@
Cflags: -I${includedir} -I${includedir}/SDL2 @SDL_CFLAGS@ Cflags: -I${includedir} -I${includedir}/SDL2 @SDL_CFLAGS@