PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/CodeCoverage.cmake

https://gitlab.com/lwiz/BasicEventEngine
CMake | 309 lines | 151 code | 40 blank | 118 comment | 12 complexity | 2b020a44196ed687407c13561712cb41 MD5 | raw file
  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. # 3.a (OPTIONAL) Set appropriate optimization flags, e.g. -O0, -O1 or -Og
  56. #
  57. # 4. If you need to exclude additional directories from the report, specify them
  58. # using the COVERAGE_LCOV_EXCLUDES variable before calling SETUP_TARGET_FOR_COVERAGE_LCOV.
  59. # Example:
  60. # set(COVERAGE_LCOV_EXCLUDES 'dir1/*' 'dir2/*')
  61. #
  62. # 5. Use the functions described below to create a custom make target which
  63. # runs your test executable and produces a code coverage report.
  64. #
  65. # 6. Build a Debug build:
  66. # cmake -DCMAKE_BUILD_TYPE=Debug ..
  67. # make
  68. # make my_coverage_target
  69. #
  70. include(CMakeParseArguments)
  71. # Check prereqs
  72. find_program( GCOV_PATH gcov )
  73. find_program( LCOV_PATH NAMES lcov lcov.bat lcov.exe lcov.perl)
  74. find_program( GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat )
  75. find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
  76. #find_package(Python COMPONENTS Interpreter)
  77. set(Python_FOUND TRUE)
  78. set(Python_EXECUTABLE "/usr/bin/env" "python")
  79. if(NOT GCOV_PATH)
  80. message(FATAL_ERROR "gcov not found! Aborting...")
  81. endif() # NOT GCOV_PATH
  82. if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
  83. if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
  84. message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
  85. endif()
  86. elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
  87. message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
  88. endif()
  89. set(COVERAGE_COMPILER_FLAGS "-g --coverage -fprofile-arcs -ftest-coverage"
  90. CACHE INTERNAL "")
  91. set(CMAKE_CXX_FLAGS_COVERAGE
  92. ${COVERAGE_COMPILER_FLAGS}
  93. CACHE STRING "Flags used by the C++ compiler during coverage builds."
  94. FORCE )
  95. set(CMAKE_C_FLAGS_COVERAGE
  96. ${COVERAGE_COMPILER_FLAGS}
  97. CACHE STRING "Flags used by the C compiler during coverage builds."
  98. FORCE )
  99. set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
  100. ""
  101. CACHE STRING "Flags used for linking binaries during coverage builds."
  102. FORCE )
  103. set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
  104. ""
  105. CACHE STRING "Flags used by the shared libraries linker during coverage builds."
  106. FORCE )
  107. mark_as_advanced(
  108. CMAKE_CXX_FLAGS_COVERAGE
  109. CMAKE_C_FLAGS_COVERAGE
  110. CMAKE_EXE_LINKER_FLAGS_COVERAGE
  111. CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
  112. if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
  113. message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
  114. endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
  115. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  116. link_libraries(gcov)
  117. else()
  118. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
  119. endif()
  120. # Defines a target for running and collection code coverage information
  121. # Builds dependencies, runs the given executable and outputs reports.
  122. # NOTE! The executable should always have a ZERO as exit code otherwise
  123. # the coverage generation will not complete.
  124. #
  125. # SETUP_TARGET_FOR_COVERAGE_LCOV(
  126. # NAME testrunner_coverage # New target name
  127. # EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  128. # DEPENDENCIES testrunner # Dependencies to build first
  129. # )
  130. function(SETUP_TARGET_FOR_COVERAGE_LCOV)
  131. set(options NONE)
  132. set(oneValueArgs NAME)
  133. set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES LCOV_ARGS GENHTML_ARGS)
  134. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  135. if(NOT LCOV_PATH)
  136. message(FATAL_ERROR "lcov not found! Aborting...")
  137. endif() # NOT LCOV_PATH
  138. if(NOT GENHTML_PATH)
  139. message(FATAL_ERROR "genhtml not found! Aborting...")
  140. endif() # NOT GENHTML_PATH
  141. # Setup target
  142. add_custom_target(${Coverage_NAME}
  143. # Cleanup lcov
  144. COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -directory . --zerocounters
  145. # Create baseline to make sure untouched files show up in the report
  146. COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -c -i -d . -o ${Coverage_NAME}.base
  147. # Run tests
  148. COMMAND ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}
  149. # Capturing lcov counters and generating report
  150. COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --directory . --capture --output-file ${Coverage_NAME}.info
  151. # add baseline counters
  152. COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.info --output-file ${Coverage_NAME}.total
  153. COMMAND ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --remove ${Coverage_NAME}.total ${COVERAGE_LCOV_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
  154. COMMAND ${GENHTML_PATH} ${Coverage_GENHTML_ARGS} -o ${Coverage_NAME} ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
  155. COMMAND ${CMAKE_COMMAND} -E remove ${Coverage_NAME}.base ${Coverage_NAME}.total ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
  156. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  157. DEPENDS ${Coverage_DEPENDENCIES}
  158. COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
  159. )
  160. # Show where to find the lcov info report
  161. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  162. COMMAND ;
  163. COMMENT "Lcov code coverage info report saved in ${Coverage_NAME}.info."
  164. )
  165. # Show info where to find the report
  166. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  167. COMMAND ;
  168. COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
  169. )
  170. endfunction() # SETUP_TARGET_FOR_COVERAGE_LCOV
  171. # Defines a target for running and collection code coverage information
  172. # Builds dependencies, runs the given executable and outputs reports.
  173. # NOTE! The executable should always have a ZERO as exit code otherwise
  174. # the coverage generation will not complete.
  175. #
  176. # SETUP_TARGET_FOR_COVERAGE_GCOVR_XML(
  177. # NAME ctest_coverage # New target name
  178. # EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  179. # DEPENDENCIES executable_target # Dependencies to build first
  180. # )
  181. function(SETUP_TARGET_FOR_COVERAGE_GCOVR_XML)
  182. set(options NONE)
  183. set(oneValueArgs NAME)
  184. set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
  185. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  186. if(NOT Python_FOUND)
  187. message(FATAL_ERROR "python not found! Aborting...")
  188. endif()
  189. if(NOT GCOVR_PATH)
  190. message(FATAL_ERROR "gcovr not found! Aborting...")
  191. endif() # NOT GCOVR_PATH
  192. # Combine excludes to several -e arguments
  193. set(GCOVR_EXCLUDES "")
  194. foreach(EXCLUDE ${COVERAGE_GCOVR_EXCLUDES})
  195. string(REPLACE "*" "\\*" EXCLUDE_REPLACED ${EXCLUDE})
  196. list(APPEND GCOVR_EXCLUDES "-e")
  197. list(APPEND GCOVR_EXCLUDES "${EXCLUDE_REPLACED}")
  198. endforeach()
  199. add_custom_target(${Coverage_NAME}
  200. # Run tests
  201. ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}
  202. # Running gcovr
  203. COMMAND ${GCOVR_PATH} --xml
  204. -r ${PROJECT_SOURCE_DIR} ${GCOVR_EXCLUDES}
  205. --object-directory=${PROJECT_BINARY_DIR}
  206. -o ${Coverage_NAME}.xml
  207. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  208. DEPENDS ${Coverage_DEPENDENCIES}
  209. COMMENT "Running gcovr to produce Cobertura code coverage report."
  210. )
  211. # Show info where to find the report
  212. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  213. COMMAND ;
  214. COMMENT "Cobertura code coverage report saved in ${Coverage_NAME}.xml."
  215. )
  216. endfunction() # SETUP_TARGET_FOR_COVERAGE_GCOVR_XML
  217. # Defines a target for running and collection code coverage information
  218. # Builds dependencies, runs the given executable and outputs reports.
  219. # NOTE! The executable should always have a ZERO as exit code otherwise
  220. # the coverage generation will not complete.
  221. #
  222. # SETUP_TARGET_FOR_COVERAGE_GCOVR_HTML(
  223. # NAME ctest_coverage # New target name
  224. # EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  225. # DEPENDENCIES executable_target # Dependencies to build first
  226. # )
  227. function(SETUP_TARGET_FOR_COVERAGE_GCOVR_HTML)
  228. set(options NONE)
  229. set(oneValueArgs NAME)
  230. set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
  231. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  232. if(NOT Python_FOUND)
  233. message(FATAL_ERROR "python not found! Aborting...")
  234. endif()
  235. if(NOT GCOVR_PATH)
  236. message(FATAL_ERROR "gcovr not found! Aborting...")
  237. endif() # NOT GCOVR_PATH
  238. # Combine excludes to several -e arguments
  239. set(GCOVR_EXCLUDES "")
  240. foreach(EXCLUDE ${COVERAGE_GCOVR_EXCLUDES})
  241. string(REPLACE "*" "\\*" EXCLUDE_REPLACED ${EXCLUDE})
  242. list(APPEND GCOVR_EXCLUDES "-e")
  243. list(APPEND GCOVR_EXCLUDES "${EXCLUDE_REPLACED}")
  244. endforeach()
  245. add_custom_target(${Coverage_NAME}
  246. # Run tests
  247. ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}
  248. # Create folder
  249. COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/${Coverage_NAME}
  250. # Running gcovr
  251. COMMAND ${Python_EXECUTABLE} ${GCOVR_PATH} --html --html-details
  252. -r ${PROJECT_SOURCE_DIR} ${GCOVR_EXCLUDES}
  253. --object-directory=${PROJECT_BINARY_DIR}
  254. -o ${Coverage_NAME}/index.html
  255. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  256. DEPENDS ${Coverage_DEPENDENCIES}
  257. COMMENT "Running gcovr to produce HTML code coverage report."
  258. )
  259. # Show info where to find the report
  260. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  261. COMMAND ;
  262. COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
  263. )
  264. endfunction() # SETUP_TARGET_FOR_COVERAGE_GCOVR_HTML
  265. function(APPEND_COVERAGE_COMPILER_FLAGS)
  266. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
  267. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
  268. message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
  269. endfunction() # APPEND_COVERAGE_COMPILER_FLAGS