SDL: Add static assertions that the version number is consistent

From 63814ec767ec2d91a3a3a33900d4805f3165ce4c Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Tue, 3 May 2022 14:39:00 +0100
Subject: [PATCH] Add static assertions that the version number is consistent

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 CMakeLists.txt | 4 ++++
 Makefile.os2   | 9 ++++++++-
 configure.ac   | 4 ++++
 src/SDL.c      | 9 +++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f08ed4b5842..5327874e7c1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2951,6 +2951,10 @@ if(SDL_STATIC)
   endif()
 endif()
 
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSDL_BUILD_MAJOR_VERSION=${SDL_MAJOR_VERSION}")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSDL_BUILD_MINOR_VERSION=${SDL_MINOR_VERSION}")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSDL_BUILD_MICRO_VERSION=${SDL_MICRO_VERSION}")
+
 ##### Tests #####
 
 if(SDL_TEST)
diff --git a/Makefile.os2 b/Makefile.os2
index 64bd9bd102a..53e7421418d 100644
--- a/Makefile.os2
+++ b/Makefile.os2
@@ -11,7 +11,10 @@
 # wmake -f Makefile.os2 HIDAPI=1
 
 LIBNAME = SDL2
-VERSION = 2.0.23
+MAJOR_VERSION = 2
+MINOR_VERSION = 0
+MICRO_VERSION = 23
+VERSION = $(MAJOR_VERSION).$(MINOR_VERSION).$(MICRO_VERSION)
 DESCRIPTION = Simple DirectMedia Layer 2
 
 LIBICONV=0
@@ -60,6 +63,10 @@ CFLAGS_DLL+= -DHAVE_LIBUSB_H=1
 # building SDL itself (for DECLSPEC):
 CFLAGS_DLL+= -DBUILD_SDL
 
+CFLAGS+= -DSDL_BUILD_MAJOR_VERSION=$(MAJOR_VERSION)
+CFLAGS+= -DSDL_BUILD_MINOR_VERSION=$(MINOR_VERSION)
+CFLAGS+= -DSDL_BUILD_MICRO_VERSION=$(MICRO_VERSION)
+
 SRCS = SDL.c SDL_assert.c SDL_error.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
 SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
 SRCS+= SDL_cpuinfo.c SDL_atomic.c SDL_spinlock.c SDL_thread.c SDL_timer.c
diff --git a/configure.ac b/configure.ac
index 5c036ca7fa0..bddb621f5bd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -209,6 +209,10 @@ case "$enable_assertions" in
         ;;
 esac
 
+AC_DEFINE_UNQUOTED([SDL_BUILD_MAJOR_VERSION], $SDL_MAJOR_VERSION, [ ])
+AC_DEFINE_UNQUOTED([SDL_BUILD_MINOR_VERSION], $SDL_MINOR_VERSION, [ ])
+AC_DEFINE_UNQUOTED([SDL_BUILD_MICRO_VERSION], $SDL_MICRO_VERSION, [ ])
+
 dnl See whether we can use gcc style dependency tracking
 AC_ARG_ENABLE(dependency-tracking,
 [AS_HELP_STRING([--enable-dependency-tracking],
diff --git a/src/SDL.c b/src/SDL.c
index a5d61980557..47ce674ca15 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -62,6 +62,15 @@ extern int SDL_HelperWindowCreate(void);
 extern int SDL_HelperWindowDestroy(void);
 #endif
 
+#ifdef SDL_BUILD_MAJOR_VERSION
+SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MAJOR_VERSION,
+                        SDL_MAJOR_VERSION == SDL_BUILD_MAJOR_VERSION);
+SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MINOR_VERSION,
+                        SDL_MINOR_VERSION == SDL_BUILD_MINOR_VERSION);
+SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MICRO_VERSION,
+                        SDL_PATCHLEVEL == SDL_BUILD_MICRO_VERSION);
+#endif
+
 SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_min, SDL_MAJOR_VERSION >= 0);
 /* Limited only by the need to fit in SDL_version */
 SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_max, SDL_MAJOR_VERSION <= 255);