Debian Codegraph @

cmake/Modules/CodeCoverage.cmake

raw
1 # - Enable Code Coverage
2 #
3 # 2012-01-31, Lars Bilke
4 #
5 # USAGE:
6 # 1. Copy this file into your cmake modules path
7 # 2. Add the following line to your CMakeLists.txt:
8 # INCLUDE(CodeCoverage)
9 #
10 # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
11 # which runs your test executable and produces a lcov code coverage report.
12 #
13
14 # Check prereqs
15 FIND_PROGRAM( GCOV_PATH gcov )
16 FIND_PROGRAM( LCOV_PATH lcov )
17 FIND_PROGRAM( GENHTML_PATH genhtml )
18 FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
19
20 IF(NOT GCOV_PATH)
21 MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
22 ENDIF() # NOT GCOV_PATH
23
24 IF(NOT CMAKE_C_COMPILER_ID STREQUAL GNU)
25 MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
26 ENDIF()
27
28 IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" )
29 MESSAGE( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
30 ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
31
32
33 # Setup compiler options
34 ADD_DEFINITIONS(-fprofile-arcs -ftest-coverage)
35 LINK_LIBRARIES(gcov)
36
37
38 # Param _targetname The name of new the custom make target
39 # Param _testrunner The name of the target which runs the tests
40 # Param _outputname lcov output is generated as _outputname.info
41 # HTML report is generated in _outputname/index.html
42 # Optional fourth parameter is passed as arguments to _testrunner
43 # Pass them in list form, e.g.: "-j;2" for -j 2
44 FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
45
46 IF(NOT LCOV_PATH)
47 MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
48 ENDIF() # NOT LCOV_PATH
49
50 IF(NOT GENHTML_PATH)
51 MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
52 ENDIF() # NOT GENHTML_PATH
53
54 # Setup target
55 ADD_CUSTOM_TARGET(${_targetname}
56
57 # Cleanup lcov
58 ${LCOV_PATH} --directory . --zerocounters
59
60 # Run tests
61 COMMAND ${_testrunner} ${ARGV3}
62
63 # Capturing lcov counters and generating report
64 COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info
65 COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned
66 COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned
67 COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
68
69 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
70 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
71 )
72
73 # Show info where to find the report
74 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
75 COMMAND ;
76 COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
77 )
78
79 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
80
81 # Param _targetname The name of new the custom make target
82 # Param _testrunner The name of the target which runs the tests
83 # Param _outputname cobertura output is generated as _outputname.xml
84 # Optional fourth parameter is passed as arguments to _testrunner
85 # Pass them in list form, e.g.: "-j;2" for -j 2
86 FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
87
88 IF(NOT PYTHON_EXECUTABLE)
89 MESSAGE(FATAL_ERROR "Python not found! Aborting...")
90 ENDIF() # NOT PYTHON_EXECUTABLE
91
92 IF(NOT GCOVR_PATH)
93 MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
94 ENDIF() # NOT GCOVR_PATH
95
96 ADD_CUSTOM_TARGET(${_targetname}
97
98 # Run tests
99 ${_testrunner} ${ARGV3}
100
101 # Running gcovr
102 COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
103 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
104 COMMENT "Running gcovr to produce Cobertura code coverage report."
105 )
106
107 # Show info where to find the report
108 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
109 COMMAND ;
110 COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
111 )
112
113 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA