name: citra-libretro

on:
  push:
    branches: [ "*" ]
    tags: [ "*" ]
  pull_request:
    branches: [ master ]
  workflow_dispatch:

env:
  CORE_ARGS: -DENABLE_LIBRETRO=ON

permissions:
  id-token: write
  contents: read
  attestations: write

jobs:
  libretro-android:
    runs-on: ubuntu-22.04
    env:
      OS: android
      TARGET: arm64-v8a
      API_LEVEL: 21
      ANDROID_NDK_VERSION: 26.2.11394342
      ANDROID_ABI: arm64-v8a
      BUILD_DIR: build/android-arm64-v8a
      EXTRA_PATH: bin/Release
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: Set tag name
        run: |
          if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
              echo "GIT_TAG_NAME=$GITHUB_REF_NAME" >> $GITHUB_ENV
          fi
          echo $GIT_TAG_NAME
      - name: Install tools
        run: |
          sudo apt-get update -y
          sudo apt-get install -y llvm
      - name: Update Android SDK CMake version
        run: |
          echo "y" | ${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager "ndk;$ANDROID_NDK_VERSION"
          echo "y" | ${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager "cmake;3.30.3"
      - name: Build
        run: |
          export NDK_ROOT=${ANDROID_SDK_ROOT}/ndk/$ANDROID_NDK_VERSION
          ${ANDROID_SDK_ROOT}/cmake/3.30.3/bin/cmake $CORE_ARGS -DANDROID_PLATFORM=android-$API_LEVEL -DCMAKE_TOOLCHAIN_FILE=$NDK_ROOT/build/cmake/android.toolchain.cmake -DANDROID_STL=c++_static -DANDROID_ABI=$ANDROID_ABI . -B $BUILD_DIR
          ${ANDROID_SDK_ROOT}/cmake/3.30.3/bin/cmake --build $BUILD_DIR --target azahar_libretro --config Release -j $(nproc)
          llvm-strip -s $BUILD_DIR/$EXTRA_PATH/azahar_libretro.*
      - name: Pack
        run: ./.ci/libretro-pack.sh
      - name: Generate SBOM
        if: ${{ github.ref_type == 'tag' }}
        uses: anchore/sbom-action@v0
        with:
          path: build/
          format: spdx-json
          output-file: libretro-android.spdx.json
          upload-artifact: false
      - name: Upload
        uses: actions/upload-artifact@v7
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: |
            ./*.zip
            ./*.spdx.json
      - name: Attest artifacts
        if: ${{ github.ref_type == 'tag' }}
        uses: actions/attest@v4
        with:
          subject-path: |
            ./*.zip
          sbom-path: libretro-android.spdx.json

  libretro-linux:
    runs-on: ubuntu-22.04
    env:
      OS: linux
      TARGET: x86_64
      BUILD_DIR: build/linux-x86_64
      EXTRA_PATH: bin/Release
      EXTRA_CORE_ARGS: -DCMAKE_C_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12 -DENABLE_LTO=OFF
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: Install tools
        run: |
          sudo apt-get update -y
          sudo apt-get install -y llvm
      - name: Build
        run: |
          cmake $CORE_ARGS $EXTRA_CORE_ARGS . -B $BUILD_DIR
          cmake --build $BUILD_DIR --target azahar_libretro --config Release -j $(nproc)
          llvm-strip -s $BUILD_DIR/$EXTRA_PATH/azahar_libretro.*
      - name: Pack
        run: ./.ci/libretro-pack.sh
      - name: Generate SBOM
        if: ${{ github.ref_type == 'tag' }}
        uses: anchore/sbom-action@v0
        with:
          path: build/
          format: spdx-json
          output-file: libretro-linux.spdx.json
          upload-artifact: false
      - name: Upload
        uses: actions/upload-artifact@v7
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: |
            ./*.zip
            ./*.spdx.json
      - name: Attest artifacts
        if: ${{ github.ref_type == 'tag' }}
        uses: actions/attest@v4
        with:
          subject-path: |
            ./*.zip
          sbom-path: libretro-linux.spdx.json

  libretro-windows:
    runs-on: ubuntu-latest
    env:
      OS: windows
      TARGET: x86_64
      BUILD_DIR: build/windows-x86_64
      EXTRA_CORE_ARGS: -DENABLE_LTO=OFF -G Ninja
      CMAKE: x86_64-w64-mingw32.static-cmake
      IMAGE: reallibretroretroarch/libretro-build-mxe-win-cross-cores:mingw12
      EXTRA_PATH: bin/Release
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: Build in cross-container
        run: |
          docker pull $IMAGE
          docker run --rm --user root \
              -v "${GITHUB_WORKSPACE}:${GITHUB_WORKSPACE}" \
              -w "${GITHUB_WORKSPACE}" \
              $IMAGE \
              bash -lc "\
                  ${CMAKE} $CORE_ARGS $EXTRA_CORE_ARGS . -B $BUILD_DIR && \
                  ${CMAKE} --build $BUILD_DIR --target azahar_libretro --config Release -j $(nproc) && \
                  x86_64-w64-mingw32.static-strip -s $BUILD_DIR/$EXTRA_PATH/azahar_libretro.*"
      - name: Pack
        run: ./.ci/libretro-pack.sh
      - name: Generate SBOM
        if: ${{ github.ref_type == 'tag' }}
        uses: anchore/sbom-action@v0
        with:
          path: build/
          format: spdx-json
          output-file: libretro-windows.spdx.json
          upload-artifact: false
      - name: Upload
        uses: actions/upload-artifact@v7
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: |
            ./*.zip
            ./*.spdx.json
      - name: Attest artifacts
        if: ${{ github.ref_type == 'tag' }}
        uses: actions/attest@v4
        with:
          subject-path: |
            ./*.zip
          sbom-path: libretro-windows.spdx.json
  libretro-macos:
    runs-on: macos-26
    strategy:
      fail-fast: false
      matrix:
        target: ["x86_64", "arm64"]
    env:
      OS: macos
      TARGET: ${{ matrix.target }}
      MACOSX_DEPLOYMENT_TARGET: 11.0
      BUILD_DIR: build/osx-${{ matrix.target }}
      EXTRA_PATH: bin/Release
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: Install tools
        run: brew install spirv-tools
      - name: Build
        run: |
          cmake $CORE_ARGS -DCMAKE_OSX_ARCHITECTURES=$TARGET . -B $BUILD_DIR
          cmake --build $BUILD_DIR --target azahar_libretro --config Release
          strip -x $BUILD_DIR/$EXTRA_PATH/azahar_libretro.*
      - name: Pack
        run: ./.ci/libretro-pack.sh
      - name: Generate SBOM
        if: ${{ github.ref_type == 'tag' }}
        uses: anchore/sbom-action@v0
        with:
          path: build/
          format: spdx-json
          output-file: libretro-macos-${{ matrix.target }}.spdx.json
          upload-artifact: false
      - name: Upload
        uses: actions/upload-artifact@v7
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: |
            ./*.zip
            ./*.spdx.json
      - name: Attest artifacts
        if: ${{ github.ref_type == 'tag' }}
        uses: actions/attest@v4
        with:
          subject-path: |
            ./*.zip
          sbom-path: libretro-macos-${{ matrix.target }}.spdx.json

  libretro-ios:
    runs-on: macos-26
    env:
      OS: ios
      TARGET: arm64
      BUILD_DIR: build/ios-arm64
      EXTRA_PATH: bin/Release
      EXTRA_CORE_ARGS: -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_C_FLAGS=-DIOS -DCMAKE_CXX_FLAGS=-DIOS -DIOS=ON -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 -DCITRA_USE_PRECOMPILED_HEADERS=OFF -DCMAKE_OSX_ARCHITECTURES=arm64 -DENABLE_OPT=OFF
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: Build
        run: |
          cmake $CORE_ARGS $EXTRA_CORE_ARGS . -B $BUILD_DIR
          cmake --build $BUILD_DIR --target azahar_libretro --config Release
          strip -x $BUILD_DIR/$EXTRA_PATH/azahar_libretro.*
      - name: Pack
        run: ./.ci/libretro-pack.sh
      - name: Generate SBOM
        if: ${{ github.ref_type == 'tag' }}
        uses: anchore/sbom-action@v0
        with:
          path: build/
          format: spdx-json
          output-file: libretro-ios.spdx.json
          upload-artifact: false
      - name: Upload
        uses: actions/upload-artifact@v7
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: |
            ./*.zip
            ./*.spdx.json
      - name: Attest artifacts
        if: ${{ github.ref_type == 'tag' }}
        uses: actions/attest@v4
        with:
          subject-path: |
            ./*.zip
          sbom-path: libretro-ios.spdx.json

  libretro-tvos:
    runs-on: macos-26
    env:
      OS: tvos
      TARGET: arm64
      BUILD_DIR: build/tvos-arm64
      EXTRA_PATH: bin/Release
      EXTRA_CORE_ARGS: -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_C_FLAGS=-DIOS -DCMAKE_CXX_FLAGS=-DIOS -DIOS=ON -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 -DCITRA_USE_PRECOMPILED_HEADERS=OFF -DCMAKE_OSX_SYSROOT=appletvos -DCMAKE_OSX_ARCHITECTURES=arm64 -DENABLE_OPT=OFF
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: Build
        run: |
          cmake $CORE_ARGS $EXTRA_CORE_ARGS . -B $BUILD_DIR
          cmake --build $BUILD_DIR --target azahar_libretro --config Release
          strip -x $BUILD_DIR/$EXTRA_PATH/azahar_libretro.*
      - name: Pack
        run: ./.ci/libretro-pack.sh
      - name: Generate SBOM
        if: ${{ github.ref_type == 'tag' }}
        uses: anchore/sbom-action@v0
        with:
          path: build/
          format: spdx-json
          output-file: libretro-tvos.spdx.json
          upload-artifact: false
      - name: Upload
        uses: actions/upload-artifact@v7
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: |
            ./*.zip
            ./*.spdx.json
      - name: Attest artifacts
        if: ${{ github.ref_type == 'tag' }}
        uses: actions/attest@v4
        with:
          subject-path: |
            ./*.zip
          sbom-path: libretro-tvos.spdx.json
