name: CI tests

on:
  - push
  - pull_request

jobs:
  build:
    name: clasp tests on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
        compiler: [default]
        build_threads: ['On']
        build_type: ['Debug']
        cmake_extra : ["-Wno-dev"]
        include:
          - os: 'ubuntu-latest'
            generator: 'Ninja'
          - os: 'macos-latest'
            generator: 'Ninja'
          - os: 'windows-latest'
            generator: 'Visual Studio 17 2022'
          - os: 'ubuntu-latest'
            build_threads: 'Off'
            generator: 'Ninja'
            build_type: 'Debug'
            cmake_extra: "-Wno-dev"
          - os: 'ubuntu-latest'
            compiler: clang-18
            generator: 'Ninja'
            build_threads: 'On'
            build_type: 'Debug'
            cmake_extra: "-DCMAKE_CXX_FLAGS_INIT=-fsanitize=address,undefined"

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup cache path
        if: ${{ matrix.os == 'windows-latest' || (matrix.os == 'ubuntu-latest' && matrix.compiler != 'clang-18') }}
        shell: bash
        run: |
          VCPKG_PATH=${VCPKG_INSTALLATION_ROOT//\\//}
          echo "CACHE_ROOT=$VCPKG_PATH" >> $GITHUB_ENV
          echo "TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=$VCPKG_PATH/scripts/buildsystems/vcpkg.cmake" >> $GITHUB_ENV

      - name: Cache vcpkg dependencies
        if: ${{ env.CACHE_ROOT != '' }}
        id: cache-vcpkg
        uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_ROOT }}/installed
          key: ${{ matrix.os }}-vcpkg-${{ hashFiles('./.github/workflows/vcpkg_deps.txt') }}

      - name: Set up compiler
        if: ${{ matrix.os == 'ubuntu-latest' && matrix.compiler == 'clang-18' }}
        run: |
          sudo apt update
          sudo apt install -y clang-18
          echo "CC=clang-18" >> $GITHUB_ENV
          echo "CXX=clang++-18" >> $GITHUB_ENV

      - name: setup (ubuntu)
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run:
          sudo apt install ninja-build

      - name: setup (macos)
        if: ${{ matrix.os == 'macos-latest' }}
        run: |
          brew update
          brew install ninja

      - name: setup (vcpkg)
        if: ${{ env.CACHE_ROOT != '' && steps.cache-vcpkg.outputs.cache-hit != 'true' }}
        run: |
          vcpkg install catch2

      - name: Configure
        env:
          BUILD_CATCH: ${{ env.CACHE_ROOT != '' && 'Off' || 'On' }}
        run: >
          cmake
          -G "${{matrix.generator}}"
          -B "${{github.workspace}}/build"
          -DCMAKE_BUILD_TYPE="${{matrix.build_type}}"
          -DCLASP_BUILD_WITH_THREADS="${{matrix.build_threads}}"
          -DCLASP_BUILD_TESTS="On"
          -DLIB_POTASSCO_BUILD_TESTS="On"
          -DLIB_POTASSCO_SETUP_CATCH2="${{env.BUILD_CATCH}}"
          "${{matrix.cmake_extra}}"
          "${{env.TOOLCHAIN || '-Wno-dev'}}"

      - name: Build
        run: >
          cmake
          --build "${{github.workspace}}/build"
          --config "${{matrix.build_type}}"

      - name: Test
        working-directory: ${{github.workspace}}/build
        run: >
          ctest  --rerun-failed --output-on-failure
          -C "${{matrix.build_type}}"
