Skip to content

Commit 9607adf

Browse files
authored
feat: automatically enable coverage for all targets excluding test targets (#897)
* feat: automatically enable coverage for all targets excluding test targets * fix: correct typo in target_compile_options for mutation testing
1 parent 07b25da commit 9607adf

File tree

5 files changed

+74
-26
lines changed

5 files changed

+74
-26
lines changed

.github/workflows/static-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
GTEST_OUTPUT: "xml:${{ github.workspace }}/testresults/"
4040
- name: Collect coverage
4141
run: |
42-
gcovr --sonarqube=coverage.xml --exclude-lines-by-pattern '.*assert\(.*\);|.*really_assert\(.*\);|.*std::abort();' --exclude-unreachable-branches --exclude-throw-branches -j "$(nproc)" --exclude=.*/generated/.* --exclude=.*/examples/.* --exclude=.*/external/.* --exclude=.*/lwip/.* --exclude=.*/tracing/.* --exclude=.*/test/.*
42+
gcovr --sonarqube=coverage.xml --exclude-lines-by-pattern '.*assert\(.*\);|.*really_assert\(.*\);|.*std::abort();' --exclude-unreachable-branches --exclude-throw-branches -j "$(nproc)" --exclude=.*/generated/.* --exclude=.*/examples/.* --exclude=.*/external/.* --exclude=.*/lwip/.* --exclude=.*/tracing/.*
4343
- name: Perform mutation testing
4444
uses: lukka/run-cmake@af1be47fd7c933593f687731bc6fdbee024d3ff4 # v10.8
4545
with:

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED On)
1515
set_directory_properties(PROPERTY USE_FOLDERS On)
1616

1717
include(CMakePackageConfigHelpers)
18+
include(emil_coverage)
1819
include(emil_antora)
1920
include(emil_build_for)
2021
include(emil_folderize)
@@ -160,6 +161,7 @@ emil_clangformat_directories(emil DIRECTORIES .)
160161

161162
if (EMIL_STANDALONE)
162163
emil_generate_antora_docs_target(antora-playbook-branch.yml)
164+
emil_coverage_targets(DIRECTORIES .)
163165
emil_folderize_all_targets()
164166
endif()
165167

cmake/emil_coverage.cmake

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
define_property(TARGET PROPERTY EXCLUDE_FROM_COVERAGE)
2+
3+
function(emil_exclude_from_coverage target)
4+
set_target_properties(${target} PROPERTIES EXCLUDE_FROM_COVERAGE TRUE)
5+
endfunction()
6+
7+
function(emil_exclude_directory_from_coverage directory)
8+
emil_get_subdirectories(directories DIRECTORIES ${directory})
9+
list(APPEND directories ${directory})
10+
emil_get_targets_from_directories(targets DIRECTORIES ${directories})
11+
12+
foreach(target ${targets})
13+
emil_exclude_from_coverage(${target})
14+
endforeach()
15+
endfunction()
16+
17+
function(emil_enable_coverage_for target)
18+
if (EMIL_ENABLE_COVERAGE)
19+
message(DEBUG "enable coverage for: ${target}")
20+
target_compile_options(${target} PRIVATE
21+
-g -O0 --coverage -fprofile-arcs -ftest-coverage -fno-inline
22+
$<$<COMPILE_LANGUAGE:CXX>:-fno-elide-constructors>
23+
)
24+
25+
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
26+
target_link_libraries(${target} PRIVATE gcov)
27+
else()
28+
target_link_options(${target} PRIVATE --coverage)
29+
endif()
30+
endif()
31+
endfunction()
32+
33+
function(emil_enable_mutation_for target)
34+
if (EMIL_ENABLE_MUTATION_TESTING)
35+
message(DEBUG "enable mutation for: ${target}")
36+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
37+
target_compile_options(${target} PRIVATE
38+
-g -O0 -grecord-command-line -fprofile-instr-generate -fcoverage-mapping -fpass-plugin=/usr/lib/mull-ir-frontend
39+
)
40+
41+
target_link_options(${target} PRIVATE -fprofile-instr-generate)
42+
else()
43+
message(FATAL_ERROR "Mutation testing is currently only supported for Clang/LLVM; not for ${CMAKE_CXX_COMPILER_ID}")
44+
endif()
45+
endif()
46+
endfunction()
47+
48+
function(emil_coverage_targets directories)
49+
set(singleArgs COVERAGE_FILE)
50+
set(multiValueArgs DIRECTORIES)
51+
cmake_parse_arguments(PARSE_ARGV 0 MY "" "${singleArgs}" "${multiValueArgs}")
52+
53+
emil_get_subdirectories(subdirectories DIRECTORIES ${MY_DIRECTORIES})
54+
emil_get_targets_from_directories(targets DIRECTORIES ${subdirectories})
55+
56+
set(non_generated_target_sources)
57+
foreach(target ${targets})
58+
get_target_property(exclude ${target} EXCLUDE_FROM_COVERAGE)
59+
get_target_property(type ${target} TYPE)
60+
61+
if (NOT exclude AND NOT ${type} STREQUAL "INTERFACE_LIBRARY" AND NOT ${type} STREQUAL "UTILITY")
62+
emil_enable_coverage_for(${target})
63+
emil_enable_mutation_for(${target})
64+
else()
65+
message(DEBUG "skipping coverage and mutation for: ${target}")
66+
endif()
67+
endforeach()
68+
endfunction()

cmake/emil_test_helpers.cmake

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,14 @@ function(emil_fetch_googletest)
1515
FetchContent_MakeAvailable(googletest)
1616

1717
set_target_properties(gtest gtest_main gmock gmock_main PROPERTIES FOLDER External/GoogleTest)
18+
set_target_properties(gtest gtest_main gmock gmock_main PROPERTIES EXCLUDE_FROM_COVERAGE TRUE)
1819
mark_as_advanced(BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS gmock_build_tests gtest_build_samples test_build_tests gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols)
1920
endfunction()
2021

2122
function(emil_enable_testing)
2223
include(GoogleTest)
2324

2425
emil_fetch_googletest()
25-
26-
if (EMIL_ENABLE_COVERAGE)
27-
add_compile_options(
28-
-g -O0 --coverage -fprofile-arcs -ftest-coverage -fno-inline
29-
$<$<COMPILE_LANGUAGE:CXX>:-fno-elide-constructors>
30-
)
31-
32-
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
33-
link_libraries(gcov)
34-
else()
35-
add_link_options(--coverage)
36-
endif()
37-
endif()
38-
39-
if (EMIL_ENABLE_MUTATION_TESTING)
40-
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
41-
add_compile_options(
42-
-g -O0 -grecord-command-line -fprofile-instr-generate -fcoverage-mapping -fpass-plugin=/usr/lib/mull-ir-frontend
43-
)
44-
45-
add_link_options(-fprofile-instr-generate)
46-
else()
47-
message(FATAL_ERROR "Mutation testing is currently only supported for Clang/LLVM; not for ${CMAKE_CXX_COMPILER_ID}")
48-
endif()
49-
endif()
5026
endfunction()
5127

5228
function(emil_enable_fuzzing)
@@ -66,6 +42,7 @@ function(emil_add_test target)
6642
else()
6743
add_test(NAME ${target} COMMAND ${target})
6844
endif()
45+
emil_exclude_from_coverage(${target})
6946
endif()
7047
endfunction()
7148

external/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ if (EMIL_INCLUDE_SEGGER_RTT)
88
endif()
99

1010
emil_exclude_directory_from_clang_format(.)
11+
emil_exclude_directory_from_coverage(.)

0 commit comments

Comments
 (0)