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