CodeCoverage.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # Copyright (c) 2012 - 2017, Lars Bilke
  2. # Copyright (c) 2021 KeePassXC Team
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without modification,
  6. # are permitted provided that the following conditions are met:
  7. #
  8. # 1. Redistributions of source code must retain the above copyright notice, this
  9. # list of conditions and the following disclaimer.
  10. #
  11. # 2. Redistributions in binary form must reproduce the above copyright notice,
  12. # this list of conditions and the following disclaimer in the documentation
  13. # and/or other materials provided with the distribution.
  14. #
  15. # 3. Neither the name of the copyright holder nor the names of its contributors
  16. # may be used to endorse or promote products derived from this software without
  17. # specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. include(CMakeParseArguments)
  30. # Check prereqs
  31. find_program(GCOV_PATH gcov)
  32. find_program(LLVM_COV_PATH llvm-cov)
  33. find_program(LLVM_PROFDATA_PATH llvm-profdata)
  34. find_program(XCRUN_PATH xcrun)
  35. find_program(GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat)
  36. find_program(GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
  37. set(COVERAGE_COMPILER_FLAGS "-g -O0" CACHE INTERNAL "")
  38. if(CMAKE_COMPILER_IS_GNUCXX)
  39. set(COVERAGE_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
  40. elseif(CMAKE_COMPILER_IS_CLANGXX)
  41. set(COVERAGE_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
  42. endif()
  43. set(CMAKE_COVERAGE_FORMAT
  44. "html" "xml"
  45. CACHE STRING "Coverage report output format.")
  46. set_property(CACHE CMAKE_COVERAGE_FORMAT PROPERTY STRINGS "html" "txt")
  47. set(CMAKE_CXX_FLAGS_COVERAGE
  48. ${COVERAGE_COMPILER_FLAGS}
  49. CACHE STRING "Flags used by the C++ compiler during coverage builds.")
  50. set(CMAKE_C_FLAGS_COVERAGE
  51. ${COVERAGE_COMPILER_FLAGS}
  52. CACHE STRING "Flags used by the C compiler during coverage builds.")
  53. set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
  54. ""
  55. CACHE STRING "Flags used for linking binaries during coverage builds.")
  56. set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
  57. ""
  58. CACHE STRING "Flags used by the shared libraries linker during coverage builds.")
  59. mark_as_advanced(
  60. CMAKE_COVERAGE_FORMAT
  61. CMAKE_CXX_FLAGS_COVERAGE
  62. CMAKE_C_FLAGS_COVERAGE
  63. CMAKE_EXE_LINKER_FLAGS_COVERAGE
  64. CMAKE_SHARED_LINKER_FLAGS_COVERAGE)
  65. if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  66. message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
  67. endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
  68. if(CMAKE_COMPILER_IS_GNUCXX)
  69. if(NOT GCOV_PATH)
  70. message(FATAL_ERROR "gcov not found! Aborting...")
  71. endif() # NOT GCOV_PATH
  72. link_libraries(gcov)
  73. endif()
  74. # Defines a target for running and collection code coverage information
  75. # Builds dependencies, runs the given executable and outputs reports.
  76. # NOTE! The executable should always have a ZERO as exit code otherwise
  77. # the coverage generation will not complete.
  78. #
  79. # SETUP_TARGET_FOR_COVERAGE_GCOVR(
  80. # NAME ctest_coverage # New target name
  81. # EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  82. # DEPENDENCIES executable_target # Dependencies to build first
  83. # )
  84. function(SETUP_TARGET_FOR_COVERAGE_GCOVR)
  85. set(options NONE)
  86. set(oneValueArgs NAME SOURCES_ROOT)
  87. set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
  88. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  89. if(NOT GCOVR_PATH)
  90. message(FATAL_ERROR "gcovr not found! Aborting...")
  91. endif() # NOT GCOVR_PATH
  92. # Combine excludes to several -e arguments
  93. set(GCOVR_EXCLUDES "")
  94. foreach(EXCLUDE ${COVERAGE_EXCLUDES})
  95. list(APPEND GCOVR_EXCLUDES "-e")
  96. list(APPEND GCOVR_EXCLUDES "${EXCLUDE}")
  97. endforeach()
  98. add_custom_target(${Coverage_NAME}
  99. # Run tests
  100. COMMAND ctest -C $<CONFIG> $ENV{ARGS} $$ARGS
  101. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  102. DEPENDS ${Coverage_DEPENDENCIES}
  103. )
  104. if("html" IN_LIST CMAKE_COVERAGE_FORMAT)
  105. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  106. # Create folder
  107. COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/${Coverage_NAME}-html
  108. # Running gcovr HTML
  109. COMMAND ${GCOVR_PATH} --html --html-details
  110. -r ${Coverage_SOURCES_ROOT} ${GCOVR_EXCLUDES}
  111. --object-directory=${PROJECT_BINARY_DIR}
  112. --exclude-unreachable-branches --exclude-throw-branches
  113. -o ${Coverage_NAME}-html/index.html
  114. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  115. COMMENT "Running gcovr to produce HTML code coverage report ${Coverage_NAME}-html."
  116. )
  117. endif()
  118. if("xml" IN_LIST CMAKE_COVERAGE_FORMAT)
  119. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  120. # Running gcovr TXT
  121. COMMAND ${GCOVR_PATH} --xml
  122. -r ${Coverage_SOURCES_ROOT} ${GCOVR_EXCLUDES}
  123. --object-directory=${PROJECT_BINARY_DIR}
  124. --exclude-unreachable-branches --exclude-throw-branches
  125. -o ${Coverage_NAME}.xml
  126. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  127. COMMENT "Running gcovr to produce XML code coverage report ${Coverage_NAME}.xml."
  128. )
  129. endif()
  130. if("txt" IN_LIST CMAKE_COVERAGE_FORMAT)
  131. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  132. # Running gcovr TXT
  133. COMMAND ${GCOVR_PATH}
  134. -r ${Coverage_SOURCES_ROOT} ${GCOVR_EXCLUDES}
  135. --object-directory=${PROJECT_BINARY_DIR}
  136. --exclude-unreachable-branches --exclude-throw-branches
  137. -o ${Coverage_NAME}.txt
  138. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  139. COMMENT "Running gcovr to produce TXT code coverage report ${Coverage_NAME}.txt."
  140. )
  141. endif()
  142. endfunction() # SETUP_TARGET_FOR_COVERAGE_GCOVR
  143. # Defines a target for running and collection code coverage information
  144. # Builds dependencies, runs the given executable and outputs reports.
  145. # NOTE! The executable should always have a ZERO as exit code otherwise
  146. # the coverage generation will not complete.
  147. #
  148. # SETUP_TARGET_FOR_COVERAGE_LLVM(
  149. # NAME ctest_coverage # New target name
  150. # EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
  151. # DEPENDENCIES executable_target # Dependencies to build first
  152. # )
  153. function(SETUP_TARGET_FOR_COVERAGE_LLVM)
  154. set(options NONE)
  155. set(oneValueArgs NAME SOURCES_ROOT PROF_FILE)
  156. set(multiValueArgs EXECUTABLE BINARY EXECUTABLE_ARGS DEPENDENCIES)
  157. cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  158. if(XCRUN_PATH)
  159. set(LLVM_COV_PATH ${XCRUN_PATH} llvm-cov)
  160. set(LLVM_PROFDATA_PATH ${XCRUN_PATH} llvm-profdata)
  161. else()
  162. if(NOT LLVM_COV_PATH)
  163. message(FATAL_ERROR "llvm-cov not found! Aborting...")
  164. endif() # NOT LLVM_COV_PATH
  165. if(NOT LLVM_PROFDATA_PATH)
  166. message(FATAL_ERROR "llvm-profdata not found! Aborting...")
  167. endif() # NOT LLVM_PROFDATA_PATH
  168. endif() # XCRUN_PATH
  169. set(LLVM_PROFILE_DIR ${PROJECT_BINARY_DIR}/llvm_profile)
  170. file(REMOVE_RECURSE ${LLVM_PROFILE_DIR})
  171. set(COV_EXCLUDES "")
  172. foreach(EXCLUDE ${COVERAGE_EXCLUDES})
  173. list(APPEND COV_EXCLUDES "-ignore-filename-regex=${EXCLUDE}")
  174. endforeach()
  175. list(GET Coverage_BINARY 0 COV_BINARY)
  176. if(Coverage_BINARY)
  177. list(REMOVE_AT Coverage_BINARY 0)
  178. foreach(BIN ${Coverage_BINARY})
  179. list(APPEND COV_BINARY -object ${BIN})
  180. endforeach()
  181. endif()
  182. add_custom_target(${Coverage_NAME}
  183. COMMAND ${CMAKE_COMMAND} -E env LLVM_PROFILE_FILE=${LLVM_PROFILE_DIR}/profile-%p.profraw ctest -C $<CONFIG> $$ARGS
  184. COMMAND ${LLVM_PROFDATA_PATH} merge -sparse ${LLVM_PROFILE_DIR}/* -o coverage.profdata
  185. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  186. DEPENDS ${Coverage_DEPENDENCIES})
  187. if("html" IN_LIST CMAKE_COVERAGE_FORMAT)
  188. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  189. COMMAND ${LLVM_COV_PATH} show -instr-profile=coverage.profdata ${COV_BINARY}
  190. --format=html --output-dir=${Coverage_NAME}-html ${COV_EXCLUDES} ${Coverage_SOURCES_ROOT}
  191. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  192. COMMENT "Running llvm-cov to produce HTML code coverage report ${Coverage_NAME}-html")
  193. endif()
  194. if("xml" IN_LIST CMAKE_COVERAGE_FORMAT)
  195. message(WARNING "XML coverage report format not supported for llvm-cov")
  196. endif()
  197. if("txt" IN_LIST CMAKE_COVERAGE_FORMAT)
  198. add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
  199. COMMAND ${LLVM_COV_PATH} show -instr-profile=coverage.profdata ${COV_BINARY}
  200. --format=text ${COV_EXCLUDES} ${Coverage_SOURCES_ROOT} > ${Coverage_NAME}.txt
  201. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  202. COMMENT "Running llvm-cov to produce TXT code coverage report ${Coverage_NAME}.txt.")
  203. endif()
  204. endfunction() # SETUP_TARGET_FOR_COVERAGE_LLVM
  205. function(APPEND_COVERAGE_COMPILER_FLAGS)
  206. message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
  207. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
  208. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
  209. endfunction() # APPEND_COVERAGE_COMPILER_FLAGS