name: Linux build

on:
  push:
    paths-ignore:
      - '.clang-format'
      - '.drone.star'
      - '.gitattributes'
      - '.gitignore'
      - '.github/*'
      - '.github/workflows/Windows_build.yml'
      - '.github/*_TEMPLATE/**'
      - '*.bat'
      - '*.md'
      - '*.ps1'
      - '*.txt'
      - '*.yml'
      - 'docs/**'
      - 'src/**/*_windows.*'
      - 'src/**/*_android.*'
      - 'src/**/*_mac.*'
      - 'LICENSE'
  pull_request:
    paths-ignore:
      - '.clang-format'
      - '.drone.star'
      - '.gitattributes'
      - '.gitignore'
      - '.github/*'
      - '.github/workflows/Windows_build.yml'
      - '.github/*_TEMPLATE/**'
      - '*.bat'
      - '*.md'
      - '*.ps1'
      - '*.txt'
      - '*.yml'
      - 'docs/**'
      - 'src/**/*_windows.*'
      - 'src/**/*_android.*'
      - 'src/**/*_mac.*'
      - 'LICENSE'
  workflow_dispatch:

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-24.04
    outputs:
      #LLVM_VERSION: ${{ steps.setup.outputs.LLVM_VERSION }}
      UBUNTU_BASE: ${{ steps.setup.outputs.UBUNTU_BASE }}
    steps:
      - uses: actions/checkout@main
      - name: Setup
        id: setup
        env:
          LLVM_VERSION: 19 # Same as Windows
        run: |
          UBUNTU_BASE=$(lsb_release -cs)
          #echo "LLVM_VERSION=$LLVM_VERSION" >> "$GITHUB_OUTPUT"
          echo "UBUNTU_BASE=$UBUNTU_BASE" >> "$GITHUB_OUTPUT"
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          sudo apt-add-repository "deb http://apt.llvm.org/${UBUNTU_BASE}/ llvm-toolchain-${UBUNTU_BASE}-$LLVM_VERSION main"
          sudo apt-get -y update
          sudo apt-get -y install clang-format-$LLVM_VERSION
      - name: Lint
        run: ./xenia-build.py lint --all

  build-clang:
    name: Build (Clang ${{ matrix.LLVM_VERSION }})
    needs: lint
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        LLVM_VERSION: [20] # '${{ needs.lint.outputs.LLVM_VERSION }}'
    steps:
      - uses: actions/checkout@main
        with:
          fetch-depth: 0
      - uses: actions/cache@main
        id: cache
        with:
          key: ${{ hashFiles('tools/build/bin/premake5.exe') }}
          path: tools/build/bin/premake5
      - name: Setup
        env:
          UBUNTU_BASE: ${{ needs.lint.outputs.UBUNTU_BASE }}
        run: |
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          sudo apt-add-repository "deb http://apt.llvm.org/${UBUNTU_BASE}/ llvm-toolchain-${UBUNTU_BASE}-${{ matrix.LLVM_VERSION }} main"
          sudo apt-get -y update
          sudo apt-get -y install mesa-vulkan-drivers valgrind libc++-dev libc++abi-dev libgtk-3-dev libsdl2-dev libvulkan-dev libx11-xcb-dev clang-${{ matrix.LLVM_VERSION }} ninja-build
          git submodule update --init --depth=1 -j$(getconf _NPROCESSORS_ONLN) $(grep -oP '(?<=path = )(?!third_party\/(DirectXShaderCompiler|FidelityFX-(CAS|FSR)|premake-(androidndk|export-compile-commands))).+' .gitmodules)
          if [ '${{ steps.cache.outputs.cache-hit }}' != 'true' ]; then
            ./xenia-build.py premake
            cp third_party/premake-core/bin/release/premake5 tools/build/bin
          fi
      - name: Build
        env:
          CC: clang-${{ matrix.LLVM_VERSION }}
          CXX: clang++-${{ matrix.LLVM_VERSION }}
        run: ./xenia-build.py build --config=Release
      - name: Prepare artifacts
        id: prepare_artifacts
        run: |
          binary=build/bin/Linux/Release/xenia_canary
          if [ $(stat -c%s $binary) -le 100000 ]; then
            echo "::error::Binary is too small."
          fi
          chmod +x $binary
          mkdir -p artifacts
          XZ_OPT=-e9 tar cJvpf artifacts/xenia_canary_linux.tar.xz $binary LICENSE
      - name: Upload xenia canary artifacts
        if: steps.prepare_artifacts.outcome == 'success'
        uses: actions/upload-artifact@main
        with:
          name: xenia_canary_linux
          path: artifacts
          if-no-files-found: error

#  build-gcc:
#    name: Build (GCC ${{ matrix.GCC_VERSION }})
#    needs: lint
#    runs-on: ubuntu-24.04
#    strategy:
#      fail-fast: false
#      matrix:
#        GCC_VERSION: [14]
#    steps:
#      - uses: actions/checkout@main
#        with:
#          fetch-depth: 0
#      - name: Setup
#        run: |
#          sudo apt-get -y update
#          sudo apt-get -y install mesa-vulkan-drivers valgrind libc++-dev libc++abi-dev libgtk-3-dev libsdl2-dev libvulkan-dev libx11-xcb-dev g++-${{ matrix.GCC_VERSION }}
#          ./xenia-build.py setup
#      - name: Build
#        env:
#          CC: gcc-${{ matrix.GCC_VERSION }}
#          CXX: g++-${{ matrix.GCC_VERSION }}
#        #  AR: ar
#        run: ./xenia-build.py build --config=Release
#      - name: Prepare artifacts
#        id: prepare_artifacts
#        run: |
#          if [ $(stat -c %s build/bin/Linux/Release/xenia_canary) -le 100000 ]; then
#            echo "::error::Binary is too small."
#          fi
#          mkdir -p artifacts
#          cp -r build/bin/Linux/Release/xenia_canary LICENSE artifacts
#      - name: Upload xenia canary artifacts
#        if: steps.prepare_artifacts.outcome == 'success'
#        uses: actions/upload-artifact@main
#        with:
#          name: xenia_canary_linux_gcc
#          path: artifacts
#          if-no-files-found: error

  create-release:
    name: Create release
    needs: [lint, build-clang] #build-gcc
    if: |
      github.repository == 'xenia-canary/xenia-canary' &&
      github.event.action != 'pull_request' &&
      github.ref == 'refs/heads/canary_experimental'
    uses: ./.github/workflows/Create_release.yml
    with:
      os: linux
    secrets:
      RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
