name: Build (Sony Playstation Vita)

on: [push, pull_request]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
  cancel-in-progress: true

defaults:
  run:
    shell: sh

jobs:
  vita:
    name: ${{ matrix.platform.name }}
    runs-on: ubuntu-latest
    container:
      image: vitasdk/vitasdk:latest
    strategy:
      fail-fast: false
      matrix:
        platform:
        - { name: Vita (GLES w/ pib),                  pib: true, version: 1.1.4, artifact: SDL-vita-pib }
        - { name: Vita (GLES w/ PVR_PSP2 + gles4vita), pvr: true, version: 3.9,   artifact: SDL-vita-pvr }

    steps:
    - uses: actions/checkout@v4
    - name: Install build requirements
      run: |
        apk update
        apk add cmake ninja pkgconf bash tar

    - uses: actions/cache/restore@v4
      id: restore-cache
      with:
        path: /vita/dependencies
        key: ${{ matrix.platform.artifact }}-${{ matrix.platform.version }}

    - name: Download PVR_PSP2 (GLES)
      if: ${{ !!matrix.platform.pvr && !steps.restore-cache.outputs.cache-hit }}
      run: |
        pvr_psp2_version=${{ matrix.platform.version }}

        mkdir -p /vita/dependencies/include
        mkdir -p /vita/dependencies/lib

        # Configure PVR_PSP2 headers
        wget https://github.com/GrapheneCt/PVR_PSP2/archive/refs/tags/v$pvr_psp2_version.zip -P/tmp
        unzip /tmp/v$pvr_psp2_version.zip -d/tmp
        cp -r /tmp/PVR_PSP2-$pvr_psp2_version/include/* /vita/dependencies/include
        rm /tmp/v$pvr_psp2_version.zip
        
        # include guard of PVR_PSP2's khrplatform.h does not match the usual one
        sed -i -e s/__drvkhrplatform_h_/__khrplatform_h_/ /vita/dependencies/include/KHR/khrplatform.h

        # Configure PVR_PSP2 stub libraries
        wget https://github.com/GrapheneCt/PVR_PSP2/releases/download/v$pvr_psp2_version/vitasdk_stubs.zip -P/tmp
        unzip /tmp/vitasdk_stubs.zip -d/tmp/pvr_psp2_stubs
        find /tmp/pvr_psp2_stubs -type f -name "*.a" -exec cp {} /vita/dependencies/lib \;
        rm /tmp/vitasdk_stubs.zip
        rm -rf /tmp/pvr_psp2_stubs

    - name: Download gl4es4vita (OpenGL)
      if: ${{ !!matrix.platform.pib && !steps.restore-cache.outputs.cache-hit }}
      run: |
        gl4es4vita_version=${{ matrix.platform.version }}

        mkdir -p /vita/dependencies/include
        mkdir -p /vita/dependencies/lib

        # Configure gl4es4vita headers
        wget https://github.com/SonicMastr/gl4es4vita/releases/download/v$gl4es4vita_version-vita/include.zip -P/tmp
        unzip -o /tmp/include.zip -d/vita/dependencies/include
        rm /tmp/include.zip

        # Configure gl4es4vita stub libraries
        wget https://github.com/SonicMastr/gl4es4vita/releases/download/v$gl4es4vita_version-vita/vitasdk_stubs.zip -P/tmp
        unzip /tmp/vitasdk_stubs.zip -d/vita/dependencies/lib

    - uses: actions/cache/save@v4
      if: ${{ !steps.restore-cache.outputs.cache-hit }}
      with:
        path: /vita/dependencies
        key: ${{ matrix.platform.artifact }}-${{ matrix.platform.version }}

    - name: Copy PVR_PSP2 (GLES) or gl4es4vita (OpenGL) to vita toolchain dir
      run: |
        cp -rv /vita/dependencies/* ${VITASDK}/arm-vita-eabi

    - name: Fix vita.toolchain.cmake
      run: |
        # cache PKG_CONFIG_PATH
        sed -i -E 's/set\( PKG_CONFIG_EXECUTABLE "\$\{VITASDK}\/bin\/arm-vita-eabi-pkg-config" )/set( PKG_CONFIG_EXECUTABLE "${VITASDK}\/bin\/arm-vita-eabi-pkg-config" CACHE PATH "Path of pkg-config executable" )/' ${VITASDK}/share/vita.toolchain.cmake

    - name: Configure (CMake)
      run: |
        cmake -S . -B build -G Ninja \
          -Wdeprecated -Wdev -Werror \
          -DCMAKE_TOOLCHAIN_FILE=${VITASDK}/share/vita.toolchain.cmake \
          -DVIDEO_VITA_PIB=${{ !!matrix.platform.pib }} \
          -DVIDEO_VITA_PVR=${{ !!matrix.platform.pvr }} \
          -DSDL_ARMNEON=ON \
          -DSDL_ARMSIMD=ON \
          -DSDL_WERROR=ON \
          -DSDL_TESTS=ON \
          -DSDL_INSTALL_TESTS=ON \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_INSTALL_PREFIX=prefix
    - name: Build (CMake)
      run: cmake --build build --verbose
    - name: Install (CMake)
      run: |
        echo "SDL3_DIR=$(pwd)/prefix" >> $GITHUB_ENV
        cmake --install build/
        ( cd prefix; find ) | LC_ALL=C sort -u
    - name: Package (CPack)
      run: |
        cmake --build build/ --config Release --target package
    - name: Verify CMake configuration files
      run: |
        cmake -S cmake/test -B cmake_config_build -G Ninja \
          -DCMAKE_TOOLCHAIN_FILE=${VITASDK}/share/vita.toolchain.cmake \
          -DTEST_SHARED=FALSE \
          -DCMAKE_PREFIX_PATH=${{ env.SDL3_DIR }} \
          -DCMAKE_BUILD_TYPE=Release
        cmake --build cmake_config_build --verbose
    - name: Verify sdl3.pc
      run: |
        export CC=arm-vita-eabi-gcc
        export PKG_CONFIG_PATH=${{ env.SDL3_DIR }}/lib/pkgconfig
        cmake/test/test_pkgconfig.sh
    - uses: actions/upload-artifact@v4
      with:
        if-no-files-found: error
        name: ${{ matrix.platform.artifact }}
        path: build/dist/SDL3*