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