| 1 |
# CoverageFunction.cmake |
| 2 |
# |
| 3 |
# description: find coverage tools and provide function add_coverage_target |
| 4 |
# |
| 5 |
# usage: |
| 6 |
# cd <debug build directory> |
| 7 |
# cmake .. -DCMAKE_BUILD_TYPE=Debug -DBA_COVERGAE=ON |
| 8 |
# make (or ninja) |
| 9 |
# ctest (or 'make check' or run BornAgain manually) WITHOUT -j, i.e. NO PARALLEL PROCESSING !! |
| 10 |
# cmake --build . --config Debug --target coverage |
| 11 |
# |
| 12 |
# -> result is in coverage/index.html |
| 13 |
# |
| 14 |
# authors: Jonathan Fisher, Joachim Wuttke |
| 15 |
# maintainer: Scientific Computing Group, JCNS at MLZ Garching |
| 16 |
# copyright: Forschungszentrum Juelich GmbH, 2016-2025 |
| 17 |
# |
| 18 |
# license: Public Domain |
| 19 |
|
| 20 |
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 21 |
message(FATAL_ERROR "C++ compiler is not GNU but '${CMAKE_CXX_COMPILER_ID}'; don't know how to set code coverage flags!") |
| 22 |
endif() |
| 23 |
string(APPEND CMAKE_CXX_FLAGS " -coverage") |
| 24 |
|
| 25 |
find_program(GCOV_COMMAND gcov REQUIRED) |
| 26 |
find_program(LCOV_COMMAND lcov REQUIRED) |
| 27 |
find_program(GENHTML_COMMAND genhtml REQUIRED) |
| 28 |
|
| 29 |
# function to add a coverage target |
| 30 |
# it will scan the working directory for coverage info, ignoring the directories in ignore_directories |
| 31 |
function(add_coverage_target targetname ignore_directories html_dir) |
| 32 |
|
| 33 |
set(lcov_output ${CMAKE_CURRENT_BINARY_DIR}/${targetname}.info) |
| 34 |
|
| 35 |
add_custom_target(${targetname} |
| 36 |
COMMAND ${LCOV_COMMAND} --ignore-errors inconsistent,mismatch,negative --directory ${CMAKE_CURRENT_BINARY_DIR} --capture --output-file ${lcov_output}) |
| 37 |
|
| 38 |
add_custom_target(${targetname}_reset |
| 39 |
COMMAND ${LCOV_COMMAND} --ignore-errors inconsistent,mismatch,negative --directory ${CMAKE_CURRENT_BINARY_DIR} --zerocounters) |
| 40 |
|
| 41 |
foreach(dirname ${ignore_directories}) |
| 42 |
message(STATUS "ignoring dirname = ${dirname} in code coverage") |
| 43 |
add_custom_command(TARGET ${targetname} POST_BUILD |
| 44 |
COMMAND ${LCOV_COMMAND} --ignore-errors inconsistent,mismatch,negative --remove ${lcov_output} ${dirname} --output-file ${lcov_output}.clean |
| 45 |
COMMAND ${CMAKE_COMMAND} -E rename ${lcov_output}.clean ${lcov_output}) |
| 46 |
endforeach() |
| 47 |
|
| 48 |
add_custom_command(TARGET ${targetname} POST_BUILD |
| 49 |
COMMAND ${GENHTML_COMMAND} ${lcov_output} --output-directory ${html_dir} |
| 50 |
COMMENT "open index.html in your webbrowser to see the code coverage report.") |
| 51 |
|
| 52 |
endfunction() |