name: citra-ci

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

jobs:
  clang-format:
    runs-on: ubuntu-latest
    container: 
      image: citraemu/build-environments:linux-fresh
      options: -u 1001
    steps:
      - uses: actions/checkout@v3
        with: 
          fetch-depth: 0
      - name: Build
        env:
          COMMIT_RANGE: ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}
        run: ./.ci/clang-format.sh
  source:
    if: ${{ !github.head_ref }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Pack
        run: ./.ci/source.sh
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: source
          path: artifacts/
  linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: ["appimage", "fresh"]
    container:
      image: citraemu/build-environments:linux-${{ matrix.target }}
      options: -u 1001
    env:
      CCACHE_DIR: ${{ github.workspace }}/.ccache
      OS: linux
      TARGET: ${{ matrix.target }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Set up cache
        uses: actions/cache@v3
        with:
          path: ${{ env.CCACHE_DIR }}
          key: ${{ runner.os }}-${{ matrix.target }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.target }}-
      - name: Build
        run: ./.ci/linux.sh
      - name: Pack
        run: ./.ci/pack.sh
        if: ${{ matrix.target == 'appimage' }}
      - name: Upload
        uses: actions/upload-artifact@v3
        if: ${{ matrix.target == 'appimage' }}
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: artifacts/
  macos:
    runs-on: macos-latest
    strategy:
      matrix:
        target: ["x86_64", "arm64"]
    env:
      CCACHE_CPP2: yes
      CCACHE_SLOPPINESS: time_macros
      CCACHE_DIR: ${{ github.workspace }}/.ccache
      OS: macos
      TARGET: ${{ matrix.target }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Set up cache
        uses: actions/cache@v3
        with:
          path: ${{ env.CCACHE_DIR }}
          key: ${{ runner.os }}-${{ matrix.target }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.target }}-
      - name: Install tools
        run: brew install ccache glslang ninja
      - name: Build
        run: ./.ci/macos.sh
      - name: Prepare outputs for caching
        run: mv build/bundle $OS-$TARGET
      - name: Cache outputs for universal build
        uses: actions/cache/save@v3
        with:
          path: ${{ env.OS }}-${{ env.TARGET }}
          key: ${{ runner.os }}-${{ matrix.target }}-${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
  macos-universal:
    runs-on: macos-latest
    needs: macos
    env:
      OS: macos
      TARGET: universal
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Download x86_64 build from cache
        uses: actions/cache/restore@v3
        with:
          path: ${{ env.OS }}-x86_64
          key: ${{ runner.os }}-x86_64-${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
          fail-on-cache-miss: true
      - name: Download ARM64 build from cache
        uses: actions/cache/restore@v3
        with:
          path: ${{ env.OS }}-arm64
          key: ${{ runner.os }}-arm64-${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
          fail-on-cache-miss: true
      - name: Create universal app
        run: ./.ci/macos-universal.sh
        env:
          ARTIFACTS: ${{ env.OS }}-x86_64 ${{ env.OS }}-arm64
      - name: Pack
        run: ./.ci/pack.sh
#      - name: Upload
#        uses: actions/upload-artifact@v3
#        with:
#          name: ${{ env.OS }}-${{ env.TARGET }}
#          path: artifacts/
  windows:
    runs-on: windows-latest
    strategy:
      matrix:
        target: ["msvc", "msys2"]
    defaults:
      run:
        shell: ${{ (matrix.target == 'msys2' && 'msys2') || 'bash' }} {0}
    env:
      CCACHE_DIR: ${{ github.workspace }}/.ccache
      OS: windows
      TARGET: ${{ matrix.target }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Set up cache
        uses: actions/cache@v3
        with:
          path: ${{ env.CCACHE_DIR }}
          key: ${{ runner.os }}-${{ matrix.target }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.target }}-
      - name: Set up MSVC
        uses: ilammy/msvc-dev-cmd@v1
        if: ${{ matrix.target == 'msvc' }}
      - name: Install MSVC extra tools
        run: choco install ccache ninja wget
        if: ${{ matrix.target == 'msvc' }}
      - name: Set up MSYS2
        uses: msys2/setup-msys2@v2
        if: ${{ matrix.target == 'msys2' }}
        with:
          msystem: clang64
          update: true
          install: git make p7zip
          pacboy: >-
            toolchain:p ccache:p cmake:p ninja:p
            qt6-base:p qt6-multimedia:p qt6-multimedia-wmf:p qt6-tools:p qt6-translations:p
      - name: Setup Vulkan SDK
        uses: humbletim/setup-vulkan-sdk@v1.2.0
        with:
          vulkan-query-version: latest
          vulkan-components: Glslang
          vulkan-use-cache: true
      - name: Test glslangValidator
        run: glslangValidator --version
      - name: Disable line ending translation
        run: git config --global core.autocrlf input
      - name: Build
        run: ./.ci/windows.sh
      - name: Pack
        run: ./.ci/pack.sh
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: artifacts/
  android:
    runs-on: ubuntu-latest
    env:
      OS: android
      TARGET: universal
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Set up cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
            ~/.ccache
          key: ${{ runner.os }}-android-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-android-
      - name: Set tag name
        run: |
          if [[ "$GITHUB_REF_NAME" =~ ^canary- ]] || [[ "$GITHUB_REF_NAME" =~ ^nightly- ]]; then
              echo "GIT_TAG_NAME=$GITHUB_REF_NAME" >> $GITHUB_ENV
          fi
          echo $GIT_TAG_NAME
      - name: Deps
        run: |
          sudo add-apt-repository -y ppa:theofficialgman/gpu-tools
          sudo apt-get update -y
          sudo apt-get install ccache glslang-dev glslang-tools apksigner -y
      - name: Build
        run: JAVA_HOME=$JAVA_HOME_17_X64 ./.ci/android.sh
        env:
          ANDROID_KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_B64 }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEYSTORE_PASS: ${{ secrets.ANDROID_KEYSTORE_PASS }}
      - name: Pack
        run: ../../../.ci/pack.sh
        working-directory: src/android/app
        env:
          UPLOAD_RAW: 1
      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: ${{ env.OS }}-${{ env.TARGET }}
          path: src/android/app/artifacts/
  transifex:
    runs-on: ubuntu-latest
    container: citraemu/build-environments:linux-fresh
    if: ${{ github.repository == 'citra-emu/citra' && !github.head_ref }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
          fetch-depth: 0
      - name: Update Translation
        run: ./.ci/transifex.sh
        env:
          TX_TOKEN: ${{ secrets.TRANSIFEX_API_TOKEN }}
  release:
    runs-on: ubuntu-latest
    needs: [windows, linux, macos-universal, android, source]
    if: ${{ startsWith(github.ref, 'refs/tags/') }}
    steps:
      - uses: actions/download-artifact@v3
      - name: Create release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref_name }}
          release_name: ${{ github.ref_name }}
          draft: false
          prerelease: false
      - name: Upload artifacts
        uses: alexellis/upload-assets@0.4.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          asset_paths: '["./**/*.tar.*","./**/*.AppImage","./**/*.7z","./**/*.zip","./**/*.apk","./**/*.aab"]'