Set the C/C++ standard version properties manually
[openal-soft.git] / CMakeLists.txt
blob3ca29401dd1b8a923ad790cb060578d6729dd3dc
1 # CMake build file list for OpenAL
3 cmake_minimum_required(VERSION 3.0.2)
5 if(APPLE)
6     # The workaround for try_compile failing with code signing
7     # since cmake-3.18.2, not required
8     set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
9         ${CMAKE_TRY_COMPILE_PLATFORM_VARIABLES}
10         "CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED"
11         "CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED")
12     set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED NO)
13     set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED NO)
14 endif()
16 if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
17     # Fix compile failure with armv7 deployment target >= 11.0, xcode clang
18     # will report:
19     # error: invalid iOS deployment version '--target=armv7-apple-ios13.6',
20     # iOS 10 is the maximum deployment target for 32-bit targets
21     # If CMAKE_OSX_DEPLOYMENT_TARGET is not defined, cmake will choose latest
22     # deployment target
23     if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ".*armv7.*")
24         if(NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET
25             OR NOT CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS "11.0")
26             message(STATUS "Forcing iOS deployment target to 10.0 for armv7")
27             set(CMAKE_OSX_DEPLOYMENT_TARGET "10.0" CACHE STRING "Minimum OS X deployment version"
28                 FORCE)
29         endif()
30     endif()
31 endif()
33 set(CMAKE_C_VISIBILITY_PRESET hidden)
34 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
36 if(COMMAND CMAKE_POLICY)
37     cmake_policy(SET CMP0003 NEW)
38     cmake_policy(SET CMP0005 NEW)
39     if(POLICY CMP0020)
40         cmake_policy(SET CMP0020 NEW)
41     endif(POLICY CMP0020)
42     if(POLICY CMP0042)
43         cmake_policy(SET CMP0042 NEW)
44     endif(POLICY CMP0042)
45     if(POLICY CMP0054)
46         cmake_policy(SET CMP0054 NEW)
47     endif(POLICY CMP0054)
48     if(POLICY CMP0058)
49         cmake_policy(SET CMP0058 NEW)
50     endif(POLICY CMP0058)
51     if(POLICY CMP0063)
52         cmake_policy(SET CMP0063 NEW)
53     endif(POLICY CMP0063)
54     if(POLICY CMP0075)
55         cmake_policy(SET CMP0075 NEW)
56     endif(POLICY CMP0075)
57     if(POLICY CMP0092)
58         cmake_policy(SET CMP0092 NEW)
59     endif(POLICY CMP0092)
60     if(POLICY CMP0117)
61         cmake_policy(SET CMP0117 NEW)
62     endif(POLICY CMP0117)
63 endif(COMMAND CMAKE_POLICY)
65 project(OpenAL)
67 if(NOT CMAKE_BUILD_TYPE)
68     set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
69         "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
70         FORCE)
71 endif()
72 if(NOT CMAKE_DEBUG_POSTFIX)
73     set(CMAKE_DEBUG_POSTFIX "" CACHE STRING
74         "Library postfix for debug builds. Normally left blank."
75         FORCE)
76 endif()
78 set(DEFAULT_TARGET_PROPS
79     # Require C++14.
80     CXX_STANDARD 14
81     CXX_STANDARD_REQUIRED TRUE
82     # Prefer C11, but support C99 and earlier when possible.
83     C_STANDARD 11)
85 set(CMAKE_MODULE_PATH "${OpenAL_SOURCE_DIR}/cmake")
87 include(CheckFunctionExists)
88 include(CheckLibraryExists)
89 include(CheckIncludeFile)
90 include(CheckIncludeFiles)
91 include(CheckSymbolExists)
92 include(CheckCCompilerFlag)
93 include(CheckCXXCompilerFlag)
94 include(CheckCSourceCompiles)
95 include(CheckCXXSourceCompiles)
96 include(CheckStructHasMember)
97 include(CMakePackageConfigHelpers)
98 include(GNUInstallDirs)
100 find_package(PkgConfig)
103 option(ALSOFT_DLOPEN  "Check for the dlopen API for loading optional libs"  ON)
105 option(ALSOFT_WERROR  "Treat compile warnings as errors"      OFF)
107 option(ALSOFT_UTILS "Build utility programs"  ON)
108 option(ALSOFT_NO_CONFIG_UTIL "Disable building the alsoft-config utility" OFF)
110 option(ALSOFT_EXAMPLES  "Build example programs"  ON)
112 option(ALSOFT_INSTALL "Install main library" ON)
113 option(ALSOFT_INSTALL_CONFIG "Install alsoft.conf sample configuration file" ON)
114 option(ALSOFT_INSTALL_HRTF_DATA "Install HRTF data files" ON)
115 option(ALSOFT_INSTALL_AMBDEC_PRESETS "Install AmbDec preset files" ON)
116 option(ALSOFT_INSTALL_EXAMPLES "Install example programs (alplay, alstream, ...)" ON)
117 option(ALSOFT_INSTALL_UTILS "Install utility programs (openal-info, alsoft-config, ...)" ON)
118 option(ALSOFT_UPDATE_BUILD_VERSION "Update git build version info" ON)
120 option(ALSOFT_EAX "Enable legacy EAX extensions" ${WIN32})
122 option(ALSOFT_SEARCH_INSTALL_DATADIR "Search the installation data directory" OFF)
123 if(ALSOFT_SEARCH_INSTALL_DATADIR)
124     set(ALSOFT_INSTALL_DATADIR ${CMAKE_INSTALL_FULL_DATADIR})
125 endif()
127 if(DEFINED SHARE_INSTALL_DIR)
128     message(WARNING "SHARE_INSTALL_DIR is deprecated.  Use the variables provided by the GNUInstallDirs module instead")
129     set(CMAKE_INSTALL_DATADIR "${SHARE_INSTALL_DIR}")
130 endif()
132 if(DEFINED LIB_SUFFIX)
133     message(WARNING "LIB_SUFFIX is deprecated.  Use the variables provided by the GNUInstallDirs module instead")
134 endif()
135 if(DEFINED ALSOFT_CONFIG)
136     message(WARNING "ALSOFT_CONFIG is deprecated. Use ALSOFT_INSTALL_CONFIG instead")
137 endif()
138 if(DEFINED ALSOFT_HRTF_DEFS)
139     message(WARNING "ALSOFT_HRTF_DEFS is deprecated. Use ALSOFT_INSTALL_HRTF_DATA instead")
140 endif()
141 if(DEFINED ALSOFT_AMBDEC_PRESETS)
142     message(WARNING "ALSOFT_AMBDEC_PRESETS is deprecated. Use ALSOFT_INSTALL_AMBDEC_PRESETS instead")
143 endif()
146 set(CPP_DEFS ) # C pre-processor, not C++
147 set(INC_PATHS )
148 set(C_FLAGS )
149 set(LINKER_FLAGS )
150 set(EXTRA_LIBS )
152 if(WIN32)
153     set(CPP_DEFS ${CPP_DEFS} _WIN32 NOMINMAX)
154     if(MINGW)
155         set(CPP_DEFS ${CPP_DEFS} __USE_MINGW_ANSI_STDIO)
156     endif()
158     option(ALSOFT_BUILD_ROUTER  "Build the router (EXPERIMENTAL; creates OpenAL32.dll and soft_oal.dll)"  OFF)
159     if(MINGW)
160         option(ALSOFT_BUILD_IMPORT_LIB "Build an import .lib using dlltool (requires sed)" ON)
161     endif()
162 elseif(APPLE)
163     option(ALSOFT_OSX_FRAMEWORK "Build as macOS framework" OFF)
164 endif()
167 # QNX's gcc do not uses /usr/include and /usr/lib pathes by default
168 if("${CMAKE_C_PLATFORM_ID}" STREQUAL "QNX")
169     set(INC_PATHS ${INC_PATHS} /usr/include)
170     set(LINKER_FLAGS ${LINKER_FLAGS} -L/usr/lib)
171 endif()
173 # When the library is built for static linking, apps should define
174 # AL_LIBTYPE_STATIC when including the AL headers.
175 if(NOT LIBTYPE)
176     set(LIBTYPE SHARED)
177 endif()
179 set(LIB_MAJOR_VERSION "1")
180 set(LIB_MINOR_VERSION "23")
181 set(LIB_REVISION "0")
182 set(LIB_VERSION "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_REVISION}")
183 set(LIB_VERSION_NUM ${LIB_MAJOR_VERSION},${LIB_MINOR_VERSION},${LIB_REVISION},0)
185 set(EXPORT_DECL "")
188 if(NOT WIN32)
189     # Check if _POSIX_C_SOURCE and _XOPEN_SOURCE needs to be set for POSIX functions
190     check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN_DEFAULT)
191     if(NOT HAVE_POSIX_MEMALIGN_DEFAULT)
192         set(OLD_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
193         set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600")
194         check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN_POSIX)
195         if(NOT HAVE_POSIX_MEMALIGN_POSIX)
196             set(CMAKE_REQUIRED_FLAGS ${OLD_REQUIRED_FLAGS})
197         else()
198             set(CPP_DEFS ${CPP_DEFS} _POSIX_C_SOURCE=200112L _XOPEN_SOURCE=600)
199         endif()
200     endif()
201     unset(OLD_REQUIRED_FLAGS)
202 endif()
204 # C99 has restrict, but C++ does not, so we can only utilize __restrict.
205 check_cxx_source_compiles("int *__restrict foo;
206 int main() { return 0; }" HAVE___RESTRICT)
207 if(HAVE___RESTRICT)
208     set(CPP_DEFS ${CPP_DEFS} RESTRICT=__restrict)
209 else()
210     set(CPP_DEFS ${CPP_DEFS} "RESTRICT=")
211 endif()
213 # Some systems may need libatomic for atomic functions to work
214 set(OLD_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
215 set(CMAKE_REQUIRED_LIBRARIES ${OLD_REQUIRED_LIBRARIES} atomic)
216 check_cxx_source_compiles("#include <atomic>
217 std::atomic<int> foo{0};
218 int main() { return foo.fetch_add(2); }"
219 HAVE_LIBATOMIC)
220 if(NOT HAVE_LIBATOMIC)
221     set(CMAKE_REQUIRED_LIBRARIES "${OLD_REQUIRED_LIBRARIES}")
222 else()
223     set(EXTRA_LIBS atomic ${EXTRA_LIBS})
224 endif()
225 unset(OLD_REQUIRED_LIBRARIES)
227 if(ANDROID)
228     # Include liblog for Android logging
229     check_library_exists(log __android_log_print "" HAVE_LIBLOG)
230     if(HAVE_LIBLOG)
231         set(EXTRA_LIBS log ${EXTRA_LIBS})
232         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} log)
233     endif()
234 endif()
236 if(MSVC)
237     set(CPP_DEFS ${CPP_DEFS} _CRT_SECURE_NO_WARNINGS)
238     check_cxx_compiler_flag(/permissive- HAVE_PERMISSIVE_SWITCH)
239     if(HAVE_PERMISSIVE_SWITCH)
240         set(C_FLAGS ${C_FLAGS} $<$<COMPILE_LANGUAGE:CXX>:/permissive->)
241     endif()
242     set(C_FLAGS ${C_FLAGS} /W4 /w14640 /wd4065 /wd4127 /wd4268 /wd4324 /wd5030 /wd5051)
244     if(NOT DXSDK_DIR)
245         string(REGEX REPLACE "\\\\" "/" DXSDK_DIR "$ENV{DXSDK_DIR}")
246     else()
247         string(REGEX REPLACE "\\\\" "/" DXSDK_DIR "${DXSDK_DIR}")
248     endif()
249     if(DXSDK_DIR)
250         message(STATUS "Using DirectX SDK directory: ${DXSDK_DIR}")
251     endif()
253     option(FORCE_STATIC_VCRT "Force /MT for static VC runtimes" OFF)
254     if(FORCE_STATIC_VCRT)
255         foreach(flag_var
256                 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
257                 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
258                 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
259                 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
260             if(${flag_var} MATCHES "/MD")
261                 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
262             endif()
263         endforeach(flag_var)
264     endif()
265 else()
266     set(C_FLAGS ${C_FLAGS} -Winline -Wunused -Wall -Wextra -Wshadow -Wconversion -Wcast-align
267         -Wpedantic
268         $<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast -Wnon-virtual-dtor -Woverloaded-virtual>)
270     check_cxx_compiler_flag(-Wno-c++20-attribute-extensions HAVE_WNO_CXX20_ATTR_EXT)
271     if(HAVE_WNO_CXX20_ATTR_EXT)
272         set(C_FLAGS ${C_FLAGS} $<$<COMPILE_LANGUAGE:CXX>:-Wno-c++20-attribute-extensions>)
273     else()
274         check_cxx_compiler_flag(-Wno-c++20-extensions HAVE_WNO_CXX20_EXT)
275         if(HAVE_WNO_CXX20_EXT)
276             set(C_FLAGS ${C_FLAGS} $<$<COMPILE_LANGUAGE:CXX>:-Wno-c++20-extensions>)
277         endif()
278     endif()
280     if(ALSOFT_WERROR)
281         set(C_FLAGS ${C_FLAGS} -Werror)
282     endif()
284     # We want RelWithDebInfo to actually include debug stuff (define _DEBUG
285     # instead of NDEBUG)
286     foreach(flag_var  CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELWITHDEBINFO)
287         if(${flag_var} MATCHES "-DNDEBUG")
288             string(REGEX REPLACE "-DNDEBUG" "-D_DEBUG" ${flag_var} "${${flag_var}}")
289         endif()
290     endforeach()
292     check_c_compiler_flag(-fno-math-errno HAVE_FNO_MATH_ERRNO)
293     if(HAVE_FNO_MATH_ERRNO)
294         set(C_FLAGS ${C_FLAGS} -fno-math-errno)
295     endif()
297     option(ALSOFT_STATIC_LIBGCC "Force -static-libgcc for static GCC runtimes" OFF)
298     if(ALSOFT_STATIC_LIBGCC)
299         set(OLD_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
300         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} -static-libgcc)
301         check_cxx_source_compiles("int main() { }" HAVE_STATIC_LIBGCC_SWITCH)
302         set(CMAKE_REQUIRED_LIBRARIES ${OLD_REQUIRED_LIBRARIES})
303         unset(OLD_REQUIRED_LIBRARIES)
305         if(NOT HAVE_STATIC_LIBGCC_SWITCH)
306             message(FATAL_ERROR "Cannot static link libgcc")
307         endif()
308         set(LINKER_FLAGS ${LINKER_FLAGS} -static-libgcc)
309     endif()
311     option(ALSOFT_STATIC_STDCXX "Static link libstdc++" OFF)
312     if(ALSOFT_STATIC_STDCXX)
313         set(OLD_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
314         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} "-Wl,--push-state,-Bstatic,-lstdc++,--pop-state")
315         check_cxx_source_compiles("int main() { }" HAVE_STATIC_LIBSTDCXX_SWITCH)
316         set(CMAKE_REQUIRED_LIBRARIES ${OLD_REQUIRED_LIBRARIES})
317         unset(OLD_REQUIRED_LIBRARIES)
319         if(NOT HAVE_STATIC_LIBSTDCXX_SWITCH)
320             message(FATAL_ERROR "Cannot static link libstdc++")
321         endif()
322         set(LINKER_FLAGS ${LINKER_FLAGS} "-Wl,--push-state,-Bstatic,-lstdc++,--pop-state")
323     endif()
325     if(WIN32)
326         option(ALSOFT_STATIC_WINPTHREAD "Static link libwinpthread" OFF)
327         if(ALSOFT_STATIC_WINPTHREAD)
328             set(OLD_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
329             set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} "-Wl,--push-state,-Bstatic,-lwinpthread,--pop-state")
330             check_cxx_source_compiles("int main() { }" HAVE_STATIC_LIBWINPTHREAD_SWITCH)
331             set(CMAKE_REQUIRED_LIBRARIES ${OLD_REQUIRED_LIBRARIES})
332             unset(OLD_REQUIRED_LIBRARIES)
334             if(NOT HAVE_STATIC_LIBWINPTHREAD_SWITCH)
335                 message(FATAL_ERROR "Cannot static link libwinpthread")
336             endif()
337             set(LINKER_FLAGS ${LINKER_FLAGS} "-Wl,--push-state,-Bstatic,-lwinpthread,--pop-state")
338         endif()
339     endif()
340 endif()
342 # Set visibility/export options if available
343 if(WIN32)
344     if(NOT LIBTYPE STREQUAL "STATIC")
345         set(EXPORT_DECL "__declspec(dllexport)")
346     endif()
347 else()
348     set(OLD_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
349     # Yes GCC, really don't accept visibility modes you don't support
350     set(CMAKE_REQUIRED_FLAGS "${OLD_REQUIRED_FLAGS} -Wattributes -Werror")
352     check_c_source_compiles("int foo() __attribute__((visibility(\"protected\")));
353                              int main() {return 0;}" HAVE_GCC_PROTECTED_VISIBILITY)
354     if(HAVE_GCC_PROTECTED_VISIBILITY)
355         if(NOT LIBTYPE STREQUAL "STATIC")
356             set(EXPORT_DECL "__attribute__((visibility(\"protected\")))")
357         endif()
358     else()
359         check_c_source_compiles("int foo() __attribute__((visibility(\"default\")));
360                                  int main() {return 0;}" HAVE_GCC_DEFAULT_VISIBILITY)
361         if(HAVE_GCC_DEFAULT_VISIBILITY)
362             if(NOT LIBTYPE STREQUAL "STATIC")
363                 set(EXPORT_DECL "__attribute__((visibility(\"default\")))")
364             endif()
365         endif()
366     endif()
368     set(CMAKE_REQUIRED_FLAGS "${OLD_REQUIRED_FLAGS}")
369 endif()
372 set(SSE2_SWITCH "")
374 if(NOT MSVC)
375     set(OLD_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
376     # Yes GCC, really don't accept command line options you don't support
377     set(CMAKE_REQUIRED_FLAGS "${OLD_REQUIRED_FLAGS} -Werror")
378     check_c_compiler_flag(-msse2 HAVE_MSSE2_SWITCH)
379     if(HAVE_MSSE2_SWITCH)
380         set(SSE2_SWITCH "-msse2")
381     endif()
382     set(CMAKE_REQUIRED_FLAGS ${OLD_REQUIRED_FLAGS})
383     unset(OLD_REQUIRED_FLAGS)
384 endif()
386 check_include_file(xmmintrin.h HAVE_XMMINTRIN_H)
387 check_include_file(emmintrin.h HAVE_EMMINTRIN_H)
388 check_include_file(pmmintrin.h HAVE_PMMINTRIN_H)
389 check_include_file(smmintrin.h HAVE_SMMINTRIN_H)
390 check_include_file(arm_neon.h HAVE_ARM_NEON_H)
392 set(HAVE_SSE        0)
393 set(HAVE_SSE2       0)
394 set(HAVE_SSE3       0)
395 set(HAVE_SSE4_1     0)
396 set(HAVE_NEON       0)
398 # Check for SSE support
399 option(ALSOFT_REQUIRE_SSE "Require SSE support" OFF)
400 if(HAVE_XMMINTRIN_H)
401     option(ALSOFT_CPUEXT_SSE "Enable SSE support" ON)
402     if(ALSOFT_CPUEXT_SSE)
403         set(HAVE_SSE 1)
404     endif()
405 endif()
406 if(ALSOFT_REQUIRE_SSE AND NOT HAVE_SSE)
407     message(FATAL_ERROR "Failed to enabled required SSE CPU extensions")
408 endif()
410 option(ALSOFT_REQUIRE_SSE2 "Require SSE2 support" OFF)
411 if(HAVE_EMMINTRIN_H)
412     option(ALSOFT_CPUEXT_SSE2 "Enable SSE2 support" ON)
413     if(HAVE_SSE AND ALSOFT_CPUEXT_SSE2)
414         set(HAVE_SSE2 1)
415     endif()
416 endif()
417 if(ALSOFT_REQUIRE_SSE2 AND NOT HAVE_SSE2)
418     message(FATAL_ERROR "Failed to enable required SSE2 CPU extensions")
419 endif()
421 option(ALSOFT_REQUIRE_SSE3 "Require SSE3 support" OFF)
422 if(HAVE_PMMINTRIN_H)
423     option(ALSOFT_CPUEXT_SSE3 "Enable SSE3 support" ON)
424     if(HAVE_SSE2 AND ALSOFT_CPUEXT_SSE3)
425         set(HAVE_SSE3 1)
426     endif()
427 endif()
428 if(ALSOFT_REQUIRE_SSE3 AND NOT HAVE_SSE3)
429     message(FATAL_ERROR "Failed to enable required SSE3 CPU extensions")
430 endif()
432 option(ALSOFT_REQUIRE_SSE4_1 "Require SSE4.1 support" OFF)
433 if(HAVE_SMMINTRIN_H)
434     option(ALSOFT_CPUEXT_SSE4_1 "Enable SSE4.1 support" ON)
435     if(HAVE_SSE3 AND ALSOFT_CPUEXT_SSE4_1)
436         set(HAVE_SSE4_1 1)
437     endif()
438 endif()
439 if(ALSOFT_REQUIRE_SSE4_1 AND NOT HAVE_SSE4_1)
440     message(FATAL_ERROR "Failed to enable required SSE4.1 CPU extensions")
441 endif()
443 # Check for ARM Neon support
444 option(ALSOFT_REQUIRE_NEON "Require ARM NEON support" OFF)
445 if(HAVE_ARM_NEON_H)
446     option(ALSOFT_CPUEXT_NEON "Enable ARM NEON support" ON)
447     if(ALSOFT_CPUEXT_NEON)
448         check_c_source_compiles("#include <arm_neon.h>
449             int main()
450             {
451                 int32x4_t ret4 = vdupq_n_s32(0);
452                 return vgetq_lane_s32(ret4, 0);
453             }" HAVE_NEON_INTRINSICS)
454         if(HAVE_NEON_INTRINSICS)
455             set(HAVE_NEON 1)
456         endif()
457     endif()
458 endif()
459 if(ALSOFT_REQUIRE_NEON AND NOT HAVE_NEON)
460     message(FATAL_ERROR "Failed to enabled required ARM NEON CPU extensions")
461 endif()
464 set(SSE_FLAGS )
465 set(FPMATH_SET "0")
466 if(CMAKE_SIZEOF_VOID_P MATCHES "4" AND HAVE_SSE2)
467     option(ALSOFT_ENABLE_SSE2_CODEGEN "Enable SSE2 code generation instead of x87 for 32-bit targets." TRUE)
468     if(ALSOFT_ENABLE_SSE2_CODEGEN)
469         if(MSVC)
470             check_c_compiler_flag("/arch:SSE2" HAVE_ARCH_SSE2)
471             if(HAVE_ARCH_SSE2)
472                 set(SSE_FLAGS ${SSE_FLAGS} "/arch:SSE2")
473                 set(C_FLAGS ${C_FLAGS} ${SSE_FLAGS})
474                 set(FPMATH_SET 2)
475             endif()
476         elseif(SSE2_SWITCH)
477             check_c_compiler_flag("${SSE2_SWITCH} -mfpmath=sse" HAVE_MFPMATH_SSE_2)
478             if(HAVE_MFPMATH_SSE_2)
479                 set(SSE_FLAGS ${SSE_FLAGS} ${SSE2_SWITCH} -mfpmath=sse)
480                 set(C_FLAGS ${C_FLAGS} ${SSE_FLAGS})
481                 set(FPMATH_SET 2)
482             endif()
483             # SSE depends on a 16-byte aligned stack, and by default, GCC
484             # assumes the stack is suitably aligned. Older Linux code or other
485             # OSs don't guarantee this on 32-bit, so externally-callable
486             # functions need to ensure an aligned stack.
487             set(EXPORT_DECL "${EXPORT_DECL}__attribute__((force_align_arg_pointer))")
488         endif()
489     endif()
490 endif()
492 if(HAVE_SSE2)
493     set(OLD_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
494     foreach(flag_var ${SSE_FLAGS})
495         set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${flag_var}")
496     endforeach()
498     check_c_source_compiles("#include <emmintrin.h>
499         int main() {_mm_pause(); return 0;}" HAVE_SSE_INTRINSICS)
501     set(CMAKE_REQUIRED_FLAGS ${OLD_REQUIRED_FLAGS})
502 endif()
505 check_include_file(malloc.h HAVE_MALLOC_H)
506 check_include_file(cpuid.h HAVE_CPUID_H)
507 check_include_file(intrin.h HAVE_INTRIN_H)
508 check_include_file(guiddef.h HAVE_GUIDDEF_H)
509 if(NOT HAVE_GUIDDEF_H)
510     check_include_file(initguid.h HAVE_INITGUID_H)
511 endif()
513 # Some systems need libm for some math functions to work
514 set(MATH_LIB )
515 check_library_exists(m pow "" HAVE_LIBM)
516 if(HAVE_LIBM)
517     set(MATH_LIB ${MATH_LIB} m)
518     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} m)
519 endif()
521 # Some systems need to link with -lrt for clock_gettime as used by the common
522 # eaxmple functions.
523 set(RT_LIB )
524 check_library_exists(rt clock_gettime "" HAVE_LIBRT)
525 if(HAVE_LIBRT)
526     set(RT_LIB rt)
527 endif()
529 # Check for the dlopen API (for dynamicly loading backend libs)
530 if(ALSOFT_DLOPEN)
531     check_include_file(dlfcn.h HAVE_DLFCN_H)
532     check_library_exists(dl dlopen "" HAVE_LIBDL)
533     if(HAVE_LIBDL)
534         set(EXTRA_LIBS dl ${EXTRA_LIBS})
535         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} dl)
536     endif()
537 endif()
539 # Check for a cpuid intrinsic
540 if(HAVE_CPUID_H)
541     check_c_source_compiles("#include <cpuid.h>
542         int main()
543         {
544             unsigned int eax, ebx, ecx, edx;
545             return __get_cpuid(0, &eax, &ebx, &ecx, &edx);
546         }" HAVE_GCC_GET_CPUID)
547 endif()
548 if(HAVE_INTRIN_H)
549     check_c_source_compiles("#include <intrin.h>
550         int main()
551         {
552             int regs[4];
553             __cpuid(regs, 0);
554             return regs[0];
555         }" HAVE_CPUID_INTRINSIC)
556 endif()
558 check_symbol_exists(posix_memalign   stdlib.h HAVE_POSIX_MEMALIGN)
559 check_symbol_exists(_aligned_malloc  malloc.h HAVE__ALIGNED_MALLOC)
560 check_symbol_exists(proc_pidpath     libproc.h HAVE_PROC_PIDPATH)
562 if(NOT WIN32)
563     # We need pthreads outside of Windows, for semaphores. It's also used to
564     # set the priority and name of threads, when possible.
565     check_include_file(pthread.h HAVE_PTHREAD_H)
566     if(NOT HAVE_PTHREAD_H)
567         message(FATAL_ERROR "PThreads is required for non-Windows builds!")
568     endif()
570     check_c_compiler_flag(-pthread HAVE_PTHREAD)
571     if(HAVE_PTHREAD)
572         set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -pthread")
573         set(C_FLAGS ${C_FLAGS} -pthread)
574         set(LINKER_FLAGS ${LINKER_FLAGS} -pthread)
575     endif()
577     check_symbol_exists(pthread_setschedparam pthread.h HAVE_PTHREAD_SETSCHEDPARAM)
579     # Some systems need pthread_np.h to get pthread_setname_np
580     check_include_files("pthread.h;pthread_np.h" HAVE_PTHREAD_NP_H)
581     if(HAVE_PTHREAD_NP_H)
582         check_symbol_exists(pthread_setname_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SETNAME_NP)
583         if(NOT HAVE_PTHREAD_SETNAME_NP)
584             check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SET_NAME_NP)
585         endif()
586     else()
587         check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
588         if(NOT HAVE_PTHREAD_SETNAME_NP)
589             check_symbol_exists(pthread_set_name_np pthread.h HAVE_PTHREAD_SET_NAME_NP)
590         endif()
591     endif()
592 endif()
594 check_symbol_exists(getopt unistd.h HAVE_GETOPT)
597 # Common sources used by both the OpenAL implementation library, the OpenAL
598 # router, and certain tools and examples.
599 set(COMMON_OBJS
600     common/albit.h
601     common/albyte.h
602     common/alcomplex.cpp
603     common/alcomplex.h
604     common/aldeque.h
605     common/alfstream.cpp
606     common/alfstream.h
607     common/almalloc.cpp
608     common/almalloc.h
609     common/alnumbers.h
610     common/alnumeric.h
611     common/aloptional.h
612     common/alspan.h
613     common/alstring.cpp
614     common/alstring.h
615     common/altraits.h
616     common/atomic.h
617     common/comptr.h
618     common/dynload.cpp
619     common/dynload.h
620     common/intrusive_ptr.h
621     common/opthelpers.h
622     common/phase_shifter.h
623     common/polyphase_resampler.cpp
624     common/polyphase_resampler.h
625     common/pragmadefs.h
626     common/ringbuffer.cpp
627     common/ringbuffer.h
628     common/strutils.cpp
629     common/strutils.h
630     common/threads.cpp
631     common/threads.h
632     common/vecmat.h
633     common/vector.h)
635 # Core library routines
636 set(CORE_OBJS
637     core/ambdec.cpp
638     core/ambdec.h
639     core/ambidefs.cpp
640     core/ambidefs.h
641     core/async_event.h
642     core/bformatdec.cpp
643     core/bformatdec.h
644     core/bs2b.cpp
645     core/bs2b.h
646     core/bsinc_defs.h
647     core/bsinc_tables.cpp
648     core/bsinc_tables.h
649     core/bufferline.h
650     core/buffer_storage.cpp
651     core/buffer_storage.h
652     core/context.cpp
653     core/context.h
654     core/converter.cpp
655     core/converter.h
656     core/cpu_caps.cpp
657     core/cpu_caps.h
658     core/cubic_defs.h
659     core/cubic_tables.cpp
660     core/cubic_tables.h
661     core/devformat.cpp
662     core/devformat.h
663     core/device.cpp
664     core/device.h
665     core/effects/base.h
666     core/effectslot.cpp
667     core/effectslot.h
668     core/except.cpp
669     core/except.h
670     core/filters/biquad.h
671     core/filters/biquad.cpp
672     core/filters/nfc.cpp
673     core/filters/nfc.h
674     core/filters/splitter.cpp
675     core/filters/splitter.h
676     core/fmt_traits.cpp
677     core/fmt_traits.h
678     core/fpu_ctrl.cpp
679     core/fpu_ctrl.h
680     core/front_stablizer.h
681     core/helpers.cpp
682     core/helpers.h
683     core/hrtf.cpp
684     core/hrtf.h
685     core/logging.cpp
686     core/logging.h
687     core/mastering.cpp
688     core/mastering.h
689     core/mixer.cpp
690     core/mixer.h
691     core/resampler_limits.h
692     core/uhjfilter.cpp
693     core/uhjfilter.h
694     core/uiddefs.cpp
695     core/voice.cpp
696     core/voice.h
697     core/voice_change.h)
699 set(HAVE_RTKIT 0)
700 if(NOT WIN32)
701     option(ALSOFT_REQUIRE_RTKIT "Require RTKit/D-Bus support" FALSE)
702     find_package(DBus1 QUIET)
703     if(NOT DBus1_FOUND AND PkgConfig_FOUND)
704         pkg_check_modules(DBUS dbus-1)
705     endif()
706     if(DBus1_FOUND OR DBUS_FOUND)
707         option(ALSOFT_RTKIT "Enable RTKit support" ON)
708         if(ALSOFT_RTKIT)
709             set(HAVE_RTKIT 1)
710             set(CORE_OBJS ${CORE_OBJS} core/dbus_wrap.cpp core/dbus_wrap.h
711                 core/rtkit.cpp core/rtkit.h)
712             if(NOT DBus1_FOUND)
713                 set(INC_PATHS ${INC_PATHS} ${DBUS_INCLUDE_DIRS})
714                 set(CPP_DEFS ${CPP_DEFS} ${DBUS_CFLAGS_OTHER})
715                 if(NOT HAVE_DLFCN_H)
716                     set(EXTRA_LIBS ${EXTRA_LIBS} ${DBUS_LINK_LIBRARIES})
717                 endif()
718             elseif(HAVE_DLFCN_H)
719                 set(INC_PATHS ${INC_PATHS} ${DBus1_INCLUDE_DIRS})
720                 set(CPP_DEFS ${CPP_DEFS} ${DBus1_DEFINITIONS})
721             else()
722                 set(EXTRA_LIBS ${EXTRA_LIBS} ${DBus1_LIBRARIES})
723             endif()
724         endif()
725     else()
726         set(MISSING_VARS "")
727         if(NOT DBus1_INCLUDE_DIRS)
728             set(MISSING_VARS "${MISSING_VARS} DBus1_INCLUDE_DIRS")
729         endif()
730         if(NOT DBus1_LIBRARIES)
731             set(MISSING_VARS "${MISSING_VARS} DBus1_LIBRARIES")
732         endif()
733         message(STATUS "Could NOT find DBus1 (missing:${MISSING_VARS})")
734         unset(MISSING_VARS)
735     endif()
736 endif()
737 if(ALSOFT_REQUIRE_RTKIT AND NOT HAVE_RTKIT)
738     message(FATAL_ERROR "Failed to enabled required RTKit support")
739 endif()
741 # Default mixers, always available
742 set(CORE_OBJS ${CORE_OBJS}
743     core/mixer/defs.h
744     core/mixer/hrtfbase.h
745     core/mixer/hrtfdefs.h
746     core/mixer/mixer_c.cpp)
748 # AL and related routines
749 set(OPENAL_OBJS
750     al/auxeffectslot.cpp
751     al/auxeffectslot.h
752     al/buffer.cpp
753     al/buffer.h
754     al/effect.cpp
755     al/effect.h
756     al/effects/autowah.cpp
757     al/effects/chorus.cpp
758     al/effects/compressor.cpp
759     al/effects/convolution.cpp
760     al/effects/dedicated.cpp
761     al/effects/distortion.cpp
762     al/effects/echo.cpp
763         al/effects/effects.cpp
764     al/effects/effects.h
765     al/effects/equalizer.cpp
766     al/effects/fshifter.cpp
767     al/effects/modulator.cpp
768     al/effects/null.cpp
769     al/effects/pshifter.cpp
770     al/effects/reverb.cpp
771     al/effects/vmorpher.cpp
772     al/error.cpp
773     al/event.cpp
774     al/event.h
775     al/extension.cpp
776     al/filter.cpp
777     al/filter.h
778     al/listener.cpp
779     al/listener.h
780     al/source.cpp
781     al/source.h
782     al/state.cpp)
784 # ALC and related routines
785 set(ALC_OBJS
786     alc/alc.cpp
787     alc/alu.cpp
788     alc/alu.h
789     alc/alconfig.cpp
790     alc/alconfig.h
791     alc/context.cpp
792     alc/context.h
793     alc/device.cpp
794     alc/device.h
795     alc/effects/base.h
796     alc/effects/autowah.cpp
797     alc/effects/chorus.cpp
798     alc/effects/compressor.cpp
799     alc/effects/convolution.cpp
800     alc/effects/dedicated.cpp
801     alc/effects/distortion.cpp
802     alc/effects/echo.cpp
803     alc/effects/equalizer.cpp
804     alc/effects/fshifter.cpp
805     alc/effects/modulator.cpp
806     alc/effects/null.cpp
807     alc/effects/pshifter.cpp
808     alc/effects/reverb.cpp
809     alc/effects/vmorpher.cpp
810     alc/inprogext.h
811     alc/panning.cpp)
813 if (ALSOFT_EAX)
814     set(OPENAL_OBJS
815         ${OPENAL_OBJS}
816         al/eax/api.cpp
817         al/eax/api.h
818         al/eax/call.cpp
819         al/eax/call.h
820         al/eax/effect.h
821         al/eax/exception.cpp
822         al/eax/exception.h
823         al/eax/fx_slot_index.cpp
824         al/eax/fx_slot_index.h
825         al/eax/fx_slots.cpp
826         al/eax/fx_slots.h
827         al/eax/globals.cpp
828         al/eax/globals.h
829         al/eax/utils.cpp
830         al/eax/utils.h
831         al/eax/x_ram.h
833 endif ()
835 # Include SIMD mixers
836 set(CPU_EXTS "Default")
837 if(HAVE_SSE)
838     set(CORE_OBJS  ${CORE_OBJS} core/mixer/mixer_sse.cpp)
839     set(CPU_EXTS "${CPU_EXTS}, SSE")
840 endif()
841 if(HAVE_SSE2)
842     set(CORE_OBJS  ${CORE_OBJS} core/mixer/mixer_sse2.cpp)
843     set(CPU_EXTS "${CPU_EXTS}, SSE2")
844 endif()
845 if(HAVE_SSE3)
846     set(CORE_OBJS  ${CORE_OBJS} core/mixer/mixer_sse3.cpp)
847     set(CPU_EXTS "${CPU_EXTS}, SSE3")
848 endif()
849 if(HAVE_SSE4_1)
850     set(CORE_OBJS  ${CORE_OBJS} core/mixer/mixer_sse41.cpp)
851     set(CPU_EXTS "${CPU_EXTS}, SSE4.1")
852 endif()
853 if(HAVE_NEON)
854     set(CORE_OBJS  ${CORE_OBJS} core/mixer/mixer_neon.cpp)
855     set(CPU_EXTS "${CPU_EXTS}, Neon")
856 endif()
859 set(HAVE_ALSA       0)
860 set(HAVE_OSS        0)
861 set(HAVE_PIPEWIRE   0)
862 set(HAVE_SOLARIS    0)
863 set(HAVE_SNDIO      0)
864 set(HAVE_DSOUND     0)
865 set(HAVE_WASAPI     0)
866 set(HAVE_WINMM      0)
867 set(HAVE_PORTAUDIO  0)
868 set(HAVE_PULSEAUDIO 0)
869 set(HAVE_COREAUDIO  0)
870 set(HAVE_OPENSL     0)
871 set(HAVE_OBOE       0)
872 set(HAVE_WAVE       0)
873 set(HAVE_SDL2       0)
875 if(WIN32 OR HAVE_DLFCN_H)
876     set(IS_LINKED "")
877     macro(ADD_BACKEND_LIBS _LIBS)
878     endmacro()
879 else()
880     set(IS_LINKED " (linked)")
881     macro(ADD_BACKEND_LIBS _LIBS)
882         set(EXTRA_LIBS ${_LIBS} ${EXTRA_LIBS})
883     endmacro()
884 endif()
886 set(BACKENDS "")
887 set(ALC_OBJS  ${ALC_OBJS}
888     alc/backends/base.cpp
889     alc/backends/base.h
890     # Default backends, always available
891     alc/backends/loopback.cpp
892     alc/backends/loopback.h
893     alc/backends/null.cpp
894     alc/backends/null.h
897 # Check ALSA backend
898 option(ALSOFT_REQUIRE_ALSA "Require ALSA backend" OFF)
899 find_package(ALSA)
900 if(ALSA_FOUND)
901     option(ALSOFT_BACKEND_ALSA "Enable ALSA backend" ON)
902     if(ALSOFT_BACKEND_ALSA)
903         set(HAVE_ALSA 1)
904         set(BACKENDS  "${BACKENDS} ALSA${IS_LINKED},")
905         set(ALC_OBJS  ${ALC_OBJS} alc/backends/alsa.cpp alc/backends/alsa.h)
906         add_backend_libs(${ALSA_LIBRARIES})
907         set(INC_PATHS ${INC_PATHS} ${ALSA_INCLUDE_DIRS})
908     endif()
909 endif()
910 if(ALSOFT_REQUIRE_ALSA AND NOT HAVE_ALSA)
911     message(FATAL_ERROR "Failed to enabled required ALSA backend")
912 endif()
914 # Check OSS backend
915 option(ALSOFT_REQUIRE_OSS "Require OSS backend" OFF)
916 find_package(OSS)
917 if(OSS_FOUND)
918     option(ALSOFT_BACKEND_OSS "Enable OSS backend" ON)
919     if(ALSOFT_BACKEND_OSS)
920         set(HAVE_OSS 1)
921         set(BACKENDS  "${BACKENDS} OSS,")
922         set(ALC_OBJS  ${ALC_OBJS} alc/backends/oss.cpp alc/backends/oss.h)
923         if(OSS_LIBRARIES)
924             set(EXTRA_LIBS ${OSS_LIBRARIES} ${EXTRA_LIBS})
925         endif()
926         set(INC_PATHS ${INC_PATHS} ${OSS_INCLUDE_DIRS})
927     endif()
928 endif()
929 if(ALSOFT_REQUIRE_OSS AND NOT HAVE_OSS)
930     message(FATAL_ERROR "Failed to enabled required OSS backend")
931 endif()
933 # Check PipeWire backend
934 option(ALSOFT_REQUIRE_PIPEWIRE "Require PipeWire backend" OFF)
935 if(PkgConfig_FOUND)
936     pkg_check_modules(PIPEWIRE libpipewire-0.3>=0.3.23)
937     if(PIPEWIRE_FOUND)
938         option(ALSOFT_BACKEND_PIPEWIRE "Enable PipeWire backend" ON)
939         if(ALSOFT_BACKEND_PIPEWIRE)
940             set(HAVE_PIPEWIRE 1)
941             set(BACKENDS  "${BACKENDS} PipeWire${IS_LINKED},")
942             set(ALC_OBJS  ${ALC_OBJS} alc/backends/pipewire.cpp alc/backends/pipewire.h)
943             add_backend_libs(${PIPEWIRE_LIBRARIES})
944             set(INC_PATHS ${INC_PATHS} ${PIPEWIRE_INCLUDE_DIRS})
945         endif()
946     endif()
947 endif()
948 if(ALSOFT_REQUIRE_PIPEWIRE AND NOT HAVE_PIPEWIRE)
949     message(FATAL_ERROR "Failed to enabled required PipeWire backend")
950 endif()
952 # Check Solaris backend
953 option(ALSOFT_REQUIRE_SOLARIS "Require Solaris backend" OFF)
954 find_package(AudioIO)
955 if(AUDIOIO_FOUND)
956     option(ALSOFT_BACKEND_SOLARIS "Enable Solaris backend" ON)
957     if(ALSOFT_BACKEND_SOLARIS)
958         set(HAVE_SOLARIS 1)
959         set(BACKENDS  "${BACKENDS} Solaris,")
960         set(ALC_OBJS  ${ALC_OBJS} alc/backends/solaris.cpp alc/backends/solaris.h)
961         set(INC_PATHS ${INC_PATHS} ${AUDIOIO_INCLUDE_DIRS})
962     endif()
963 endif()
964 if(ALSOFT_REQUIRE_SOLARIS AND NOT HAVE_SOLARIS)
965     message(FATAL_ERROR "Failed to enabled required Solaris backend")
966 endif()
968 # Check SndIO backend
969 option(ALSOFT_REQUIRE_SNDIO "Require SndIO backend" OFF)
970 find_package(SoundIO)
971 if(SOUNDIO_FOUND)
972     option(ALSOFT_BACKEND_SNDIO "Enable SndIO backend" ON)
973     if(ALSOFT_BACKEND_SNDIO)
974         set(HAVE_SNDIO 1)
975         set(BACKENDS  "${BACKENDS} SndIO (linked),")
976         set(ALC_OBJS  ${ALC_OBJS} alc/backends/sndio.cpp alc/backends/sndio.h)
977         set(EXTRA_LIBS ${SOUNDIO_LIBRARIES} ${EXTRA_LIBS})
978         set(INC_PATHS ${INC_PATHS} ${SOUNDIO_INCLUDE_DIRS})
979     endif()
980 endif()
981 if(ALSOFT_REQUIRE_SNDIO AND NOT HAVE_SNDIO)
982     message(FATAL_ERROR "Failed to enabled required SndIO backend")
983 endif()
985 # Check Windows-only backends
986 option(ALSOFT_REQUIRE_WINMM "Require Windows Multimedia backend" OFF)
987 option(ALSOFT_REQUIRE_DSOUND "Require DirectSound backend" OFF)
988 option(ALSOFT_REQUIRE_WASAPI "Require WASAPI backend" OFF)
989 if(WIN32)
990     # Check MMSystem backend
991     option(ALSOFT_BACKEND_WINMM "Enable Windows Multimedia backend" ON)
992     if(ALSOFT_BACKEND_WINMM)
993         set(HAVE_WINMM 1)
994         set(BACKENDS "${BACKENDS} WinMM,")
995         set(ALC_OBJS ${ALC_OBJS} alc/backends/winmm.cpp alc/backends/winmm.h)
996         # There doesn't seem to be good way to search for winmm.lib for MSVC.
997         # find_library doesn't find it without being told to look in a specific
998         # place in the WindowsSDK, but it links anyway. If there ends up being
999         # Windows targets without this, another means to detect it is needed.
1000         set(EXTRA_LIBS winmm ${EXTRA_LIBS})
1001     endif()
1003     # Check DSound backend
1004     check_include_file(dsound.h HAVE_DSOUND_H)
1005     if(DXSDK_DIR)
1006         find_path(DSOUND_INCLUDE_DIR NAMES "dsound.h"
1007             PATHS "${DXSDK_DIR}" PATH_SUFFIXES include
1008             DOC "The DirectSound include directory")
1009     endif()
1010     if(HAVE_DSOUND_H OR DSOUND_INCLUDE_DIR)
1011         option(ALSOFT_BACKEND_DSOUND "Enable DirectSound backend" ON)
1012         if(ALSOFT_BACKEND_DSOUND)
1013             set(HAVE_DSOUND 1)
1014             set(BACKENDS "${BACKENDS} DirectSound,")
1015             set(ALC_OBJS ${ALC_OBJS} alc/backends/dsound.cpp alc/backends/dsound.h)
1017             if(NOT HAVE_DSOUND_H)
1018                 set(INC_PATHS ${INC_PATHS} ${DSOUND_INCLUDE_DIR})
1019             endif()
1020         endif()
1021     endif()
1023     # Check for WASAPI backend
1024     check_include_file(mmdeviceapi.h HAVE_MMDEVICEAPI_H)
1025     if(HAVE_MMDEVICEAPI_H)
1026         option(ALSOFT_BACKEND_WASAPI "Enable WASAPI backend" ON)
1027         if(ALSOFT_BACKEND_WASAPI)
1028             set(HAVE_WASAPI 1)
1029             set(BACKENDS  "${BACKENDS} WASAPI,")
1030             set(ALC_OBJS  ${ALC_OBJS} alc/backends/wasapi.cpp alc/backends/wasapi.h)
1031         endif()
1032     endif()
1033 endif()
1034 if(ALSOFT_REQUIRE_WINMM AND NOT HAVE_WINMM)
1035     message(FATAL_ERROR "Failed to enabled required WinMM backend")
1036 endif()
1037 if(ALSOFT_REQUIRE_DSOUND AND NOT HAVE_DSOUND)
1038     message(FATAL_ERROR "Failed to enabled required DSound backend")
1039 endif()
1040 if(ALSOFT_REQUIRE_WASAPI AND NOT HAVE_WASAPI)
1041     message(FATAL_ERROR "Failed to enabled required WASAPI backend")
1042 endif()
1044 # Check PortAudio backend
1045 option(ALSOFT_REQUIRE_PORTAUDIO "Require PortAudio backend" OFF)
1046 find_package(PortAudio)
1047 if(PORTAUDIO_FOUND)
1048     option(ALSOFT_BACKEND_PORTAUDIO "Enable PortAudio backend" ON)
1049     if(ALSOFT_BACKEND_PORTAUDIO)
1050         set(HAVE_PORTAUDIO 1)
1051         set(BACKENDS  "${BACKENDS} PortAudio${IS_LINKED},")
1052         set(ALC_OBJS  ${ALC_OBJS} alc/backends/portaudio.cpp alc/backends/portaudio.h)
1053         add_backend_libs(${PORTAUDIO_LIBRARIES})
1054         set(INC_PATHS ${INC_PATHS} ${PORTAUDIO_INCLUDE_DIRS})
1055     endif()
1056 endif()
1057 if(ALSOFT_REQUIRE_PORTAUDIO AND NOT HAVE_PORTAUDIO)
1058     message(FATAL_ERROR "Failed to enabled required PortAudio backend")
1059 endif()
1061 # Check PulseAudio backend
1062 option(ALSOFT_REQUIRE_PULSEAUDIO "Require PulseAudio backend" OFF)
1063 find_package(PulseAudio)
1064 if(PULSEAUDIO_FOUND)
1065     option(ALSOFT_BACKEND_PULSEAUDIO "Enable PulseAudio backend" ON)
1066     if(ALSOFT_BACKEND_PULSEAUDIO)
1067         set(HAVE_PULSEAUDIO 1)
1068         set(BACKENDS  "${BACKENDS} PulseAudio${IS_LINKED},")
1069         set(ALC_OBJS  ${ALC_OBJS} alc/backends/pulseaudio.cpp alc/backends/pulseaudio.h)
1070         add_backend_libs(${PULSEAUDIO_LIBRARY})
1071         set(INC_PATHS ${INC_PATHS} ${PULSEAUDIO_INCLUDE_DIR})
1072     endif()
1073 endif()
1074 if(ALSOFT_REQUIRE_PULSEAUDIO AND NOT HAVE_PULSEAUDIO)
1075     message(FATAL_ERROR "Failed to enabled required PulseAudio backend")
1076 endif()
1078 # Check JACK backend
1079 option(ALSOFT_REQUIRE_JACK "Require JACK backend" OFF)
1080 find_package(JACK)
1081 if(JACK_FOUND)
1082     option(ALSOFT_BACKEND_JACK "Enable JACK backend" ON)
1083     if(ALSOFT_BACKEND_JACK)
1084         set(HAVE_JACK 1)
1085         set(BACKENDS  "${BACKENDS} JACK${IS_LINKED},")
1086         set(ALC_OBJS  ${ALC_OBJS} alc/backends/jack.cpp alc/backends/jack.h)
1087         add_backend_libs(${JACK_LIBRARIES})
1088         set(INC_PATHS ${INC_PATHS} ${JACK_INCLUDE_DIRS})
1089     endif()
1090 endif()
1091 if(ALSOFT_REQUIRE_JACK AND NOT HAVE_JACK)
1092     message(FATAL_ERROR "Failed to enabled required JACK backend")
1093 endif()
1095 # Check CoreAudio backend
1096 option(ALSOFT_REQUIRE_COREAUDIO "Require CoreAudio backend" OFF)
1097 find_library(COREAUDIO_FRAMEWORK NAMES CoreAudio)
1098 find_path(AUDIOUNIT_INCLUDE_DIR NAMES AudioUnit/AudioUnit.h)
1099 if(COREAUDIO_FRAMEWORK AND AUDIOUNIT_INCLUDE_DIR)
1100     option(ALSOFT_BACKEND_COREAUDIO "Enable CoreAudio backend" ON)
1101     if(ALSOFT_BACKEND_COREAUDIO)
1102         set(HAVE_COREAUDIO 1)
1103         set(ALC_OBJS  ${ALC_OBJS} alc/backends/coreaudio.cpp alc/backends/coreaudio.h)
1104         set(BACKENDS  "${BACKENDS} CoreAudio,")
1106         set(EXTRA_LIBS -Wl,-framework,CoreAudio ${EXTRA_LIBS})
1107         if(CMAKE_SYSTEM_NAME MATCHES "^(iOS|tvOS)$")
1108             find_library(COREFOUNDATION_FRAMEWORK NAMES CoreFoundation)
1109             if(COREFOUNDATION_FRAMEWORK)
1110                 set(EXTRA_LIBS -Wl,-framework,CoreFoundation ${EXTRA_LIBS})
1111             endif()
1112         else()
1113             set(EXTRA_LIBS -Wl,-framework,AudioUnit,-framework,ApplicationServices ${EXTRA_LIBS})
1114         endif()
1116         # Some versions of OSX may need the AudioToolbox framework. Add it if
1117         # it's found.
1118         find_library(AUDIOTOOLBOX_LIBRARY NAMES AudioToolbox)
1119         if(AUDIOTOOLBOX_LIBRARY)
1120             set(EXTRA_LIBS -Wl,-framework,AudioToolbox ${EXTRA_LIBS})
1121         endif()
1123         set(INC_PATHS ${INC_PATHS} ${AUDIOUNIT_INCLUDE_DIR})
1124     endif()
1125 endif()
1126 if(ALSOFT_REQUIRE_COREAUDIO AND NOT HAVE_COREAUDIO)
1127     message(FATAL_ERROR "Failed to enabled required CoreAudio backend")
1128 endif()
1130 # Check for OpenSL (Android) backend
1131 option(ALSOFT_REQUIRE_OPENSL "Require OpenSL backend" OFF)
1132 find_package(OpenSL)
1133 if(OPENSL_FOUND)
1134     option(ALSOFT_BACKEND_OPENSL "Enable OpenSL backend" ON)
1135     if(ALSOFT_BACKEND_OPENSL)
1136         set(HAVE_OPENSL 1)
1137         set(ALC_OBJS  ${ALC_OBJS} alc/backends/opensl.cpp alc/backends/opensl.h)
1138         set(BACKENDS  "${BACKENDS} OpenSL,")
1139         set(EXTRA_LIBS "OpenSL::OpenSLES" ${EXTRA_LIBS})
1140     endif()
1141 endif()
1142 if(ALSOFT_REQUIRE_OPENSL AND NOT HAVE_OPENSL)
1143     message(FATAL_ERROR "Failed to enabled required OpenSL backend")
1144 endif()
1146 # Check for Oboe (Android) backend
1147 set(OBOE_TARGET )
1148 if(ANDROID)
1149     set(OBOE_SOURCE "" CACHE STRING "Source directory for Oboe.")
1150     if(OBOE_SOURCE)
1151         add_subdirectory(${OBOE_SOURCE} ./oboe)
1152         set(OBOE_TARGET oboe)
1153     else()
1154         find_package(oboe CONFIG)
1155         if(NOT TARGET oboe::oboe)
1156             find_package(Oboe)
1157         endif()
1158         if(TARGET oboe::oboe)
1159             set(OBOE_TARGET "oboe::oboe")
1160         endif()
1161     endif()
1162 endif()
1164 option(ALSOFT_REQUIRE_OBOE "Require Oboe backend" OFF)
1165 if(OBOE_TARGET)
1166     option(ALSOFT_BACKEND_OBOE "Enable Oboe backend" ON)
1167     if(ALSOFT_BACKEND_OBOE)
1168         set(HAVE_OBOE 1)
1169         set(ALC_OBJS  ${ALC_OBJS} alc/backends/oboe.cpp alc/backends/oboe.h)
1170         set(BACKENDS  "${BACKENDS} Oboe,")
1171         set(EXTRA_LIBS ${OBOE_TARGET} ${EXTRA_LIBS})
1172     endif()
1173 endif()
1174 if(ALSOFT_REQUIRE_OBOE AND NOT HAVE_OBOE)
1175     message(FATAL_ERROR "Failed to enabled required Oboe backend")
1176 endif()
1178 # Check for SDL2 backend
1179 option(ALSOFT_REQUIRE_SDL2 "Require SDL2 backend" OFF)
1180 find_package(SDL2 QUIET)
1181 if(SDL2_FOUND)
1182     # Off by default, since it adds a runtime dependency
1183     option(ALSOFT_BACKEND_SDL2 "Enable SDL2 backend" OFF)
1184     if(ALSOFT_BACKEND_SDL2)
1185         set(HAVE_SDL2 1)
1186         set(ALC_OBJS  ${ALC_OBJS} alc/backends/sdl2.cpp alc/backends/sdl2.h)
1187         set(BACKENDS  "${BACKENDS} SDL2,")
1188         set(EXTRA_LIBS ${EXTRA_LIBS} SDL2::SDL2)
1189     endif()
1190 else()
1191     message(STATUS "Could NOT find SDL2")
1192 endif()
1193 if(ALSOFT_REQUIRE_SDL2 AND NOT SDL2_FOUND)
1194     message(FATAL_ERROR "Failed to enabled required SDL2 backend")
1195 endif()
1197 # Optionally enable the Wave Writer backend
1198 option(ALSOFT_BACKEND_WAVE "Enable Wave Writer backend" ON)
1199 if(ALSOFT_BACKEND_WAVE)
1200     set(HAVE_WAVE 1)
1201     set(ALC_OBJS  ${ALC_OBJS} alc/backends/wave.cpp alc/backends/wave.h)
1202     set(BACKENDS  "${BACKENDS} WaveFile,")
1203 endif()
1205 # This is always available
1206 set(BACKENDS  "${BACKENDS} Null")
1209 find_package(Git)
1210 if(ALSOFT_UPDATE_BUILD_VERSION AND GIT_FOUND AND EXISTS "${OpenAL_SOURCE_DIR}/.git")
1211     set(GIT_DIR "${OpenAL_SOURCE_DIR}/.git")
1213     # Check if this is a submodule, if it is then find the .git directory
1214     if(NOT IS_DIRECTORY "${OpenAL_SOURCE_DIR}/.git")
1215         file(READ ${GIT_DIR} submodule)
1216         string(REGEX REPLACE "gitdir: (.*)$" "\\1" GIT_DIR_RELATIVE ${submodule})
1217         string(STRIP ${GIT_DIR_RELATIVE} GIT_DIR_RELATIVE)
1218         get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
1219         get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
1220     endif()
1222     # Get the current working branch and its latest abbreviated commit hash
1223     add_custom_command(OUTPUT "${OpenAL_BINARY_DIR}/version_witness.txt"
1224         BYPRODUCTS "${OpenAL_BINARY_DIR}/version.h"
1225         COMMAND ${CMAKE_COMMAND} -D GIT_EXECUTABLE=${GIT_EXECUTABLE} -D LIB_VERSION=${LIB_VERSION}
1226             -D LIB_VERSION_NUM=${LIB_VERSION_NUM} -D SRC=${OpenAL_SOURCE_DIR}/version.h.in
1227             -D DST=${OpenAL_BINARY_DIR}/version.h -P ${OpenAL_SOURCE_DIR}/version.cmake
1228         COMMAND ${CMAKE_COMMAND} -E touch "${OpenAL_BINARY_DIR}/version_witness.txt"
1229         WORKING_DIRECTORY "${OpenAL_SOURCE_DIR}"
1230         MAIN_DEPENDENCY "${OpenAL_SOURCE_DIR}/version.h.in"
1231         DEPENDS "${GIT_DIR}/index" "${OpenAL_SOURCE_DIR}/version.cmake"
1232         VERBATIM
1233     )
1235     add_custom_target(build_version DEPENDS "${OpenAL_BINARY_DIR}/version_witness.txt")
1236 else()
1237     set(GIT_BRANCH "UNKNOWN")
1238     set(GIT_COMMIT_HASH "unknown")
1239     configure_file(
1240         "${OpenAL_SOURCE_DIR}/version.h.in"
1241         "${OpenAL_BINARY_DIR}/version.h")
1242 endif()
1245 option(ALSOFT_EMBED_HRTF_DATA "Embed the HRTF data files (increases library footprint)" ON)
1246 if(ALSOFT_EMBED_HRTF_DATA)
1247     macro(make_hrtf_header FILENAME VARNAME)
1248         set(infile  "${OpenAL_SOURCE_DIR}/hrtf/${FILENAME}")
1249         set(outfile  "${OpenAL_BINARY_DIR}/${VARNAME}.txt")
1251         add_custom_command(OUTPUT "${outfile}"
1252             COMMAND ${CMAKE_COMMAND} -D "INPUT_FILE=${infile}" -D "OUTPUT_FILE=${outfile}"
1253                 -P "${CMAKE_MODULE_PATH}/bin2h.script.cmake"
1254             WORKING_DIRECTORY "${OpenAL_SOURCE_DIR}"
1255             DEPENDS "${infile}" "${CMAKE_MODULE_PATH}/bin2h.script.cmake"
1256             VERBATIM
1257         )
1258         set(ALC_OBJS  ${ALC_OBJS} "${outfile}")
1259     endmacro()
1261     make_hrtf_header("Default HRTF.mhr" "default_hrtf")
1262 endif()
1265 if(ALSOFT_UTILS)
1266     find_package(MySOFA)
1267     if(NOT ALSOFT_NO_CONFIG_UTIL)
1268         find_package(Qt5Widgets QUIET)
1269         if(NOT Qt5Widgets_FOUND)
1270             message(STATUS "Could NOT find Qt5Widgets")
1271         endif()
1272     endif()
1273 endif()
1274 if(ALSOFT_UTILS OR ALSOFT_EXAMPLES)
1275     find_package(SndFile)
1276     if(SDL2_FOUND)
1277         find_package(FFmpeg COMPONENTS AVFORMAT AVCODEC AVUTIL SWSCALE SWRESAMPLE)
1278     endif()
1279 endif()
1281 if(NOT WIN32)
1282     set(LIBNAME "openal")
1283 else()
1284     set(LIBNAME "OpenAL32")
1285 endif()
1287 # Needed for openal.pc.in
1288 set(prefix ${CMAKE_INSTALL_PREFIX})
1289 set(exec_prefix "\${prefix}")
1290 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
1291 set(bindir "${CMAKE_INSTALL_FULL_BINDIR}")
1292 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
1293 set(PACKAGE_VERSION "${LIB_VERSION}")
1294 set(PKG_CONFIG_CFLAGS )
1295 set(PKG_CONFIG_PRIVATE_LIBS )
1296 if(LIBTYPE STREQUAL "STATIC")
1297     set(PKG_CONFIG_CFLAGS -DAL_LIBTYPE_STATIC)
1298     foreach(FLAG  ${LINKER_FLAGS} ${EXTRA_LIBS} ${MATH_LIB})
1299         # If this is already a linker flag, or is a full path+file, add it
1300         # as-is. If it's an SDL2 target, add the link flag for it. Otherwise,
1301         # it's a name intended to be dressed as -lname.
1302         if(FLAG MATCHES "^-.*" OR EXISTS "${FLAG}")
1303             set(PKG_CONFIG_PRIVATE_LIBS "${PKG_CONFIG_PRIVATE_LIBS} ${FLAG}")
1304         elseif(FLAG MATCHES "^SDL2::SDL2")
1305             set(PKG_CONFIG_PRIVATE_LIBS "${PKG_CONFIG_PRIVATE_LIBS} -lSDL2")
1306         else()
1307             set(PKG_CONFIG_PRIVATE_LIBS "${PKG_CONFIG_PRIVATE_LIBS} -l${FLAG}")
1308         endif()
1309     endforeach()
1310 endif()
1312 # End configuration
1313 configure_file(
1314     "${OpenAL_SOURCE_DIR}/config.h.in"
1315     "${OpenAL_BINARY_DIR}/config.h")
1316 configure_file(
1317     "${OpenAL_SOURCE_DIR}/openal.pc.in"
1318     "${OpenAL_BINARY_DIR}/openal.pc"
1319     @ONLY)
1322 add_library(common STATIC EXCLUDE_FROM_ALL ${COMMON_OBJS})
1323 target_include_directories(common PRIVATE ${OpenAL_BINARY_DIR} ${OpenAL_SOURCE_DIR}/include)
1324 target_compile_definitions(common PRIVATE ${CPP_DEFS})
1325 target_compile_options(common PRIVATE ${C_FLAGS})
1326 set_target_properties(common PROPERTIES ${DEFAULT_TARGET_PROPS} POSITION_INDEPENDENT_CODE TRUE)
1329 unset(HAS_ROUTER)
1330 set(IMPL_TARGET OpenAL) # Either OpenAL or soft_oal.
1332 # Build main library
1333 if(LIBTYPE STREQUAL "STATIC")
1334     add_library(${IMPL_TARGET} STATIC ${COMMON_OBJS} ${OPENAL_OBJS} ${ALC_OBJS} ${CORE_OBJS})
1335     target_compile_definitions(${IMPL_TARGET} PUBLIC AL_LIBTYPE_STATIC)
1336     target_link_libraries(${IMPL_TARGET} PRIVATE ${LINKER_FLAGS} ${EXTRA_LIBS} ${MATH_LIB})
1338     if(WIN32)
1339         # This option is for static linking OpenAL Soft into another project
1340         # that already defines the IDs. It is up to that project to ensure all
1341         # required IDs are defined.
1342         option(ALSOFT_NO_UID_DEFS "Do not define GUIDs, IIDs, CLSIDs, or PropertyKeys" OFF)
1343         if(ALSOFT_NO_UID_DEFS)
1344             target_compile_definitions(${IMPL_TARGET} PRIVATE AL_NO_UID_DEFS)
1345         endif()
1346     endif()
1347 else()
1348     set(RC_CONFIG resources/openal32.rc)
1349     if(WIN32 AND ALSOFT_BUILD_ROUTER)
1350         add_library(OpenAL SHARED
1351             resources/router.rc
1352             router/router.cpp
1353             router/router.h
1354             router/alc.cpp
1355             router/al.cpp
1356         )
1357         target_compile_definitions(OpenAL
1358             PRIVATE AL_BUILD_LIBRARY AL_ALEXT_PROTOTYPES "ALC_API=${EXPORT_DECL}"
1359             "AL_API=${EXPORT_DECL}" ${CPP_DEFS})
1360         target_compile_options(OpenAL PRIVATE ${C_FLAGS})
1361         target_link_libraries(OpenAL PRIVATE common ${LINKER_FLAGS})
1362         target_include_directories(OpenAL
1363           PUBLIC
1364             $<BUILD_INTERFACE:${OpenAL_SOURCE_DIR}/include>
1365             $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
1366           PRIVATE
1367             ${OpenAL_SOURCE_DIR}/common
1368             ${OpenAL_BINARY_DIR}
1369         )
1370         set_target_properties(OpenAL PROPERTIES ${DEFAULT_TARGET_PROPS} PREFIX ""
1371             OUTPUT_NAME ${LIBNAME})
1372         if(TARGET build_version)
1373             add_dependencies(OpenAL build_version)
1374         endif()
1375         set(HAS_ROUTER 1)
1377         set(LIBNAME "soft_oal")
1378         set(IMPL_TARGET soft_oal)
1379         set(RC_CONFIG resources/soft_oal.rc)
1380     endif()
1382     # !important: for osx framework public header works, the headers must be in
1383     # the project
1384     set(TARGET_PUBLIC_HEADERS include/AL/al.h include/AL/alc.h include/AL/alext.h include/AL/efx.h
1385         include/AL/efx-presets.h)
1386     add_library(${IMPL_TARGET} SHARED ${OPENAL_OBJS} ${ALC_OBJS} ${CORE_OBJS} ${RC_CONFIG}
1387         ${TARGET_PUBLIC_HEADERS})
1388     if(WIN32)
1389         set_target_properties(${IMPL_TARGET} PROPERTIES PREFIX "")
1390     endif()
1391     target_link_libraries(${IMPL_TARGET} PRIVATE common ${LINKER_FLAGS} ${EXTRA_LIBS} ${MATH_LIB})
1393     if(APPLE AND ALSOFT_OSX_FRAMEWORK)
1394         # Sets framework name to soft_oal to avoid ambiguity with the system OpenAL.framework
1395         set(LIBNAME "soft_oal")
1396         if(GIT_FOUND)
1397             execute_process(COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
1398                 TIMEOUT 5
1399                 OUTPUT_VARIABLE BUNDLE_VERSION
1400                 OUTPUT_STRIP_TRAILING_WHITESPACE)
1401         else()
1402             set(BUNDLE_VERSION 1)
1403         endif()
1405         # Build: Fix rpath in iOS shared libraries
1406         # If this flag is not set, the final dylib binary ld path will be
1407         # /User/xxx/*/soft_oal.framework/soft_oal, which can't be loaded by iOS devices.
1408         # See also: https://github.com/libjpeg-turbo/libjpeg-turbo/commit/c80ddef7a4ce21ace9e3ca0fd190d320cc8cdaeb
1409         if(NOT CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG)
1410             set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")
1411         endif()
1413         set_target_properties(${IMPL_TARGET} PROPERTIES
1414             FRAMEWORK TRUE
1415             FRAMEWORK_VERSION C
1416             MACOSX_FRAMEWORK_NAME "${IMPL_TARGET}"
1417             MACOSX_FRAMEWORK_IDENTIFIER "org.openal-soft.openal"
1418             MACOSX_FRAMEWORK_SHORT_VERSION_STRING "${LIB_VERSION}"
1419             MACOSX_FRAMEWORK_BUNDLE_VERSION "${BUNDLE_VERSION}"
1420             XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ""
1421             XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO"
1422             XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO"
1423             PUBLIC_HEADER "${TARGET_PUBLIC_HEADERS}"
1424             MACOSX_RPATH TRUE)
1425     endif()
1426 endif()
1428 target_include_directories(${IMPL_TARGET}
1429   PUBLIC
1430     $<BUILD_INTERFACE:${OpenAL_SOURCE_DIR}/include>
1431   INTERFACE
1432     $<BUILD_INTERFACE:${OpenAL_SOURCE_DIR}/include/AL>
1433     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
1434     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/AL>
1435   PRIVATE
1436     ${INC_PATHS}
1437     ${OpenAL_BINARY_DIR}
1438     ${OpenAL_SOURCE_DIR}
1439     ${OpenAL_SOURCE_DIR}/common
1442 set_target_properties(${IMPL_TARGET} PROPERTIES ${DEFAULT_TARGET_PROPS}
1443     OUTPUT_NAME ${LIBNAME}
1444     VERSION ${LIB_VERSION}
1445     SOVERSION ${LIB_MAJOR_VERSION}
1447 target_compile_definitions(${IMPL_TARGET}
1448     PRIVATE AL_BUILD_LIBRARY AL_ALEXT_PROTOTYPES "ALC_API=${EXPORT_DECL}" "AL_API=${EXPORT_DECL}"
1449     ${CPP_DEFS})
1450 target_compile_options(${IMPL_TARGET} PRIVATE ${C_FLAGS})
1452 if(TARGET build_version)
1453     add_dependencies(${IMPL_TARGET} build_version)
1454 endif()
1456 if(WIN32 AND MINGW AND ALSOFT_BUILD_IMPORT_LIB AND NOT LIBTYPE STREQUAL "STATIC")
1457     find_program(SED_EXECUTABLE NAMES sed DOC "sed executable")
1458     if(NOT SED_EXECUTABLE OR NOT CMAKE_DLLTOOL)
1459         message(STATUS "")
1460         if(NOT SED_EXECUTABLE)
1461             message(STATUS "WARNING: Cannot find sed, disabling .def/.lib generation")
1462         endif()
1463         if(NOT CMAKE_DLLTOOL)
1464             message(STATUS "WARNING: Cannot find dlltool, disabling .def/.lib generation")
1465         endif()
1466     else()
1467         set_property(TARGET OpenAL APPEND_STRING PROPERTY
1468             LINK_FLAGS " -Wl,--output-def,OpenAL32.def")
1469         add_custom_command(TARGET OpenAL POST_BUILD
1470             COMMAND "${SED_EXECUTABLE}" -i -e "s/ @[^ ]*//" OpenAL32.def
1471             COMMAND "${CMAKE_DLLTOOL}" -d OpenAL32.def -l OpenAL32.lib -D OpenAL32.dll
1472             # Technically OpenAL32.def was created by the build, but cmake
1473             # doesn't recognize it due to -Wl,--output-def,OpenAL32.def being
1474             # manually specified. But declaring the file here allows it to be
1475             # properly cleaned, e.g. during make clean.
1476             BYPRODUCTS OpenAL32.def OpenAL32.lib
1477             COMMENT "Stripping ordinals from OpenAL32.def and generating OpenAL32.lib..."
1478             VERBATIM
1479         )
1480     endif()
1481 endif()
1483 if(HAS_ROUTER)
1484     message(STATUS "")
1485     message(STATUS "Building DLL router")
1486 endif()
1488 message(STATUS "")
1489 message(STATUS "Building OpenAL with support for the following backends:")
1490 message(STATUS "   ${BACKENDS}")
1491 message(STATUS "")
1492 message(STATUS "Building with support for CPU extensions:")
1493 message(STATUS "    ${CPU_EXTS}")
1494 message(STATUS "")
1495 if(FPMATH_SET)
1496     message(STATUS "Building with SSE${FPMATH_SET} codegen")
1497     message(STATUS "")
1498 endif()
1500 if(ALSOFT_EAX)
1501     message(STATUS "Building with legacy EAX extension support")
1502     message(STATUS "")
1503 endif()
1505 if(ALSOFT_EMBED_HRTF_DATA)
1506     message(STATUS "Embedding HRTF datasets")
1507     message(STATUS "")
1508 endif()
1510 # An alias for sub-project builds
1511 add_library(OpenAL::OpenAL ALIAS OpenAL)
1513 # Install main library
1514 if(ALSOFT_INSTALL)
1515     configure_package_config_file(OpenALConfig.cmake.in OpenALConfig.cmake
1516         INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/OpenAL)
1517     install(TARGETS OpenAL EXPORT OpenAL
1518         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
1519         LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
1520         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
1521         FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}
1522         INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${CMAKE_INSTALL_INCLUDEDIR}/AL)
1523     export(TARGETS OpenAL
1524         NAMESPACE OpenAL::
1525         FILE OpenALTargets.cmake)
1526     install(EXPORT OpenAL
1527         DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/OpenAL
1528         NAMESPACE OpenAL::
1529         FILE OpenALTargets.cmake)
1530     install(DIRECTORY include/AL
1531         DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
1532     install(FILES "${OpenAL_BINARY_DIR}/openal.pc"
1533         DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
1534     install(FILES "${OpenAL_BINARY_DIR}/OpenALConfig.cmake"
1535         DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/OpenAL")
1536     if(TARGET soft_oal)
1537         install(TARGETS soft_oal
1538             RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
1539     endif()
1540     message(STATUS "Installing library and headers")
1541 else()
1542     message(STATUS "NOT installing library and headers")
1543 endif()
1545 if(ALSOFT_INSTALL_CONFIG)
1546     install(FILES alsoftrc.sample
1547         DESTINATION ${CMAKE_INSTALL_DATADIR}/openal)
1548     message(STATUS "Installing sample configuration")
1549 endif()
1551 if(ALSOFT_INSTALL_HRTF_DATA)
1552     install(DIRECTORY hrtf
1553         DESTINATION ${CMAKE_INSTALL_DATADIR}/openal)
1554     message(STATUS "Installing HRTF data files")
1555 endif()
1557 if(ALSOFT_INSTALL_AMBDEC_PRESETS)
1558     install(DIRECTORY presets
1559         DESTINATION ${CMAKE_INSTALL_DATADIR}/openal)
1560     message(STATUS "Installing AmbDec presets")
1561 endif()
1562 message(STATUS "")
1564 set(UNICODE_FLAG )
1565 if(MINGW)
1566     set(UNICODE_FLAG ${UNICODE_FLAG} -municode)
1567 endif()
1568 set(EXTRA_INSTALLS )
1569 if(ALSOFT_UTILS)
1570     add_executable(openal-info utils/openal-info.c)
1571     target_include_directories(openal-info PRIVATE ${OpenAL_SOURCE_DIR}/common)
1572     target_compile_options(openal-info PRIVATE ${C_FLAGS})
1573     target_link_libraries(openal-info PRIVATE ${LINKER_FLAGS} OpenAL ${UNICODE_FLAG})
1574     set_target_properties(openal-info PROPERTIES ${DEFAULT_TARGET_PROPS})
1575     if(ALSOFT_INSTALL_EXAMPLES)
1576         set(EXTRA_INSTALLS ${EXTRA_INSTALLS} openal-info)
1577     endif()
1579     if(SNDFILE_FOUND)
1580         add_executable(uhjdecoder utils/uhjdecoder.cpp)
1581         target_compile_definitions(uhjdecoder PRIVATE ${CPP_DEFS})
1582         target_include_directories(uhjdecoder
1583             PRIVATE ${OpenAL_BINARY_DIR} ${OpenAL_SOURCE_DIR}/common)
1584         target_compile_options(uhjdecoder PRIVATE ${C_FLAGS})
1585         target_link_libraries(uhjdecoder PUBLIC common
1586             PRIVATE ${LINKER_FLAGS} SndFile::SndFile ${UNICODE_FLAG})
1587         set_target_properties(uhjdecoder PROPERTIES ${DEFAULT_TARGET_PROPS})
1589         add_executable(uhjencoder utils/uhjencoder.cpp)
1590         target_compile_definitions(uhjencoder PRIVATE ${CPP_DEFS})
1591         target_include_directories(uhjencoder
1592             PRIVATE ${OpenAL_BINARY_DIR} ${OpenAL_SOURCE_DIR}/common)
1593         target_compile_options(uhjencoder PRIVATE ${C_FLAGS})
1594         target_link_libraries(uhjencoder PUBLIC common
1595             PRIVATE ${LINKER_FLAGS} SndFile::SndFile ${UNICODE_FLAG})
1596         set_target_properties(uhjencoder PROPERTIES ${DEFAULT_TARGET_PROPS})
1597     endif()
1599     if(MYSOFA_FOUND)
1600         set(SOFA_SUPPORT_SRCS
1601             utils/sofa-support.cpp
1602             utils/sofa-support.h)
1603         add_library(sofa-support STATIC EXCLUDE_FROM_ALL ${SOFA_SUPPORT_SRCS})
1604         target_compile_definitions(sofa-support PRIVATE ${CPP_DEFS})
1605         target_include_directories(sofa-support PUBLIC ${OpenAL_SOURCE_DIR}/common)
1606         target_compile_options(sofa-support PRIVATE ${C_FLAGS})
1607         target_link_libraries(sofa-support PUBLIC common MySOFA::MySOFA PRIVATE ${LINKER_FLAGS})
1608         set_target_properties(sofa-support PROPERTIES ${DEFAULT_TARGET_PROPS})
1610         set(MAKEMHR_SRCS
1611             utils/makemhr/loaddef.cpp
1612             utils/makemhr/loaddef.h
1613             utils/makemhr/loadsofa.cpp
1614             utils/makemhr/loadsofa.h
1615             utils/makemhr/makemhr.cpp
1616             utils/makemhr/makemhr.h)
1617         if(NOT HAVE_GETOPT)
1618             set(MAKEMHR_SRCS  ${MAKEMHR_SRCS} utils/getopt.c utils/getopt.h)
1619         endif()
1620         add_executable(makemhr ${MAKEMHR_SRCS})
1621         target_compile_definitions(makemhr PRIVATE ${CPP_DEFS})
1622         target_include_directories(makemhr
1623             PRIVATE ${OpenAL_BINARY_DIR} ${OpenAL_SOURCE_DIR}/utils)
1624         target_compile_options(makemhr PRIVATE ${C_FLAGS})
1625         target_link_libraries(makemhr PRIVATE ${LINKER_FLAGS} sofa-support ${UNICODE_FLAG})
1626         set_target_properties(makemhr PROPERTIES ${DEFAULT_TARGET_PROPS})
1627         if(ALSOFT_INSTALL_EXAMPLES)
1628             set(EXTRA_INSTALLS ${EXTRA_INSTALLS} makemhr)
1629         endif()
1631         set(SOFAINFO_SRCS  utils/sofa-info.cpp)
1632         add_executable(sofa-info ${SOFAINFO_SRCS})
1633         target_compile_definitions(sofa-info PRIVATE ${CPP_DEFS})
1634         target_include_directories(sofa-info PRIVATE ${OpenAL_SOURCE_DIR}/utils)
1635         target_compile_options(sofa-info PRIVATE ${C_FLAGS})
1636         target_link_libraries(sofa-info PRIVATE ${LINKER_FLAGS} sofa-support ${UNICODE_FLAG})
1637         set_target_properties(sofa-info PROPERTIES ${DEFAULT_TARGET_PROPS})
1638     endif()
1639     message(STATUS "Building utility programs")
1641     if(NOT ALSOFT_NO_CONFIG_UTIL)
1642         add_subdirectory(utils/alsoft-config)
1643     endif()
1644     message(STATUS "")
1645 endif()
1648 # Add a static library with common functions used by multiple example targets
1649 add_library(ex-common STATIC EXCLUDE_FROM_ALL
1650     examples/common/alhelpers.c
1651     examples/common/alhelpers.h)
1652 target_compile_definitions(ex-common PUBLIC ${CPP_DEFS})
1653 target_include_directories(ex-common PUBLIC ${OpenAL_SOURCE_DIR}/common)
1654 target_compile_options(ex-common PUBLIC ${C_FLAGS})
1655 target_link_libraries(ex-common PUBLIC OpenAL PRIVATE ${RT_LIB})
1656 set_target_properties(ex-common PROPERTIES ${DEFAULT_TARGET_PROPS})
1658 if(ALSOFT_EXAMPLES)
1659     add_executable(altonegen examples/altonegen.c)
1660     target_link_libraries(altonegen PRIVATE ${LINKER_FLAGS} ${MATH_LIB} ex-common ${UNICODE_FLAG})
1661     set_target_properties(altonegen PROPERTIES ${DEFAULT_TARGET_PROPS})
1663     add_executable(alrecord examples/alrecord.c)
1664     target_link_libraries(alrecord PRIVATE ${LINKER_FLAGS} ex-common ${UNICODE_FLAG})
1665     set_target_properties(alrecord PROPERTIES ${DEFAULT_TARGET_PROPS})
1667     if(ALSOFT_INSTALL_EXAMPLES)
1668         set(EXTRA_INSTALLS ${EXTRA_INSTALLS} altonegen alrecord)
1669     endif()
1671     message(STATUS "Building example programs")
1673     if(SNDFILE_FOUND)
1674         add_executable(alplay examples/alplay.c)
1675         target_link_libraries(alplay PRIVATE ${LINKER_FLAGS} SndFile::SndFile ex-common
1676             ${UNICODE_FLAG})
1677         set_target_properties(alplay PROPERTIES ${DEFAULT_TARGET_PROPS})
1679         add_executable(alstream examples/alstream.c)
1680         target_link_libraries(alstream PRIVATE ${LINKER_FLAGS} SndFile::SndFile ex-common
1681             ${UNICODE_FLAG})
1682         set_target_properties(alstream PROPERTIES ${DEFAULT_TARGET_PROPS})
1684         add_executable(alreverb examples/alreverb.c)
1685         target_link_libraries(alreverb PRIVATE ${LINKER_FLAGS} SndFile::SndFile ex-common
1686             ${UNICODE_FLAG})
1687         set_target_properties(alreverb PROPERTIES ${DEFAULT_TARGET_PROPS})
1689         add_executable(almultireverb examples/almultireverb.c)
1690         target_link_libraries(almultireverb
1691             PRIVATE ${LINKER_FLAGS} SndFile::SndFile ex-common ${MATH_LIB} ${UNICODE_FLAG})
1692         set_target_properties(almultireverb PROPERTIES ${DEFAULT_TARGET_PROPS})
1694         add_executable(allatency examples/allatency.c)
1695         target_link_libraries(allatency PRIVATE ${LINKER_FLAGS} SndFile::SndFile ex-common
1696             ${UNICODE_FLAG})
1697         set_target_properties(allatency PROPERTIES ${DEFAULT_TARGET_PROPS})
1699         add_executable(alhrtf examples/alhrtf.c)
1700         target_link_libraries(alhrtf
1701             PRIVATE ${LINKER_FLAGS} SndFile::SndFile ex-common ${MATH_LIB} ${UNICODE_FLAG})
1702         set_target_properties(alhrtf PROPERTIES ${DEFAULT_TARGET_PROPS})
1704         add_executable(alstreamcb examples/alstreamcb.cpp)
1705         target_link_libraries(alstreamcb PRIVATE ${LINKER_FLAGS} SndFile::SndFile ex-common
1706             ${UNICODE_FLAG})
1707         set_target_properties(alstreamcb PROPERTIES ${DEFAULT_TARGET_PROPS})
1709         add_executable(alconvolve examples/alconvolve.c)
1710         target_link_libraries(alconvolve PRIVATE ${LINKER_FLAGS} common SndFile::SndFile ex-common
1711             ${UNICODE_FLAG})
1712         set_target_properties(alconvolve PROPERTIES ${DEFAULT_TARGET_PROPS})
1714         if(ALSOFT_INSTALL_EXAMPLES)
1715             set(EXTRA_INSTALLS ${EXTRA_INSTALLS} alplay alstream alreverb almultireverb allatency
1716                 alhrtf)
1717         endif()
1719         message(STATUS "Building SndFile example programs")
1720     endif()
1722     if(SDL2_FOUND)
1723         add_executable(alloopback examples/alloopback.c)
1724         target_link_libraries(alloopback
1725             PRIVATE ${LINKER_FLAGS} SDL2::SDL2 ex-common ${MATH_LIB})
1726         set_target_properties(alloopback PROPERTIES ${DEFAULT_TARGET_PROPS})
1728         if(ALSOFT_INSTALL_EXAMPLES)
1729             set(EXTRA_INSTALLS ${EXTRA_INSTALLS} alloopback)
1730         endif()
1732         message(STATUS "Building SDL example programs")
1734         set(FFVER_OK FALSE)
1735         if(FFMPEG_FOUND)
1736             set(FFVER_OK TRUE)
1737             if(AVFORMAT_VERSION VERSION_LESS "59.27.100")
1738                 message(STATUS "libavformat is too old! (${AVFORMAT_VERSION}, wanted 59.27.100)")
1739                 set(FFVER_OK FALSE)
1740             endif()
1741             if(AVCODEC_VERSION VERSION_LESS "59.37.100")
1742                 message(STATUS "libavcodec is too old! (${AVCODEC_VERSION}, wanted 59.37.100)")
1743                 set(FFVER_OK FALSE)
1744             endif()
1745             if(AVUTIL_VERSION VERSION_LESS "57.28.100")
1746                 message(STATUS "libavutil is too old! (${AVUTIL_VERSION}, wanted 57.28.100)")
1747                 set(FFVER_OK FALSE)
1748             endif()
1749             if(SWSCALE_VERSION VERSION_LESS "6.7.100")
1750                 message(STATUS "libswscale is too old! (${SWSCALE_VERSION}, wanted 6.7.100)")
1751                 set(FFVER_OK FALSE)
1752             endif()
1753             if(SWRESAMPLE_VERSION VERSION_LESS "4.7.100")
1754                 message(STATUS "libswresample is too old! (${SWRESAMPLE_VERSION}, wanted 4.7.100)")
1755                 set(FFVER_OK FALSE)
1756             endif()
1757         endif()
1758         if(FFVER_OK)
1759             add_executable(alffplay examples/alffplay.cpp)
1760             target_include_directories(alffplay PRIVATE ${FFMPEG_INCLUDE_DIRS})
1761             target_link_libraries(alffplay
1762                 PRIVATE ${LINKER_FLAGS} SDL2::SDL2 ${FFMPEG_LIBRARIES} ex-common)
1763             set_target_properties(alffplay PROPERTIES ${DEFAULT_TARGET_PROPS})
1765             if(ALSOFT_INSTALL_EXAMPLES)
1766                 set(EXTRA_INSTALLS ${EXTRA_INSTALLS} alffplay)
1767             endif()
1768             message(STATUS "Building SDL+FFmpeg example programs")
1769         endif()
1770     endif()
1771     message(STATUS "")
1772 endif()
1774 if(EXTRA_INSTALLS)
1775     install(TARGETS ${EXTRA_INSTALLS}
1776         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
1777         BUNDLE  DESTINATION ${CMAKE_INSTALL_BINDIR}
1778         LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
1779         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
1780 endif()