|
| 1 | +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 | +# file Copyright.txt or https://cmake.org/licensing for details. |
| 3 | + |
| 4 | +#[=======================================================================[.rst: |
| 5 | +FindOpenAL |
| 6 | +---------- |
| 7 | +
|
| 8 | +Finds Open Audio Library (OpenAL). |
| 9 | +
|
| 10 | +Projects using this module should use ``#include "al.h"`` to include the OpenAL |
| 11 | +header file, **not** ``#include <AL/al.h>``. The reason for this is that the |
| 12 | +latter is not entirely portable. Windows/Creative Labs does not by default put |
| 13 | +their headers in ``AL/`` and macOS uses the convention ``<OpenAL/al.h>``. |
| 14 | +
|
| 15 | +Hints |
| 16 | +^^^^^ |
| 17 | +
|
| 18 | +Environment variable ``$OPENALDIR`` can be used to set the prefix of OpenAL |
| 19 | +installation to be found. |
| 20 | +
|
| 21 | +By default on macOS, system framework is search first. In other words, |
| 22 | +OpenAL is searched in the following order: |
| 23 | +
|
| 24 | +1. System framework: ``/System/Library/Frameworks``, whose priority can be |
| 25 | + changed via setting the :variable:`CMAKE_FIND_FRAMEWORK` variable. |
| 26 | +2. Environment variable ``$OPENALDIR``. |
| 27 | +3. System paths. |
| 28 | +4. User-compiled framework: ``~/Library/Frameworks``. |
| 29 | +5. Manually compiled framework: ``/Library/Frameworks``. |
| 30 | +6. Add-on package: ``/opt``. |
| 31 | +
|
| 32 | +IMPORTED Targets |
| 33 | +^^^^^^^^^^^^^^^^ |
| 34 | +
|
| 35 | +.. versionadded:: 3.25 |
| 36 | +
|
| 37 | +This module defines the :prop_tgt:`IMPORTED` target: |
| 38 | +
|
| 39 | +``OpenAL::OpenAL`` |
| 40 | + The OpenAL library, if found. |
| 41 | +
|
| 42 | +Result Variables |
| 43 | +^^^^^^^^^^^^^^^^ |
| 44 | +
|
| 45 | +This module defines the following variables: |
| 46 | +
|
| 47 | +``OPENAL_FOUND`` |
| 48 | + If false, do not try to link to OpenAL |
| 49 | +``OPENAL_INCLUDE_DIR`` |
| 50 | + OpenAL include directory |
| 51 | +``OPENAL_LIBRARY`` |
| 52 | + Path to the OpenAL library |
| 53 | +``OPENAL_VERSION_STRING`` |
| 54 | + Human-readable string containing the version of OpenAL |
| 55 | +#]=======================================================================] |
| 56 | + |
| 57 | +# For Windows, Creative Labs seems to have added a registry key for their |
| 58 | +# OpenAL 1.1 installer. I have added that key to the list of search paths, |
| 59 | +# however, the key looks like it could be a little fragile depending on |
| 60 | +# if they decide to change the 1.00.0000 number for bug fix releases. |
| 61 | +# Also, they seem to have laid down groundwork for multiple library platforms |
| 62 | +# which puts the library in an extra subdirectory. Currently there is only |
| 63 | +# Win32 and I have hardcoded that here. This may need to be adjusted as |
| 64 | +# platforms are introduced. |
| 65 | +# The OpenAL 1.0 installer doesn't seem to have a useful key I can use. |
| 66 | +# I do not know if the Nvidia OpenAL SDK has a registry key. |
| 67 | + |
| 68 | +find_path(OPENAL_INCLUDE_DIR al.h |
| 69 | + HINTS |
| 70 | + ENV OPENALDIR |
| 71 | + PATHS |
| 72 | + ~/Library/Frameworks |
| 73 | + /Library/Frameworks |
| 74 | + /opt |
| 75 | + [HKEY_LOCAL_MACHINE\\SOFTWARE\\Creative\ Labs\\OpenAL\ 1.1\ Software\ Development\ Kit\\1.00.0000;InstallDir] |
| 76 | + PATH_SUFFIXES include/AL include/OpenAL include AL OpenAL |
| 77 | + ) |
| 78 | + |
| 79 | +find_library(OPENAL_LIBRARY |
| 80 | + NAMES OpenAL al openal OpenAL32 openal32 |
| 81 | + HINTS |
| 82 | + ENV OPENALDIR |
| 83 | + PATHS |
| 84 | + ~/Library/Frameworks |
| 85 | + /Library/Frameworks |
| 86 | + /opt |
| 87 | + [HKEY_LOCAL_MACHINE\\SOFTWARE\\Creative\ Labs\\OpenAL\ 1.1\ Software\ Development\ Kit\\1.00.0000;InstallDir] |
| 88 | + PATH_SUFFIXES libx32 lib64 lib libs64 libs |
| 89 | + ) |
| 90 | + |
| 91 | +include(FindPackageHandleStandardArgs) |
| 92 | +find_package_handle_standard_args( |
| 93 | + OpenAL |
| 94 | + REQUIRED_VARS OPENAL_LIBRARY OPENAL_INCLUDE_DIR |
| 95 | + VERSION_VAR OPENAL_VERSION_STRING |
| 96 | + ) |
| 97 | + |
| 98 | +mark_as_advanced(OPENAL_LIBRARY OPENAL_INCLUDE_DIR) |
| 99 | + |
| 100 | +if(OPENAL_INCLUDE_DIR AND OPENAL_LIBRARY) |
| 101 | + if(NOT TARGET OpenAL::OpenAL) |
| 102 | + if(EXISTS "${OPENAL_LIBRARY}") |
| 103 | + if(WIN32) |
| 104 | + get_filename_component(OPENAL_PATH ${OPENAL_LIBRARY} DIRECTORY) |
| 105 | + add_library(OpenAL::OpenAL SHARED IMPORTED) |
| 106 | + if (ARCH_x86) |
| 107 | + set(DLL_PATH "${OPENAL_PATH}/../../bin/x86/openal32.dll") |
| 108 | + elseif(ARCH_X64) |
| 109 | + set(DLL_PATH "${OPENAL_PATH}/../../bin/x64/openal32.dll") |
| 110 | + else() |
| 111 | + set(DLL_PATH "${OPENAL_PATH}/../../bin/ARM64/openal32.dll") |
| 112 | + endif() |
| 113 | + set_target_properties(OpenAL::OpenAL PROPERTIES |
| 114 | + IMPORTED_LOCATION "${DLL_PATH}" |
| 115 | + IMPORTED_IMPLIB "${OPENAL_LIBRARY}") |
| 116 | + elseif(APPLE) |
| 117 | + if(IS_DIRECTORY "${OPENAL_LIBRARY}") |
| 118 | + # Framework bundle directory: search for the binary inside it. |
| 119 | + find_file(OPENAL_FULL_PATH OpenAL OpenAL.tbd PATHS "${OPENAL_LIBRARY}" REQUIRED) |
| 120 | + add_library(OpenAL::OpenAL SHARED IMPORTED) |
| 121 | + set_target_properties(OpenAL::OpenAL PROPERTIES |
| 122 | + IMPORTED_LOCATION "${OPENAL_FULL_PATH}") |
| 123 | + else() |
| 124 | + # Plain dylib (e.g. Homebrew openal-soft on macOS 15 where |
| 125 | + # OpenAL.framework is no longer present in the system). |
| 126 | + add_library(OpenAL::OpenAL SHARED IMPORTED) |
| 127 | + set_target_properties(OpenAL::OpenAL PROPERTIES |
| 128 | + IMPORTED_LOCATION "${OPENAL_LIBRARY}") |
| 129 | + endif() |
| 130 | + else() |
| 131 | + add_library(OpenAL::OpenAL UNKNOWN IMPORTED) |
| 132 | + set_target_properties(OpenAL::OpenAL PROPERTIES |
| 133 | + IMPORTED_LOCATION "${OPENAL_LIBRARY}") |
| 134 | + endif() |
| 135 | + else() |
| 136 | + add_library(OpenAL::OpenAL INTERFACE IMPORTED) |
| 137 | + set_target_properties(OpenAL::OpenAL PROPERTIES |
| 138 | + IMPORTED_LIBNAME "${OPENAL_LIBRARY}") |
| 139 | + endif() |
| 140 | + set_target_properties(OpenAL::OpenAL PROPERTIES |
| 141 | + INTERFACE_INCLUDE_DIRECTORIES "${OPENAL_INCLUDE_DIR}") |
| 142 | + endif() |
| 143 | +endif() |
0 commit comments