SDL_mixer: cmake: build cpack packages

From 5a815a972aa3094cf19a27debe70f6aba3332e5f Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 25 Oct 2024 14:17:13 +0200
Subject: [PATCH] cmake: build cpack packages

---
 .github/workflows/main.yml                    | 21 +++++++++++----
 .gitignore                                    | 27 -------------------
 CMakeLists.txt                                | 22 +++++++++++++++
 .../cmake/SDL3_mixerConfigVersion.cmake.in    |  4 +--
 4 files changed, 40 insertions(+), 34 deletions(-)
 delete mode 100644 .gitignore

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 46eded913..3a5485382 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -15,12 +15,12 @@ jobs:
       fail-fast: false
       matrix:
         platform:
-        - { name: Windows (MSVC),       os: windows-2019, shell: sh,   cmake: '-DPerl_ROOT=C:/Strawberry/perl/bin/ -DSDLMIXER_VENDORED=ON -GNinja', msvc: 1, shared: 1, static: 0 }
-        - { name: Windows (mingw64),    os: windows-latest, shell: 'msys2 {0}', msystem: mingw64, msys-env: mingw-w64-x86_64, shared: 1, static: 0,
+        - { name: Windows (MSVC),       os: windows-2019, shell: sh,   cmake: '-DPerl_ROOT=C:/Strawberry/perl/bin/ -DSDLMIXER_VENDORED=ON -GNinja', msvc: 1, shared: 1, static: 0, artifact: 'SDL3_mixer-VC-x64' }
+        - { name: Windows (mingw64),    os: windows-latest, shell: 'msys2 {0}', msystem: mingw64, msys-env: mingw-w64-x86_64, shared: 1, static: 0, artifact: 'SDL3_mixer-mingw64',
             cmake: '-DSDLMIXER_VENDORED=OFF -G "Ninja Multi-Config"' }
-        - { name: Linux,                os: ubuntu-20.04,   shell: sh, cmake: '-DSDLMIXER_VENDORED=ON -GNinja', shared: 1, static: 0 }
-        - { name: 'Linux (static)',     os: ubuntu-20.04,   shell: sh, cmake: '-DSDLMIXER_VENDORED=ON -DBUILD_SHARED_LIBS=OFF -GNinja', shared: 0, static: 1 }
-        - { name: Macos,                os: macos-latest,   shell: sh, cmake: '-DSDLMIXER_VENDORED=ON -GNinja', shared: 1, static: 0 }
+        - { name: Linux,                os: ubuntu-20.04,   shell: sh, cmake: '-DSDLMIXER_VENDORED=ON -GNinja', shared: 1, static: 0, artifact: 'SDL3_mixer-linux-x64' }
+        - { name: 'Linux (static)',     os: ubuntu-20.04,   shell: sh, cmake: '-DSDLMIXER_VENDORED=ON -DBUILD_SHARED_LIBS=OFF -GNinja', shared: 0, static: 1, artifact: 'SDL3_mixer-static-linux-x64' }
+        - { name: Macos,                os: macos-latest,   shell: sh, cmake: '-DSDLMIXER_VENDORED=ON -GNinja', shared: 1, static: 0, artifact: 'SDL3_mixer-macos' }
 
 
     steps:
@@ -138,6 +138,10 @@ jobs:
         cmake --install build/ --config Release
         echo "SDL3_mixer_ROOT=$(pwd)/prefix_cmake" >> $GITHUB_ENV
         ( cd prefix_cmake; find . ) | LC_ALL=C sort -u
+    - name: Package (CPack)
+      if: ${{ always() && steps.build.outcome == 'success' }}
+      run: |
+        cmake --build build/ --target package
 
     - name: Verify CMake configuration files
       run: |
@@ -146,3 +150,10 @@ jobs:
           -DTEST_SHARED=${{ matrix.platform.shared }} \
           -DTEST_STATIC=${{ matrix.platform.static }}
         cmake --build cmake_config_build --verbose --config Release
+
+    - uses: actions/upload-artifact@v4
+      if: ${{ always() && steps.build.outcome == 'success' }}
+      with:
+        if-no-files-found: error
+        name: ${{ matrix.platform.artifact }}
+        path: build/dist/SDL3_mixer*
diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index b121de87c..000000000
--- a/.gitignore
+++ /dev/null
@@ -1,27 +0,0 @@
-build*
-!build-scripts/
-aclocal.m4
-autom4te*
-config.cache
-config.log
-config.status
-Makefile
-libtool
-.deps
-.libs
-*.lo
-*.o
-*.la
-*.lai
-Debug
-Release
-*.user
-*.ncb
-*.suo
-*.sdf
-.DS_Store
-xcuserdata
-*.xcworkspace
-.vs
-.vscode
-Xcode/build.xcconfig
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7a0beaf8c..38dd827c5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,7 +19,9 @@ project(SDL3_mixer
 
 include("${SDL3_mixer_SOURCE_DIR}/cmake/GetGitRevisionDescription.cmake")
 include("${SDL3_mixer_SOURCE_DIR}/cmake/PrivateSdlFunctions.cmake")
+include("${SDL3_mixer_SOURCE_DIR}/cmake/sdlcpu.cmake")
 include("${SDL3_mixer_SOURCE_DIR}/cmake/sdlmanpages.cmake")
+include("${SDL3_mixer_SOURCE_DIR}/cmake/sdlplatform.cmake")
 sdl_calculate_derived_version_variables(${MAJOR_VERSION} ${MINOR_VERSION} ${MICRO_VERSION})
 
 message(STATUS "Configuring ${PROJECT_NAME} ${PROJECT_VERSION}")
@@ -86,6 +88,7 @@ option(CMAKE_POSITION_INDEPENDENT_CODE "Build static libraries with -fPIC" ${PLA
 cmake_dependent_option(BUILD_SHARED_LIBS "Build the library as a shared library" ON PLATFORM_SUPPORTS_SHARED OFF)
 
 cmake_dependent_option(SDLMIXER_INSTALL "Enable SDL3mixer install target" ${SDLMIXER_ROOTPROJECT} "${sdl3mixer_install_enableable}" OFF)
+cmake_dependent_option(SDLMIXER_INSTALL_CPACK "Create binary SDL3_mixer archive using CPack" ${SDLMIXER_ROOTPROJECT} "SDLMIXER_INSTALL" OFF)
 cmake_dependent_option(SDLMIXER_INSTALL_MAN "Install man pages for SDL3_mixer" OFF "SDLMIXER_INSTALL" OFF)
 cmake_dependent_option(SDLMIXER_DEPS_SHARED "Load dependencies dynamically" ON PLATFORM_SUPPORTS_SHARED OFF)
 option(SDLMIXER_VENDORED "Use vendored third-party libraries" ${vendored_default})
@@ -223,6 +226,9 @@ if(NOT TARGET SDL3::Headers OR NOT TARGET ${sdl3_target_name})
     find_package(SDL3 ${SDL_REQUIRED_VERSION} REQUIRED COMPONENTS ${sdl_required_components})
 endif()
 
+SDL_DetectTargetCPUArchitectures(SDL_CPU_NAMES)
+SDL_DetectCMakePlatform()
+
 set(BUILD_SHARED_LIBS ${SDLMIXER_BUILD_SHARED_LIBS})
 add_library(${sdl3_mixer_target_name}
     src/codecs/load_aiff.c
@@ -1168,6 +1174,22 @@ if(SDLMIXER_INSTALL)
         COMPONENT library
     )
 
+    if(SDLMIXER_INSTALL_CPACK)
+      if(APPLE)
+        set(CPACK_GENERATOR "DragNDrop")
+      elseif(MSVC)
+        set(CPACK_GENERATOR "ZIP")
+      else()
+        set(CPACK_GENERATOR "TGZ")
+      endif()
+      configure_file(cmake/CPackProjectConfig.cmake.in CPackProjectConfig.cmake @ONLY)
+      set(CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/CPackProjectConfig.cmake")
+      # CPACK_SOURCE_PACKAGE_FILE_NAME must end with "-src" (so we can block creating a source archive)
+      set(CPACK_SOURCE_PACKAGE_FILE_NAME "SDL${PROJECT_VERSION_MAJOR}-${PROJECT_VERSION}-src")
+      set(CPACK_PACKAGE_DIRECTORY "${CMAKE_BINARY_DIR}/dist")
+      include(CPack)
+    endif()
+
     if(SDLMIXER_INSTALL_MAN)
         sdl_get_git_revision_hash(SDLMIXER_REVISION)
         SDL_generate_manpages(
diff --git a/build-scripts/pkg-support/android/cmake/SDL3_mixerConfigVersion.cmake.in b/build-scripts/pkg-support/android/cmake/SDL3_mixerConfigVersion.cmake.in
index abb35d1d8..6e65da6fe 100644
--- a/build-scripts/pkg-support/android/cmake/SDL3_mixerConfigVersion.cmake.in
+++ b/build-scripts/pkg-support/android/cmake/SDL3_mixerConfigVersion.cmake.in
@@ -1,5 +1,5 @@
-# SDL_image CMake version configuration file:
-# This file is meant to be placed in a lib/cmake/SDL3_image subfolder of a reconstructed Android SDL3_image SDK
+# SDL3_mixer CMake version configuration file:
+# This file is meant to be placed in a lib/cmake/SDL3_mixer subfolder of a reconstructed Android SDL3_mixer SDK
 
 set(PACKAGE_VERSION "@<@PROJECT_VERSION@>@")