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