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