name: CMake
on: [push, pull_request]
jobs:
  ci-cmake:
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Ubuntu GCC
            os: ubuntu-latest
            compiler: gcc
            cflags: -Wall -Wextra
            pkgtgt: package package_source
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

          - name: Ubuntu GCC no shared
            os: ubuntu-latest
            compiler: gcc
            cflags: -Wall -Wextra
            pkgtgt: package package_source
            cmake-args: -DZLIB_BUILD_SHARED=OFF -DMINIZIP_ENABLE_BZIP2=ON

          - name: Ubuntu GCC no static
            os: ubuntu-latest
            compiler: gcc
            cflags: -Wall -Wextra
            pkgtgt: package package_source
            cmake-args: -DZLIB_BUILD_STATIC=OFF -DMINIZIP_ENABLE_BZIP2=ON

          - name: Ubuntu GCC -O3
            os: ubuntu-latest
            compiler: gcc
            cflags: -O3 -Wall -Wextra
            pkgtgt: package package_source
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

          - name: Ubuntu Clang
            os: ubuntu-latest
            compiler: clang
            cflags: -Wall -Wextra
            pkgtgt: package package_source
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

          - name: Ubuntu Clang Debug
            os: ubuntu-latest
            compiler: clang
            cflags: -Wall -Wextra
            build-config: Debug
            pkgtgt: package package_source
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

          - name: Windows MSVC Win32
            os: windows-latest
            compiler: cl
            cflags: /W3
            cmake-args: -A Win32
            pkgtgt: PACKAGE

          - name: Windows MSVC Win64
            os: windows-latest
            compiler: cl
            cflags: /W3
            cmake-args: -A x64 -DMINIZIP_ENABLE_BZIP2=OFF
            pkgtgt: PACKAGE

          - name: Windows MSVC ARM64
            os: windows-11-arm
            compiler: cl
            cflags: /W3
            cmake-args: -A ARM64 -DMINIZIP_ENABLE_BZIP2=OFF
            pkgtgt: PACKAGE

          - name: Windows GCC
            os: windows-latest
            compiler: gcc
            cflags: -Wall -Wextra
            cmake-args: -G Ninja -DMINIZIP_ENABLE_BZIP2=OFF
            pkgtgt: package

          - name: macOS Clang
            os: macos-latest
            compiler: clang
            cflags: -Wall -Wextra
            pkgtgt: package
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

          - name: macOS GCC 13
            os: macos-latest
            compiler: gcc-13
            cflags: -Wall -Wextra
            pkgtgt: package
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

          - name: macOS GCC 14
            os: macos-latest
            compiler: gcc-14
            cflags: -Wall -Wextra
            pkgtgt: package
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

          - name: macOS GCC 15
            os: macos-latest
            compiler: gcc-15
            cflags: -Wall -Wextra
            pkgtgt: package
            cmake-args: -DMINIZIP_ENABLE_BZIP2=ON

    steps:
    - name: Checkout repository
      uses: actions/checkout@v6

    - name: Install packages (Windows)
      if: runner.os == 'Windows'
      run: |
        choco install --no-progress nsis

    - name: Install packages (Linux)
      if: runner.os == 'Linux'
      run: |
        sudo apt install libbz2-dev
    - name: Generate project files
      run: cmake -S . -B ../build ${{ matrix.cmake-args }} -D CMAKE_BUILD_TYPE=${{ matrix.build-config || 'Release' }} -DZLIB_BUILD_MINIZIP=ON
      env:
        CC: ${{ matrix.compiler }}
        CFLAGS: ${{ matrix.cflags }}

    - name: Compile source code
      run: cmake --build ../build --config ${{ matrix.build-config || 'Release' }}

    - name: Run test cases
      run: ctest -C Release --output-on-failure --max-width 120
      working-directory: ../build

    - name: create packages
      run: cmake --build ../build --config ${{ matrix.build-config || 'Release' }} -t ${{ matrix.pkgtgt }}

    - name: Upload build errors
      uses: actions/upload-artifact@v6
      if: failure()
      with:
        name: ${{ matrix.name }} (cmake)
        path: |
          ../build/CMakeFiles/CMakeOutput.log
          ../build/CMakeFiles/CMakeError.log
        retention-days: 7
