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