1# 2# Boost Software License - Version 1.0 - August 17th, 2003 3# 4# Permission is hereby granted, free of charge, to any person or organization 5# obtaining a copy of the software and accompanying documentation covered by 6# this license (the "Software") to use, reproduce, display, distribute, 7# execute, and transmit the Software, and to prepare derivative works of the 8# Software, and to permit third-parties to whom the Software is furnished to 9# do so, all subject to the following: 10# 11# The copyright notices in the Software and this entire statement, including 12# the above license grant, this restriction and the following disclaimer, 13# must be included in all copies of the Software, in whole or in part, and 14# all derivative works of the Software, unless such copies or derivative 15# works are solely in the form of machine-executable object code generated by 16# a source language processor. 17# 18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 21# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 22# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 23# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24# DEALINGS IN THE SOFTWARE. 25# 26# 2012-01-31, Lars Bilke 27# - Enable Code Coverage 28# 29# 2013-09-17, Joakim Söderberg 30# - Added support for Clang. 31# - Some additional usage instructions. 32# 33# USAGE: 34# 1. Copy this file into your cmake modules path. 35# 36# 2. Add the following line to your CMakeLists.txt: 37# INCLUDE(CodeCoverage) 38# 39# 3. Set compiler flags to turn off optimization and enable coverage: 40# SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") 41# SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") 42# 43# 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target 44# which runs your test executable and produces a lcov code coverage report: 45# Example: 46# SETUP_TARGET_FOR_COVERAGE( 47# my_coverage_target # Name for custom target. 48# test_driver # Name of the test driver executable that runs the tests. 49# # NOTE! This should always have a ZERO as exit code 50# # otherwise the coverage generation will not complete. 51# coverage # Name of output directory. 52# ) 53# 54# 4. Build a Debug build: 55# cmake -DCMAKE_BUILD_TYPE=Debug .. 56# make 57# make my_coverage_target 58# 59# 60 61# Check prereqs 62FIND_PROGRAM( GCOV_PATH gcov ) 63FIND_PROGRAM( LCOV_PATH lcov ) 64FIND_PROGRAM( GENHTML_PATH genhtml ) 65FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests) 66 67IF(NOT GCOV_PATH) 68 MESSAGE(FATAL_ERROR "gcov not found! Aborting...") 69ENDIF() # NOT GCOV_PATH 70 71IF(NOT CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_GNUCXX) 72 # Clang version 3.0.0 and greater now supports gcov as well. 73 MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.") 74 75 IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 76 MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") 77 ENDIF() 78ENDIF() # NOT CMAKE_COMPILER_IS_GNUCC 79 80IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" ) 81 MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" ) 82ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug" 83 84 85# Param _targetname The name of new the custom make target 86# Param _testrunner The name of the target which runs the tests. 87# MUST return ZERO always, even on errors. 88# If not, no coverage report will be created! 89# Param _outputname lcov output is generated as _outputname.info 90# HTML report is generated in _outputname/index.html 91# Optional fourth parameter is passed as arguments to _testrunner 92# Pass them in list form, e.g.: "-j;2" for -j 2 93FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname) 94 95 IF(NOT LCOV_PATH) 96 MESSAGE(FATAL_ERROR "lcov not found! Aborting...") 97 ENDIF() # NOT LCOV_PATH 98 99 IF(NOT GENHTML_PATH) 100 MESSAGE(FATAL_ERROR "genhtml not found! Aborting...") 101 ENDIF() # NOT GENHTML_PATH 102 103 # Setup target 104 ADD_CUSTOM_TARGET(${_targetname} 105 106 # Cleanup lcov 107 ${LCOV_PATH} --directory . --zerocounters 108 109 # Run tests 110 COMMAND ${_testrunner} ${ARGV3} 111 112 # Capturing lcov counters and generating report 113 COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info 114 COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned 115 COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned 116 COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned 117 118 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 119 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." 120 ) 121 122 # Show info where to find the report 123 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD 124 COMMAND ; 125 COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report." 126 ) 127 128ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE 129 130# Param _targetname The name of new the custom make target 131# Param _testrunner The name of the target which runs the tests 132# Param _outputname cobertura output is generated as _outputname.xml 133# Optional fourth parameter is passed as arguments to _testrunner 134# Pass them in list form, e.g.: "-j;2" for -j 2 135FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname) 136 137 IF(NOT PYTHON_EXECUTABLE) 138 MESSAGE(FATAL_ERROR "Python not found! Aborting...") 139 ENDIF() # NOT PYTHON_EXECUTABLE 140 141 IF(NOT GCOVR_PATH) 142 MESSAGE(FATAL_ERROR "gcovr not found! Aborting...") 143 ENDIF() # NOT GCOVR_PATH 144 145 ADD_CUSTOM_TARGET(${_targetname} 146 147 # Run tests 148 ${_testrunner} ${ARGV3} 149 150 # Running gcovr 151 COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml 152 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 153 COMMENT "Running gcovr to produce Cobertura code coverage report." 154 ) 155 156 # Show info where to find the report 157 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD 158 COMMAND ; 159 COMMENT "Cobertura code coverage report saved in ${_outputname}.xml." 160 ) 161 162ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA 163