| 1 |
# Copyright (c) 2012 - 2015, Lars Bilke |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without modification, |
| 5 |
# are permitted provided that the following conditions are met: |
| 6 |
# |
| 7 |
# 1. Redistributions of source code must retain the above copyright notice, this |
| 8 |
# list of conditions and the following disclaimer. |
| 9 |
# |
| 10 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
# this list of conditions and the following disclaimer in the documentation |
| 12 |
# and/or other materials provided with the distribution. |
| 13 |
# |
| 14 |
# 3. Neither the name of the copyright holder nor the names of its contributors |
| 15 |
# may be used to endorse or promote products derived from this software without |
| 16 |
# specific prior written permission. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 22 |
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 25 |
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
# |
| 29 |
# |
| 30 |
# |
| 31 |
# 2012-01-31, Lars Bilke |
| 32 |
# - Enable Code Coverage |
| 33 |
# |
| 34 |
# 2013-09-17, Joakim Söderberg |
| 35 |
# - Added support for Clang. |
| 36 |
# - Some additional usage instructions. |
| 37 |
# |
| 38 |
# USAGE: |
| 39 |
|
| 40 |
# 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here: |
| 41 |
# http://stackoverflow.com/a/22404544/80480 |
| 42 |
# |
| 43 |
# 1. Copy this file into your cmake modules path. |
| 44 |
# |
| 45 |
# 2. Add the following line to your CMakeLists.txt: |
| 46 |
# INCLUDE(CodeCoverage) |
| 47 |
# |
| 48 |
# 3. Set compiler flags to turn off optimization and enable coverage: |
| 49 |
# SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") |
| 50 |
# SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") |
| 51 |
# |
| 52 |
# 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target |
| 53 |
# which runs your test executable and produces a lcov code coverage report: |
| 54 |
# Example: |
| 55 |
# SETUP_TARGET_FOR_COVERAGE( |
| 56 |
# my_coverage_target # Name for custom target. |
| 57 |
# test_driver # Name of the test driver executable that runs the tests. |
| 58 |
# # NOTE! This should always have a ZERO as exit code |
| 59 |
# # otherwise the coverage generation will not complete. |
| 60 |
# coverage # Name of output directory. |
| 61 |
# ) |
| 62 |
# |
| 63 |
# 4. Build a Debug build: |
| 64 |
# cmake -DCMAKE_BUILD_TYPE=Debug .. |
| 65 |
# make |
| 66 |
# make my_coverage_target |
| 67 |
# |
| 68 |
# |
| 69 |
|
| 70 |
# Check prereqs |
| 71 |
FIND_PROGRAM( GCOV_PATH gcov ) |
| 72 |
FIND_PROGRAM( LCOV_PATH lcov ) |
| 73 |
FIND_PROGRAM( GENHTML_PATH genhtml ) |
| 74 |
FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests) |
| 75 |
|
| 76 |
IF(NOT GCOV_PATH) |
| 77 |
MESSAGE(FATAL_ERROR "gcov not found! Aborting...") |
| 78 |
ENDIF() # NOT GCOV_PATH |
| 79 |
|
| 80 |
IF("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang") |
| 81 |
IF("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3) |
| 82 |
MESSAGE(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...") |
| 83 |
ENDIF() |
| 84 |
ELSEIF(NOT CMAKE_COMPILER_IS_GNUCXX) |
| 85 |
MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") |
| 86 |
ENDIF() # CHECK VALID COMPILER |
| 87 |
|
| 88 |
SET(CMAKE_CXX_FLAGS_COVERAGE |
| 89 |
"-g -O0 --coverage -fprofile-arcs -ftest-coverage" |
| 90 |
CACHE STRING "Flags used by the C++ compiler during coverage builds." |
| 91 |
FORCE ) |
| 92 |
SET(CMAKE_C_FLAGS_COVERAGE |
| 93 |
"-g -O0 --coverage -fprofile-arcs -ftest-coverage" |
| 94 |
CACHE STRING "Flags used by the C compiler during coverage builds." |
| 95 |
FORCE ) |
| 96 |
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE |
| 97 |
"" |
| 98 |
CACHE STRING "Flags used for linking binaries during coverage builds." |
| 99 |
FORCE ) |
| 100 |
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE |
| 101 |
"" |
| 102 |
CACHE STRING "Flags used by the shared libraries linker during coverage builds." |
| 103 |
FORCE ) |
| 104 |
MARK_AS_ADVANCED( |
| 105 |
CMAKE_CXX_FLAGS_COVERAGE |
| 106 |
CMAKE_C_FLAGS_COVERAGE |
| 107 |
CMAKE_EXE_LINKER_FLAGS_COVERAGE |
| 108 |
CMAKE_SHARED_LINKER_FLAGS_COVERAGE ) |
| 109 |
|
| 110 |
IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage")) |
| 111 |
MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" ) |
| 112 |
ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug" |
| 113 |
|
| 114 |
|
| 115 |
# Param _targetname The name of new the custom make target |
| 116 |
# Param _testrunner The name of the target which runs the tests. |
| 117 |
# MUST return ZERO always, even on errors. |
| 118 |
# If not, no coverage report will be created! |
| 119 |
# Param _outputname lcov output is generated as _outputname.info |
| 120 |
# HTML report is generated in _outputname/index.html |
| 121 |
# Optional fourth parameter is passed as arguments to _testrunner |
| 122 |
# Pass them in list form, e.g.: "-j;2" for -j 2 |
| 123 |
FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname) |
| 124 |
|
| 125 |
IF(NOT LCOV_PATH) |
| 126 |
MESSAGE(FATAL_ERROR "lcov not found! Aborting...") |
| 127 |
ENDIF() # NOT LCOV_PATH |
| 128 |
|
| 129 |
IF(NOT GENHTML_PATH) |
| 130 |
MESSAGE(FATAL_ERROR "genhtml not found! Aborting...") |
| 131 |
ENDIF() # NOT GENHTML_PATH |
| 132 |
|
| 133 |
SET(coverage_info "${CMAKE_BINARY_DIR}/${_outputname}.info") |
| 134 |
SET(coverage_cleaned "${coverage_info}.cleaned") |
| 135 |
|
| 136 |
SEPARATE_ARGUMENTS(test_command UNIX_COMMAND "${_testrunner}") |
| 137 |
|
| 138 |
# Setup target |
| 139 |
ADD_CUSTOM_TARGET(${_targetname} |
| 140 |
|
| 141 |
# Cleanup lcov |
| 142 |
${LCOV_PATH} --directory . --zerocounters |
| 143 |
|
| 144 |
# Run tests |
| 145 |
COMMAND ${test_command} ${ARGV3} |
| 146 |
|
| 147 |
# Capturing lcov counters and generating report |
| 148 |
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${coverage_info} |
| 149 |
COMMAND ${LCOV_PATH} --remove ${coverage_info} 'tests/*' '/usr/*' --output-file ${coverage_cleaned} |
| 150 |
COMMAND ${GENHTML_PATH} -o ${_outputname} ${coverage_cleaned} |
| 151 |
COMMAND ${CMAKE_COMMAND} -E remove ${coverage_info} ${coverage_cleaned} |
| 152 |
|
| 153 |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
| 154 |
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." |
| 155 |
) |
| 156 |
|
| 157 |
# Show info where to find the report |
| 158 |
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD |
| 159 |
COMMAND ; |
| 160 |
COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report." |
| 161 |
) |
| 162 |
|
| 163 |
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE |
| 164 |
|
| 165 |
# Param _targetname The name of new the custom make target |
| 166 |
# Param _testrunner The name of the target which runs the tests |
| 167 |
# Param _outputname cobertura output is generated as _outputname.xml |
| 168 |
# Optional fourth parameter is passed as arguments to _testrunner |
| 169 |
# Pass them in list form, e.g.: "-j;2" for -j 2 |
| 170 |
FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname) |
| 171 |
|
| 172 |
IF(NOT PYTHON_EXECUTABLE) |
| 173 |
MESSAGE(FATAL_ERROR "Python not found! Aborting...") |
| 174 |
ENDIF() # NOT PYTHON_EXECUTABLE |
| 175 |
|
| 176 |
IF(NOT GCOVR_PATH) |
| 177 |
MESSAGE(FATAL_ERROR "gcovr not found! Aborting...") |
| 178 |
ENDIF() # NOT GCOVR_PATH |
| 179 |
|
| 180 |
ADD_CUSTOM_TARGET(${_targetname} |
| 181 |
|
| 182 |
# Run tests |
| 183 |
${_testrunner} ${ARGV3} |
| 184 |
|
| 185 |
# Running gcovr |
| 186 |
COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml |
| 187 |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
| 188 |
COMMENT "Running gcovr to produce Cobertura code coverage report." |
| 189 |
) |
| 190 |
|
| 191 |
# Show info where to find the report |
| 192 |
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD |
| 193 |
COMMAND ; |
| 194 |
COMMENT "Cobertura code coverage report saved in ${_outputname}.xml." |
| 195 |
) |
| 196 |
|
| 197 |
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA |