sdl2-compat: cmake: do test build in c++ mode to verify SDL3 symbols

From 75e51dd0faacd7b6aaa3e2ccc86decaa6c86e313 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Tue, 8 Aug 2023 23:18:13 +0200
Subject: [PATCH] cmake: do test build in c++ mode to verify SDL3 symbols

---
 .github/workflows/main.yml |  3 +++
 CMakeLists.txt             | 15 +++++++++++++++
 src/sdl2_compat.c          |  7 +++++--
 test/test_sdl3_syms.cpp    |  2 ++
 4 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 test/test_sdl3_syms.cpp

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 17f3176..fe0a730 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -79,6 +79,9 @@ jobs:
         set -eu
         export SDL_TESTS_QUICK=1
         ctest -VV --test-dir build/
+    - name: Run build tests (CMake)
+      run: |
+        cmake --build build/ --verbose --target sdl2compat-build-tests
     - name: Check that versioning is consistent
       # We only need to run this once: arbitrarily use the Linux build
       if: ${{ runner.os == 'Linux' }}
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 57833ba..afa0a54 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -76,6 +76,7 @@ endif()
 include(CheckCSourceCompiles)
 include(CheckIncludeFile)
 include(CheckCCompilerFlag)
+include(CheckLanguage)
 include(CMakePackageConfigHelpers)
 include(CMakeParseArguments)
 include(CMakePushCheckState)
@@ -553,6 +554,20 @@ if(SDL2COMPAT_TESTS)
   if(HAVE_WFORMAT_EXTRA_ARGS)
     target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_EXTRA_ARGS)
   endif()
+
+  add_custom_target(sdl2compat-build-tests)
+
+  check_language(CXX)
+  if(CMAKE_CXX_COMPILER)
+    enable_language(CXX)
+    add_library(test_sdl3_prototypes STATIC EXCLUDE_FROM_ALL "test/test_sdl3_syms.cpp")
+    target_link_libraries(test_sdl3_prototypes PRIVATE SDL3::Headers)
+    SDL_AddCommonCompilerFlags(test_sdl3_prototypes WERROR TRUE)
+    set_target_properties(test_sdl3_prototypes PROPERTIES SUFFIX ".do_not_use")
+    # Make sure test_sdl3_prototypes is always out-of-date
+    add_custom_command(TARGET test_sdl3_prototypes POST_BUILD COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_CURRENT_SOURCE_DIR}/test/test_sdl3_syms.cpp")
+    add_dependencies(sdl2compat-build-tests test_sdl3_prototypes)
+  endif()
 endif()
 
 set(installed_targets SDL2)
diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 40ffe26..d7b70ad 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -117,10 +117,13 @@ extern "C" {
 #include "sdl2_protos.h"
 
 
-/** Enable this to have warnings about wrong prototypes of src/sdl3_sym.h
+/** Define SDL2COMPAT_TEST_SYMS=1 to have warnings about wrong prototypes of src/sdl3_sym.h
  *  It won't compile but it helps to make sure it's sync'ed with SDL3 headers.
  */
-#if 0
+#ifndef SDL2COMPAT_TEST_SYMS
+#define SDL2COMPAT_TEST_SYMS 0
+#endif
+#if SDL2COMPAT_TEST_SYMS
 #define SDL3_SYM(rc,fn,params,args,ret) \
     typedef rc (SDLCALL *SDL3_##fn##_t) params; \
     static SDL3_##fn##_t SDL3_##fn = IGNORE_THIS_VERSION_OF_SDL_##fn;
diff --git a/test/test_sdl3_syms.cpp b/test/test_sdl3_syms.cpp
new file mode 100644
index 0000000..24aad23
--- /dev/null
+++ b/test/test_sdl3_syms.cpp
@@ -0,0 +1,2 @@
+#define SDL2COMPAT_TEST_SYMS 1
+#include "../src/sdl2_compat.c"