Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / CMakeCommon / CodeCoverage.cmake
1 #
2 # 2012-01-31, Lars Bilke
3 # - Enable Code Coverage
4 #
5 # 2013-09-17, Joakim Söderberg
6 # - Added support for Clang.
7 # - Some additional usage instructions.
8 #
9 # USAGE:
10
11 # 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here:
12 #      http://stackoverflow.com/a/22404544/80480
13 #
14 # 1. Copy this file into your cmake modules path.
15 #
16 # 2. Add the following line to your CMakeLists.txt:
17 #      INCLUDE(CodeCoverage)
18 #
19 # 3. Set compiler flags to turn off optimization and enable coverage: 
20 #    SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
21 #        SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
22 #  
23 # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
24 #    which runs your test executable and produces a lcov code coverage report:
25 #    Example:
26 #        SETUP_TARGET_FOR_COVERAGE(
27 #                               my_coverage_target  # Name for custom target.
28 #                               test_driver         # Name of the test driver executable that runs the tests.
29 #                                                                       # NOTE! This should always have a ZERO as exit code
30 #                                                                       # otherwise the coverage generation will not complete.
31 #                               coverage            # Name of output directory.
32 #                               )
33 #
34 # 4. Build a Debug build:
35 #        cmake -DCMAKE_BUILD_TYPE=Debug ..
36 #        make
37 #        make my_coverage_target
38 #
39 #
40
41 # Check prereqs
42 FIND_PROGRAM( GCOV_PATH gcov )
43 FIND_PROGRAM( LCOV_PATH lcov )
44 FIND_PROGRAM( GENHTML_PATH genhtml )
45 FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
46
47 IF(NOT GCOV_PATH)
48         MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
49 ENDIF() # NOT GCOV_PATH
50
51 IF(NOT CMAKE_COMPILER_IS_GNUCXX)
52         # Clang version 3.0.0 and greater now supports gcov as well.
53         MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
54         
55         IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
56                 MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
57         ENDIF()
58 ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX
59
60 SET(CMAKE_CXX_FLAGS_COVERAGE
61     "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
62     CACHE STRING "Flags used by the C++ compiler during coverage builds."
63     FORCE )
64 SET(CMAKE_C_FLAGS_COVERAGE
65     "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
66     CACHE STRING "Flags used by the C compiler during coverage builds."
67     FORCE )
68 SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
69     ""
70     CACHE STRING "Flags used for linking binaries during coverage builds."
71     FORCE )
72 SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
73     ""
74     CACHE STRING "Flags used by the shared libraries linker during coverage builds."
75     FORCE )
76 MARK_AS_ADVANCED(
77     CMAKE_CXX_FLAGS_COVERAGE
78     CMAKE_C_FLAGS_COVERAGE
79     CMAKE_EXE_LINKER_FLAGS_COVERAGE
80     CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
81
82 IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage"))
83   MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
84 ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
85
86
87 # Param _targetname     The name of new the custom make target
88 # Param _testrunner     The name of the target which runs the tests.
89 #                                               MUST return ZERO always, even on errors. 
90 #                                               If not, no coverage report will be created!
91 # Param _outputname     lcov output is generated as _outputname.info
92 #                       HTML report is generated in _outputname/index.html
93 # Optional fourth parameter is passed as arguments to _testrunner
94 #   Pass them in list form, e.g.: "-j;2" for -j 2
95 FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
96
97         IF(NOT LCOV_PATH)
98                 MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
99         ENDIF() # NOT LCOV_PATH
100
101         IF(NOT GENHTML_PATH)
102                 MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
103         ENDIF() # NOT GENHTML_PATH
104
105         # Setup target
106         ADD_CUSTOM_TARGET(${_targetname}
107                 
108                 # Cleanup lcov
109                 ${LCOV_PATH} --directory . --zerocounters
110                 
111                 # Run tests
112                 COMMAND ${_testrunner} ${ARGV3}
113                 
114                 # Capturing lcov counters and generating report
115                 COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info
116                 COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned
117                 COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned
118                 COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
119                 
120                 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
121                 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
122         )
123         
124         # Show info where to find the report
125         ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
126                 COMMAND ;
127                 COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
128         )
129
130 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
131
132 # Param _targetname     The name of new the custom make target
133 # Param _testrunner     The name of the target which runs the tests
134 # Param _outputname     cobertura output is generated as _outputname.xml
135 # Optional fourth parameter is passed as arguments to _testrunner
136 #   Pass them in list form, e.g.: "-j;2" for -j 2
137 FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
138
139         IF(NOT PYTHON_EXECUTABLE)
140                 MESSAGE(FATAL_ERROR "Python not found! Aborting...")
141         ENDIF() # NOT PYTHON_EXECUTABLE
142
143         IF(NOT GCOVR_PATH)
144                 MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
145         ENDIF() # NOT GCOVR_PATH
146
147         ADD_CUSTOM_TARGET(${_targetname}
148
149                 # Run tests
150                 ${_testrunner} ${ARGV3}
151
152                 # Running gcovr
153                 COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/'  -o ${_outputname}.xml
154                 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
155                 COMMENT "Running gcovr to produce Cobertura code coverage report."
156         )
157
158         # Show info where to find the report
159         ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
160                 COMMAND ;
161                 COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
162         )
163
164 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA