CodeCoverage.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #
  2. # Boost Software License - Version 1.0 - August 17th, 2003
  3. #
  4. # Permission is hereby granted, free of charge, to any person or organization
  5. # obtaining a copy of the software and accompanying documentation covered by
  6. # this license (the "Software") to use, reproduce, display, distribute,
  7. # execute, and transmit the Software, and to prepare derivative works of the
  8. # Software, and to permit third-parties to whom the Software is furnished to
  9. # do so, all subject to the following:
  10. #
  11. # The copyright notices in the Software and this entire statement, including
  12. # the above license grant, this restriction and the following disclaimer,
  13. # must be included in all copies of the Software, in whole or in part, and
  14. # all derivative works of the Software, unless such copies or derivative
  15. # works are solely in the form of machine-executable object code generated by
  16. # a source language processor.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  21. # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  22. # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  23. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. # DEALINGS IN THE SOFTWARE.
  25. #
  26. # 2012-01-31, Lars Bilke
  27. # - Enable Code Coverage
  28. #
  29. # 2013-09-17, Joakim Söderberg
  30. # - Added support for Clang.
  31. # - Some additional usage instructions.
  32. #
  33. # USAGE:
  34. # 1. Copy this file into your cmake modules path.
  35. #
  36. # 2. Add the following line to your CMakeLists.txt:
  37. # INCLUDE(CodeCoverage)
  38. #
  39. # 3. Set compiler flags to turn off optimization and enable coverage:
  40. # SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
  41. # SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
  42. #
  43. # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
  44. # which runs your test executable and produces a lcov code coverage report:
  45. # Example:
  46. # SETUP_TARGET_FOR_COVERAGE(
  47. # my_coverage_target # Name for custom target.
  48. # test_driver # Name of the test driver executable that runs the tests.
  49. # # NOTE! This should always have a ZERO as exit code
  50. # # otherwise the coverage generation will not complete.
  51. # coverage # Name of output directory.
  52. # )
  53. #
  54. # 4. Build a Debug build:
  55. # cmake -DCMAKE_BUILD_TYPE=Debug ..
  56. # make
  57. # make my_coverage_target
  58. #
  59. #
  60. # Check prereqs
  61. FIND_PROGRAM( GCOV_PATH gcov )
  62. FIND_PROGRAM( LCOV_PATH lcov )
  63. FIND_PROGRAM( GENHTML_PATH genhtml )
  64. FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
  65. IF(NOT GCOV_PATH)
  66. MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
  67. ENDIF() # NOT GCOV_PATH
  68. IF(NOT (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCC))
  69. # Clang version 3.0.0 and greater now supports gcov as well.
  70. MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
  71. IF(NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang"))
  72. MESSAGE(FATAL_ERROR "Compiler is not GNU gcc or Clang! Aborting...")
  73. ENDIF()
  74. ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX
  75. IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" )
  76. MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
  77. ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
  78. # Param _targetname The name of new the custom make target
  79. # Param _outputname lcov output is generated as _outputname.info
  80. # HTML report is generated in _outputname/index.html
  81. # Param _testrunner The name of the target which runs the tests.
  82. # MUST return ZERO always, even on errors.
  83. # If not, no coverage report will be created!
  84. # Optional fourth parameter is passed as arguments to _testrunner
  85. # Pass them in list form, e.g.: "-j;2" for -j 2
  86. FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _outputname _testrunner)
  87. IF(NOT LCOV_PATH)
  88. MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
  89. ENDIF() # NOT LCOV_PATH
  90. IF(NOT GENHTML_PATH)
  91. MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
  92. ENDIF() # NOT GENHTML_PATH
  93. # Setup target
  94. ADD_CUSTOM_TARGET(${_targetname}
  95. # Cleanup lcov
  96. ${LCOV_PATH} --directory . --zerocounters
  97. # Run tests
  98. COMMAND ${_testrunner} ${ARGV3}
  99. # Capturing lcov counters and generating report
  100. COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info
  101. COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned
  102. COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned
  103. COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
  104. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  105. COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
  106. )
  107. # Show info where to find the report
  108. ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
  109. COMMAND ;
  110. COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
  111. )
  112. ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
  113. # Param _targetname The name of new the custom make target
  114. # Param _testrunner The name of the target which runs the tests
  115. # Param _outputname cobertura output is generated as _outputname.xml
  116. # Optional fourth parameter is passed as arguments to _testrunner
  117. # Pass them in list form, e.g.: "-j;2" for -j 2
  118. FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
  119. IF(NOT PYTHON_EXECUTABLE)
  120. MESSAGE(FATAL_ERROR "Python not found! Aborting...")
  121. ENDIF() # NOT PYTHON_EXECUTABLE
  122. IF(NOT GCOVR_PATH)
  123. MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
  124. ENDIF() # NOT GCOVR_PATH
  125. ADD_CUSTOM_TARGET(${_targetname}
  126. # Run tests
  127. ${_testrunner} ${ARGV3}
  128. # Running gcovr
  129. COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
  130. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  131. COMMENT "Running gcovr to produce Cobertura code coverage report."
  132. )
  133. # Show info where to find the report
  134. ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
  135. COMMAND ;
  136. COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
  137. )
  138. ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA