SDL: cmake: don't create relocatable packages by default with non-MSVC toolchains

From df1f1c2e12f8baf8ba06af50feca520a23980fb9 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 28 Dec 2024 02:04:00 +0100
Subject: [PATCH] cmake: don't create relocatable packages by default with
 non-MSVC toolchains

---
 CMakeLists.txt                  | 10 ++++++++--
 build-scripts/release-info.json |  6 ++++--
 cmake/sdltargets.cmake          | 16 ++++++++++------
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f5bcf2a76b5dd..dee9e2c201f80 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -172,6 +172,11 @@ if(WINDOWS OR MACOS OR IOS OR TVOS OR VISIONOS OR WATCHOS)
   set(SDL_SYSTEM_ICONV_DEFAULT OFF)
 endif()
 
+set(SDL_RELOCATABLE_DEFAULT OFF)
+if(MSVC)
+  set(SDL_RELOCATABLE_DEFAULT ON)
+endif()
+
 if(MSVC)
   if(NOT SDL_LIBC)
     # Make sure /RTC1 is disabled, otherwise it will use functions from the CRT
@@ -277,11 +282,12 @@ if(COMMAND SDL_Preseed_CMakeCache)
 endif()
 
 # Allow some projects to be built conditionally.
-set_option(SDL_DISABLE_INSTALL    "Disable installation of SDL3" ${SDL3_SUBPROJECT})
+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)
 cmake_dependent_option(SDL_DISABLE_INSTALL_DOCS "Install docs for SDL3" ON "NOT SDL_DISABLE_INSTALL;NOT SDL_FRAMEWORK" ON)
-set_option(SDL_DISABLE_UNINSTALL  "Disable uninstallation of SDL3" OFF)
+set_option(SDL_DISABLE_UNINSTALL   "Disable uninstallation of SDL3" OFF)
 cmake_dependent_option(SDL_PRESEED "Preseed CMake cache to speed up configuration" ON "${SDL_PRESEED_AVAILABLE}" OFF)
+cmake_dependent_option(SDL_RELOCATABLE "Create relocatable SDL package" ${SDL_RELOCATABLE_DEFAULT} "NOT SDL_DISABLE_INSTALL" OFF)
 
 cmake_dependent_option(SDL_DISABLE_ANDROID_JAR  "Disable creation of SDL3.jar" ${SDL3_SUBPROJECT} "ANDROID" ON)
 
diff --git a/build-scripts/release-info.json b/build-scripts/release-info.json
index b47c3d2ba6aef..ab103c8d36438 100644
--- a/build-scripts/release-info.json
+++ b/build-scripts/release-info.json
@@ -36,9 +36,10 @@
         "-DSDL_SHARED=ON",
         "-DSDL_STATIC=OFF",
         "-DSDL_DISABLE_INSTALL_DOCS=ON",
+        "-DSDL_RELOCATABLE=ON",
         "-DSDL_TEST_LIBRARY=ON",
-        "-DSDL_VENDOR_INFO=libsdl.org",
-        "-DSDL_TESTS=OFF"
+        "-DSDL_TESTS=OFF",
+        "-DSDL_VENDOR_INFO=libsdl.org"
       ],
       "shared-static": "args"
     },
@@ -100,6 +101,7 @@
         "-DSDL_TEST_LIBRARY=ON",
         "-DSDL_TESTS=OFF",
         "-DSDL_DISABLE_INSTALL_DOCS=ON",
+        "-DSDL_RELOCATABLE=ON",
         "-DSDL_VENDOR_INFO=libsdl.org"
       ],
       "files-lib": {
diff --git a/cmake/sdltargets.cmake b/cmake/sdltargets.cmake
index 781ca01c8bebd..d658eb3f8e04a 100644
--- a/cmake/sdltargets.cmake
+++ b/cmake/sdltargets.cmake
@@ -338,13 +338,17 @@ function(configure_sdl3_pc)
     message(STATUS "\"pkg-config --static --libs sdl3\" will return invalid information")
   endif()
 
-  # Calculate prefix relative to location of sdl3.pc
-  if(NOT IS_ABSOLUTE "${CMAKE_INSTALL_PREFIX}")
-    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_PREFIX}")
+  if(SDL_RELOCATABLE)
+    # Calculate prefix relative to location of sdl3.pc
+    if(NOT IS_ABSOLUTE "${CMAKE_INSTALL_PREFIX}")
+      set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_PREFIX}")
+    endif()
+    file(RELATIVE_PATH SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${CMAKE_INSTALL_PREFIX}/${SDL_PKGCONFIG_INSTALLDIR}" "${CMAKE_INSTALL_PREFIX}")
+    string(REGEX REPLACE "[/]+$" "" SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
+    set(SDL_PKGCONFIG_PREFIX "\${pcfiledir}/${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
+  else()
+    set(SDL_PKGCONFIG_PREFIX "${CMAKE_INSTALL_PREFIX}")
   endif()
-  file(RELATIVE_PATH SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${CMAKE_INSTALL_PREFIX}/${SDL_PKGCONFIG_INSTALLDIR}" "${CMAKE_INSTALL_PREFIX}")
-  string(REGEX REPLACE "[/]+$" "" SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
-  set(SDL_PKGCONFIG_PREFIX "\${pcfiledir}/${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
 
   if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
     set(INCLUDEDIR_FOR_PKG_CONFIG "${CMAKE_INSTALL_INCLUDEDIR}")