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