|
| 1 | +# Check prereqs |
| 2 | +FIND_PROGRAM(GCOV_PATH gcov) |
| 3 | +FIND_PROGRAM(LCOV_PATH lcov) |
| 4 | +FIND_PROGRAM(GENHTML_PATH genhtml) |
| 5 | + |
| 6 | +IF(NOT GCOV_PATH) |
| 7 | + MESSAGE(FATAL_ERROR "gcov not found! Aborting...") |
| 8 | +ENDIF() |
| 9 | + |
| 10 | +IF(NOT CMAKE_COMPILER_IS_GNUCC) |
| 11 | + # Clang version 3.0.0 and greater now supports gcov as well. |
| 12 | + MESSAGE(STATUS "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.") |
| 13 | + IF(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" AND NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang") |
| 14 | + MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") |
| 15 | + ENDIF() |
| 16 | +ENDIF() |
| 17 | + |
| 18 | +SET(COVERAGE_FLAGS "-g -O0 --coverage") |
| 19 | + |
| 20 | +FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname) |
| 21 | + |
| 22 | + IF(NOT LCOV_PATH) |
| 23 | + MESSAGE(FATAL_ERROR "lcov not found! Aborting...") |
| 24 | + ENDIF() |
| 25 | + |
| 26 | + IF(NOT GENHTML_PATH) |
| 27 | + MESSAGE(FATAL_ERROR "genhtml not found! Aborting...") |
| 28 | + ENDIF() |
| 29 | + |
| 30 | + # Setup target |
| 31 | + ADD_CUSTOM_TARGET(${_targetname} |
| 32 | + |
| 33 | + # Cleanup lcov |
| 34 | + ${LCOV_PATH} --directory . --zerocounters |
| 35 | + |
| 36 | + # Run tests |
| 37 | + COMMAND ${_testrunner} ${ARGV3} |
| 38 | + |
| 39 | + # Capturing lcov counters and generating report |
| 40 | + COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info --base-directory ${CMAKE_SOURCE_DIR} --no-external --quiet |
| 41 | + COMMAND ${LCOV_PATH} --remove ${_outputname}.info '*/test/*' '*/fuzz/*' --output-file ${_outputname}.info.cleaned --quiet |
| 42 | + COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned --prefix ${CMAKE_SOURCE_DIR} |
| 43 | + # COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned |
| 44 | + |
| 45 | + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
| 46 | + COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." |
| 47 | + ) |
| 48 | + |
| 49 | + # Show info where to find the report |
| 50 | + ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD |
| 51 | + COMMAND ; |
| 52 | + COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report." |
| 53 | + ) |
| 54 | + |
| 55 | +ENDFUNCTION() |
0 commit comments