CodeCoverage.cmake 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ###########################################################################
  2. #
  3. # Copyright 2016 Realm Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. ###########################################################################
  18. find_program(LCOV_PATH lcov)
  19. find_program(GENHTML_PATH genhtml)
  20. find_program(GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
  21. set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 -fprofile-arcs -ftest-coverage -DCATCH_CONFIG_FAST_COMPILE"
  22. CACHE STRING "Flags used by the C++ compiler during coverage builds.")
  23. mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE)
  24. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  25. if(NOT (LCOV_PATH AND GENHTML_PATH AND GCOVR_PATH))
  26. message(FATAL_ERROR "Generating a coverage report requires lcov and gcovr")
  27. endif()
  28. function(create_coverage_target targetname testrunner)
  29. add_custom_target(${targetname}
  30. # Clear previous coverage information
  31. COMMAND ${LCOV_PATH} --directory . --zerocounters
  32. # Run the tests
  33. COMMAND ${testrunner}
  34. # Generate new coverage report
  35. COMMAND ${LCOV_PATH} --directory . --capture --output-file coverage.info
  36. COMMAND ${LCOV_PATH} --extract coverage.info '${CMAKE_SOURCE_DIR}/src/*' --output-file coverage.info.cleaned
  37. COMMAND ${GENHTML_PATH} -o coverage coverage.info.cleaned
  38. COMMAND ${CMAKE_COMMAND} -E remove coverage.info coverage.info.cleaned
  39. COMMAND echo Open coverage/index.html in your browser to view the coverage report.
  40. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  41. )
  42. add_custom_target(${targetname}-cobertura
  43. COMMAND ${testrunner}
  44. COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR}/src ./src -o coverage.xml
  45. COMMAND echo Code coverage report written to coverage.xml
  46. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  47. )
  48. endfunction()
  49. else()
  50. function(create_coverage_target targetname testrunner)
  51. add_custom_target(${targetname}
  52. COMMAND echo "Configure with -DCMAKE_BUILD_TYPE=Coverage to generate coverage reports")
  53. add_custom_target(${targetname}-cobertura
  54. COMMAND echo "Configure with -DCMAKE_BUILD_TYPE=Coverage to generate coverage reports")
  55. endfunction()
  56. endif()