This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from frederikvannoote/develop
Release v1.2
- Loading branch information
Showing
3 changed files
with
94 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,66 @@ | ||
# Enable Code Coverage | ||
# | ||
# USAGE: | ||
# 1. Copy this file into your cmake modules path. | ||
# | ||
# 2. Add the following line to your CMakeLists.txt: | ||
# 1. Add the following line to your CMakeLists.txt: | ||
# INCLUDE(CodeCoverage) | ||
# | ||
# 3. Call the function ENABLE_CODE_COVERAGE | ||
# This functions sets compiler flags to turn off optimization and enable coverage: | ||
# SET(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -fprofile-arcs -ftest-coverage") | ||
# 2. Build a Debug build: | ||
# Add the following option: -DCODE_COVERAGE=True | ||
# | ||
# 4. Build a Debug build: | ||
# cmake -DCMAKE_BUILD_TYPE=Debug .. | ||
# make | ||
# make my_coverage_target | ||
# 3. Run the application(s) where you want to analyse the coverage | ||
# | ||
# Note: code coverage can only be enabled when in debug build! | ||
# 4. Generate the report by calling one of these targets: | ||
# - coverage-html (HTML format) | ||
# - coverage-xml (cobertura XML format) | ||
# | ||
# Requirements: gcov and gcovr | ||
|
||
# Check prereqs | ||
find_package(Gcov) | ||
find_package(Gcovr) | ||
|
||
function(ENABLE_CODE_COVERAGE) | ||
if (GCOV_FOUND AND GCOVR_FOUND) | ||
if(NOT CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") | ||
message(WARNING "Compiler is not GNU gcc or Clang! Code coverage disabled.") | ||
elseif( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" ) | ||
message(FATAL "Code coverage results with an optimized (non-Debug) build may be misleading. Code coverage will not run." ) | ||
else() | ||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -fprofile-arcs -ftest-coverage") | ||
if (CMAKE_COMPILER_IS_GNUCXX AND NOT UNIX) | ||
link_libraries(debug gcov) | ||
endif(WIN32 AND NOT UNIX) | ||
endif(CMAKE_COMPILER_IS_GNUCXX AND NOT UNIX) | ||
if (NOT WIN32) | ||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fPIC") | ||
endif (NOT WIN32) | ||
|
||
set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG} PARENT_SCOPE) | ||
|
||
message(STATUS "Code Coverage Enabled") | ||
else() | ||
message("-- Code Coverage NOT Enabled because gcov and/or gcovr was not found.") | ||
endif() | ||
endfunction() | ||
|
||
option(CODE_COVERAGE "Code coverage" OFF) | ||
if(CODE_COVERAGE) | ||
find_package(Gcov REQUIRED) | ||
find_package(Lcov REQUIRED) | ||
|
||
enable_code_coverage() | ||
|
||
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/coverage) | ||
|
||
add_custom_target("coverage-report") | ||
add_custom_command(TARGET "coverage-report" | ||
COMMAND ${LCOV_EXECUTABLE} --quiet --capture --directory . --base-directory ${CMAKE_SOURCE_DIR} --no-external -o coverage.info | ||
COMMAND ${LCOV_EXECUTABLE} --quiet --remove coverage.info \*moc_\* -o coverage.info | ||
COMMAND ${LCOV_EXECUTABLE} --quiet --remove coverage.info \*.moc -o coverage.info | ||
COMMAND ${LCOV_EXECUTABLE} --quiet --remove coverage.info \*tst_\* -o coverage.info | ||
COMMAND ${LCOV_EXECUTABLE} --list coverage.info | ||
COMMAND ${LCOV_EXECUTABLE} --summary coverage.info | ||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} | ||
COMMENT "Create code coverage report" | ||
VERBATIM) | ||
|
||
add_custom_target("coverage-html" DEPENDS "coverage-report") | ||
add_custom_command(TARGET "coverage-html" | ||
COMMAND ${GENHTML_EXECUTABLE} coverage.info --show-details -o coverage | ||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} | ||
COMMENT "Create code coverage html report" | ||
VERBATIM) | ||
|
||
endif(CODE_COVERAGE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Try to find lcov tool | ||
# | ||
# Cache Variables: | ||
# LCOV_EXECUTABLE | ||
# GENHTML_EXECUTABLE | ||
# | ||
# Non-cache variables you might use in your CMakeLists.txt: | ||
# LCOV_FOUND | ||
# | ||
# Requires these CMake modules: | ||
# FindPackageHandleStandardArgs (known included with CMake >=2.6.2) | ||
|
||
if(LCOV_EXECUTABLE AND NOT EXISTS "${LCOV_EXECUTABLE}") | ||
set(LCOV_EXECUTABLE "notfound" CACHE PATH FORCE "") | ||
endif() | ||
|
||
if(GENHTML_EXECUTABLE AND NOT EXISTS "${GENHTML_EXECUTABLE}") | ||
set(GENHTML_EXECUTABLE "notfound" CACHE PATH FORCE "") | ||
endif() | ||
|
||
find_program(LCOV_EXECUTABLE NAMES lcov) | ||
find_program(GENHTML_EXECUTABLE NAMES genhtml) | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args( Lcov DEFAULT_MSG | ||
LCOV_EXECUTABLE GENHTML_EXECUTABLE) | ||
|
||
mark_as_advanced(LCOV_EXECUTABLE GENHTML_EXECUTABLE) |