name: CI-Tests
on:
  pull_request:
    types: [opened, synchronize]
  workflow_dispatch:
  push:
    branches:
      - 'master'

jobs:
  Test-Setup:
    name: Test-Setup
    runs-on: ubuntu-latest
    strategy:
      matrix:
        n-chunks: [5]
    outputs:
      n-chunks: ${{ matrix.n-chunks }}
      chunks: ${{ steps.set-test-ids.outputs.chunks }}
    steps:
      - name: checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          fetch-tags: true
      - id: set-test-ids
        uses: ./.github/actions/set-test-ids
        with:
          n-chunks: ${{ matrix.n-chunks }}

  Ubuntu-CMake:
    needs: Test-Setup
    timeout-minutes: 150

    name: Ubuntu-CMake-${{ matrix.domain }} (chunk ${{ matrix.chunk }})

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest]
        domain: [32bit, 64bit, 64bit-noomp]
        chunk: ${{ fromJSON(needs.Test-Setup.outputs.chunks) }}

    runs-on: ${{ matrix.os }}

    steps:
    - name: checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        fetch-tags: true

    - name: install-lcov
      if: ${{ matrix.domain == '32bit' }}
      run: sudo apt-get update && sudo apt-get install lcov

    - name: install-deps
      run: sudo sh/setup/install_ubuntu_deps.sh

    - name: cmake-test-32bit
      if: ${{ matrix.domain == '32bit' }}
      uses: ./.github/actions/cmake-test
      with:
        cmake-flags: -DSOUFFLE_CODE_COVERAGE=ON -DSOUFFLE_TEST_DEBUG_REPORT=ON
        n-chunks: ${{ needs.Test-Setup.outputs.n-chunks }}
        chunk: ${{ matrix.chunk }}

    - name: cmake-test-64bit
      if: ${{ matrix.domain == '64bit' }}
      uses: ./.github/actions/cmake-test
      with:
        cmake-flags: -DSOUFFLE_DOMAIN_64BIT=ON
        n-chunks: ${{ needs.Test-Setup.outputs.n-chunks }}
        chunk: ${{ matrix.chunk }}

    - name: cmake-test-64bit-noomp
      if: ${{ matrix.domain == '64bit-noomp' }}
      uses: ./.github/actions/cmake-test
      with:
        cmake-flags: -DSOUFFLE_DOMAIN_64BIT=ON -DSOUFFLE_USE_OPENMP=OFF
        n-chunks: ${{ needs.Test-Setup.outputs.n-chunks }}
        chunk: ${{ matrix.chunk }}

    - name: create-coverage-report
      if: ${{ matrix.domain == '32bit' }}
      run: lcov --capture --directory build --output-file coverage.info --ignore-errors mismatch

    - name: remove-system-files-from-coverage-report
      if: ${{ matrix.domain == '32bit' }}
      run: lcov --remove coverage.info '/usr/*' --output-file coverage.info

    - name: upload-coverage-artifact
      if: ${{ matrix.domain == '32bit' }}
      uses: actions/upload-artifact@v4
      with:
        name: coverage-${{ matrix.domain }}-${{ matrix.chunk }}
        path: coverage.info

  OSX-CMake:
    needs: Test-Setup
    timeout-minutes: 150

    name: OSX-CMake (chunk ${{ matrix.chunk }})

    strategy:
      fail-fast: false
      matrix:
        chunk: ${{ fromJSON(needs.Test-Setup.outputs.chunks) }}

    runs-on: macos-13

    steps:
    - name: checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        fetch-tags: true

    - name: install-deps
      run: sh/setup/install_macos_deps.sh

    - name: cmake-test-32bit
      uses: ./.github/actions/cmake-test
      with:
        cmake-flags: ''
        n-chunks: ${{ needs.Test-Setup.outputs.n-chunks }}
        chunk: ${{ matrix.chunk }}

  AppleM-CMake:
    needs: Test-Setup
    timeout-minutes: 150

    name: AppleM-CMake (chunk ${{ matrix.chunk }})

    strategy:
      fail-fast: false
      matrix:
        chunk: ${{ fromJSON(needs.Test-Setup.outputs.chunks) }}

    runs-on: macos-15

    steps:
    - name: checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        fetch-tags: true

    - name: install-deps
      run: sh/setup/install_macos_arm_deps.sh

    - name: cmake-test-64bit
      uses: ./.github/actions/cmake-test
      with:
        # disable openmp on ARM architecture, see souffle-lang/souffle#2476
        cmake-flags: -DSOUFFLE_DOMAIN_64BIT=ON -DSOUFFLE_USE_OPENMP=OFF
        n-chunks: ${{ needs.Test-Setup.outputs.n-chunks }}
        chunk: ${{ matrix.chunk }}

  Memory-Check:
    needs: Test-Setup
    timeout-minutes: 150

    name: Memory-Check (chunk ${{ matrix.chunk }})

    strategy:
      fail-fast: false
      matrix:
        chunk: ${{ fromJSON(needs.Test-Setup.outputs.chunks) }}

    runs-on: ubuntu-latest

    steps:
    - name: checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        fetch-tags: true

    - name: install-deps
      run: sudo sh/setup/install_ubuntu_deps.sh

    - name: fix mmap_rnd_bits
      # issue with ubuntu:latest runner and ASAN
      # https://github.com/actions/runner-images/issues/9491
      run: sudo sysctl vm.mmap_rnd_bits=28

    - name: cmake-test-32bit
      uses: ./.github/actions/cmake-test
      with:
        cmake-flags: -DSOUFFLE_SANITISE_MEMORY=ON -DSOUFFLE_TEST_EVALUATION=OFF
        n-chunks: ${{ needs.Test-Setup.outputs.n-chunks }}
        chunk: ${{ matrix.chunk }}

  Code-Coverage:
    needs: Ubuntu-CMake
    timeout-minutes: 150

    runs-on: ubuntu-latest

    steps:
    - name: checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        fetch-tags: true

    - name: install-lcov
      run: sudo apt-get update && sudo apt-get install lcov

    - name: download-coverage-artifacts
      uses: actions/download-artifact@v4

    - name: merge-coverage-report
      run: lcov $(for i in coverage-*-*/coverage.info; do echo -a $i; done) --output-file coverage.info

    - name: upload-coverage-report
      uses: codecov/codecov-action@v4
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        files: coverage.info
