name: Check a pull request

on: [pull_request]

env:
  QT_VERSION: 5.12.12

jobs:
  build_linux:
    strategy:
      matrix:
        configuration: [Debug, Release]
        compiler: [gcc-9, gcc-13, clang-16]
        cmake_options: [""]
        include:
          # Also include configurations that check if the code compiles without the graphics backends
          - configuration: Debug
            compiler: gcc-13
            cmake_options: -DFSO_BUILD_WITH_OPENGL=OFF -DFSO_BUILD_WITH_VULKAN=OFF
          - configuration: Release
            compiler: gcc-13
            cmake_options: -DFSO_BUILD_WITH_OPENGL=OFF -DFSO_BUILD_WITH_VULKAN=OFF
          - configuration: Debug
            compiler: gcc-13
            cmake_options: -DFSO_BUILD_WITH_OPENGL=OFF -DFSO_BUILD_WITH_VULKAN=ON
          - configuration: Release
            compiler: gcc-13
            cmake_options: -DFSO_BUILD_WITH_OPENGL=OFF -DFSO_BUILD_WITH_VULKAN=ON
    name: Linux
    runs-on: ubuntu-latest
    container: ghcr.io/scp-fs2open/linux_build:sha-71099c9
    steps:
      - name: Install Qt dependencies
        run: apt-get -yq update && apt-get -yq install libfontconfig1
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
          fetch-depth: 0
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: ccache
        uses: hendrikmuhs/ccache-action@v1
        with:
          key: ${{ runner.os }}-${{ matrix.configuration }}-${{ matrix.compiler }}
          save: false # Caches are created by a separate job and only restored for PRs
          verbose: 1
      - name: Configure ccache
        run: ccache --set-config=sloppiness=pch_defines,time_macros
      - name: Configure CMake
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: ${{ matrix.compiler }}
          JOB_CMAKE_OPTIONS: ${{ matrix.cmake_options }}
          CCACHE_PATH: /usr/local/bin/ccache
          ENABLE_QTFRED: ON
          Qt5_DIR: /qt/${{ env.QT_VERSION }}/gcc_64/lib/cmake/Qt5
        run: $GITHUB_WORKSPACE/ci/linux/configure_cmake.sh
      - name: Compile
        working-directory: ./build
        run: LD_LIBRARY_PATH=$Qt5_DIR/lib:$LD_LIBRARY_PATH ninja -k 20 all
      - name: Run Tests
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          XDG_RUNTIME_DIR: /root
        run: $GITHUB_WORKSPACE/ci/linux/run_tests.sh
      - name: Show CCache statistics
        run: ccache --show-stats
      - name: Run Clang Tidy
        # Clang-tidy reuses the precompiled headers so this only makes sense for the clang compilers
        if: startsWith(matrix.compiler, 'clang-')
        run: $GITHUB_WORKSPACE/ci/linux/clang_tidy.sh ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}
      - name: Process clang-tidy warnings
        if: startsWith(matrix.compiler, 'clang-')
        uses: asarium/clang-tidy-action@v1
        with:
          fixesFile: clang-fixes.yaml

  build_windows:
    strategy:
      matrix:
        configuration: [Debug, FastDebug, Release]
        compiler: [MSVC]
        arch: [Win32, x64]
    name: Windows
    runs-on: windows-2022
    steps:
      - name: Prepare Environment
        run: choco install ninja
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      # - name: Cache Qt
        # id: cache-qt-win
        # uses: actions/cache@v1
        # with:
          # path: ${{ github.workspace }}/../Qt
          # key: ${{ runner.os }}-${{ matrix.arch }}-QtCache-${{ env.QT_VERSION }}
      # - name: Install Qt (32 bit)
        # uses: jurplel/install-qt-action@v2
        # if: ${{ matrix.compiler  == 'MSVC' && matrix.arch == 'Win32' }}
        # with:
          # version: ${{ env.QT_VERSION }}
          # dir: ${{ github.workspace }}/..
          # arch: win32_msvc2017
          # cached: ${{ steps.cache-qt-win.outputs.cache-hit }}
          # aqtversion: ==0.8
      # - name: Install Qt (64 bit)
        # uses: jurplel/install-qt-action@v2
        # if: ${{ matrix.compiler  == 'MSVC' && matrix.arch == 'x64' }}
        # with:
          # version: ${{ env.QT_VERSION }}
          # dir: ${{ github.workspace }}/..
          # arch: win64_msvc2017_64
          # cached: ${{ steps.cache-qt-win.outputs.cache-hit }}
          # aqtversion: ==0.8
      - name: Prepare Vulkan SDK
        uses: humbletim/setup-vulkan-sdk@v1.2.1
        with:
          vulkan-query-version: 1.4.304.1
          vulkan-components: Vulkan-Headers, Vulkan-Loader
          vulkan-use-cache: true
      - name: Configure CMake
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: ${{ matrix.compiler }}
          ARCHITECTURE: ${{ matrix.arch }}
        shell: bash
        run: |
          mkdir build
          cd build

          if [ "$COMPILER" = "MinGW" ]; then
              if [ "$ARCHITECTURE" = "Win32" ]; then
                  cmake -DFSO_USE_SPEECH="OFF" -DFSO_FATAL_WARNINGS="ON" -DFSO_USE_VOICEREC="OFF" -DFSO_BUILD_TESTS="ON" \
                      -DFSO_BUILD_FRED2="OFF" -DFSO_BUILD_WITH_VULKAN="OFF" -DCMAKE_BUILD_TYPE=$CONFIGURATION -G "Ninja" ..
              else
                  cmake -DFSO_USE_SPEECH="OFF" -DFSO_FATAL_WARNINGS="ON" -DFSO_USE_VOICEREC="OFF" -DFSO_BUILD_TESTS="ON" \
                      -DFSO_BUILD_FRED2="OFF" -DCMAKE_BUILD_TYPE=$CONFIGURATION -G "Ninja" ..
              fi
          else
              if [ "$ARCHITECTURE" = "Win32" ]; then
                  cmake -DFSO_USE_SPEECH="ON" -DFSO_FATAL_WARNINGS="ON" -DFSO_USE_VOICEREC="OFF" -DFSO_BUILD_TESTS="ON" \
                      -DFORCED_SIMD_INSTRUCTIONS=SSE2 -DFSO_BUILD_FRED2="ON" -DFSO_BUILD_WITH_VULKAN="OFF" -G "Visual Studio 17 2022" \
                      -DFSO_BUILD_QTFRED=OFF -T "v143" -A "$ARCHITECTURE" -DCMAKE_BUILD_TYPE=$CONFIGURATION ..
              else
                  cmake -DFSO_USE_SPEECH="ON" -DFSO_FATAL_WARNINGS="ON" -DFSO_USE_VOICEREC="OFF" -DFSO_BUILD_TESTS="ON" \
                      -DFORCED_SIMD_INSTRUCTIONS=SSE2 -DFSO_BUILD_FRED2="ON" -G "Visual Studio 17 2022" \
                      -DFSO_BUILD_QTFRED=OFF -T "v143" -A "$ARCHITECTURE" -DCMAKE_BUILD_TYPE=$CONFIGURATION ..
              fi
          fi
      - name: Compile
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: ${{ matrix.compiler }}
        shell: bash
        run: |
          if [ "$COMPILER" = "MinGW" ]; then
              cmake --build . --config "$CONFIGURATION"
          else
              cmake --build . --config "$CONFIGURATION" -- /verbosity:minimal
          fi
      - name: Run Tests
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: ${{ matrix.compiler }}
        shell: bash
        run: |
          if [ "$COMPILER" = "MinGW" ]; then
             ./bin/unittests --gtest_shuffle
          else
              "./bin/$CONFIGURATION/unittests" --gtest_shuffle
          fi

  build_mac:
    strategy:
      matrix:
        configuration: [Debug, Release]
        compiler: [clang]
        arch: [x86_64, arm64]
        cmake_options: [""]
    name: Mac
    runs-on: macos-latest
    steps:
      # - name: Cache Qt
        # id: cache-qt-mac
        # uses: actions/cache@v1
        # with:
          # path: ${{ github.workspace }}/../Qt
          # key: ${{ runner.os }}-QtCache-${{ env.QT_VERSION }}
      # - name: Install Qt
        # uses: jurplel/install-qt-action@v2
        # with:
          # version: ${{ env.QT_VERSION }}
          # dir: ${{ github.workspace }}/..
          # cached: ${{ steps.cache-qt-mac.outputs.cache-hit }}
          # setup-python: 'false'
          # aqtversion: ==1.1.3
          # py7zrversion: '==0.19.*'
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
          fetch-depth: 0
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: ccache
        uses: hendrikmuhs/ccache-action@v1
        with:
          key: ${{ runner.os }}-${{ matrix.configuration }}-${{ matrix.compiler }}-${{ matrix.arch }}
          save: false # Caches are created by a separate job and only restored for PRs
      - name: Prepare Vulkan SDK
        uses: humbletim/setup-vulkan-sdk@v1.2.1
        with:
          vulkan-query-version: 1.4.304.1
          vulkan-components: Vulkan-Headers, Vulkan-Loader
          vulkan-use-cache: true
      - name: Configure CMake
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: ${{ matrix.compiler }}
          ARCHITECTURE: ${{ matrix.arch }}
          JOB_CMAKE_OPTIONS: ${{ matrix.cmake_options }}
          ENABLE_QTFRED: OFF
        run: $GITHUB_WORKSPACE/ci/linux/configure_cmake.sh
      - name: Compile
        working-directory: ./build
        run: LD_LIBRARY_PATH=$Qt5_DIR/lib:$LD_LIBRARY_PATH ninja all
      - name: Run Tests
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          ARCH: ${{ matrix.arch }}
          XDG_RUNTIME_DIR: /root
        run: $GITHUB_WORKSPACE/ci/linux/run_tests.sh
      - name: Run Clang Tidy
        # Clang-tidy reuses the precompiled headers so this only makes sense for the clang compilers
        if: startsWith(matrix.compiler, 'clang-')
        run: $GITHUB_WORKSPACE/ci/linux/clang_tidy.sh ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}
      - name: Process clang-tidy warnings
        if: startsWith(matrix.compiler, 'clang-')
        uses: asarium/clang-tidy-action@v1
        with:
          fixesFile: clang-fixes.yaml
