CodeCoverage.cmake 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE)
  23. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  24. if(NOT (LCOV_PATH AND GENHTML_PATH AND GCOVR_PATH))
  25. message(FATAL_ERROR "Generating a coverage report requires lcov and gcovr")
  26. endif()
  27. function(create_coverage_target targetname testrunner)
  28. add_custom_target(${targetname}
  29. # Clear previous coverage information
  30. COMMAND ${LCOV_PATH} --directory . --zerocounters
  31. # Run the tests
  32. COMMAND ${testrunner}
  33. # Generate new coverage report
  34. COMMAND ${LCOV_PATH} --directory . --capture --output-file coverage.info
  35. COMMAND ${LCOV_PATH} --extract coverage.info '${CMAKE_SOURCE_DIR}/src/*' --output-file coverage.info.cleaned
  36. COMMAND ${GENHTML_PATH} -o coverage coverage.info.cleaned
  37. COMMAND ${CMAKE_COMMAND} -E remove coverage.info coverage.info.cleaned
  38. COMMAND echo Open coverage/index.html in your browser to view the coverage report.
  39. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  40. )
  41. add_custom_target(${targetname}-cobertura
  42. COMMAND ${testrunner}
  43. COMMAND ${GCOVR_PATH} -x -o coverage.xml -f 'src/.*' -f "${CMAKE_SOURCE_DIR}/src.*" --exclude-directories "${CMAKE_BINARY_DIR}/tests" --exclude-directories="${CMAKE_SOURCE_DIR}/\.tmp" --exclude ".*realm\-core.*" --exclude ".*realm\-sync.*"
  44. COMMAND echo Code coverage report written to coverage.xml
  45. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  46. )
  47. endfunction()
  48. else()
  49. function(create_coverage_target targetname testrunner)
  50. add_custom_target(${targetname}
  51. COMMAND echo "Configure with -DCMAKE_BUILD_TYPE=Coverage to generate coverage reports")
  52. add_custom_target(${targetname}-cobertura
  53. COMMAND echo "Configure with -DCMAKE_BUILD_TYPE=Coverage to generate coverage reports")
  54. endfunction()
  55. endif()