11find_package (Catch2 3 REQUIRED)
2+
23add_executable (tests tests.cpp)
34target_include_directories (tests PRIVATE ${PROJECT_SOURCE_DIR} /include )
45target_link_libraries (tests PRIVATE Catch2::Catch2WithMain)
56
67include (CTest)
78include (Catch)
8- catch_discover_tests(tests)
9+ catch_discover_tests(tests)
10+
11+ if (CHECK_COVERAGE)
12+ function (verbose_find_program PROG_VAR PROG_NAME)
13+ find_program (${PROG_VAR} ${PROG_NAME} )
14+ if (NOT ${PROGRAM_VAR} )
15+ message (ERROR "Cannot produce coverage report: ${PROGRAM_NAME} not found" )
16+ endif ()
17+ endfunction ()
18+
19+ verbose_find_program(GCOV gcov)
20+ verbose_find_program(LCOV lcov)
21+ verbose_find_program(GENHTML genhtml)
22+
23+ if (LCOV AND GCOV AND GENHTML)
24+ set (COVERAGE_DIR ${PROJECT_BINARY_DIR} )
25+ set (COVFILE ${COVERAGE_DIR} /coverage.info)
26+ target_compile_options (tests PRIVATE --coverage)
27+ target_link_options (tests PRIVATE --coverage)
28+ add_custom_target (coverage DEPENDS ${COVFILE} )
29+ add_custom_command (
30+ OUTPUT ${COVFILE}
31+ COMMAND ${LCOV} -d . --zerocounters
32+ COMMAND ${CMAKE_CTEST_COMMAND}
33+ COMMAND ${LCOV} -d . --capture -o ${COVFILE}
34+ COMMAND ${LCOV} --remove ${COVFILE} -o ${COVFILE} '/usr/*'
35+ COMMAND ${LCOV} --list ${COVFILE}
36+ COMMAND ${GENHTML} ${COVFILE} -o ${COVERAGE_DIR} /coverage --quiet
37+ COMMENT "Running tests and generating coverage report"
38+ )
39+ set_directory_properties (PROPERTIES
40+ ADDITIONAL_CLEAN_FILES ${COVFILE}
41+ )
42+ endif ()
43+ endif ()
0 commit comments