Skip to content

Commit 27de808

Browse files
Fix #81: Correct broken code coverage setup (#82)
Co-authored-by: julianlitz <[email protected]>
1 parent 91e3a9b commit 27de808

File tree

7 files changed

+182
-810
lines changed

7 files changed

+182
-810
lines changed

.github/actions/build/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ runs:
1616
export CC=/usr/bin/gcc-13
1717
export CXX=/usr/bin/g++-13
1818
mkdir build && cd build
19-
cmake -DCMAKE_BUILD_TYPE=Debug -DGMGPOLAR_TEST_COVERAGE=ON ..
19+
cmake -DCMAKE_BUILD_TYPE=Debug -DGMGPOLAR_ENABLE_COVERAGE=ON ..
2020
make -j4
2121
- name: create build dir archive
2222
shell: bash

.github/actions/test/action.yml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ runs:
3030
cd build/tests
3131
sudo chmod a+x gmgpolar_tests
3232
./gmgpolar_tests --gtest_output="xml:testreport.xml"
33-
# - name: Compute code coverage
34-
# shell: bash
35-
# # compute code coverage
36-
# run: |
37-
# cd build
38-
# cmake --build . --target coverage/fast
39-
# - name: Upload test report
40-
# uses: actions/upload-artifact@v4
41-
# with:
42-
# name: test-report
43-
# path: build/tests/testreport.xml
44-
# if-no-files-found: error
45-
# retention-days: 3
46-
# - name: Upload coverage reports
47-
# uses: actions/upload-artifact@v4
48-
# with:
49-
# name: test-coverage-reports
50-
# path: |
51-
# build/coverage.info
52-
# build/coverage
53-
# if-no-files-found: error
54-
# retention-days: 1
33+
- name: Compute code coverage
34+
shell: bash
35+
# compute code coverage
36+
run: |
37+
cd build
38+
cmake --build . --target coverage
39+
- name: Upload test report
40+
uses: actions/upload-artifact@v4
41+
with:
42+
name: test-report
43+
path: build/tests/testreport.xml
44+
if-no-files-found: error
45+
retention-days: 3
46+
- name: Upload coverage reports
47+
uses: actions/upload-artifact@v4
48+
with:
49+
name: test-coverage-reports
50+
path: |
51+
build/coverage-filtered.info
52+
build/coverage
53+
if-no-files-found: error
54+
retention-days: 1

.github/workflows/main.yml

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,19 @@ jobs:
2626
with:
2727
build-artifact: build-cpp-linux-gmgpolar
2828

29-
# codecov:
30-
# if: github.event.pull_request.draft == false
31-
# needs: [run-unit-test]
32-
# runs-on: ubuntu-latest
33-
# steps:
34-
# - uses: actions/checkout@v4
35-
# - name: Install dependencies
36-
# run: |
37-
# sudo apt-get -qq update
38-
# sudo apt-get -qq -y install git curl
39-
# - name: Download cpp coverage report
40-
# uses: actions/download-artifact@v4
41-
# with:
42-
# name: test-coverage-reports
43-
# - name: Deploy to codecov.io
44-
# uses: codecov/codecov-action@v3
45-
# with:
46-
# token: ${{ secrets.CODECOV_TOKEN }}
47-
# files: ./coverage.info
48-
# verbose: true
29+
codecov:
30+
if: github.event.pull_request.draft == false
31+
needs: [run-unit-test]
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v4
35+
- name: Download cpp coverage report
36+
uses: actions/download-artifact@v4
37+
with:
38+
name: test-coverage-reports
39+
- name: Deploy to codecov.io
40+
uses: codecov/codecov-action@v3
41+
with:
42+
token: ${{ secrets.CODECOV_TOKEN }}
43+
files: coverage-filtered.info
44+
verbose: true

CMakeLists.txt

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(GMGPolar VERSION 2.0.0 LANGUAGES CXX)
33

4+
# Options should be defined before they're used
45
option(GMGPOLAR_BUILD_TESTS "Build GMGPolar unit tests." ON)
56
option(GMGPOLAR_USE_LIKWID "Use LIKWID to measure code (regions)." OFF)
67
option(GMGPOLAR_USE_MUMPS "Use MUMPS to solve linear systems." OFF)
8+
option(GMGPOLAR_ENABLE_COVERAGE "Enable code coverage reporting (requires GCC/Clang)" OFF)
79

810
set(CMAKE_CXX_STANDARD 20)
911
set(CMAKE_CXX_STANDARD_REQUIRED True)
1012

