Mercurial > thymian
comparison cmake/CodeCoverage.cmake @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 # | |
| 2 # 2012-01-31, Lars Bilke | |
| 3 # - Enable Code Coverage | |
| 4 # | |
| 5 # 2013-09-17, Joakim Söderberg | |
| 6 # - Added support for Clang. | |
| 7 # - Some additional usage instructions. | |
| 8 # | |
| 9 # USAGE: | |
| 10 # 1. Copy this file into your cmake modules path. | |
| 11 # | |
| 12 # 2. Add the following line to your CMakeLists.txt: | |
| 13 # INCLUDE(CodeCoverage) | |
| 14 # | |
| 15 # 3. Set compiler flags to turn off optimization and enable coverage: | |
| 16 # SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") | |
| 17 # SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") | |
| 18 # | |
| 19 # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target | |
| 20 # which runs your test executable and produces a lcov code coverage report: | |
| 21 # Example: | |
| 22 # SETUP_TARGET_FOR_COVERAGE( | |
| 23 # my_coverage_target # Name for custom target. | |
| 24 # test_driver # Name of the test driver executable that runs the tests. | |
| 25 # # NOTE! This should always have a ZERO as exit code | |
| 26 # # otherwise the coverage generation will not complete. | |
| 27 # coverage # Name of output directory. | |
| 28 # ) | |
| 29 # | |
| 30 # 4. Build a Debug build: | |
| 31 # cmake -DCMAKE_BUILD_TYPE=Debug .. | |
| 32 # make | |
| 33 # make my_coverage_target | |
| 34 # | |
| 35 # | |
| 36 | |
| 37 # Check prereqs | |
| 38 FIND_PROGRAM( GCOV_PATH gcov ) | |
| 39 FIND_PROGRAM( LCOV_PATH lcov ) | |
| 40 FIND_PROGRAM( GENHTML_PATH genhtml ) | |
| 41 FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests) | |
| 42 | |
| 43 IF(NOT GCOV_PATH) | |
| 44 MESSAGE(FATAL_ERROR "gcov not found! Aborting...") | |
| 45 ENDIF() # NOT GCOV_PATH | |
| 46 | |
| 47 IF(NOT CMAKE_COMPILER_IS_GNUCXX) | |
| 48 # Clang version 3.0.0 and greater now supports gcov as well. | |
| 49 MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.") | |
| 50 | |
| 51 IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") | |
| 52 MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") | |
| 53 ENDIF() | |
| 54 ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX | |
| 55 | |
| 56 IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" ) | |
| 57 MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" ) | |
| 58 ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug" | |
| 59 | |
| 60 | |
| 61 # Param _targetname The name of new the custom make target | |
| 62 # Param _testrunner The name of the target which runs the tests. | |
| 63 # MUST return ZERO always, even on errors. | |
| 64 # If not, no coverage report will be created! | |
| 65 # Param _outputname lcov output is generated as _outputname.info | |
| 66 # HTML report is generated in _outputname/index.html | |
| 67 # Optional fourth parameter is passed as arguments to _testrunner | |
| 68 # Pass them in list form, e.g.: "-j;2" for -j 2 | |
| 69 FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname) | |
| 70 | |
| 71 IF(NOT LCOV_PATH) | |
| 72 MESSAGE(FATAL_ERROR "lcov not found! Aborting...") | |
| 73 ENDIF() # NOT LCOV_PATH | |
| 74 | |
| 75 IF(NOT GENHTML_PATH) | |
| 76 MESSAGE(FATAL_ERROR "genhtml not found! Aborting...") | |
| 77 ENDIF() # NOT GENHTML_PATH | |
| 78 | |
| 79 # Setup target | |
| 80 ADD_CUSTOM_TARGET(${_targetname} | |
| 81 | |
| 82 # Cleanup lcov | |
| 83 ${LCOV_PATH} --directory . --zerocounters | |
| 84 | |
| 85 # Run tests | |
| 86 COMMAND ${_testrunner} ${ARGV3} | |
| 87 | |
| 88 # Capturing lcov counters and generating report | |
| 89 COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info | |
| 90 COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned | |
| 91 COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned | |
| 92 COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned | |
| 93 | |
| 94 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} | |
| 95 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." | |
| 96 ) | |
| 97 | |
| 98 # Show info where to find the report | |
| 99 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD | |
| 100 COMMAND ; | |
| 101 COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report." | |
| 102 ) | |
| 103 | |
| 104 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE | |
| 105 | |
| 106 # Param _targetname The name of new the custom make target | |
| 107 # Param _testrunner The name of the target which runs the tests | |
| 108 # Param _outputname cobertura output is generated as _outputname.xml | |
| 109 # Optional fourth parameter is passed as arguments to _testrunner | |
| 110 # Pass them in list form, e.g.: "-j;2" for -j 2 | |
| 111 FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname) | |
| 112 | |
| 113 IF(NOT PYTHON_EXECUTABLE) | |
| 114 MESSAGE(FATAL_ERROR "Python not found! Aborting...") | |
| 115 ENDIF() # NOT PYTHON_EXECUTABLE | |
| 116 | |
| 117 IF(NOT GCOVR_PATH) | |
| 118 MESSAGE(FATAL_ERROR "gcovr not found! Aborting...") | |
| 119 ENDIF() # NOT GCOVR_PATH | |
| 120 | |
| 121 ADD_CUSTOM_TARGET(${_targetname} | |
| 122 | |
| 123 # Run tests | |
| 124 ${_testrunner} ${ARGV3} | |
| 125 | |
| 126 # Running gcovr | |
| 127 COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml | |
| 128 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} | |
| 129 COMMENT "Running gcovr to produce Cobertura code coverage report." | |
| 130 ) | |
| 131 | |
| 132 # Show info where to find the report | |
| 133 ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD | |
| 134 COMMAND ; | |
| 135 COMMENT "Cobertura code coverage report saved in ${_outputname}.xml." | |
| 136 ) | |
| 137 | |
| 138 ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA |
