name: Build
on:
  pull_request:
  push:
  release:
    types: published
jobs:
  build-cmake:
    name: ${{ matrix.case.name }}
    runs-on: ${{ matrix.case.os }}
    container: ${{ matrix.case.container }}

    strategy:
      fail-fast: false
      matrix:
        case:
        - { name: 'Windows (DirectX 9)',  os: 'windows-latest', platform: 'D3D9' }
        - { name: 'Windows (GL3, glfw)',  os: 'windows-latest', platform: 'GL3',  gl3_gfxlib: 'GLFW' }
        - { name: 'Windows (GL3, SDL2)',  os: 'windows-latest', platform: 'GL3',  gl3_gfxlib: 'SDL2', sdl-version: '2-latest' }
        - { name: 'Windows (GL3, SDL3)',  os: 'windows-latest', platform: 'GL3',  gl3_gfxlib: 'SDL3', sdl-version: '3-latest' }
        - { name: 'Windows (null)',       os: 'windows-latest', platform: 'NULL' }
        - { name: 'macOS (GL3, glfw)',    os: 'macos-latest',   platform: 'GL3',  gl3_gfxlib: 'GLFW' }
        - { name: 'macOS (GL3, SDL2)',    os: 'macos-latest',   platform: 'GL3',  gl3_gfxlib: 'SDL2', sdl-version: '2-latest' }
        - { name: 'macOS (GL3, SDL3)',    os: 'macos-latest',   platform: 'GL3',  gl3_gfxlib: 'SDL3', sdl-version: '3-latest' }
        - { name: 'macOS (null)',         os: 'macos-latest',   platform: 'NULL' }
        - { name: 'Ubuntu (GL3, glfw)',   os: 'ubuntu-latest',  platform: 'GL3',  gl3_gfxlib: 'GLFW' }
        - { name: 'Ubuntu (GL3, SDL2)',   os: 'ubuntu-latest',  platform: 'GL3',  gl3_gfxlib: 'SDL2', sdl-version: '2-latest' }
        - { name: 'Ubuntu (null)',        os: 'ubuntu-latest',  platform: 'NULL' }
        - { name: 'PlayStation 2',        os: 'ubuntu-latest',  platform: 'PS2',  ps2: true,                              container: 'ps2dev/ps2dev:latest',        cmake-toolchain-file: 'cmake/ps2/cmaketoolchain/toolchain_ps2_ee.cmake' }
        - { name: 'Nintendo Switch',      os: 'ubuntu-latest',  platform: 'GL3',  gl3_gfxlib: 'GLFW', glfw-nobuild: true, container: 'devkitpro/devkita64:latest',  cmake-toolchain-file: '/opt/devkitpro/cmake/Switch.cmake' }

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v4
        with:
          python-version: '3.x'
      - name: 'Install PS2 dependencies'
        if: ${{ matrix.case.ps2 }}
        run: |
          apk add cmake gmp mpc1 mpfr4 make pkgconf git
      - name: 'Setup SDL'
        if: ${{ !!matrix.case.sdl-version }}
        uses: libsdl-org/setup-sdl@main
        with:
          version: ${{ matrix.case.sdl-version }}
          install-linux-dependencies: true
          cmake-toolchain-file: ${{ matrix.case.cmake-toolchain-file }}
      - name: 'Setup GLFW'
        if: ${{ matrix.case.gl3_gfxlib == 'GLFW' && !matrix.case.glfw-nobuild }}
        shell: sh
        run: |
          ${{ (runner.os == 'Linux' && 'sudo apt-get update -y && sudo apt-get install -y libwayland-dev libxkbcommon-dev xorg-dev') || '' }}
          git clone "https://github.com/glfw/glfw.git" glfw-source --depth 1
          cmake -S glfw-source -B glfw-build -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF -DGLFW_BUILD_DOCS=OFF -DGLFW_INSTALL=ON -DCMAKE_INSTALL_PREFIX=glfw-prefix -DCMAKE_TOOLCHAIN_FILE="${{ matrix.case.cmake-toolchain-file }}" -DCMAKE_BUILD_TYPE=Release
          cmake --build glfw-build --config Release
          cmake --install glfw-build --config Release
          echo "glfw3_ROOT=${{ github.workspace}}/glfw-prefix" >${GITHUB_ENV}
      - name: 'Configure (CMake)'
        shell: sh
        run: |
          cmake -S . -B build \
            -DLIBRW_PLATFORM=${{ matrix.case.platform }} \
            -DLIBRW_GL3_GFXLIB=${{ matrix.case.gl3_gfxlib }} \
            -DCMAKE_TOOLCHAIN_FILE=${{ matrix.case.cmake-toolchain-file }} \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_POLICY_DEFAULT_CMP0074=NEW
      - name: 'Build (CMake)'
        run: |
          cmake --build build --config Release --verbose
      - name: 'Create binary package (CPack)'
        run: |
          cd build
          cpack . -C Release
      - name: 'Archive binary package (GitHub artifacts)'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.case.os }}-${{ matrix.case.platform }}${{ (matrix.case.gl3_gfxlib != '' && format('-{0}', matrix.case.gl3_gfxlib)) || '' }}
          path: build/*.tar.xz
          if-no-files-found: error
