name: code_coverage

on:
  workflow_dispatch:

env:
  BUILD_TYPE: Debug

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    
    - name: Set up environment
      run: |
        echo "CXX=clang++-16" >> "$GITHUB_ENV"
        echo "CC=clang-16" >> "$GITHUB_ENV"
        echo "CXXFLAGS="-fprofile-instr-generate -fcoverage-mapping"" >> "$GITHUB_ENV"
        echo "LDFLAGS="-fprofile-instr-generate"" >> "$GITHUB_ENV"
        echo "LLVM_PROFILE_FILE="code-%p.profraw"" >> "$GITHUB_ENV"

    - name: Have llvm-cov point to llvm-cov-16
      run: |
        mkdir -p /home/runner/.local/bin
        ln -s `which llvm-cov-16` /home/runner/.local/bin/llvm-cov
        ln -s `which llvm-profdata-16` /home/runner/.local/bin/llvm-profdata

    - name: Configure CMake
      run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

    - name: Configure CMake with code coverage enabled
      run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCODE_COVERAGE=ON

    - name: Build code coverage target
      run: cmake --build build --config ${{env.BUILD_TYPE}} --target ccov-all -j 2

    - name: Archive code coverage results
      uses: actions/upload-artifact@v3
      with:
        name: code-coverage-report
        path: ${{github.workspace}}/build/ccov/all-merged
