1 ## Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 # 2012-01-31, Lars Bilke
5 # - Enable Code Coverage
7 # 2013-09-17, Joakim Söderberg
8 # - Added support for Clang.
9 # - Some additional usage instructions.
13 # 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here:
14 # http://stackoverflow.com/a/22404544/80480
16 # 1. Copy this file into your cmake modules path.
18 # 2. Add the following line to your CMakeLists.txt:
19 # INCLUDE(CodeCoverage)
21 # 3. Set compiler flags to turn off optimization and enable coverage:
22 # SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
23 # SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
25 # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
26 # which runs your test executable and produces a lcov code coverage report:
28 # SETUP_TARGET_FOR_COVERAGE(
29 # my_coverage_target # Name for custom target.
30 # test_driver # Name of the test driver executable that runs the tests.
31 # # NOTE! This should always have a ZERO as exit code
32 # # otherwise the coverage generation will not complete.
33 # coverage # Name of output directory.
36 # 4. Build a Debug build:
37 # cmake -DCMAKE_BUILD_TYPE=Debug ..
39 # make my_coverage_target
44 FIND_PROGRAM( GCOV_PATH gcov )
45 FIND_PROGRAM( LCOV_PATH lcov )
46 FIND_PROGRAM( GENHTML_PATH genhtml )
47 FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
50 MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
51 ENDIF() # NOT GCOV_PATH
53 IF(NOT CMAKE_COMPILER_IS_GNUCXX)
54 # Clang version 3.0.0 and greater now supports gcov as well.
55 MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
57 IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
58 MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
60 ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX
62 SET(CMAKE_CXX_FLAGS_COVERAGE
63 "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
64 CACHE STRING "Flags used by the C++ compiler during coverage builds."
66 SET(CMAKE_C_FLAGS_COVERAGE
67 "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
68 CACHE STRING "Flags used by the C compiler during coverage builds."
70 SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
72 CACHE STRING "Flags used for linking binaries during coverage builds."
74 SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
76 CACHE STRING "Flags used by the shared libraries linker during coverage builds."
79 CMAKE_CXX_FLAGS_COVERAGE
80 CMAKE_C_FLAGS_COVERAGE
81 CMAKE_EXE_LINKER_FLAGS_COVERAGE
82 CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
84 IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage"))
85 MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
86 ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
89 # Param _targetname The name of new the custom make target
90 # Param _testrunner The name of the target which runs the tests.
91 # MUST return ZERO always, even on errors.
92 # If not, no coverage report will be created!
93 # Param _outputname lcov output is generated as _outputname.info
94 # HTML report is generated in _outputname/index.html
95 # Optional fourth parameter is passed as arguments to _testrunner
96 # Pass them in list form, e.g.: "-j;2" for -j 2
97 FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
100 MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
101 ENDIF() # NOT LCOV_PATH
104 MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
105 ENDIF() # NOT GENHTML_PATH
108 ADD_CUSTOM_TARGET(${_targetname}
111 ${LCOV_PATH} --directory . --zerocounters
114 COMMAND ${_testrunner} ${ARGV3}
116 # Capturing lcov counters and generating report
117 COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info
118 COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned
119 COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned
120 COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
122 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
123 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
126 # Show info where to find the report
127 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
129 COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
132 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
134 # Param _targetname The name of new the custom make target
135 # Param _testrunner The name of the target which runs the tests
136 # Param _outputname cobertura output is generated as _outputname.xml
137 # Optional fourth parameter is passed as arguments to _testrunner
138 # Pass them in list form, e.g.: "-j;2" for -j 2
139 FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
141 IF(NOT PYTHON_EXECUTABLE)
142 MESSAGE(FATAL_ERROR "Python not found! Aborting...")
143 ENDIF() # NOT PYTHON_EXECUTABLE
146 MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
147 ENDIF() # NOT GCOVR_PATH
149 ADD_CUSTOM_TARGET(${_targetname}
152 ${_testrunner} ${ARGV3}
155 COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
156 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
157 COMMENT "Running gcovr to produce Cobertura code coverage report."
160 # Show info where to find the report
161 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
163 COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
166 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA