SDL: cmake: add -Wl,-rpath,${libdir} to Libs section of pc file for Apple platforms

From e03746b25f485fdf8637cbcb3126dc2c52bd32a7 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 25 Feb 2024 16:33:46 +0100
Subject: [PATCH] cmake: add -Wl,-rpath,${libdir} to Libs section of pc file
 for Apple platforms

---
 CMakeLists.txt | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8f3bd5e27192..f4e675b18c70 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -262,6 +262,12 @@ if(SDL_FRAMEWORK)
   set(SDL_STATIC_AVAILABLE FALSE)
 endif()
 
+if(UNIX AND NOT ANDROID AND NOT RISCOS AND NOT SDL_FRAMEWORK)
+  set(SDL_RPATH_DEFAULT ON)
+else()
+  set(SDL_RPATH_DEFAULT OFF)
+endif()
+
 # Allow some projects to be built conditionally.
 set_option(SDL_DISABLE_INSTALL    "Disable installation of SDL3" ${SDL3_SUBPROJECT})
 cmake_dependent_option(SDL_DISABLE_INSTALL_CPACK "Create binary SDL3 archive using CPack" ${SDL3_SUBPROJECT} "NOT SDL_DISABLE_INSTALL" ON)
@@ -312,7 +318,7 @@ dep_option(SDL_PULSEAUDIO          "Use PulseAudio" ${UNIX_SYS} "SDL_AUDIO" OFF)
 dep_option(SDL_PULSEAUDIO_SHARED   "Dynamically load PulseAudio support" ON "SDL_PULSEAUDIO" OFF)
 dep_option(SDL_SNDIO               "Support the sndio audio API" ${UNIX_SYS} "SDL_AUDIO" OFF)
 dep_option(SDL_SNDIO_SHARED        "Dynamically load the sndio audio API" ON "SDL_SNDIO" OFF)
-set_option(SDL_RPATH               "Use an rpath when linking SDL" ${UNIX_SYS})
+set_option(SDL_RPATH               "Use an rpath when linking SDL" ${SDL_RPATH_DEFAULT})
 set_option(SDL_CLOCK_GETTIME       "Use clock_gettime() instead of gettimeofday()" ${SDL_CLOCK_GETTIME_DEFAULT})
 dep_option(SDL_X11                 "Use X11 video driver" ${UNIX_SYS} "SDL_VIDEO" OFF)
 dep_option(SDL_X11_SHARED          "Dynamically load X11 support" ON "SDL_X11" OFF)
@@ -1763,10 +1769,11 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
       else()
         set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir}")
       endif()
+      set(HAVE_RPATH TRUE)
     elseif(SOLARIS)
       set(SDL_RLD_FLAGS "-R\${libdir}")
+      set(HAVE_RPATH TRUE)
     endif()
-    set(HAVE_RPATH TRUE)
   endif()
 
   if(QNX)
@@ -2347,6 +2354,11 @@ elseif(APPLE)
 
   CheckPTHREAD()
 
+  if(SDL_RPATH AND SDL_SHARED)
+    set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir}")
+    set(HAVE_RPATH TRUE)
+  endif()
+
 elseif(HAIKU)
   if(SDL_AUDIO)
     set(SDL_AUDIO_DRIVER_HAIKU 1)