SDL_mixer: cmake: add FindOpus.cmake

From b5082e294048c4a223ec424a2a1f620ae2ad881a Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 12 Jan 2024 19:18:16 +0100
Subject: [PATCH] cmake: add FindOpus.cmake

---
 CMakeLists.txt           |  1 +
 cmake/FindOpus.cmake     | 51 ++++++++++++++++++++++++++++++++++++++++
 cmake/FindOpusFile.cmake |  6 ++---
 3 files changed, 55 insertions(+), 3 deletions(-)
 create mode 100644 cmake/FindOpus.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fd33e6b1..2dc2b957 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1147,6 +1147,7 @@ if(SDL3MIXER_INSTALL)
             cmake/Findlibxmp-lite.cmake
             cmake/Findmodplug.cmake
             cmake/FindOgg.cmake
+            cmake/FindOpus.cmake
             cmake/FindOpusFile.cmake
             cmake/Findmpg123.cmake
             cmake/FindVorbis.cmake
diff --git a/cmake/FindOpus.cmake b/cmake/FindOpus.cmake
new file mode 100644
index 00000000..030bb987
--- /dev/null
+++ b/cmake/FindOpus.cmake
@@ -0,0 +1,51 @@
+include(FindPackageHandleStandardArgs)
+
+find_package(PkgConfig QUIET)
+pkg_check_modules(PC_OPUS QUIET opus)
+
+find_library(Opus_LIBRARY
+    NAMES opus
+    HINTS ${PC_OPUS_LIBDIR}
+)
+
+find_path(Opus_INCLUDE_PATH
+    NAMES opus.h
+    PATH_SUFFIXES opus
+    HINTS ${PC_OPUS_INCLUDEDIR}
+)
+list(APPEND Opus_INCLUDE_PATH "${Opus_INCLUDE_PATH}/opus")
+
+if(PC_OPUS_FOUND)
+    get_flags_from_pkg_config("${Opus_LIBRARY}" "PC_OPUS" "_opus")
+endif()
+
+set(Opus_COMPILE_OPTIONS "${_opus_compile_options}" CACHE STRING "Extra compile options of opus")
+
+set(Opus_LINK_LIBRARIES "${_opus_link_libraries}" CACHE STRING "Extra link libraries of opus")
+
+set(Opus_LINK_OPTIONS "${_opus_link_options}" CACHE STRING "Extra link flags of opus")
+
+set(Opus_LINK_DIRECTORIES "${_opus_link_directories}" CACHE PATH "Extra link directories of opus")
+
+find_package(Ogg ${required})
+
+find_package_handle_standard_args(Opus
+    REQUIRED_VARS Opus_LIBRARY Opus_INCLUDE_PATH Ogg_FOUND
+)
+
+if(Opus_FOUND)
+    set(Opus_dirs ${Opus_INCLUDE_PATH})
+    if(NOT TARGET Opus::opus)
+        add_library(Opus::opus UNKNOWN IMPORTED)
+        set_target_properties(Opus::opus PROPERTIES
+            IMPORTED_LOCATION "${Opus_LIBRARY}"
+            INTERFACE_INCLUDE_DIRECTORIES "${Opus_dirs}"
+            INTERFACE_COMPILE_OPTIONS "${Opus_COMPILE_OPTIONS}:$<TARGET_PROPERTY:Ogg::ogg,INTERFACE_INCLUDE_DIRECTORIES>"
+            INTERFACE_LINK_LIBRARIES "${Opus_LINK_LIBRARIES}"
+            INTERFACE_LINK_OPTIONS "${Opus_LINK_OPTIONS}"
+            INTERFACE_LINK_DIRECTORIES "${Opus_LINK_DIRECTORIES}"
+        )
+    endif()
+endif()
+
+set(Opus_INCLUDE_DIRS ${Opus_INCLUDE_PATH})
diff --git a/cmake/FindOpusFile.cmake b/cmake/FindOpusFile.cmake
index 410c1e17..3936bb5f 100644
--- a/cmake/FindOpusFile.cmake
+++ b/cmake/FindOpusFile.cmake
@@ -25,10 +25,10 @@ set(OpusFile_LINK_OPTIONS "${_opusfile_link_options}" CACHE STRING "Extra link f
 
 set(OpusFile_LINK_DIRECTORIES "${_opusfile_link_directories}" CACHE PATH "Extra link directories of opusfile")
 
-find_package(Ogg ${required})
+find_package(Opus)
 
 find_package_handle_standard_args(OpusFile
-    REQUIRED_VARS OpusFile_LIBRARY OpusFile_INCLUDE_PATH Ogg_FOUND
+    REQUIRED_VARS OpusFile_LIBRARY OpusFile_INCLUDE_PATH Opus_FOUND
 )
 
 if(OpusFile_FOUND)
@@ -40,7 +40,7 @@ if(OpusFile_FOUND)
         add_library(OpusFile::opusfile UNKNOWN IMPORTED)
         set_target_properties(OpusFile::opusfile PROPERTIES
             IMPORTED_LOCATION "${OpusFile_LIBRARY}"
-            INTERFACE_INCLUDE_DIRECTORIES "${OpusFile_dirs};$<TARGET_PROPERTY:Ogg::ogg,INTERFACE_INCLUDE_DIRECTORIES>"
+            INTERFACE_INCLUDE_DIRECTORIES "${OpusFile_dirs};$<TARGET_PROPERTY:Opus::opus,INTERFACE_INCLUDE_DIRECTORIES>"
             INTERFACE_COMPILE_OPTIONS "${OpusFile_COMPILE_OPTIONS}"
             INTERFACE_LINK_LIBRARIES "${OpusFile_LINK_LIBRARIES}"
             INTERFACE_LINK_OPTIONS "${OpusFile_LINK_OPTIONS}"