sdl2-compat: Add test-versioning.sh and update-version.sh scripts

From 98ea6be0552f0b690fd3e2703653a15d43f35057 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 30 Jul 2023 21:28:00 +0200
Subject: [PATCH] Add test-versioning.sh and update-version.sh scripts

---
 .github/workflows/main.yml       |   6 +-
 build-scripts/test-versioning.sh | 121 +++++++++++++++++++++++++++++++
 build-scripts/update-version.sh  |  64 ++++++++++++++++
 update_version.sh                |  30 --------
 4 files changed, 190 insertions(+), 31 deletions(-)
 create mode 100755 build-scripts/test-versioning.sh
 create mode 100755 build-scripts/update-version.sh
 delete mode 100755 update_version.sh

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index ddf699b..b55dc58 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -26,8 +26,12 @@ jobs:
         repository: libsdl-org/SDL
         ref: main
         path: SDL3
+    - name: Check that versioning is consistent
+      # We only need to run this once: arbitrarily use the Linux build
+      if: ${{ runner.os == 'Linux' }}
+      shell: bash
+      run: ./build-scripts/test-versioning.sh
     - name: Configure CMake
       run: cmake -DSDL3_INCLUDE_DIRS="${{ github.workspace }}/SDL3/include" -B build ${{ matrix.platform.flags }}
     - name: Build
       run: cmake --build build/
