Skip to content

Commit

Permalink
Generate code coverage report with llvm-cov (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Mar 28, 2024
1 parent 8999411 commit e5d106d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Coverage

on: [pull_request, push]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
coverage:
name: "Coverage"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# TODO: Remove this once clang 15 is default; clang 14 had a stdlib bug
- name: Install clang 15
run: |
sudo apt-get update -q
sudo apt-get install llvm-15-dev libc++-15-dev
sudo update-alternatives --install \
/usr/bin/clang clang /usr/bin/clang-15 200
sudo update-alternatives --install \
/usr/bin/clang++ clang++ /usr/bin/clang++-15 200
sudo update-alternatives --install \
/usr/bin/llvm-cov llvm-cov /usr/bin/llvm-cov-15 200
sudo update-alternatives --install \
/usr/bin/llvm-profdata llvm-profdata /usr/bin/llvm-profdata-15 200
- run: ./tools/coverage-generate.sh SleipnirTest

- uses: actions/upload-artifact@v4
with:
name: Coverage report
path: ./build-coverage/coverage-line-by-line-SleipnirTest.html

- name: Write to job summary
run: |
echo '```bash' >> $GITHUB_STEP_SUMMARY
cat ./build-coverage/coverage-report-SleipnirTest.txt >> $GITHUB_STEP_SUMMARY
echo '' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
9 changes: 9 additions & 0 deletions cmake/modules/SleipnirBuildTypes.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,14 @@ if(NOT MSVC)
"${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}"
)

sleipnir_add_build_type(
allowedBuildTypes
"Coverage"
"${CMAKE_C_FLAGS_RELWITHDEBINFO} -fprofile-instr-generate -fcoverage-mapping"
"${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -fprofile-instr-generate -fcoverage-mapping"
"${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}"
"${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}"
)

sleipnir_check_build_type(allowedBuildTypes)
endif()
19 changes: 19 additions & 0 deletions tools/coverage-generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e

if [ $# -eq 0 ]; then
echo -e "Usage: coverage-generate.sh CMAKE_TARGET\nGenerates coverage of CMake target executable with llvm-cov."
exit 1
fi

# Build executable
cmake -B build-coverage -S . -DCMAKE_BUILD_TYPE=Coverage -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
cmake --build build-coverage --target $1

# Run executable and generate reports
pushd build-coverage
./$1
llvm-profdata merge -sparse default.profraw -o default.profdata
llvm-cov show -ignore-filename-regex=_deps/ ./$1 -instr-profile=default.profdata -format=html > coverage-line-by-line-$1.html
llvm-cov report -ignore-filename-regex=_deps/ ./$1 -instr-profile=default.profdata > coverage-report-$1.txt
popd

0 comments on commit e5d106d

Please sign in to comment.