2 # 2012-01-31, Lars Bilke
3 # - Enable Code Coverage
5 # 2013-09-17, Joakim Söderberg
6 # - Added support for Clang.
7 # - Some additional usage instructions.
11 # 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here:
12 # http://stackoverflow.com/a/22404544/80480
14 # 1. Copy this file into your cmake modules path.
16 # 2. Add the following line to your CMakeLists.txt:
17 # INCLUDE(CodeCoverage)
19 # 3. Set compiler flags to turn off optimization and enable coverage:
20 # SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
21 # SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
23 # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
24 # which runs your test executable and produces a lcov code coverage report:
26 # SETUP_TARGET_FOR_COVERAGE(
27 # my_coverage_target # Name for custom target.
28 # test_driver # Name of the test driver executable that runs the tests.
29 # # NOTE! This should always have a ZERO as exit code
30 # # otherwise the coverage generation will not complete.
31 # coverage # Name of output directory.
34 # 4. Build a Debug build:
35 # cmake -DCMAKE_BUILD_TYPE=Debug ..
37 # make my_coverage_target
42 FIND_PROGRAM( GCOV_PATH gcov )
43 FIND_PROGRAM( LCOV_PATH lcov )
44 FIND_PROGRAM( GENHTML_PATH genhtml )
45 FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
48 MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
49 ENDIF() # NOT GCOV_PATH
51 IF(NOT CMAKE_COMPILER_IS_GNUCXX)
52 # Clang version 3.0.0 and greater now supports gcov as well.
53 MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
55 IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
56 MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
58 ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX
60 SET(CMAKE_CXX_FLAGS_COVERAGE
61 "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
62 CACHE STRING "Flags used by the C++ compiler during coverage builds."
64 SET(CMAKE_C_FLAGS_COVERAGE
65 "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
66 CACHE STRING "Flags used by the C compiler during coverage builds."
68 SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
70 CACHE STRING "Flags used for linking binaries during coverage builds."
72 SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
74 CACHE STRING "Flags used by the shared libraries linker during coverage builds."
77 CMAKE_CXX_FLAGS_COVERAGE
78 CMAKE_C_FLAGS_COVERAGE
79 CMAKE_EXE_LINKER_FLAGS_COVERAGE
80 CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
82 IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage"))
83 MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
84 ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
87 # Param _targetname The name of new the custom make target
88 # Param _testrunner The name of the target which runs the tests.
89 # MUST return ZERO always, even on errors.
90 # If not, no coverage report will be created!
91 # Param _outputname lcov output is generated as _outputname.info
92 # HTML report is generated in _outputname/index.html
93 # Optional fourth parameter is passed as arguments to _testrunner
94 # Pass them in list form, e.g.: "-j;2" for -j 2
95 FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
98 MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
99 ENDIF() # NOT LCOV_PATH
102 MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
103 ENDIF() # NOT GENHTML_PATH
106 ADD_CUSTOM_TARGET(${_targetname}
109 ${LCOV_PATH} --directory . --zerocounters
112 COMMAND ${_testrunner} ${ARGV3}
114 # Capturing lcov counters and generating report
115 COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info
116 COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned
117 COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned
118 COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
120 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
121 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
124 # Show info where to find the report
125 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
127 COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
130 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
132 # Param _targetname The name of new the custom make target
133 # Param _testrunner The name of the target which runs the tests
134 # Param _outputname cobertura output is generated as _outputname.xml
135 # Optional fourth parameter is passed as arguments to _testrunner
136 # Pass them in list form, e.g.: "-j;2" for -j 2
137 FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
139 IF(NOT PYTHON_EXECUTABLE)
140 MESSAGE(FATAL_ERROR "Python not found! Aborting...")
141 ENDIF() # NOT PYTHON_EXECUTABLE
144 MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
145 ENDIF() # NOT GCOVR_PATH
147 ADD_CUSTOM_TARGET(${_targetname}
150 ${_testrunner} ${ARGV3}
153 COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
154 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
155 COMMENT "Running gcovr to produce Cobertura code coverage report."
158 # Show info where to find the report
159 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
161 COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
164 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA