Skip to content

Commit e4971b6

Browse files
committed
Add CMake target to get coverage information
1 parent e468828 commit e4971b6

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

CMakeLists.txt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,34 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
77

88
# Set compiler flags based on build type
99
if(NOT DEFINED CMAKE_BUILD_TYPE)
10-
set(CMAKE_BUILD_TYPE Debug)
10+
set(CMAKE_BUILD_TYPE Release)
1111
endif()
1212

1313
if(NOT CMAKE_BUILD_TYPE)
1414
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
1515
endif()
1616

17-
set(CMAKE_CXX_FLAGS_DEBUG "-g -Og -Wall -Wall -Wextra")
18-
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g -Og -Wall -Wextra")
19-
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -Wextra")
20-
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-O3 -Wall -Wextra")
17+
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wpedantic")
18+
19+
set(CMAKE_CXX_FLAGS_DEBUG "-g -Og")
20+
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g -Og")
21+
22+
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -flto")
23+
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-O3 -flto")
2124

2225
# Build MEX interface.
2326
option(BUILD_MEX "Build MEX interface" True)
2427
if (BUILD_MEX)
2528
add_subdirectory("mex")
2629
endif()
2730

28-
# Build tests.
31+
# Build tests and check coverage, if required.
32+
option(CHECK_COVERAGE "Check test coverate" False)
2933
option(BUILD_TESTS "Run Catch2 tests" False)
34+
if (CHECK_COVERAGE)
35+
set(BUILD_TESTS True)
36+
set(CMAKE_BUILD_TYPE Debug)
37+
endif()
3038
if (BUILD_TESTS)
3139
enable_testing()
3240
add_subdirectory("tests")

tests/CMakeLists.txt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
11
find_package(Catch2 3 REQUIRED)
2+
23
add_executable(tests tests.cpp)
34
target_include_directories(tests PRIVATE ${PROJECT_SOURCE_DIR}/include)
45
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
56

67
include(CTest)
78
include(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

Comments
 (0)