name: Valgrind Check & Clang Sanitizers

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup CMake
        uses: lukka/get-cmake@latest

      - name: Setup Clang
        uses: KyleMayes/install-llvm-action@v2
        with:
          version: "18"
          arch: "x64"

      - name: Install valgrind
        run: |
          sudo apt update
          sudo apt install -y valgrind

      - name: Configure CMake & Build
        run: |
          cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_OPTIMIZATION_FAST_MATH=OFF -DENABLE_OPTIMIZATION=OFF -DAPPEND_VERSION=OFF -DENABLE_LTO=OFF -DENABLE_WARNINGS=ON -DENABLE_SANITIZER=OFF -DENABLE_TESTS=OFF .
          cmake --build .

      - name: Run Valgrind
        run: |
          valgrind --error-exitcode=1 --errors-for-leak-kinds=definite,indirect,possible --show-leak-kinds=definite,indirect,possible --leak-check=full --show-reachable=no ./Zagreus fastbench

      - name: Delete CMake cache
        run: rm -rf CMakeFiles CMakeCache.txt cmake_install.cmake Makefile

      - name: Delete build directory
        run: rm -rf build

      - name: Build w/ sanitizers
        run: |
          cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_OPTIMIZATION_FAST_MATH=OFF -DENABLE_OPTIMIZATION=OFF -DAPPEND_VERSION=OFF -DENABLE_LTO=OFF -DENABLE_WARNINGS=ON -DENABLE_SANITIZER=ON -DENABLE_TESTS=OFF .
          cmake --build .

      - name: Run w/ sanitizers
        run: |
          ./Zagreus fastbench