Skip to content
This repository has been archived by the owner on Aug 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #6 from frederikvannoote/develop
Browse files Browse the repository at this point in the history
Release v1.2
  • Loading branch information
frederikvannoote authored Feb 14, 2017
2 parents 2dd971f + 51765e6 commit a0424d8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 19 deletions.
62 changes: 43 additions & 19 deletions CodeCoverage.cmake
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)
23 changes: 23 additions & 0 deletions CommonConfig.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
cmake_minimum_required(VERSION 3.5.2 FATAL_ERROR)

#######################
# Prepare environment #
#######################

if (DEFINED ENV{CMAKE_MODULE_PATH})
set(_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
set(CMAKE_MODULE_PATH $ENV{CMAKE_MODULE_PATH} CACHE PATH "Set module path to $ENV{CMAKE_MODULE_PATH}" FORCE)
list(APPEND CMAKE_MODULE_PATH ${_CMAKE_MODULE_PATH})
endif (DEFINED ENV{CMAKE_MODULE_PATH})

if (DEFINED ENV{CMAKE_INSTALL_PREFIX})
set(CMAKE_INSTALL_PREFIX $ENV{CMAKE_INSTALL_PREFIX} CACHE PATH "Set install prefix to $ENV{CMAKE_INSTALL_PREFIX}" FORCE)
list(APPEND CMAKE_PREFIX_PATH $ENV{CMAKE_INSTALL_PREFIX})
endif (DEFINED ENV{CMAKE_INSTALL_PREFIX})

#################
# Build options #
#################
Expand Down Expand Up @@ -69,5 +84,13 @@ enable_testing()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
include(AddQtTest)

# Code analysis with cppcheck
# ---------------------------
include(Cppcheck)

# Code coverage with gcov
# -----------------------
include(CodeCoverage)

# Common compiler flags
include(CompilerFlags)
28 changes: 28 additions & 0 deletions FindLcov.cmake
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)

0 comments on commit a0424d8

Please sign in to comment.