SDL: workflows: Add a test to assert that all the version numbers agree

From 2a8297e427b822b3a50d912d09fdc2b07da4780b Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Wed, 4 May 2022 16:43:59 +0100
Subject: [PATCH] workflows: Add a test to assert that all the version numbers
 agree

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 .github/workflows/main.yml |   3 +
 docs/release_checklist.md  |   8 ++
 test/versioning.sh         | 151 +++++++++++++++++++++++++++++++++++++
 3 files changed, 162 insertions(+)
 create mode 100755 test/versioning.sh

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 59ab6fe4104..a49008f4d52 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -78,6 +78,9 @@ jobs:
         ninja -v -C build
         sudo meson install -C build
     - uses: actions/checkout@v2
+    - name: Check that versioning is consistent
+      if: runner.os == 'Linux'
+      run: ./test/versioning.sh
     - name: Configure CMake
       run: cmake -B build -DSDL_TEST=ON ${{ matrix.platform.flags }}
     - name: Build
diff --git a/docs/release_checklist.md b/docs/release_checklist.md
index d7e9c0c691f..bb38f4fece9 100644
--- a/docs/release_checklist.md
+++ b/docs/release_checklist.md
@@ -25,6 +25,8 @@
         * if backwards compatibility has been broken,
             increase `DYLIB_COMPATIBILITY_VERSION` (?)
 
+* Run test/versioning.sh to verify that everything is consistent
+
 * Regenerate `configure`
 
 * Do the release
@@ -45,6 +47,8 @@
         `DYLIB_CURRENT_VERSION`, `DYLIB_COMPATIBILITY_VERSION`
         * set second number in `DYLIB_CURRENT_VERSION` to *patchlevel*
 
+* Run test/versioning.sh to verify that everything is consistent
+
 * Regenerate `configure`
 
 * Do the release
@@ -62,6 +66,8 @@
     * Same places as listed above
     * Assume that the next feature release will contain new API/ABI
 
+* Run test/versioning.sh to verify that everything is consistent
+
 ## New development prerelease
 
 * Bump version number from 2.Y.Z to 2.Y.(Z+1) (Y is odd)
@@ -78,6 +84,8 @@
         * if backwards compatibility has been broken,
             increase `DYLIB_COMPATIBILITY_VERSION` (?)
 
+* Run test/versioning.sh to verify that everything is consistent
+
 * Regenerate `configure`
 
 * Do the release
diff --git a/test/versioning.sh b/test/versioning.sh
new file mode 100755
index 00000000000..1e160bc268b
--- /dev/null
+++ b/test/versioning.sh
@@ -0,0 +1,151 @@
+#!/bin/sh
+# Copyright 2022 Collabora Ltd.
+# SPDX-License-Identifier: Zlib
+
+set -eu
+
+ref_major=$(sed -ne 's/^#define SDL_MAJOR_VERSION  *//p' include/SDL_version.h)
+ref_minor=$(sed -ne 's/^#define SDL_MINOR_VERSION  *//p' include/SDL_version.h)
+ref_micro=$(sed -ne 's/^#define SDL_PATCHLEVEL  *//p' include/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
+}
+
+major=$(sed -ne 's/^SDL_MAJOR_VERSION=//p' configure.ac)
+minor=$(sed -ne 's/^SDL_MINOR_VERSION=//p' configure.ac)
+micro=$(sed -ne 's/^SDL_MICRO_VERSION=//p' configure.ac)
+version="${major}.${minor}.${micro}"
+
+if [ "$ref_version" = "$version" ]; then
+    ok "configure.ac $version"
+else
+    not_ok "configure.ac $version disagrees with SDL_version.h $ref_version"
+fi
+
+major=$(sed -ne 's/^set(SDL_MAJOR_VERSION \([0-9]\+\))$/\1/p' CMakeLists.txt)
+minor=$(sed -ne 's/^set(SDL_MINOR_VERSION \([0-9]\+\))$/\1/p' CMakeLists.txt)
+micro=$(sed -ne 's/^set(SDL_MICRO_VERSION \([0-9]\+\))$/\1/p' CMakeLists.txt)
+version="${major}.${minor}.${micro}"
+
+if [ "$ref_version" = "$version" ]; then
+    ok "CMakeLists.txt $version"
+else
+    not_ok "CMakeLists.txt $version disagrees with SDL_version.h $ref_version"
+fi
+
+major=$(sed -ne 's/^MAJOR_VERSION *= *//p' Makefile.os2)
+minor=$(sed -ne 's/^MINOR_VERSION *= *//p' Makefile.os2)
+micro=$(sed -ne 's/^MICRO_VERSION *= *//p' Makefile.os2)
+version="${major}.${minor}.${micro}"
+
+if [ "$ref_version" = "$version" ]; then
+    ok "Makefile.os2 $version"
+else
+    not_ok "Makefile.os2 $version disagrees with SDL_version.h $ref_version"
+fi
+
+version=$(sed -Ene 's/^[$]SDLVersion = "([0-9.]+)"\r?$/\1/p' build-scripts/winrtbuild.ps1)
+
+if [ "$ref_version" = "$version" ]; then
+    ok "winrtbuild.ps1 $version"
+else
+    not_ok "winrtbuild.ps1 $version disagrees with SDL_version.h $ref_version"
+fi
+
+tuple=$(sed -ne 's/^ *FILEVERSION *//p' src/main/windows/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/main/windows/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/main/windows/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/main/windows/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 '/CFBundleShortVersionString/,+1 s/.*<string>(.*)<\/string>.*/\1/p' Xcode/SDL/Info-Framework.plist)
+
+if [ "$ref_version" = "$version" ]; then
+    ok "Info-Framework.plist CFBundleShortVersionString $version"
+else
+    not_ok "Info-Framework.plist CFBundleShortVersionString $version disagrees with SDL_version.h $ref_version"
+fi
+
+version=$(sed -Ene '/CFBundleVersion/,+1 s/.*<string>(.*)<\/string>.*/\1/p' Xcode/SDL/Info-Framework.plist)
+
+if [ "$ref_version" = "$version" ]; then
+    ok "Info-Framework.plist CFBundleVersion $version"
+else
+    not_ok "Info-Framework.plist CFBundleVersion $version disagrees with SDL_version.h $ref_version"
+fi
+
+# For simplicity this assumes we'll never break ABI before SDL 3.
+dylib_compat=$(sed -Ene 's/.*DYLIB_COMPATIBILITY_VERSION = (.*);$/\1/p' Xcode/SDL/SDL.xcodeproj/project.pbxproj)
+ref='1.0.0
+1.0.0'
+
+if [ "$ref" = "$dylib_compat" ]; then
+    ok "project.pbxproj DYLIB_COMPATIBILITY_VERSION is consistent"
+else
+    not_ok "project.pbxproj DYLIB_COMPATIBILITY_VERSION is inconsistent"
+fi
+
+dylib_cur=$(sed -Ene 's/.*DYLIB_CURRENT_VERSION = (.*);$/\1/p' Xcode/SDL/SDL.xcodeproj/project.pbxproj)
+
+case "$ref_minor" in
+    (*[02468])
+        major="$(( ref_minor * 100 + 1 ))"
+        minor="$ref_micro"
+        ;;
+    (*)
+        major="$(( ref_minor * 100 + ref_micro + 1 ))"
+        minor="0"
+        ;;
+esac
+
+ref="${major}.${minor}.0
+${major}.${minor}.0"
+
+if [ "$ref" = "$dylib_cur" ]; then
+    ok "project.pbxproj DYLIB_CURRENT_VERSION is consistent"
+else
+    not_ok "project.pbxproj DYLIB_CURRENT_VERSION is inconsistent"
+fi
+
+echo "1..$tests"
+exit "$failed"