-
diff --git a/build-scripts/test-versioning.sh b/build-scripts/test-versioning.sh
new file mode 100755
index 0000000..493db1c
--- /dev/null
+++ b/build-scripts/test-versioning.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+# Copyright 2022 Collabora Ltd.
+# SPDX-License-Identifier: Zlib
+
+set -eu
+
+cd `dirname $0`/..
+
+ref_major=$(sed -ne 's/^#define SDL_MAJOR_VERSION  *//p' include/SDL2/SDL_version.h)
+ref_minor=$(sed -ne 's/^#define SDL_MINOR_VERSION  *//p' include/SDL2/SDL_version.h)
+ref_micro=$(sed -ne 's/^#define SDL_PATCHLEVEL  *//p' include/SDL2/SDL_version.h)
+ref_version="${ref_major}.${ref_minor}.${ref_micro}"
+
+tests=0
+failed=0
+
+ok () {
+    tests=$(( tests + 1 ))
+    echo "ok - $*"
+}
+
+not_ok () {
+    tests=$(( tests + 1 ))
+    echo "not ok - $*"
+    failed=1
+}
+
+version=$(sed -Ene 's/^project\(sdl[0-9]+_compat VERSION ([0-9.]*) LANGUAGES C\)$/\1/p' CMakeLists.txt)
+
+if [ "$ref_version" = "$version" ]; then
+    ok "CMakeLists.txt $version"
+else
+    not_ok "CMakeLists.txt $version disagrees with SDL_version.h $ref_version"
+fi
+
+tuple=$(sed -ne 's/^ *FILEVERSION *//p' src/version.rc | tr -d '\r')
+ref_tuple="${ref_major},${ref_minor},${ref_micro},0"
+
+if [ "$ref_tuple" = "$tuple" ]; then
+    ok "version.rc FILEVERSION $tuple"
+else
+    not_ok "version.rc FILEVERSION $tuple disagrees with SDL_version.h $ref_tuple"
+fi
+
+tuple=$(sed -ne 's/^ *PRODUCTVERSION *//p' src/version.rc | tr -d '\r')
+
+if [ "$ref_tuple" = "$tuple" ]; then
+    ok "version.rc PRODUCTVERSION $tuple"
+else
+    not_ok "version.rc PRODUCTVERSION $tuple disagrees with SDL_version.h $ref_tuple"
+fi
+
+tuple=$(sed -Ene 's/^ *VALUE "FileVersion", "([0-9, ]*)\\0"\r?$/\1/p' src/version.rc | tr -d '\r')
+ref_tuple="${ref_major}, ${ref_minor}, ${ref_micro}, 0"
+
+if [ "$ref_tuple" = "$tuple" ]; then
+    ok "version.rc FileVersion $tuple"
+else
+    not_ok "version.rc FileVersion $tuple disagrees with SDL_version.h $ref_tuple"
+fi
+
+tuple=$(sed -Ene 's/^ *VALUE "ProductVersion", "([0-9, ]*)\\0"\r?$/\1/p' src/version.rc | tr -d '\r')
+
+if [ "$ref_tuple" = "$tuple" ]; then
+    ok "version.rc ProductVersion $tuple"
+else
+    not_ok "version.rc ProductVersion $tuple disagrees with SDL_version.h $ref_tuple"
+fi
+
+version=$(sed -Ene 's/^VERSION = ([0-9.]+)$/\1/p' src/Makefile.w32)
+
+if [ "$ref_version" = "$version" ]; then
+    ok "Makefile.w32 $version"
+else
+    not_ok "Makefile.w32 $version disagrees with SDL_version.h $ref_version"
+fi
+
+minor=$(sed -Ene 's/^#define SDL2_COMPAT_VERSION_MINOR ([0-9]+)$/\1/p' src/sdl2_compat.c)
+
+if [ "$ref_minor" = "$minor" ]; then
+    ok "sdl2_compat.c SDL2_COMPAT_VERSION_MINOR $minor"
+else
+    not_ok "sdl2_compat.c SDL2_COMPAT_VERSION_MINOR $minor disagrees with SDL_version.h $ref_minor"
+fi
+
+micro=$(sed -Ene 's/^#define SDL2_COMPAT_VERSION_PATCH ([0-9]+)$/\1/p' src/sdl2_compat.c)
+
+if [ "$ref_micro" = "$micro" ]; then
+    ok "sdl2_compat.c SDL2_COMPAT_VERSION_PATCH $micro"
+else
+    not_ok "sdl2_compat.c SDL2_COMPAT_VERSION_PATCH $micro disagrees with SDL_version.h $ref_micro"
+fi
+
+if [ "x$(($ref_minor%2))" = "x0" ]; then
+    so_version="$((100 * $ref_minor)).$ref_micro"
+    dylib_version="$((100 * $ref_minor + 1)).$ref_micro"
+else
+    so_version="$((100 * $ref_minor + $ref_micro)).0"
+    dylib_version="$((100 * $ref_minor + $ref_micro + 1)).0"
+fi
+
+ref_dylib_versions="$dylib_version $dylib_version"
+dylib_versions=$(sed -Ene 's/^LDFLAGS\+= -Wl,-compatibility_version,([0-9.]+) -Wl,-current_version,([0-9.]+)$/\1 \2/p' src/Makefile.darwin)
+
+if [ "$ref_dylib_versions" = "$dylib_versions" ]; then
+    ok "Makefile.darwin LDFLAGS $dylib_versions"
+else
+    not_ok "Makefile.darwin LDFLAGS $dylib_versions disagrees with reference $ref_dylib_versions"
+fi
+
+ref_so_version="$so_version"
+so_version=$(sed -Ene 's/^SHLIB = libSDL2-2.0.so.0.([0-9.]+)$/\1/p' src/Makefile.linux)
+
+if [ "$ref_so_version" = "$so_version" ]; then
+    ok "Makefile.linux SHLIB $so_version"
+else
+    not_ok "Makefile.linux SHLIB $so_version disagrees with reference $ref_so_version"
+fi
+
+echo "1..$tests"
+exit "$failed"
diff --git a/build-scripts/update-version.sh b/build-scripts/update-version.sh
new file mode 100755
index 0000000..b525e5b
--- /dev/null
+++ b/build-scripts/update-version.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+#set -eux
+
+cd `dirname $0`/..
+
+ARGSOKAY=1
+if [ -z $1 ]; then
+    ARGSOKAY=0
+fi
+if [ -z $2 ]; then
+    ARGSOKAY=0
+fi
+if [ -z $3 ]; then
+    ARGSOKAY=0
+fi
+
+if [ "x$ARGSOKAY" = "x0" ]; then
+    echo "USAGE: $0 <major> <minor> <patch>" 1>&2
+    exit 1
+fi
+
+MAJOR="$1"
+MINOR="$2"
+PATCH="$3"
+NEWVERSION="${MAJOR}.${MINOR}.${PATCH}"
+
+echo "Updating version to '$NEWVERSION' ..."
+
+perl -w -pi -e 's/(\#define SDL_MAJOR_VERSION\s+)\d+/${1}'${MAJOR}'/;' include/SDL2/SDL_version.h
+perl -w -pi -e 's/(\#define SDL_MINOR_VERSION\s+)\d+/${1}'${MINOR}'/;' include/SDL2/SDL_version.h
+perl -w -pi -e 's/(\#define SDL_PATCHLEVEL\s+)\d+/${1}'${PATCH}'/;' include/SDL2/SDL_version.h
+
+perl -w -pi -e 's/\A(project\(sdl[0-9]+_compat VERSION )[0-9.]+/${1}'$NEWVERSION'/;' CMakeLists.txt
+
+perl -w -pi -e 's/(FILEVERSION\s+)\d+,\d+,\d+/${1}'${MAJOR}','${MINOR}','${PATCH}'/;' src/version.rc
+perl -w -pi -e 's/(PRODUCTVERSION\s+)\d+,\d+,\d+/${1}'${MAJOR}','${MINOR}','${PATCH}'/;' src/version.rc
+perl -w -pi -e 's/(VALUE "FileVersion", ")\d+, \d+, \d+/${1}'${MAJOR}', '${MINOR}', '${PATCH}'/;' src/version.rc
+perl -w -pi -e 's/(VALUE "ProductVersion", ")\d+, \d+, \d+/${1}'${MAJOR}', '${MINOR}', '${PATCH}'/;' src/version.rc
+
+perl -w -pi -e 's/(VERSION\s+=\s+)\d+.\d+.\d+/${1}'${MAJOR}'.'${MINOR}'.'${PATCH}'/;' src/Makefile.w32
+
+perl -w -pi -e 's/(\#define SDL2_COMPAT_VERSION_MINOR\s+)\d+/${1}'${MINOR}'/;' src/sdl2_compat.c
+perl -w -pi -e 's/(\#define SDL2_COMPAT_VERSION_PATCH\s+)\d+/${1}'${PATCH}'/;' src/sdl2_compat.c
+
+if [[ "x$((${MINOR}%2))" = "x0" ]]; then
+    SOVER="$((100 * ${MINOR})).${PATCH}"
+    DYVER="$((100 * ${MINOR} + 1)).${PATCH}"
+else
+    SOVER="$((100 * ${MINOR} + ${PATCH})).0"
+    DYVER="$((100 * ${MINOR} + ${PATCH} + 1)).0"
+fi
+
+perl -w -pi -e 's/(SHLIB\s+=\s+libSDL2-2\.0\.so\.0\.)\d+\.\d+/${1}'${SOVER}'/;' src/Makefile.linux
+perl -w -pi -e 's/(-Wl,-compatibility_version,)\d+\.\d+/${1}'${DYVER}'/;' src/Makefile.darwin
+perl -w -pi -e 's/(-Wl,-current_version,)\d+.\d+/${1}'${DYVER}'/;' src/Makefile.darwin
+
+echo "Running build-scripts/test-versioning.sh to verify changes..."
+./build-scripts/test-versioning.sh
+
+echo "All done."
+echo "Run 'git diff' and make sure this looks correct, before 'git commit'."
+
+exit 0
diff --git a/update_version.sh b/update_version.sh
deleted file mode 100755
index be9aa62..0000000
--- a/update_version.sh
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/bin/sh
-
-if [ -z $1 ]; then
-    echo "USAGE: $0 <new_patch_version>" 1>&2
-    exit 1
-fi
-
-NEWVERSION="$1"
-echo "Updating version to '2.90.$NEWVERSION' ..."
-
-# !!! FIXME: update all of these.
-
-perl -w -pi -e 's/(VERSION 2\.90\.)\d+/${1}'$NEWVERSION'/;' CMakeLists.txt
-perl -w -pi -e 's/(DYLIB_CURRENT_VERSION 12\.)\d+/${1}'$NEWVERSION'/;' CMakeLists.txt
-perl -w -pi -e 's/(\-current_version,12\.)\d+/${1}'$NEWVERSION'/;' src/Makefile.darwin
-perl -w -pi -e 's/(libSDL\-1\.2\.so\.1\.2\.)\d+/${1}'$NEWVERSION'/;' src/Makefile.linux
-perl -w -pi -e 's/(VERSION = 1\.2\.)\d+/${1}'$NEWVERSION'/;' src/Makefile.os2
-perl -w -pi -e 's/(VERSION = 1\.2\.)\d+/${1}'$NEWVERSION'/;' src/Makefile.w32
-perl -w -pi -e 's/(\#define SDL12_COMPAT_VERSION )\d+/${1}'$NEWVERSION'/;' src/SDL12_compat.c
-perl -w -pi -e 's/(\#define SDL_PATCHLEVEL )\d+/${1}'$NEWVERSION'/;' include/SDL/SDL_version.h
-perl -w -pi -e 's/(FILEVERSION 1,2,)\d+/${1}'$NEWVERSION'/;' src/version.rc
-perl -w -pi -e 's/(PRODUCTVERSION 1,2,)\d+/${1}'$NEWVERSION'/;' src/version.rc
-perl -w -pi -e 's/(VALUE "FileVersion", "1, 2, )\d+/${1}'$NEWVERSION'/;' src/version.rc
-perl -w -pi -e 's/(VALUE "ProductVersion", "1, 2, )\d+/${1}'$NEWVERSION'/;' src/version.rc
-
-echo "All done."
-echo "Run 'git diff' and make sure this looks correct before 'git commit'."
-
-exit 0
-