SDL: cmake: build tests against installed SDL prefix

From 75a71bde4b471a1b6c335538007dc015b7a0d89d Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Wed, 2 Aug 2023 16:03:32 +0200
Subject: [PATCH] cmake: build tests against installed SDL prefix

---
 cmake/test/CMakeLists.txt        | 20 +++++++++++++++++---
 test/CMakeLists.txt              |  7 ++++++-
 test/testautomation_intrinsics.c |  2 ++
 test/testautomation_pen.c        | 21 +++++++++++++++++++++
 test/testevdev.c                 |  2 ++
 5 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt
index f03590f20e74a..d3fd8a09ad805 100644
--- a/cmake/test/CMakeLists.txt
+++ b/cmake/test/CMakeLists.txt
@@ -1,4 +1,4 @@
-# This cmake build script is meant for verifying the various CMake configuration script.
+# This cmake build script is meant for verifying the various CMake configuration scripts.
 
 cmake_minimum_required(VERSION 3.12)
 project(sdl_test LANGUAGES C)
@@ -35,9 +35,14 @@ add_feature_info("TEST_STATIC" TEST_STATIC "Test linking with static library")
 option(TEST_TEST "Test linking to SDL3_test library" ON)
 add_feature_info("TEST_TEST" TEST_STATIC "Test linking to SDL test library")
 
+option(TEST_FULL "Run complete SDL test suite" OFF)
+add_feature_info("TEST_FULL" TEST_FULL "Build full SDL testsuite")
+
 find_package(SDL3 REQUIRED CONFIG COMPONENTS Headers)
-add_library(headers_test OBJECT inc_sdl_slash.c inc_sdl_noslash.c)
-target_link_libraries(headers_test PRIVATE SDL3::Headers)
+add_library(headers_test_slash OBJECT inc_sdl_slash.c)
+target_link_libraries(headers_test_slash PRIVATE SDL3::Headers)
+add_library(headers_test_noslash OBJECT inc_sdl_noslash.c)
+target_link_libraries(headers_test_noslash PRIVATE SDL3::Headers)
 
 if(TEST_SHARED)
     find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3-shared)
@@ -93,6 +98,15 @@ find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
 add_executable(gui-whatever WIN32 main_gui.c)
 target_link_libraries(gui-whatever PRIVATE SDL3::SDL3)
 
+if(TEST_FULL)
+    enable_testing()
+    set(SDL_TESTS_TIMEOUT_MULTIPLIER "1" CACHE STRING "Test timeout multiplier")
+    set(SDL_TESTS_LINK_SHARED ${TEST_SHARED})
+
+    add_definitions(-DNO_BUILD_CONFIG)
+    add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../../test" SDL_test)
+endif()
+
 if(ANDROID)
     find_package(SDL3 REQUIRED CONFIG COMPONENTS Jar)
 endif()
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index fea1130eb2430..abf1ec22f26b9 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -2,7 +2,12 @@
 # CMake script for building the SDL tests
 #
 
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake")
+
+include(CheckIncludeFile)
 include(CheckStructHasMember)
+include(CMakePushCheckState)
+include(sdlcompilers)
 
 if(SDL_TESTS_LINK_SHARED)
     set(sdl_name_component SDL3-shared)
@@ -220,7 +225,7 @@ if(HAVE_LIBUDEV_H)
 endif()
 
 set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE)
-include("${SDL3_SOURCE_DIR}/cmake/FindFFmpeg.cmake")
+include("${CMAKE_CURRENT_LIST_DIR}/../cmake/FindFFmpeg.cmake")
 if(FFmpeg_FOUND)
     cmake_push_check_state()
     list(APPEND CMAKE_REQUIRED_INCLUDES "${FFmpeg_AVUTIL_INCLUDE_DIRS}")
diff --git a/test/testautomation_intrinsics.c b/test/testautomation_intrinsics.c
index 8c1dc7a7a3445..98dabf0fb5553 100644
--- a/test/testautomation_intrinsics.c
+++ b/test/testautomation_intrinsics.c
@@ -2,8 +2,10 @@
  * Intrinsics test suite
  */
 
+#ifndef NO_BUILD_CONFIG
 /* Disable intrinsics that are unsupported by the current compiler */
 #include <build_config/SDL_build_config.h>
+#endif
 
 #include <SDL3/SDL.h>
 #include <SDL3/SDL_intrin.h>
diff --git a/test/testautomation_pen.c b/test/testautomation_pen.c
index f5c3bff981417..67cdebb1a0383 100644
--- a/test/testautomation_pen.c
+++ b/test/testautomation_pen.c
@@ -19,6 +19,7 @@
   3. This notice may not be removed or altered from any source distribution.
 */
 
+#ifndef NO_BUILD_CONFIG
 #include <stddef.h>
 
 /**
@@ -1907,3 +1908,23 @@ SDLTest_TestSuiteReference penTestSuite = {
     penTests,
     NULL
 };
+
+#else
+
+#include <SDL3/SDL_test.h>
+#include "testautomation_suites.h"
+
+/* Sequence of Mouse test cases */
+static const SDLTest_TestCaseReference *penTests[] = {
+    NULL
+};
+
+/* Mouse test suite (global) */
+SDLTest_TestSuiteReference penTestSuite = {
+    "Pen",
+    NULL,
+    penTests,
+    NULL
+};
+
+#endif
diff --git a/test/testevdev.c b/test/testevdev.c
index a991d48381db6..d086e45457b7a 100644
--- a/test/testevdev.c
+++ b/test/testevdev.c
@@ -22,7 +22,9 @@
 #endif
 #define SDL_DYNAMIC_API 0
 
+#ifndef NO_BUILD_CONFIG
 #include "../src/SDL_internal.h"
+#endif
 
 #include <stdio.h>
 #include <string.h>