ferencd@0: # ferencd@0: # 2012-01-31, Lars Bilke ferencd@0: # - Enable Code Coverage ferencd@0: # ferencd@0: # 2013-09-17, Joakim Söderberg ferencd@0: # - Added support for Clang. ferencd@0: # - Some additional usage instructions. ferencd@0: # ferencd@0: # USAGE: ferencd@0: # 1. Copy this file into your cmake modules path. ferencd@0: # ferencd@0: # 2. Add the following line to your CMakeLists.txt: ferencd@0: # INCLUDE(CodeCoverage) ferencd@0: # ferencd@0: # 3. Set compiler flags to turn off optimization and enable coverage: ferencd@0: # SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") ferencd@0: # SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") ferencd@0: # ferencd@0: # 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target ferencd@0: # which runs your test executable and produces a lcov code coverage report: ferencd@0: # Example: ferencd@0: # SETUP_TARGET_FOR_COVERAGE( ferencd@0: # my_coverage_target # Name for custom target. ferencd@0: # test_driver # Name of the test driver executable that runs the tests. ferencd@0: # # NOTE! This should always have a ZERO as exit code ferencd@0: # # otherwise the coverage generation will not complete. ferencd@0: # coverage # Name of output directory. ferencd@0: # ) ferencd@0: # ferencd@0: # 4. Build a Debug build: ferencd@0: # cmake -DCMAKE_BUILD_TYPE=Debug .. ferencd@0: # make ferencd@0: # make my_coverage_target ferencd@0: # ferencd@0: # ferencd@0: ferencd@0: # Check prereqs ferencd@0: FIND_PROGRAM( GCOV_PATH gcov ) ferencd@0: FIND_PROGRAM( LCOV_PATH lcov ) ferencd@0: FIND_PROGRAM( GENHTML_PATH genhtml ) ferencd@0: FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests) ferencd@0: ferencd@0: IF(NOT GCOV_PATH) ferencd@0: MESSAGE(FATAL_ERROR "gcov not found! Aborting...") ferencd@0: ENDIF() # NOT GCOV_PATH ferencd@0: ferencd@0: IF(NOT CMAKE_COMPILER_IS_GNUCXX) ferencd@0: # Clang version 3.0.0 and greater now supports gcov as well. ferencd@0: MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.") ferencd@0: ferencd@0: IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") ferencd@0: MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") ferencd@0: ENDIF() ferencd@0: ENDIF() # NOT CMAKE_COMPILER_IS_GNUCXX ferencd@0: ferencd@0: IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" ) ferencd@0: MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" ) ferencd@0: ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug" ferencd@0: ferencd@0: ferencd@0: # Param _targetname The name of new the custom make target ferencd@0: # Param _testrunner The name of the target which runs the tests. ferencd@0: # MUST return ZERO always, even on errors. ferencd@0: # If not, no coverage report will be created! ferencd@0: # Param _outputname lcov output is generated as _outputname.info ferencd@0: # HTML report is generated in _outputname/index.html ferencd@0: # Optional fourth parameter is passed as arguments to _testrunner ferencd@0: # Pass them in list form, e.g.: "-j;2" for -j 2 ferencd@0: FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname) ferencd@0: ferencd@0: IF(NOT LCOV_PATH) ferencd@0: MESSAGE(FATAL_ERROR "lcov not found! Aborting...") ferencd@0: ENDIF() # NOT LCOV_PATH ferencd@0: ferencd@0: IF(NOT GENHTML_PATH) ferencd@0: MESSAGE(FATAL_ERROR "genhtml not found! Aborting...") ferencd@0: ENDIF() # NOT GENHTML_PATH ferencd@0: ferencd@0: # Setup target ferencd@0: ADD_CUSTOM_TARGET(${_targetname} ferencd@0: ferencd@0: # Cleanup lcov ferencd@0: ${LCOV_PATH} --directory . --zerocounters ferencd@0: ferencd@0: # Run tests ferencd@0: COMMAND ${_testrunner} ${ARGV3} ferencd@0: ferencd@0: # Capturing lcov counters and generating report ferencd@0: COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info ferencd@0: COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned ferencd@0: COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned ferencd@0: COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned ferencd@0: ferencd@0: WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ferencd@0: COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." ferencd@0: ) ferencd@0: ferencd@0: # Show info where to find the report ferencd@0: ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD ferencd@0: COMMAND ; ferencd@0: COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report." ferencd@0: ) ferencd@0: ferencd@0: ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE ferencd@0: ferencd@0: # Param _targetname The name of new the custom make target ferencd@0: # Param _testrunner The name of the target which runs the tests ferencd@0: # Param _outputname cobertura output is generated as _outputname.xml ferencd@0: # Optional fourth parameter is passed as arguments to _testrunner ferencd@0: # Pass them in list form, e.g.: "-j;2" for -j 2 ferencd@0: FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname) ferencd@0: ferencd@0: IF(NOT PYTHON_EXECUTABLE) ferencd@0: MESSAGE(FATAL_ERROR "Python not found! Aborting...") ferencd@0: ENDIF() # NOT PYTHON_EXECUTABLE ferencd@0: ferencd@0: IF(NOT GCOVR_PATH) ferencd@0: MESSAGE(FATAL_ERROR "gcovr not found! Aborting...") ferencd@0: ENDIF() # NOT GCOVR_PATH ferencd@0: ferencd@0: ADD_CUSTOM_TARGET(${_targetname} ferencd@0: ferencd@0: # Run tests ferencd@0: ${_testrunner} ${ARGV3} ferencd@0: ferencd@0: # Running gcovr ferencd@0: COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml ferencd@0: WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ferencd@0: COMMENT "Running gcovr to produce Cobertura code coverage report." ferencd@0: ) ferencd@0: ferencd@0: # Show info where to find the report ferencd@0: ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD ferencd@0: COMMAND ; ferencd@0: COMMENT "Cobertura code coverage report saved in ${_outputname}.xml." ferencd@0: ) ferencd@0: ferencd@0: ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA