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