SDL_rtf: ci: use setup-sdl to install SDL3 + SDL3_ttf

From e64d56c0da82a08bd910e109af5833dc57b0086d Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 15 Jan 2024 18:31:40 +0100
Subject: [PATCH] ci: use setup-sdl to install SDL3 + SDL3_ttf

---
 .github/workflows/main.yml       | 115 +++++++++++++++++++++++++++++++
 build-scripts/test-versioning.sh |   6 +-
 cmake/test/CMakeLists.txt        |  37 ++++++++++
 cmake/test/main.c                |  25 +++++++
 4 files changed, 180 insertions(+), 3 deletions(-)
 create mode 100644 .github/workflows/main.yml
 create mode 100644 cmake/test/CMakeLists.txt
 create mode 100644 cmake/test/main.c

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
new file mode 100644
index 0000000..d435dd3
--- /dev/null
+++ b/.github/workflows/main.yml
@@ -0,0 +1,115 @@
+name: Build
+
+on: [push, pull_request]
+
+jobs:
+  Build:
+    name: ${{ matrix.platform.name }}
+    runs-on: ${{ matrix.platform.os }}
+
+    defaults:
+      run:
+        shell: ${{ matrix.platform.shell }}
+
+    strategy:
+      fail-fast: false
+      matrix:
+        platform:
+        - { name: Windows (MSVC+CMake),         os: windows-latest, shell: sh,   cmake: '-DPerl_ROOT=C:/Strawberry/perl/bin/ -GNinja', msvc: 1, shared: 1, static: 0 }
+        - { name: Windows (mingw64+CMake),      os: windows-latest, shell: 'msys2 {0}', msystem: mingw64, msys-env: mingw-w64-x86_64, shared: 1, static: 0,
+            cmake: '-G "Ninja Multi-Config"' }
+        - { name: Linux,                        os: ubuntu-20.04,   shell: sh,   cmake: '-GNinja', shared: 1, static: 0 }
+        - { name: 'Linux (static)',             os: ubuntu-20.04,   shell: sh,   cmake: '-GNinja -DBUILD_SHARED_LIBS=OFF', shared: 0, static: 1 }
+        - { name: Macos,                        os: macos-latest,   shell: sh,   cmake: '-GNinja', shared: 1, static: 0 }
+
+    steps:
+    - uses: ilammy/msvc-dev-cmd@v1
+      if: ${{ matrix.platform.msvc }}
+      with:
+        arch: x64
+    - name: Set up Ninja
+      if: ${{ !contains(matrix.platform.shell, 'msys2') }}
+      uses: ashutoshvarma/setup-ninja@master
+      with:
+        version: 1.11.1
+    - name: Set up MSYS2
+      if: ${{ matrix.platform.shell == 'msys2 {0}' }}
+      uses: msys2/setup-msys2@v2
+      with:
+        msystem: ${{ matrix.platform.msystem }}
+        install: >-
+          ${{ matrix.platform.msys-env }}-cmake
+          ${{ matrix.platform.msys-env }}-gcc
+          ${{ matrix.platform.msys-env }}-freetype
+          ${{ matrix.platform.msys-env }}-harfbuzz
+          ${{ matrix.platform.msys-env }}-ninja
+          ${{ matrix.platform.msys-env }}-perl
+          ${{ matrix.platform.msys-env }}-pkg-config
+          ${{ matrix.platform.msys-env }}-zlib
+    - name: Set up SDL
+      id: sdl
+      uses: libsdl-org/setup-sdl@main
+      with:
+        cmake-generator: Ninja
+        version: 3-head
+        version-sdl-ttf: 3-head
+        sdl-test: true
+        shell: ${{ matrix.platform.shell }}
+    - name: Setup Macos dependencies
+      if: ${{ runner.os == 'macOS' }}
+      run: |
+          brew install \
+            freetype \
+            harfbuzz \
+            ninja \
+            pkg-config \
+            ${NULL+}
+    - name: Set up Linux dependencies
+      if: ${{ runner.os == 'Linux' }}
+      run: |
+        sudo apt-get update
+        sudo apt-get -y install \
+          cmake \
+          file \
+          fonts-dejavu-core \
+          libfreetype-dev \
+          libharfbuzz-dev \
+          ninja-build \
+          pkg-config \
+          ${NULL+}
+    - uses: actions/checkout@v3
+      with:
+        submodules: recursive
+
+    - name: Check that versioning is consistent
+      # We only need to run this once: arbitrarily use the Linux build
+      if: ${{ runner.os == 'Linux' }}
+      run: ./build-scripts/test-versioning.sh
+
+    - name: Configure
+      run: |
+        cmake -B build-cmake \
+          -DBUILD_SHARED_LIBS=ON \
+          -DSDL3RTF_WERROR=ON \
+          -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
+          -DCMAKE_BUILD_TYPE=Release \
+          -DCMAKE_INSTALL_PREFIX=prefix_cmake \
+          ${{ matrix.platform.cmake }}
+    - name: Build
+      run: |
+        cmake --build build-cmake --config Release --verbose
+    - name: Install
+      run: |
+        set -eu
+        rm -rf prefix_cmake
+        cmake --install build-cmake/ --config Release --verbose
+        echo "SDL3_rtf_ROOT=$(pwd)/prefix_cmake" >> $GITHUB_ENV
+        ( cd prefix_cmake; find . ) | LC_ALL=C sort -u
+
+    - name: Verify CMake configuration files
+      run: |
+          cmake -S cmake/test -B cmake_config_build \
+            -DCMAKE_BUILD_TYPE=Release \
+            -DTEST_SHARED=${{ matrix.platform.shared }} \
+            -DTEST_STATIC=${{ matrix.platform.static }}
+          cmake --build cmake_config_build --verbose --config Release
diff --git a/build-scripts/test-versioning.sh b/build-scripts/test-versioning.sh
index 23c125a..a73fe97 100755
--- a/build-scripts/test-versioning.sh
+++ b/build-scripts/test-versioning.sh
@@ -9,9 +9,9 @@ cd `dirname $0`/..
 # Needed so sed doesn't report illegal byte sequences on macOS
 export LC_CTYPE=C
 
-ref_major=$(sed -ne 's/^#define SDL_RTF_MAJOR_VERSION  *//p' include/SDL3/SDL_rtf.h)
-ref_minor=$(sed -ne 's/^#define SDL_RTF_MINOR_VERSION  *//p' include/SDL3/SDL_rtf.h)
-ref_micro=$(sed -ne 's/^#define SDL_RTF_PATCHLEVEL  *//p' include/SDL3/SDL_rtf.h)
+ref_major=$(sed -ne 's/^#define SDL_RTF_MAJOR_VERSION  *//p' include/SDL3_rtf/SDL_rtf.h)
+ref_minor=$(sed -ne 's/^#define SDL_RTF_MINOR_VERSION  *//p' include/SDL3_rtf/SDL_rtf.h)
+ref_micro=$(sed -ne 's/^#define SDL_RTF_PATCHLEVEL  *//p' include/SDL3_rtf/SDL_rtf.h)
 ref_version="${ref_major}.${ref_minor}.${ref_micro}"
 
 tests=0
diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt
new file mode 100644
index 0000000..c963398
--- /dev/null
+++ b/cmake/test/CMakeLists.txt
@@ -0,0 +1,37 @@
+# This cmake build script is meant for verifying the various CMake configuration script.
+
+cmake_minimum_required(VERSION 3.12)
+project(sdl_test LANGUAGES C)
+
+cmake_policy(SET CMP0074 NEW)
+
+# Override CMAKE_FIND_ROOT_PATH_MODE to allow search for SDL3_rtf outside of sysroot
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)
+
+include(FeatureSummary)
+
+option(TEST_SHARED "Test linking to shared SDL3_rtf library" ON)
+add_feature_info("TEST_SHARED" TEST_SHARED "Test linking with shared library")
+
+option(TEST_STATIC "Test linking to static SDL3_rtf libary" ON)
+add_feature_info("TEST_STATIC" TEST_STATIC "Test linking with static library")
+
+if(TEST_SHARED)
+    find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
+    find_package(SDL3_rtf REQUIRED CONFIG)
+    find_package(SDL3_ttf REQUIRED CONFIG)
+    add_executable(main_shared main.c)
+    target_link_libraries(main_shared PRIVATE SDL3_rtf::SDL3_rtf-shared SDL3_ttf::SDL3_ttf SDL3::SDL3)
+endif()
+
+if(TEST_STATIC)
+    find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
+    # some static vendored libraries use c++ (enable CXX after `find_package` might show a warning)
+    enable_language(CXX)
+    find_package(SDL3_rtf REQUIRED CONFIG)
+    find_package(SDL3_ttf REQUIRED CONFIG)
+    add_executable(main_static main.c)
+    target_link_libraries(main_static PRIVATE SDL3_rtf::SDL3_rtf-static SDL3_ttf::SDL3_ttf SDL3::SDL3)
+endif()
+
+feature_summary(WHAT ALL)
diff --git a/cmake/test/main.c b/cmake/test/main.c
new file mode 100644
index 0000000..1504291
--- /dev/null
+++ b/cmake/test/main.c
@@ -0,0 +1,25 @@
+#include <SDL3/SDL.h>
+#include <SDL3/SDL_main.h>
+#include <SDL3_ttf/SDL_ttf.h>
+#include <SDL3_rtf/SDL_rtf.h>
+
+int main(int argc, char *argv[])
+{
+    (void)argc;
+    (void)argv;
+    SDL_version *v;
+
+    if (SDL_Init(0) < 0) {
+        SDL_Log("SDL_Init: could not initialize SDL: %s", SDL_GetError());
+        return 1;
+    }
+    if (TTF_Init() == -1) {
+        SDL_Log("TTF_Init: %s", TTF_GetError());
+    }
+    v = RTF_Linked_Version();
+    SDL_Log("SDL_rtf linked version: %u.%u.%u", v->major, v->minor, v->patch);
+
+    TTF_Quit();
+    SDL_Quit();
+    return 0;
+}