SDL_mixer: CMake: Add and install a helper for pkg-config.

From dceb5a8add128dbe6a8a65cba0dd0171471649be Mon Sep 17 00:00:00 2001
From: Pierre Wendling <[EMAIL REDACTED]>
Date: Tue, 7 Mar 2023 16:22:01 -0500
Subject: [PATCH] CMake: Add and install a helper for pkg-config.

---
 CMakeLists.txt              |  1 +
 cmake/PkgConfigHelper.cmake | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 cmake/PkgConfigHelper.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fd685a3f..681aa192 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -932,6 +932,7 @@ if(SDL3MIXER_INSTALL)
         FILES
             "${CMAKE_CURRENT_BINARY_DIR}/SDL3_mixerConfig.cmake"
             "${CMAKE_CURRENT_BINARY_DIR}/SDL3_mixerConfigVersion.cmake"
+            "${CMAKE_CURRENT_LIST_DIR}/cmake/PkgConfigHelper.cmake"
             cmake/FindFLAC.cmake
             cmake/FindFluidSynth.cmake
             cmake/Findgme.cmake
diff --git a/cmake/PkgConfigHelper.cmake b/cmake/PkgConfigHelper.cmake
new file mode 100644
index 00000000..7070fac7
--- /dev/null
+++ b/cmake/PkgConfigHelper.cmake
@@ -0,0 +1,34 @@
+# Helper for Find modules
+
+function(get_flags_from_pkg_config _library _pc_prefix _out_prefix)
+  if("${_library}" MATCHES "${CMAKE_STATIC_LIBRARY_SUFFIX}$")
+    set(_cflags ${_pc_prefix}_STATIC_CFLAGS_OTHER)
+    set(_link_libraries ${_pc_prefix}_STATIC_LIBRARIES)
+    set(_link_options ${_pc_prefix}_STATIC_LDFLAGS_OTHER)
+    set(_library_dirs ${_pc_prefix}_STATIC_LIBRARY_DIRS)
+  else()
+    set(_cflags ${_pc_prefix}_CFLAGS_OTHER)
+    set(_link_libraries ${_pc_prefix}_LIBRARIES)
+    set(_link_options ${_pc_prefix}_LDFLAGS_OTHER)
+    set(_library_dirs ${_pc_prefix}_LIBRARY_DIRS)
+  endif()
+
+  # The *_LIBRARIES lists always start with the library itself
+  list(POP_FRONT "${_link_libraries}")
+
+  # Work around CMake's flag deduplication when pc files use `-framework A` instead of `-Wl,-framework,A`
+  string(REPLACE "-framework;" "-Wl,-framework," "_filtered_link_options" "${${_link_options}}")
+
+  set(${_out_prefix}_compile_options
+      "${${_cflags}}"
+      PARENT_SCOPE)
+  set(${_out_prefix}_link_libraries
+      "${${_link_libraries}}"
+      PARENT_SCOPE)
+  set(${_out_prefix}_link_options
+      "${_filtered_link_options}"
+      PARENT_SCOPE)
+  set(${_out_prefix}_link_directories
+      "${${_library_dirs}}"
+      PARENT_SCOPE)
+endfunction()