CodeCoverage.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # Copyright (c) 2012 - 2017, Lars Bilke
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without modification,
  5. # are permitted provided that the following conditions are met:
  6. #
  7. # 1. Redistributions of source code must retain the above copyright notice, this
  8. # list of conditions and the following disclaimer.
  9. #
  10. # 2. Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. #
  14. # 3. Neither the name of the copyright holder nor the names of its contributors
  15. # may be used to endorse or promote products derived from this software without
  16. # specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  22. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #
  29. # CHANGES:
  30. #
  31. # 2012-01-31, Lars Bilke
  32. # - Enable Code Coverage
  33. #
  34. # 2013-09-17, Joakim Söderberg
  35. # - Added support for Clang.
  36. # - Some additional usage instructions.
  37. #
  38. # 2016-02-03, Lars Bilke
  39. # - Refactored functions to use named parameters
  40. #
  41. # 2017-06-02, Lars Bilke
  42. # - Merged with modified version from github.com/ufz/ogs
  43. #
  44. #
  45. # USAGE:
  46. #
  47. # 1. Copy this file into your cmake modules path.
  48. #
  49. # 2. Add the following line to your CMakeLists.txt:
  50. # include(CodeCoverage)
  51. #
  52. # 3. Append necessary compiler flags:
  53. # APPEND_COVERAGE_COMPILER_FLAGS()
  54. #
  55. # 4. If you need to exclude additional directories from the report, specify them
  56. # using the COVERAGE_LCOV_EXCLUDES variable before calling SETUP_TARGET_FOR_COVERAGE_LCOV.
  57. # Example:
  58. # set(COVERAGE_LCOV_EXCLUDES 'dir1/*' 'dir2/*')
  59. #
  60. # 5. Use the functions described below to create a custom make target which
  61. # runs your test executable and produces a code coverage report.
  62. #
  63. # 6. Build a Debug build:
  64. # cmake -DCMAKE_BUILD_TYPE=Debug ..
  65. # make
  66. # make my_coverage_target
  67. #
  68. include(CMakeParseArguments)
  69. # Check prereqs
  70. find_program( GCOV_PATH gcov )
  71. find_program( LCOV_PATH NAMES lcov lcov.bat lcov.exe lcov.perl)
  72. find_program( GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat )
  73. find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
  74. find_program( SIMPLE_PYTHON_EXECUTABLE python )
  75. if(NOT GCOV_PATH)
  76. message(FATAL_ERROR "gcov not found! Aborting...")
  77. endif() # NOT GCOV_PATH
  78. if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
  79. if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
  80. message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
  81. endif()
  82. elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
  83. message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
  84. endif()
  85. set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
  86. CACHE INTERNAL "")
  87. set(CMAKE_CXX_FLAGS_COVERAGE
  88. ${COVERAGE_COMPILER_FLAGS}
  89. CACHE STRING "Flags used by the C++ compiler during coverage builds."
  90. FORCE )
  91. set(CMAKE_C_FLAGS_COVERAGE
  92. ${COVERAGE_COMPILER_FLAGS}
  93. CACHE STRING "Flags used by the C compiler during coverage builds."
  94. FORCE )
  95. set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
  96. ""
  97. CACHE STRING "Flags used for linking binaries during coverage builds."
  98. FORCE )
  99. set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
  100. ""
  101. CACHE STRING "Flags used by the shared libraries linker during coverage builds."
  102. FORCE )
  103. mark_as_advanced(
  104. CMAKE_CXX_FLAGS_COVERAGE
  105. CMAKE_C_FLAGS_COVERAGE
  106. CMAKE_EXE_LINKER_FLAGS_COVERAGE
  107. CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
  108. if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  109. message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
  110. endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
  111. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  112. link_libraries(gcov)
  113. else()
  114. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
  115. endif()
  116. # Defines a target for running and collection code coverage information
  117. # Builds dependencies, runs the given executable and outputs reports.
  118. # NOTE! The executable should always have a ZERO as exit code otherwise
  119. # the coverage generation will not complete.
  120. #
  121. # SETUP_TARGET_FOR_COVERAGE_LCOV(
  122. # NAME testrunner_coverage # New target name
  123. # EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  124. # DEPENDENCIES testrunner # Dependencies to build first
  125. # )
  126. function(SETUP_TARGET_FOR_COVERAGE_LCOV)
  127. set(options NONE)
  128. set(oneValueArgs NAME)
  129. set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
  130. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  131. if(NOT LCOV_PATH)
  132. message(FATAL_ERROR "lcov not found! Aborting...")
  133. endif() # NOT LCOV_PATH
  134. if(NOT GENHTML_PATH)
  135. message(FATAL_ERROR "genhtml not found! Aborting...")
  136. endif() # NOT GENHTML_PATH
  137. # Setup target
  138. add_custom_target(${Coverage_NAME}
  139. # Cleanup lcov
  140. COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} -directory . --zerocounters
  141. # Create baseline to make sure untouched files show up in the report
  142. COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} -c -i -d . -o ${Coverage_NAME}.base
  143. # Run tests
  144. COMMAND ${Coverage_EXECUTABLE}
  145. # Capturing lcov counters and generating report
  146. COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --directory . --capture --output-file ${Coverage_NAME}.info
  147. # add baseline counters
  148. COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.info --output-file ${Coverage_NAME}.total
  149. COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --remove ${Coverage_NAME}.total ${COVERAGE_LCOV_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
  150. COMMAND ${GENHTML_PATH} -o ${Coverage_NAME} ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
  151. COMMAND ${CMAKE_COMMAND} -E remove ${Coverage_NAME}.base ${Coverage_NAME}.total ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
  152. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  153. DEPENDS ${Coverage_DEPENDENCIES}
  154. COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
  155. )
  156. # Show where to find the lcov info report
  157. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  158. COMMAND ;
  159. COMMENT "Lcov code coverage info report saved in ${Coverage_NAME}.info."
  160. )
  161. # Show info where to find the report
  162. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  163. COMMAND ;
  164. COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
  165. )
  166. endfunction() # SETUP_TARGET_FOR_COVERAGE_LCOV
  167. # Defines a target for running and collection code coverage information
  168. # Builds dependencies, runs the given executable and outputs reports.
  169. # NOTE! The executable should always have a ZERO as exit code otherwise
  170. # the coverage generation will not complete.
  171. #
  172. # SETUP_TARGET_FOR_COVERAGE_GCOVR_XML(
  173. # NAME ctest_coverage # New target name
  174. # EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  175. # DEPENDENCIES executable_target # Dependencies to build first
  176. # )
  177. function(SETUP_TARGET_FOR_COVERAGE_GCOVR_XML)
  178. set(options NONE)
  179. set(oneValueArgs NAME)
  180. set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
  181. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  182. if(NOT SIMPLE_PYTHON_EXECUTABLE)
  183. message(FATAL_ERROR "python not found! Aborting...")
  184. endif() # NOT SIMPLE_PYTHON_EXECUTABLE
  185. if(NOT GCOVR_PATH)
  186. message(FATAL_ERROR "gcovr not found! Aborting...")
  187. endif() # NOT GCOVR_PATH
  188. # Combine excludes to several -e arguments
  189. set(GCOVR_EXCLUDES "")
  190. foreach(EXCLUDE ${COVERAGE_GCOVR_EXCLUDES})
  191. list(APPEND GCOVR_EXCLUDES "-e")
  192. list(APPEND GCOVR_EXCLUDES "${EXCLUDE}")
  193. endforeach()
  194. add_custom_target(${Coverage_NAME}
  195. # Run tests
  196. ${Coverage_EXECUTABLE}
  197. # Running gcovr
  198. COMMAND ${GCOVR_PATH} --xml
  199. -r ${PROJECT_SOURCE_DIR} ${GCOVR_EXCLUDES}
  200. --object-directory=${PROJECT_BINARY_DIR}
  201. -o ${Coverage_NAME}.xml
  202. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  203. DEPENDS ${Coverage_DEPENDENCIES}
  204. COMMENT "Running gcovr to produce Cobertura code coverage report."
  205. )
  206. # Show info where to find the report
  207. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  208. COMMAND ;
  209. COMMENT "Cobertura code coverage report saved in ${Coverage_NAME}.xml."
  210. )
  211. endfunction() # SETUP_TARGET_FOR_COVERAGE_GCOVR_XML
  212. # Defines a target for running and collection code coverage information
  213. # Builds dependencies, runs the given executable and outputs reports.
  214. # NOTE! The executable should always have a ZERO as exit code otherwise
  215. # the coverage generation will not complete.
  216. #
  217. # SETUP_TARGET_FOR_COVERAGE_GCOVR_HTML(
  218. # NAME ctest_coverage # New target name
  219. # EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  220. # DEPENDENCIES executable_target # Dependencies to build first
  221. # )
  222. function(SETUP_TARGET_FOR_COVERAGE_GCOVR_HTML)
  223. set(options NONE)
  224. set(oneValueArgs NAME)
  225. set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
  226. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  227. if(NOT SIMPLE_PYTHON_EXECUTABLE)
  228. message(FATAL_ERROR "python not found! Aborting...")
  229. endif() # NOT SIMPLE_PYTHON_EXECUTABLE
  230. if(NOT GCOVR_PATH)
  231. message(FATAL_ERROR "gcovr not found! Aborting...")
  232. endif() # NOT GCOVR_PATH
  233. # Combine excludes to several -e arguments
  234. set(GCOVR_EXCLUDES "")
  235. foreach(EXCLUDE ${COVERAGE_GCOVR_EXCLUDES})
  236. list(APPEND GCOVR_EXCLUDES "-e")
  237. list(APPEND GCOVR_EXCLUDES "${EXCLUDE}")
  238. endforeach()
  239. add_custom_target(${Coverage_NAME}
  240. # Run tests
  241. ${Coverage_EXECUTABLE}
  242. # Create folder
  243. COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/${Coverage_NAME}
  244. # Running gcovr
  245. COMMAND ${GCOVR_PATH} --html --html-details
  246. -r ${PROJECT_SOURCE_DIR} ${GCOVR_EXCLUDES}
  247. --object-directory=${PROJECT_BINARY_DIR}
  248. -o ${Coverage_NAME}/index.html
  249. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  250. DEPENDS ${Coverage_DEPENDENCIES}
  251. COMMENT "Running gcovr to produce HTML code coverage report."
  252. )
  253. # Show info where to find the report
  254. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  255. COMMAND ;
  256. COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
  257. )
  258. endfunction() # SETUP_TARGET_FOR_COVERAGE_GCOVR_HTML
  259. function(APPEND_COVERAGE_COMPILER_FLAGS)
  260. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
  261. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
  262. message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
  263. endfunction() # APPEND_COVERAGE_COMPILER_FLAGS