cmake: use FindALSA.cmake to find ALSA libraries

Co-authored-by: nfarid <54642193+nfarid@users.noreply.github.com>
This commit is contained in:
Anonymous Maarten 2022-12-12 00:18:06 +01:00
parent 053ce39d67
commit 9eb4d1f020
7 changed files with 74 additions and 25 deletions

6
.gitignore vendored
View File

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

View File

@ -18,6 +18,9 @@ endif()
set(EXTRA_LIBS)
set(EXTRA_LDFLAGS)
set(CMAKE_DEPENDS)
set(PKGCONFIG_DEPENDS)
# 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
# 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)
if(WINDOWS_STORE)
cmake_minimum_required(VERSION 3.11.0)
target_compile_definitions(sdl-build-options INTERFACE "-DSDL_BUILDING_WINRT=1")
target_compile_options(sdl-build-options INTERFACE "-ZW")
endif()
@ -3051,8 +3053,7 @@ if (SDL_ASAN)
endif()
endif()
if(SDL_CCACHE)
cmake_minimum_required(VERSION 3.4)
if(SDL_CCACHE AND NOT CMAKE_VERSION VERSION_LESS 3.4)
find_program(CCACHE_BINARY ccache)
if(CCACHE_BINARY)
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_BINARY})
@ -3062,6 +3063,8 @@ if(SDL_CCACHE)
else()
set(HAVE_CCACHE OFF)
endif()
else()
set(HAVE_CCACHE OFF)
endif()
if(SDL_TESTS)
@ -3222,7 +3225,7 @@ if(SDL_SHARED)
set_target_properties(SDL3 PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
endif()
# FIXME: if CMAKE_VERSION >= 3.13, use target_link_options for EXTRA_LDFLAGS
target_link_libraries(SDL3 PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS} ${EXTRA_LDFLAGS_BUILD})
target_link_libraries(SDL3 PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS} ${EXTRA_LDFLAGS_BUILD} ${CMAKE_DEPENDS})
target_include_directories(SDL3
PRIVATE
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>"
@ -3257,7 +3260,7 @@ if(SDL_STATIC)
target_compile_definitions(SDL3-static PRIVATE SDL_STATIC_LIB)
# TODO: Win32 platforms keep the same suffix .lib for import and static
# libraries - do we need to consider this?
target_link_libraries(SDL3-static PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
target_link_libraries(SDL3-static PRIVATE ${EXTRA_LIBS} ${EXTRA_LDFLAGS} ${CMAKE_DEPENDS})
target_include_directories(SDL3-static
PRIVATE
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>"
@ -3402,6 +3405,7 @@ if(NOT SDL3_DISABLE_INSTALL)
FILES
${CMAKE_CURRENT_BINARY_DIR}/SDL3Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/SDL3ConfigVersion.cmake
${SDL3_SOURCE_DIR}/cmake/sdlfind.cmake
DESTINATION "${SDL_INSTALL_CMAKEDIR}"
COMPONENT Devel
)

View File

@ -30,6 +30,16 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3testTargets.cmake")
set(SDL3_SDL3_test_FOUND TRUE)
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(SDL3)
# Create SDL3::SDL3 alias for static-only builds

View File

@ -29,7 +29,7 @@ ENDMACRO()
# Message Output
macro(MESSAGE_WARN _TEXT)
message(STATUS "*** WARNING: ${_TEXT}")
message(WARNING "${_TEXT}")
endmacro()
macro(MESSAGE_ERROR _TEXT)
@ -78,7 +78,7 @@ macro(LISTTOSTR _LIST _OUTPUT)
endforeach()
endmacro()
macro(LISTTOSTRREV _LIST _OUTPUT)
function(LISTTOSTRREV _LIST _OUTPUT)
if(${ARGC} EQUAL 3)
# prefix for each element
set(_LPREFIX ${ARGV2})
@ -87,10 +87,12 @@ macro(LISTTOSTRREV _LIST _OUTPUT)
endif()
# Do not use string(REPLACE ";" " ") here to avoid messing up list
# entries
set(res)
foreach(_ITEM ${${_LIST}})
set(${_OUTPUT} "${_LPREFIX}${_ITEM} ${${_OUTPUT}}")
set(res "${res} ${_LPREFIX}${_ITEM}")
endforeach()
endmacro()
set($_OUTPUT} "${res}" PARENT_SCOPE)
endfunction()
if(CMAKE_VERSION VERSION_LESS 3.16.0 OR SDL3_SUBPROJECT)
# - CMake versions <3.16 do not support the OBJC language

View File

@ -1,4 +1,6 @@
include(CMakeParseArguments)
include(${SDL3_SOURCE_DIR}/cmake/sdlfind.cmake)
macro(FindLibraryAndSONAME _LIB)
cmake_parse_arguments(FLAS "" "" "LIBDIRS" ${ARGN})
@ -6,6 +8,13 @@ macro(FindLibraryAndSONAME _LIB)
string(REGEX REPLACE "\\-" "_" _LNAME "${_UPPERLNAME}")
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)
# reduce the library name for shared linking
@ -83,27 +92,36 @@ endmacro()
# - HAVE_SDL_LOADSO opt
macro(CheckALSA)
if(SDL_ALSA)
CHECK_INCLUDE_FILE(alsa/asoundlib.h HAVE_ASOUNDLIB_H)
if(HAVE_ASOUNDLIB_H)
CHECK_LIBRARY_EXISTS(asound snd_pcm_recover "" HAVE_LIBASOUND)
endif()
if(HAVE_LIBASOUND)
set(HAVE_ALSA TRUE)
file(GLOB ALSA_SOURCES ${SDL3_SOURCE_DIR}/src/audio/alsa/*.c)
sdlFindALSA()
if(ALSA_FOUND)
file(GLOB ALSA_SOURCES "${SDL3_SOURCE_DIR}/src/audio/alsa/*.c")
list(APPEND SOURCE_FILES ${ALSA_SOURCES})
set(SDL_AUDIO_DRIVER_ALSA 1)
if(SDL_ALSA_SHARED AND NOT HAVE_SDL_LOADSO)
message_warn("You must have SDL_LoadObject() support for dynamic ALSA loading")
set(HAVE_ALSA TRUE)
set(HAVE_ALSA_SHARED FALSE)
if(SDL_ALSA_SHARED)
if(HAVE_SDL_LOADSO)
FindLibraryAndSONAME("asound")
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(HAVE_ALSA_SHARED TRUE)
else()
message(WARNING "Unable to find asound shared object")
endif()
else()
message(WARNING "You must have SDL_LoadObject() support for dynamic ALSA loading")
endif()
endif()
FindLibraryAndSONAME("asound")
if(SDL_ALSA_SHARED AND ASOUND_LIB AND HAVE_SDL_LOADSO)
set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${ASOUND_LIB_SONAME}\"")
set(HAVE_ALSA_SHARED TRUE)
else()
list(APPEND EXTRA_LIBS asound)
if(NOT HAVE_ALSA_SHARED)
list(APPEND CMAKE_DEPENDS ALSA::ALSA)
list(APPEND PKGCONFIG_DEPENDS alsa)
endif()
set(HAVE_SDL_AUDIO TRUE)
endif()
else()
set(HAVE_ALSA FALSE)
message(WARNING "Unable to find the alsa development library")
endif()
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()

View File

@ -8,7 +8,7 @@ includedir=@includedir@
Name: sdl3
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@
Requires:
Requires.private: @PKGCONFIG_DEPENDS@
Conflicts:
Libs: -L${libdir} @SDL_RLD_FLAGS@ @SDL_LIBS@ @PKGCONFIG_LIBS_PRIV@ @SDL_STATIC_LIBS@
Cflags: -I${includedir} @SDL_CFLAGS@