Build: add GCC-13, Clang-14, Clang-15, Clang-16, Clang-17
[marnav.git] / cmake / CodeCoverage.cmake
blobf6751915159bb83527f5bb61e50851b164a4cd47
1 # Copyright (c) 2012 - 2015, Lars Bilke
2 # All rights reserved.
4 # Redistribution and use in source and binary forms, with or without modification,
5 # are permitted provided that the following conditions are met:
7 # 1. Redistributions of source code must retain the above copyright notice, this
8 #    list of conditions and the following disclaimer.
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.
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.
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.
31 # 2012-01-31, Lars Bilke
32 # - Enable Code Coverage
34 # 2013-09-17, Joakim Söderberg
35 # - Added support for Clang.
36 # - Some additional usage instructions.
38 # 2015-03-19, Mario Konrad
39 # - Changed excluded paths to match the projects.
41 # USAGE:
43 # 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here:
44 #      http://stackoverflow.com/a/22404544/80480
46 # 1. Copy this file into your cmake modules path.
48 # 2. Add the following line to your CMakeLists.txt:
49 #      INCLUDE(CodeCoverage)
51 # 3. Set compiler flags to turn off optimization and enable coverage:
52 #    SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
53 #        SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
55 # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
56 #    which runs your test executable and produces a lcov code coverage report:
57 #    Example:
58 #        SETUP_TARGET_FOR_COVERAGE(
59 #                               my_coverage_target  # Name for custom target.
60 #                               test_driver         # Name of the test driver executable that runs the tests.
61 #                                                                       # NOTE! This should always have a ZERO as exit code
62 #                                                                       # otherwise the coverage generation will not complete.
63 #                               coverage            # Name of output directory.
64 #                               )
66 # 4. Build a Debug build:
67 #        cmake -DCMAKE_BUILD_TYPE=Debug ..
68 #        make
69 #        make my_coverage_target
73 # Check prereqs
74 if(NOT GCOV_PATH)
75         FIND_PROGRAM(GCOV_PATH gcov)
76 endif()
77 if(NOT LCOV_PATH)
78         FIND_PROGRAM(LCOV_PATH lcov)
79 endif()
81 get_filename_component(__lcov_dir ${LCOV_PATH} DIRECTORY)
82 FIND_PROGRAM(GENHTML_PATH genhtml PATHS ${__lcov_dir})
84 FIND_PROGRAM(GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
86 message(STATUS "Found gcov: ${GCOV_PATH}")
87 message(STATUS "Found lcov: ${LCOV_PATH}")
88 message(STATUS "Found genhtml: ${GENHTML_PATH}")
90 IF(NOT GCOV_PATH)
91         MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
92 ENDIF() # NOT GCOV_PATH
94 IF(NOT CMAKE_COMPILER_IS_GNUCXX)
95         # Clang version 3.0.0 and greater now supports gcov as well.
96         MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
98         IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
99                 MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
100         ENDIF()
101 ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX
103 SET(CMAKE_CXX_FLAGS_COVERAGE
104     "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
105     CACHE STRING "Flags used by the C++ compiler during coverage builds."
106     FORCE )
107 SET(CMAKE_C_FLAGS_COVERAGE
108     "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
109     CACHE STRING "Flags used by the C compiler during coverage builds."
110     FORCE )
111 SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
112     ""
113     CACHE STRING "Flags used for linking binaries during coverage builds."
114     FORCE )
115 SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
116     ""
117     CACHE STRING "Flags used by the shared libraries linker during coverage builds."
118     FORCE )
119 MARK_AS_ADVANCED(
120     CMAKE_CXX_FLAGS_COVERAGE
121     CMAKE_C_FLAGS_COVERAGE
122     CMAKE_EXE_LINKER_FLAGS_COVERAGE
123     CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
125 IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage"))
126   MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
127 ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
130 # Param _targetname     The name of new the custom make target
131 # Param _testrunner     The name of the target which runs the tests.
132 #                                               MUST return ZERO always, even on errors.
133 #                                               If not, no coverage report will be created!
134 # Param _outputname     lcov output is generated as _outputname.info
135 #                       HTML report is generated in _outputname/index.html
136 # Optional fourth parameter is passed as arguments to _testrunner
137 #   Pass them in list form, e.g.: "-j;2" for -j 2
138 FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
140         IF(NOT LCOV_PATH)
141                 MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
142         ENDIF() # NOT LCOV_PATH
144         IF(NOT GENHTML_PATH)
145                 MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
146         ENDIF() # NOT GENHTML_PATH
148         # Setup target
149         ADD_CUSTOM_TARGET(${_targetname}
150                 # Cleanup lcov
151                 ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --rc lcov_branch_coverage=1 --directory . --zerocounters
153                 # Run tests
154                 COMMAND ${_testrunner} ${ARGV3}
156                 # Capturing lcov counters and generating report
157                 COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --rc lcov_branch_coverage=1 --directory . --capture --output-file ${_outputname}.info
158                 COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --rc lcov_branch_coverage=1 --remove ${_outputname}.info '/usr/*' 'local/*' 'test/*' 'build/*' --output-file ${_outputname}.info.cleaned
159                 COMMAND ${GENHTML_PATH} --branch-coverage --demangle-cpp -o ${_outputname} ${_outputname}.info.cleaned
160                 COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
162                 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
163                 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
164         )
166         # Show info where to find the report
167         ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
168                 COMMAND ;
169                 COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
170         )
172 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
174 # Param _targetname     The name of new the custom make target
175 # Param _testrunner     The name of the target which runs the tests
176 # Param _outputname     cobertura output is generated as _outputname.xml
177 # Optional fourth parameter is passed as arguments to _testrunner
178 #   Pass them in list form, e.g.: "-j;2" for -j 2
179 FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
181         IF(NOT PYTHON_EXECUTABLE)
182                 MESSAGE(FATAL_ERROR "Python not found! Aborting...")
183         ENDIF() # NOT PYTHON_EXECUTABLE
185         IF(NOT GCOVR_PATH)
186                 MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
187         ENDIF() # NOT GCOVR_PATH
189         ADD_CUSTOM_TARGET(${_targetname}
191                 # Run tests
192                 ${_testrunner} ${ARGV3}
194                 # Running gcovr
195                 COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/'  -o ${_outputname}.xml
196                 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
197                 COMMENT "Running gcovr to produce Cobertura code coverage report."
198         )
200         # Show info where to find the report
201         ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
202                 COMMAND ;
203                 COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
204         )
206 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA