SDL: build: Mechanically generate ABI versions from version number

From fff97c95ebd50cd21f2f3739bc4efb7b0ca575cd Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Wed, 4 May 2022 16:40:11 +0100
Subject: [PATCH] build: Mechanically generate ABI versions from version number

If we're strict about applying something resembling semantic versioning
to the "marketing" version number, then we can mechanically generate
the ABI version from it.

This limits the range of valid micro versions (patchlevels) to 0-99.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 CMakeLists.txt                          | 32 +++++++++++++++++++------
 Xcode/SDL/SDL.xcodeproj/project.pbxproj |  4 ++--
 configure.ac                            | 19 +++++++++++----
 docs/release_checklist.md               | 30 +++++++++++------------
 src/SDL.c                               |  7 +-----
 5 files changed, 57 insertions(+), 35 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 718d38f21e8..ca8571449ed 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -60,12 +60,7 @@ include(${SDL2_SOURCE_DIR}/cmake/sdlchecks.cmake)
 set(SDL_MAJOR_VERSION 2)
 set(SDL_MINOR_VERSION 23)
 set(SDL_MICRO_VERSION 0)
-set(SDL_INTERFACE_AGE 0)
-set(SDL_BINARY_AGE 23)
 set(SDL_VERSION "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}.${SDL_MICRO_VERSION}")
