Skip to content

Commit

Permalink
Generate code coverage report with llvm-cov
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Mar 29, 2024
1 parent 33f12f0 commit 0dc69d6
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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-22.04
container: wpilib/roborio-cross-ubuntu:2024-22.04
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 -y clang-15 llvm-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
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y libopencv-dev libopencv4.5-java python-is-python3 libprotobuf-dev protobuf-compiler ninja-build

- name: Install QuickBuffers
run: wget https://github.com/HebiRobotics/QuickBuffers/releases/download/1.3.3/protoc-gen-quickbuf_1.3.3_amd64.deb && sudo apt install ./protoc-gen-quickbuf_1.3.3_amd64.deb

- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3

- name: Install jinja
run: python -m pip install jinja2

- uses: actions/checkout@v4

- name: configure
run: cmake -S . -B build -G "Ninja" -DCMAKE_C_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache ${{ matrix.flags }} -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
env:
SCCACHE_GHA_ENABLED: "true"

- name: build
working-directory: build
run: cmake --build . --parallel $(nproc) --target wpimath_test
env:
SCCACHE_GHA_ENABLED: "true"

- name: Run executable and generate reports
run: |
pushd build
./bin/wpimath-test
llvm-profdata merge -sparse default.profraw -o default.profdata
llvm-cov show -ignore-filename-regex=_deps/ ./bin/wpimath_test -instr-profile=default.profdata -format=html > coverage-line-by-line-wpimath_test.html
llvm-cov report -ignore-filename-regex=_deps/ ./bin/wpimath_test -instr-profile=default.profdata > coverage-report-wpimath_test.txt
popd
- uses: actions/upload-artifact@v4
with:
name: Coverage report
path: ./build-coverage/coverage-line-by-line-wpimath_test.html

- name: Write to job summary
run: |
echo '```bash' >> $GITHUB_STEP_SUMMARY
cat ./build-coverage/coverage-report-wpimath_test.txt >> $GITHUB_STEP_SUMMARY
echo '' >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
32 changes: 32 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ if(isMultiConfig)
if(NOT "Ubsan" IN_LIST CMAKE_CONFIGURATION_TYPES)
list(APPEND CMAKE_CONFIGURATION_TYPES Ubsan)
endif()
if(NOT "Coverage" IN_LIST CMAKE_CONFIGURATION_TYPES)
list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)
endif()
else()
set(allowedBuildTypes
Asan
Tsan
Ubsan
Coverage
Debug
Release
RelWithDebInfo
Expand Down Expand Up @@ -344,6 +348,34 @@ set(CMAKE_SHARED_LINKER_FLAGS_UBSAN
FORCE
)

set(CMAKE_C_FLAGS_COVERAGE
"${CMAKE_C_FLAGS_RELWITHDEBINFO} -fprofile-instr-generate -fcoverage-mapping"
CACHE STRING
"Flags used by the C compiler for Coverage build type or configuration."
FORCE
)

set(CMAKE_CXX_FLAGS_COVERAGE
"${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -fprofile-instr-generate -fcoverage-mapping"
CACHE STRING
"Flags used by the C++ compiler for Coverage build type or configuration."
FORCE
)

set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
"${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}"
CACHE STRING
"Linker flags to be used to create executables for Coverage build type."
FORCE
)

set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
"${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}"
CACHE STRING
"Linker lags to be used to create shared libraries for Coverage build type."
FORCE
)

if(WITH_TESTS)
enable_testing()
add_subdirectory(googletest)
Expand Down

0 comments on commit 0dc69d6

Please sign in to comment.