1113
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
1214

15+
# Set build type - must come before compiler flags
1316
if(NOT CMAKE_BUILD_TYPE)
1417
set(CMAKE_BUILD_TYPE Release)
1518
endif()
1619

20+
# Set compiler flags
1721
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -pedantic -Wno-unused -Wno-psabi")
1822
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -mtune=generic -Wno-psabi")
1923

20-
# code coverage analysis
21-
# Note: this only works under linux and with make
22-
# Ninja creates different directory names which do not work together with this scrupt
23-
# as STREQUAL is case-sensitive https://github.com/TriBITSPub/TriBITS/issues/131, also allow DEBUG as accepted input
24-
option(GMGPOLAR_TEST_COVERAGE "Enable GCov coverage analysis (adds a 'coverage' target)" OFF)
25-
26-
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG")
27-
if(GMGPOLAR_TEST_COVERAGE)
28-
message(STATUS "Coverage enabled")
29-
include(CodeCoverage)
30-
append_coverage_compiler_flags()
31-
setup_target_for_coverage_lcov(
32-
NAME coverage
33-
EXECUTABLE tests/gmgpolar_tests
34-
LCOV_ARGS --ignore-errors gcov,mismatch
35-
EXCLUDE "${CMAKE_SOURCE_DIR}/tests*" "${CMAKE_SOURCE_DIR}/src/InputFunctions*" "${CMAKE_BINARY_DIR}/*" "/usr*"
36-
)
24+
# Set coverage compiler flags - must come before any targets are defined
25+
if(GMGPOLAR_ENABLE_COVERAGE)
26+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
27+
message(STATUS "Enabling code coverage flags")
28+
# Use generator expressions to apply only to specific configurations
29+
add_compile_options($<$<CONFIG:Debug>:--coverage>)
30+
add_link_options($<$<CONFIG:Debug>:--coverage>)
31+
# Force Debug build when coverage is enabled
32+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
33+
message(STATUS "Forcing Debug build for coverage")
34+
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
35+
endif()
36+
else()
37+
message(WARNING "Code coverage requires GCC or Clang. Current compiler: ${CMAKE_CXX_COMPILER_ID}")
3738
endif()
3839
endif()
3940

@@ -54,4 +55,40 @@ if(GMGPOLAR_BUILD_TESTS)
5455
enable_testing()
5556
add_subdirectory(third-party)
5657
add_subdirectory(tests)
58+
59+
# Add coverage target - moved after test configuration
60+
if(GMGPOLAR_ENABLE_COVERAGE)
61+
find_program(LCOV_PATH lcov)
62+
find_program(GENHTML_PATH genhtml)
63+
64+
if(LCOV_PATH AND GENHTML_PATH)
65+
add_custom_target(coverage
66+
# Reset counters
67+
COMMAND ${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --zerocounters
68+
# Run tests
69+
COMMAND ctest --test-dir ${CMAKE_BINARY_DIR} || true
70+
# Capture coverage data
71+
COMMAND ${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --capture
72+
--output-file ${CMAKE_BINARY_DIR}/coverage.info
73+
--ignore-errors mismatch,unused
74+
--rc geninfo_unexecuted_blocks=1
75+
# Filter out system and unwanted directories
76+
COMMAND ${LCOV_PATH} --remove ${CMAKE_BINARY_DIR}/coverage.info
77+
'/usr/*'
78+
'*/tests/*'
79+
'*/third-party/*'
80+
'*/_deps/googletest-src/*'
81+
'*/include/InputFunctions/*'
82+
'*/src/InputFunctions/*'
83+
--output-file ${CMAKE_BINARY_DIR}/coverage-filtered.info
84+
--ignore-errors unused
85+
# Generate HTML report
86+
COMMAND ${GENHTML_PATH} ${CMAKE_BINARY_DIR}/coverage-filtered.info
87+
--output-directory ${CMAKE_BINARY_DIR}/coverage-report
88+
--ignore-errors source
89+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
90+
COMMENT "Generating code coverage report"
91+
)
92+
endif()
93+
endif()
5794
endif()

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[![GMGPolarCI](https://github.com/SciCompMod/GMGPolar/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/SciCompMod/GMGPolar/actions/workflows/main.yml)
2+
[![codecov](https://codecov.io/gh/SciCompMod/GMGPolar/graph/badge.svg?token=D0IVLUW51J)](https://codecov.io/gh/SciCompMod/GMGPolar)
3+
14
GMGPolar
25
=======
36

0 commit comments

Comments
 (0)