-# the following should match the versions in Xcode project file:
-set(DYLIB_CURRENT_VERSION 24.0.0)
-set(DYLIB_COMPATIBILITY_VERSION 1.0.0)
 
 # Set defaults preventing destination file conflicts
 set(SDL_CMAKE_DEBUG_POSTFIX "d"
@@ -74,9 +69,22 @@ set(SDL_CMAKE_DEBUG_POSTFIX "d"
 mark_as_advanced(CMAKE_IMPORT_LIBRARY_SUFFIX SDL_CMAKE_DEBUG_POSTFIX)
 
 # Calculate a libtool-like version number
-math(EXPR LT_CURRENT "${SDL_MINOR_VERSION} - ${SDL_INTERFACE_AGE}")
+math(EXPR SDL_BINARY_AGE "${SDL_MINOR_VERSION} * 100 + ${SDL_MICRO_VERSION}")
+if(SDL_MINOR_VERSION MATCHES "[02468]$")
+    # Stable branch, 2.24.1 -> libSDL2-2.0.so.0.2400.1
+    set(SDL_INTERFACE_AGE ${SDL_MICRO_VERSION})
+else()
+    # Development branch, 2.23.1 -> libSDL2-2.0.so.0.2301.0
+    set(SDL_INTERFACE_AGE 0)
+endif()
+
+# Increment this if there is an incompatible change - but if that happens,
+# we should rename the library from SDL2 to SDL3, at which point this would
+# reset to 0 anyway.
+set(LT_MAJOR "0")
+
 math(EXPR LT_AGE "${SDL_BINARY_AGE} - ${SDL_INTERFACE_AGE}")
-math(EXPR LT_MAJOR "${LT_CURRENT}- ${LT_AGE}")
+math(EXPR LT_CURRENT "${LT_MAJOR} + ${LT_AGE}")
 set(LT_REVISION "${SDL_INTERFACE_AGE}")
 # For historical reasons, the library name redundantly includes the major
 # version twice: libSDL2-2.0.so.0.
@@ -85,6 +93,16 @@ set(LT_REVISION "${SDL_INTERFACE_AGE}")
 set(LT_RELEASE "2.0")
 set(LT_VERSION "${LT_MAJOR}.${LT_AGE}.${LT_REVISION}")
 
+# The following should match the versions in the Xcode project file.
+# Each version is 1 higher than you might expect, for compatibility
+# with libtool: macOS ABI versioning is 1-based, unlike other platforms
+# which are normally 0-based.
+math(EXPR DYLIB_CURRENT_VERSION_MAJOR "${LT_MAJOR} + ${LT_AGE} + 1")
+math(EXPR DYLIB_CURRENT_VERSION_MINOR "${LT_REVISION}")
+math(EXPR DYLIB_COMPAT_VERSION_MAJOR "${LT_MAJOR} + 1")
+set(DYLIB_CURRENT_VERSION "${DYLIB_CURRENT_VERSION_MAJOR}.${DYLIB_CURRENT_VERSION_MINOR}.0")
+set(DYLIB_COMPATIBILITY_VERSION "${DYLIB_COMPAT_VERSION_MAJOR}.0.0")
+
 #message(STATUS "${LT_VERSION} :: ${LT_AGE} :: ${LT_REVISION} :: ${LT_CURRENT} :: ${LT_RELEASE}")
 
 # General settings & flags
diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
index 6b23c9d82ef..efdc40cdfda 100644
--- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj
+++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
@@ -9208,7 +9208,7 @@
 				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
 				DEPLOYMENT_POSTPROCESSING = YES;
 				DYLIB_COMPATIBILITY_VERSION = 1.0.0;
-				DYLIB_CURRENT_VERSION = 24.0.0;
+				DYLIB_CURRENT_VERSION = 2301.0.0;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				GCC_ALTIVEC_EXTENSIONS = YES;
@@ -9292,7 +9292,7 @@
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
 				DEBUG_INFORMATION_FORMAT = dwarf;
 				DYLIB_COMPATIBILITY_VERSION = 1.0.0;
-				DYLIB_CURRENT_VERSION = 24.0.0;
+				DYLIB_CURRENT_VERSION = 2301.0.0;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				ENABLE_TESTABILITY = YES;
diff --git a/configure.ac b/configure.ac
index 27c931660be..4cab718570b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -14,10 +14,17 @@ dnl Set various version strings - taken gratefully from the GTk sources
 SDL_MAJOR_VERSION=2
 SDL_MINOR_VERSION=23
 SDL_MICRO_VERSION=0
-SDL_INTERFACE_AGE=0
-SDL_BINARY_AGE=23
 SDL_VERSION=$SDL_MAJOR_VERSION.$SDL_MINOR_VERSION.$SDL_MICRO_VERSION
 
+SDL_BINARY_AGE=`expr $SDL_MINOR_VERSION \* 100 + $SDL_MICRO_VERSION`
+AS_CASE(["$SDL_MINOR_VERSION"],
+  [*[02468]],
+    dnl Stable branch, 2.24.1 -> libSDL2-2.0.so.0.2400.1
+    [SDL_INTERFACE_AGE="$SDL_MICRO_VERSION"],
+  [*],
+    dnl Development branch, 2.23.1 -> libSDL2-2.0.so.0.2301.0
+    [SDL_INTERFACE_AGE=0])
+
 AC_SUBST(SDL_MAJOR_VERSION)
 AC_SUBST(SDL_MINOR_VERSION)
 AC_SUBST(SDL_MICRO_VERSION)
@@ -33,9 +40,13 @@ LT_LANG([Windows Resource])
 # version twice: libSDL2-2.0.so.0.
 # TODO: in SDL 3, stop using -release, which will simplify it to libSDL3.so.0
 LT_RELEASE=2.0
-LT_CURRENT=`expr $SDL_MINOR_VERSION - $SDL_INTERFACE_AGE`
-LT_REVISION=$SDL_INTERFACE_AGE
+# Increment this if there is an incompatible change - but if that happens,
+# we should rename the library from SDL2 to SDL3, at which point this would
+# reset to 0 anyway.
+LT_MAJOR=0
 LT_AGE=`expr $SDL_BINARY_AGE - $SDL_INTERFACE_AGE`
+LT_CURRENT=`expr $LT_MAJOR + $LT_AGE`
+LT_REVISION=$SDL_INTERFACE_AGE
 m4_pattern_allow([^LT_])
 
 AC_SUBST(LT_RELEASE)
diff --git a/docs/release_checklist.md b/docs/release_checklist.md
index 2d4c5f9117f..d7e9c0c691f 100644
--- a/docs/release_checklist.md
+++ b/docs/release_checklist.md
@@ -17,14 +17,10 @@
 
 * Bump ABI version information
 
-    * `configure.ac`: `CMakeLists.txt`: `SDL_INTERFACE_AGE`, `SDL_BINARY_AGE`
-        * `SDL_BINARY_AGE += 1`
-        * set `SDL_INTERFACE_AGE` to 0
-        * if backwards compatibility has been broken,
-            set both `SDL_BINARY_AGE` and `SDL_INTERFACE_AGE` to 0
-    * `Xcode/SDL/SDL.xcodeproj/project.pbxproj`: `DYLIB_CURRENT_VERSION`,
-        `DYLIB_COMPATIBILITY_VERSION`
-        * increment first number in `DYLIB_CURRENT_VERSION`
+    * `CMakeLists.txt`, `Xcode/SDL/SDL.xcodeproj/project.pbxproj`:
+        `DYLIB_CURRENT_VERSION`, `DYLIB_COMPATIBILITY_VERSION`
+        * set first number in `DYLIB_CURRENT_VERSION` to
+            (100 * *minor*) + 1
         * set second number in `DYLIB_CURRENT_VERSION` to 0
         * if backwards compatibility has been broken,
             increase `DYLIB_COMPATIBILITY_VERSION` (?)
@@ -45,12 +41,9 @@
 
 * Bump ABI version information
 
-    * `configure.ac`: `CMakeLists.txt`: `SDL_INTERFACE_AGE`, `SDL_BINARY_AGE`
-        * `SDL_INTERFACE_AGE += 1`
-        * `SDL_BINARY_AGE += 1`
-    * `Xcode/SDL/SDL.xcodeproj/project.pbxproj`: `DYLIB_CURRENT_VERSION`,
-        `DYLIB_COMPATIBILITY_VERSION`
-        * increment second number in `DYLIB_CURRENT_VERSION`
+    * `CMakeLists.txt`, `Xcode/SDL/SDL.xcodeproj/project.pbxproj`:
+        `DYLIB_CURRENT_VERSION`, `DYLIB_COMPATIBILITY_VERSION`
+        * set second number in `DYLIB_CURRENT_VERSION` to *patchlevel*
 
 * Regenerate `configure`
 
@@ -77,8 +70,13 @@
 
 * Bump ABI version information
 
-    * Same places as listed above
-    * Assume that the next feature release will contain new API/ABI
+    * `CMakeLists.txt`, `Xcode/SDL/SDL.xcodeproj/project.pbxproj`:
+        `DYLIB_CURRENT_VERSION`, `DYLIB_COMPATIBILITY_VERSION`
+        * set first number in `DYLIB_CURRENT_VERSION` to
+            (100 * *minor*) + *patchlevel* + 1
+        * set second number in `DYLIB_CURRENT_VERSION` to 0
+        * if backwards compatibility has been broken,
+            increase `DYLIB_COMPATIBILITY_VERSION` (?)
 
 * Regenerate `configure`
 
diff --git a/src/SDL.c b/src/SDL.c
index 47ce674ca15..a65d9c51933 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -80,13 +80,8 @@ SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_min, SDL_MINOR_VERSION >= 0);
 SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_max, SDL_MINOR_VERSION <= 255);
 
 SDL_COMPILE_TIME_ASSERT(SDL_PATCHLEVEL_min, SDL_PATCHLEVEL >= 0);
-#if SDL_MAJOR_VERSION < 3
-/* Limited by its encoding in SDL_VERSIONNUM */
+/* Limited by its encoding in SDL_VERSIONNUM and in the ABI versions */
 SDL_COMPILE_TIME_ASSERT(SDL_PATCHLEVEL_max, SDL_PATCHLEVEL <= 99);
-#else
-/* Limited only by the need to fit in SDL_version */
-SDL_COMPILE_TIME_ASSERT(SDL_PATCHLEVEL_max, SDL_PATCHLEVEL <= 255);
-#endif
 
 /* This is not declared in any header, although it is shared between some
     parts of SDL, because we don't want anything calling it without an