SDL: test: Add a `make install` target for the tests

From 38ef6789909c04bb688731fbc3c5433f7fb5bc81 Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Wed, 25 Nov 2020 13:30:29 +0000
Subject: [PATCH] test: Add a `make install` target for the tests

This makes it more convenient to compile them alongside SDL, install
them in an optional package and use them as smoke-tests or diagnostic
tools. The default installation directory is taken from GNOME's
installed-tests, which seems as good a convention as any other:
https://wiki.gnome.org/Initiatives/GnomeGoals/InstalledTests

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 CMakeLists.txt      |  1 +
 test/CMakeLists.txt | 87 +++++++++++++++++++++++++++++++++++++++++++++
 test/Makefile.in    | 15 ++++++++
 test/configure.ac   |  9 +++++
 4 files changed, 112 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5c695deac47..ff430ab4c63 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -472,6 +472,7 @@ set(SDL_STATIC ${SDL_STATIC_ENABLED_BY_DEFAULT} CACHE BOOL "Build a static versi
 
 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)
+set_option(SDL_INSTALL_TESTS   "Install test-cases" OFF)
 
 if(VITA)
   set_option(VIDEO_VITA_PIB  "Build with PSVita piglet gles2 support" OFF)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index f8a21fcf731..0a7b8a12c43 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.0.0)
 project(SDL2 C)
 include(CTest)
 
+if(SDL_INSTALL_TESTS)
+    include(GNUInstallDirs)
+endif()
+
 # Global settings for all of the test targets
 # FIXME: is this wrong?
 remove_definitions(-DUSING_GENERATED_CONFIG_H)
@@ -136,6 +140,77 @@ add_executable(controllermap controllermap.c testutils.c)
 add_executable(testvulkan testvulkan.c)
 add_executable(testoffscreen testoffscreen.c)
 
+SET(ALL_TESTS
+    checkkeys
+    checkkeysthreads
+    controllermap
+    loopwave
+    loopwavequeue
+    testatomic
+    testaudiocapture
+    testaudiohotplug
+    testaudioinfo
+    testautomation
+    testbounds
+    testcustomcursor
+    testdisplayinfo
+    testdraw2
+    testdrawchessboard
+    testdropfile
+    testerror
+    testfile
+    testfilesystem
+    testgamecontroller
+    testgeometry
+    testgesture
+    testgl2
+    testgles
+    testgles2
+    testhaptic
+    testhittesting
+    testhotplug
+    testiconv
+    testime
+    testintersections
+    testjoystick
+    testkeys
+    testloadso
+    testlocale
+    testlock
+    testmessage
+    testmouse
+    testmultiaudio
+    testnative
+    testoffscreen
+    testoverlay2
+    testplatform
+    testpower
+    testqsort
+    testrelative
+    testrendercopyex
+    testrendertarget
+    testresample
+    testrumble
+    testscale
+    testsem
+    testsensor
+    testshader
+    testshape
+    testsprite2
+    testspriteminimal
+    teststreaming
+    testsurround
+    testthread
+    testtimer
+    testurl
+    testver
+    testviewport
+    testvulkan
+    testwm2
+    testyuv
+    torturethread
+)
+
 set(NONINTERACTIVE
     testatomic
     testerror
@@ -151,6 +226,7 @@ set(NONINTERACTIVE
 )
 
 if(LINUX)
+    list(APPEND ALL_TESTS testevdev)
     list(APPEND NONINTERACTIVE testevdev)
 endif()
 
@@ -354,3 +430,14 @@ foreach(TESTCASE ${NONINTERACTIVE} ${NEEDS_AUDIO} ${NEEDS_DISPLAY})
         PROPERTIES ENVIRONMENT "${TESTS_ENVIRONMENT}"
     )
 endforeach()
+
+if(SDL_INSTALL_TESTS)
+    install(
+        TARGETS ${ALL_TESTS}
+        DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL2
+    )
+    install(
+        FILES ${RESOURCE_FILES}
+        DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL2
+    )
+endif()
diff --git a/test/Makefile.in b/test/Makefile.in
index 8c11dbf88e3..d14e0735800 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -2,6 +2,15 @@
 
 srcdir  = @srcdir@
 
+prefix = @prefix@
+exec_prefix = @exec_prefix@
+bindir = @bindir@
+libdir = @libdir@
+libexecdir = @libexecdir@
+includedir = @includedir@
+datarootdir = @datarootdir@
+datadir = @datadir@
+
 CC      = @CC@
 EXE	= @EXE@
 CFLAGS  = @CFLAGS@ -g
@@ -81,6 +90,12 @@ TARGETS = \
 
 all: Makefile $(TARGETS) copydatafiles
 
+installedtestsdir = $(libexecdir)/installed-tests/SDL2
+
+install: all
+	install -D -t $(DESTDIR)$(installedtestsdir) $(TARGETS)
+	install -m644 -D -t $(DESTDIR)$(installedtestsdir) $(DATA)
+
 Makefile: $(srcdir)/Makefile.in
 	$(SHELL) config.status $@
 
diff --git a/test/configure.ac b/test/configure.ac
index 694551c6135..3d37b6abdb6 100644
--- a/test/configure.ac
+++ b/test/configure.ac
@@ -99,6 +99,15 @@ AC_SUBST(ISWINDOWS)
 AC_SUBST(ISUNIX)
 AC_SUBST(ISOS2)
 
+AC_SUBST([prefix])
+AC_SUBST([exec_prefix])
+AC_SUBST([bindir])
+AC_SUBST([libdir])
+AC_SUBST([libexecdir])
+AC_SUBST([includedir])
+AC_SUBST([datarootdir])
+AC_SUBST([datadir])
+
 dnl Check for SDL
 SDL_VERSION=2.0.18
 AM_PATH_SDL2($SDL_VERSION,