SDL: Make the sdl2-config script relocatable

From 799d39a3022415f698cb92f44b872c8513d83249 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 3 Jun 2022 23:08:22 +0200
Subject: [PATCH] Make the sdl2-config script relocatable

---
 CMakeLists.txt | 1 +
 configure      | 5 +++++
 configure.ac   | 5 +++++
 sdl2-config.in | 6 +++++-
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e434a8acd99..8c512bb2924 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2697,6 +2697,7 @@ configure_file("${SDL2_SOURCE_DIR}/include/SDL_revision.h.cmake"
 if(NOT WINDOWS OR CYGWIN OR MINGW)
 
   set(prefix ${CMAKE_INSTALL_PREFIX})
+  file(RELATIVE_PATH bin_prefix_relpath "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_PREFIX}")
 
   set(exec_prefix "\${prefix}")
   set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
diff --git a/configure b/configure
index 9d37869e611..027fa94cb05 100755
--- a/configure
+++ b/configure
@@ -653,6 +653,7 @@ SDL_RLD_FLAGS
 SDL_STATIC_LIBS
 SDL_LIBS
 SDL_CFLAGS
+bin_prefix_relpath
 cmake_prefix_relpath
 INSTALL_SDL2_CONFIG
 LIBUSB_LIBS
@@ -27058,6 +27059,10 @@ eval pkg_cmakedir=$libdir/cmake/SDL2
 cmake_prefix_relpath="$(echo -n "$pkg_cmakedir" | sed -E "s#^$pkg_prefix##" | sed -E "s#[A-Za-z0-9_ .-]+#..#g" )"
 
 
+eval pkg_bindir=$bindir
+bin_prefix_relpath="$(echo -n "pkg_bindir" | sed -E "s#^$pkg_prefix##" | sed -E "s#[A-Za-z0-9_ .-]+#..#g" )"
+
+
 
 
 
diff --git a/configure.ac b/configure.ac
index b81286efe51..2686db6b32b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4694,6 +4694,11 @@ eval pkg_cmakedir=$libdir/cmake/SDL2
 cmake_prefix_relpath="$(echo -n "$pkg_cmakedir" | sed -E "s#^$pkg_prefix##" | sed -E "s#[A-Za-z0-9_ .-]+#..#g" )"
 AC_SUBST([cmake_prefix_relpath])
 
+dnl Calculate the location of the prefix, relative to bindir
+eval pkg_bindir=$bindir
+bin_prefix_relpath="$(echo -n "pkg_bindir" | sed -E "s#^$pkg_prefix##" | sed -E "s#[A-Za-z0-9_ .-]+#..#g" )"
+AC_SUBST([bin_prefix_relpath])
+
 dnl Expand the cflags and libraries needed by apps using SDL
 AC_SUBST(SDL_CFLAGS)
 AC_SUBST(SDL_LIBS)
diff --git a/sdl2-config.in b/sdl2-config.in
index 5a2aed292b9..f6e5a50b3fb 100644
--- a/sdl2-config.in
+++ b/sdl2-config.in
@@ -1,6 +1,10 @@
 #!/bin/sh
 
-prefix=@prefix@
+# Get the canonical path of the folder containing this script
+bindir=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)")
+
+# Calculate the canonical path of the prefix, relative to the folder of this script
+prefix=$(cd -P -- "$bindir/@bin_prefix_relpath@" && printf '%s\n' "$(pwd -P)")
 exec_prefix=@exec_prefix@
 exec_prefix_set=no
 libdir=@libdir@