CMake module to enable code coverage easily and generate coverage reports with CMake targets.
To use Findcodecov.cmake, simply add this repository as git submodule into your own repository
mkdir externals
git submodule add git://github.com/alehaa/cmake-codecov.git externals/cmake-codecov
and adding externals/cmake-codecov/cmake
to your CMAKE_MODULE_PATH
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/externals/cmake-codecov/cmake" ${CMAKE_MODULE_PATH})
If you don't use git or dislike submodules you can copy the Findcodecov.cmake, FindGcov.cmake and FindLcov.cmake files into your repository. Be careful when there are version updates of this repository!
For coverage evaluation you have to add coverage_evaluate()
after all other targets have been defined. A good place for this is your root CMakeLists.txt in the last lines after you included your sub-directories and added targets.
To enable coverage support in general, you have to enable ENABLE_COVERAGE
option in your CMake configuration. You can do this by passing -DENABLE_COVERAGE=On
on your command line or with your graphical interface.
If coverage is supported by your compiler, the specified targets will be build with coverage support. If your compiler has no coverage capabilities (I asume intel compiler doesn't) you'll get a warning but CMake will continue processing and coverage will simply just be ignored.
To enable coverage support you have two options: You can mark targets explictly for coverage by adding your target with add_coverage()
. This call must be done in the same directory as your add_executable()
or add_library()
call:
add_executable(some_exe foo.c bar.c)
add_coverage(some_exe)
add_library(some_lib foo.c bar.c)
add_coverage(some_lib)
The second option is to enable ENABLE_COVERAGE_ALL
option, which will enable coverage for all targets. You can do this by passing -ENABLE_COVERAGE_ALL=On
on your command line or with your graphical interface.
To be able to evaluate your coverage data, you have to run your application first. Some projects include CMake tests - it might me a good idea to execute them now by make test
, but you can run your application however you want (e.g. by running ./a.out
).
Gcov is a console program to evaluate the generated coverage data. You can evaluate the data by calling the following targets:
target | description |
---|---|
<TARGET>-gcov |
Evaluate coverage data for target <TARGET> . |
gcov |
Evaluate the coverage data of all your targets. Warning: You have to run programs generated by every target before you can call this target without any error. Otherwise you might get errors, if *.gcda files will not be found. |
The files generated by Gcov reside in the binary directory of the target <TARGET>
you're evaluating the coverage data for (e.g. for target bar
in this repository it'll be ${CMAKE_BINARY_DIR}/src/bar/CMakeFiles/bar.dir/
).
Lcov is a console program to evaluate the generate coverage data, but instead of writing the results into a copy of the original source file (like Gcov does) a HTML report will be generated, which is much easier to read than several gcov files.
target | description |
---|---|
<TARGET>-geninfo |
Evaluate coverage data for target <TARGET> . |
<TARGET>-genhtml |
Generate a report for a specific target (and only this one, even if it has dependencies!). This target will call <TARGET>-geninfo before. Reports will be generated in ${CMAKE_BINARY_DIR}/lcov/html/<TARGET> . |
lcov-geninfo |
Evaluate the coverage data of all your targets. |
lcov-genhtml |
Generate a single report for all evaluated data that is available now. Note: You have to call <TARGET>-geninfo for all targets you want to have in this report before calling this target or lcov-geninfo . You can use this option, if you like to have a single report for the targets foo and bar together, but without all the other targets. Reports will be generated in ${CMAKE_BINARY_DIR}/lcov/html/selected_targets . |
lcov |
Generate a single report for all targets. This target will call lcov-geninfo before. Reports will be generated in ${CMAKE_BINARY_DIR}/lcov/html/all_targets . |
Copyright (c) 2015 RWTH Aachen University, Federal Republic of Germany.
Written by Alexander Haase.