SDL: cmake: install SDL2::SDL2test when configuring with SDL2_TEST enabled

From 1087eaad7ef3328a03ec7ff9b0afe598be12986b Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 27 May 2022 01:40:11 +0200
Subject: [PATCH] cmake: install SDL2::SDL2test when configuring with SDL2_TEST
 enabled

---
 CMakeLists.txt       | 31 ++++++++++++++++++++++++++++---
 SDL2Config.cmake     |  3 +++
 docs/README-cmake.md |  2 +-
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1ac60d07ce0..cb158c10bf4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -346,6 +346,7 @@ if(EMSCRIPTEN)
   set(SDL_ATOMIC_ENABLED_BY_DEFAULT OFF)
   set(SDL_LOADSO_ENABLED_BY_DEFAULT OFF)
   set(SDL_CPUINFO_ENABLED_BY_DEFAULT OFF)
+  set(SDL_TEST_ENABLED_BY_DEFAULT OFF)
 endif()
 
 if(VITA OR PSP)
@@ -372,6 +373,10 @@ if (NOT DEFINED SDL_SHARED_ENABLED_BY_DEFAULT)
   endif()
 endif()
 
+if (NOT DEFINED SDL_TEST_ENABLED_BY_DEFAULT)
+  set(SDL_TEST_ENABLED_BY_DEFAULT ON)
+endif()
+
 set(LONGESTOPTIONNAME 0)  # set_option and friends will change this.
 
 set(SDL_SUBSYSTEMS
@@ -469,9 +474,10 @@ set_option(SDL_ASAN                "Use AddressSanitizer to detect memory errors
 
 set(SDL_SHARED ${SDL_SHARED_ENABLED_BY_DEFAULT} CACHE BOOL "Build a shared version of the library")
 set(SDL_STATIC ${SDL_STATIC_ENABLED_BY_DEFAULT} CACHE BOOL "Build a static version of the library")
+set(SDL_TEST   ${SDL_TEST_ENABLED_BY_DEFAULT} CACHE BOOL "Build the SDL2_test library")
 
 dep_option(SDL_STATIC_PIC      "Static version of the library should be built with Position Independent Code" OFF "SDL_STATIC" OFF)
-set_option(SDL_TEST            "Build the test directory" OFF)
+dep_option(SDL_TESTS           "Build the test directory" OFF SDL_TEST OFF)
 set_option(SDL_INSTALL_TESTS   "Install test-cases" OFF)
 
 if(VITA)
@@ -2987,11 +2993,14 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSDL_BUILD_MICRO_VERSION=${SDL_MICRO_VERSIO
 ##### Tests #####
 
 if(SDL_TEST)
-  include(CTest)
   include_directories(BEFORE "${SDL2_BINARY_DIR}/include")
   include_directories(AFTER "${SDL2_SOURCE_DIR}/include")
   file(GLOB TEST_SOURCES ${SDL2_SOURCE_DIR}/src/test/*.c)
   add_library(SDL2_test STATIC ${TEST_SOURCES})
+  add_library(SDL2::SDL2test ALIAS SDL2_test)
+  set_target_properties(SDL2_test PROPERTIES
+      EXPORT_NAME SDL2test)
+  target_include_directories(SDL2_test PUBLIC "$<BUILD_INTERFACE:${SDL2_SOURCE_DIR}/include>" $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL2>)
 endif()
 
 ##### Installation targets #####
@@ -3017,6 +3026,13 @@ if(NOT SDL2_DISABLE_INSTALL)
       RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
   endif()
 
+  if(SDL_TEST)
+    install(TARGETS SDL2_test EXPORT SDL2testTargets
+      LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+      ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+      RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
+  endif()
+
   ##### Export files #####
   if (WINDOWS AND NOT MINGW)
     set(PKG_PREFIX "cmake")
@@ -3054,6 +3070,14 @@ if(NOT SDL2_DISABLE_INSTALL)
     )
   endif()
 
+  if(SDL_TEST)
+    install(EXPORT SDL2testTargets
+      FILE SDL2testTargets.cmake
+      NAMESPACE SDL2::
+      DESTINATION ${PKG_PREFIX}
+    )
+  endif()
+
   install(
     FILES
       ${CMAKE_CURRENT_SOURCE_DIR}/SDL2Config.cmake
@@ -3119,7 +3143,8 @@ endif()
 
 ##### Tests subproject (must appear after the install/uninstall targets) #####
 
-if(SDL_TEST)
+if(SDL_TESTS)
+  enable_testing()
   add_subdirectory(test)
 endif()
 
diff --git a/SDL2Config.cmake b/SDL2Config.cmake
index 3e4ebe1e845..39be60b639d 100644
--- a/SDL2Config.cmake
+++ b/SDL2Config.cmake
@@ -7,6 +7,9 @@ endif()
 if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL2staticTargets.cmake")
   include("${CMAKE_CURRENT_LIST_DIR}/SDL2staticTargets.cmake")
 endif()
+if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL2testTargets.cmake")
+  include("${CMAKE_CURRENT_LIST_DIR}/SDL2testTargets.cmake")
+endif()
 
 # on static-only builds create an alias
 if(NOT TARGET SDL2::SDL2 AND TARGET SDL2::SDL2-static)
diff --git a/docs/README-cmake.md b/docs/README-cmake.md
index b13f2d278bc..03fd2442116 100644
--- a/docs/README-cmake.md
+++ b/docs/README-cmake.md
@@ -73,7 +73,7 @@ To use, set the following CMake variables when running CMake's configuration sta
 
 - for iOS-Simulator, using the latest, installed SDK, and building SDL test apps (as .app bundles):
 
-    `cmake ~/sdl -DSDL_TEST=1 -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64`
+    `cmake ~/sdl -DSDL_TESTS=1 -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64`
 
 - for tvOS-Simulator, using the latest, installed SDK: