Skip to content

Commit

Permalink
[CMake/OpenAL] OpenAL is now a dedicated library target
Browse files Browse the repository at this point in the history
- It is a shared imported library under Windows and a simple interface one on other platforms
  - This distinction is because it is actually from a file under Windows, while it should be found system-wide (installed from a package manager) under Linux & macOS
  • Loading branch information
Razakhel committed Mar 9, 2025
1 parent 541e40a commit 6211272
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,8 @@ if (RAZ_USE_AUDIO)
endif ()

if (NOT RAZ_USE_EMSCRIPTEN)
# Emscripten manages the includes & library on its own; adding the include directory messes up predefined types
target_include_directories(RaZ SYSTEM PRIVATE ${OPENAL_INCLUDE_DIR})
target_link_libraries(RaZ PRIVATE ${OPENAL_LIBRARY})
# Emscripten manages OpenAL on its own; linking to it messes up predefined types
target_link_libraries(RaZ PRIVATE OpenAL::OpenAL)
endif ()
target_compile_definitions(RaZ PUBLIC RAZ_USE_AUDIO)

Expand Down
44 changes: 38 additions & 6 deletions cmake/FindOpenAL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,38 @@ if (WIN32 OR CYGWIN)
OPENAL_LIBRARY

NAMES
OpenAL al openal OpenAL32
OpenAL
al
openal
OpenAL32
HINTS
ENV OPENALDIR
ENV OPENAL_SDK_PATH
PATHS
${OPENAL_SEARCH_PATHS}
PATH_SUFFIXES
lib64 lib libs64 libs libs/Win64
lib64
lib
libs64
libs
libs/Win64
)

if (WIN32)
# Under Windows, finding the DLL may be useful in some cases
find_file(
OPENAL_DLL

NAMES
soft_oal.dll libopenal-1.dll
soft_oal.dll
libopenal-1.dll
HINTS
ENV OPENALDIR
ENV OPENAL_SDK_PATH
PATHS
${OPENAL_SEARCH_PATHS}
PATH_SUFFIXES
bin bin/Win64
bin
bin/Win64
)
endif ()
elseif (APPLE)
Expand Down Expand Up @@ -106,7 +114,11 @@ find_path(
PATHS
${OPENAL_SEARCH_PATHS}
PATH_SUFFIXES
include/AL include/OpenAL include AL OpenAL
include/AL
include/OpenAL
include
AL
OpenAL
)

if (OPENAL_LIBRARY AND OPENAL_INCLUDE_DIR)
Expand All @@ -118,4 +130,24 @@ if (OPENAL_LIBRARY AND OPENAL_INCLUDE_DIR)
if (WIN32)
message(STATUS " - DLL: ${OPENAL_DLL}")
endif ()

if (WIN32)
add_library(OpenAL SHARED IMPORTED)
set_target_properties(OpenAL PROPERTIES IMPORTED_IMPLIB ${OPENAL_LIBRARY})

# The DLL isn't set as the found file doesn't have the expected name; as there doesn't seem to be any easier way, it is copied manually later
# Executables made through MSYS2 need libopenal-1.dll available, while in other cases they require OpenAL32.dll. The one found is named soft_oal.dll
# If later it eventually is loaded manually from the code, or there's a way to easily change the name of a required DLL, it should be reenabled
#if (WIN32)
# set_target_properties(OpenAL PROPERTIES IMPORTED_LOCATION ${OPENAL_DLL})
#endif ()
else ()
# Linux & macOS don't reference a file but the system's library; it can't use an imported library target
add_library(OpenAL INTERFACE)
target_link_libraries(OpenAL INTERFACE ${OPENAL_LIBRARY})
endif ()

target_include_directories(OpenAL SYSTEM INTERFACE ${OPENAL_INCLUDE_DIR})

add_library(OpenAL::OpenAL ALIAS OpenAL)
endif ()

0 comments on commit 6211272

Please sign in to comment.