Commit 6aa58775 authored by Emmanuel Promayon's avatar Emmanuel Promayon
Browse files

Merge branch 'bug/activate-coverage' into develop

parents d7653d11 3780542b
......@@ -25,24 +25,15 @@ set(CAMITK_EXTENSION_PROJECT_SET_NAME "camitk-opensource" CACHE STRING "CEP Set
# Configure CamiTK Opensource packaging.
# Must be done before parsing extensions CMakeLists.txt files.
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/sdk/cmake/modules/macros)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/sdk/cmake/modules/macros ${CMAKE_CURRENT_SOURCE_DIR}/sdk/cmake/modules)
set(CAMITK_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
include(camitk/packaging/CamiTKOpenSourcePackaging)
camitk_opensource_packaging()
# When using gcc, enable test coverage
# using https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake
# requires lcov to be installed on the machine
if(CMAKE_COMPILER_IS_GNUCXX)
option(CAMITK_COVERAGE "Do you want to check test coverage?" OFF)
if (CAMITK_COVERAGE)
include(${CMAKE_CURRENT_SOURCE_DIR}/sdk/cmake/modules/CodeCoverage.cmake)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
endif()
endif()
# Configure CamiTK test coverage report generation (if and only if CAMITK_TEST_COVERAGE is set to TRUE)
include(camitk/test/CamiTKTestCoverage)
# CamiTK 4.0 is C++11 compliant
# From now on, CamiTK is C++11 compliant (this flag is needed for early support of C++11 in g++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Specific to SDK: first compile the SDK (it looks like a CEP but is sooooo special...)
......@@ -63,4 +54,5 @@ if(CAMITK_BINDING_PYTHON)
add_subdirectory(python_sdk)
endif()
camitk_sub_project_validate(CEP_SET)
......@@ -29,12 +29,7 @@ camitk_add_test(EXECUTABLE_ARGS "-error"
# should pass because reading test.lml should not modify it (input == output)
if(NOT WIN32) #this test fails on windows7 VM due to missing tweek for -debug
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/test.lml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/test.lml
PROJECT_NAME ${LMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/test.lml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/test.lml
PROJECT_NAME ${LMLTEST_BASENAME} TEST_SUFFIX "-")
endif()
if(CAMITK_COVERAGE)
setup_target_for_coverage(${LMLTEST_TARGET_NAME}-coverage ${LMLTEST_TARGET_NAME} ${LMLTEST_TARGET_NAME}-coverage "-i;${CMAKE_CURRENT_SOURCE_DIR}/test.lml" "-error")
#target_link_libraries(${LMLTEST_TARGET_NAME} gcov)
endif()
\ No newline at end of file
......@@ -33,56 +33,40 @@ camitk_add_test(EXECUTABLE_ARGS "-error"
# should pass because reading a file and writing it back should not modify it (input == output)
if(NOT WIN32)#this test fails on windows7 VM due to missing tweek for -debug
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube1.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube1.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube2.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube2.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube1.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube1.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube2.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube2.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube3.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube3.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube3.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube3.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube4.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube4.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/customProperty-example.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/customProperty-example.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/diaph.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/diaph.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/human-trunk.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/human-trunk.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/icosahedron.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/icosahedron.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/visage.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/visage.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/cube4.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cube4.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/customProperty-example.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/customProperty-example.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/diaph.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/diaph.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/human-trunk.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/human-trunk.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/icosahedron.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/icosahedron.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
camitk_add_test(EXECUTABLE_ARGS "-i ${CMAKE_CURRENT_SOURCE_DIR}/visage.pml"
PASS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/visage.pml
PROJECT_NAME ${PMLTEST_BASENAME} TEST_SUFFIX "-")
endif()
if(CAMITK_COVERAGE)
setup_target_for_coverage(${PMLTEST_TARGET_NAME}-coverage ${PMLTEST_TARGET_NAME} ${PMLTEST_TARGET_NAME}-coverage
""
"-error"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/cube1.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/cube2.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/cube3.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/cube4.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/customProperty-example.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/diaph.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/human-trunk.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/icosahedron.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/visage.pml"
"-i;${CMAKE_CURRENT_SOURCE_DIR}/cube1.pml;-stat")
endif()
\ No newline at end of file
#--------------------------------------------
# CamiTK Extension Project
# CamiTK SDK
#
# You should probably NOT modify this file...
# Please edit CEPDescription.cmake instead
......@@ -15,7 +15,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CAMITK_DIR}/share/${CAMITK_SHORT_VE
include(CamiTKConfig)
message(STATUS "Compilation build type: ${CMAKE_BUILD_TYPE}")
if (CAMITK_COVERAGE)
if (CAMITK_TEST_COVERAGE)
message(STATUS "Test coverage: enabled")
else()
message(STATUS "Test coverage: disabled")
......@@ -110,10 +110,3 @@ camitk_sub_project_validate()
# Validate CamiTKTestsLevel.txt file for wiki
camitk_parse_test_validate()
# test coverage
if(CAMITK_COVERAGE)
# -I [Start,End,Stride,test#,test#|Test file]
setup_target_for_coverage(ctest-coverage ctest ctest-coverage "--parallel;9") # quick test : only 50 by 50 "-I;1,,50") # not parallel ""
endif()
# Copyright (c) 2012 - 2015, Lars Bilke
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
#
# 2012-01-31, Lars Bilke
# - Enable Code Coverage
#
# 2013-09-17, Joakim Söderberg
# - Added support for Clang.
# - Some additional usage instructions.
#
# USAGE:
# 0. (Mac only) If you use Xcode 5.1 make sure to patch geninfo as described here:
# http://stackoverflow.com/a/22404544/80480
#
# 1. Copy this file into your cmake modules path.
#
# 2. Add the following line to your CMakeLists.txt:
# INCLUDE(CodeCoverage)
#
# 3. Set compiler flags to turn off optimization and enable coverage:
# SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
# SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
#
# 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
# which runs your test executable and produces a lcov code coverage report:
# Example:
# SETUP_TARGET_FOR_COVERAGE(
# my_coverage_target # Name for custom target.
# test_driver # Name of the test driver executable that runs the tests.
# # NOTE! This should always have a ZERO as exit code
# # otherwise the coverage generation will not complete.
# coverage # Name of output directory.
# )
#
# 4. Build a Debug build:
# cmake -DCMAKE_BUILD_TYPE=Debug ..
# make
# make my_coverage_target
#
#
# Check prereqs
FIND_PROGRAM( GCOV_PATH gcov )
FIND_PROGRAM( LCOV_PATH lcov )
FIND_PROGRAM( GENHTML_PATH genhtml )
FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
IF(NOT GCOV_PATH)
MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
ENDIF() # NOT GCOV_PATH
IF("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
IF("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
MESSAGE(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
ENDIF()
ELSEIF(NOT CMAKE_COMPILER_IS_GNUCXX)
MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
ENDIF() # CHECK VALID COMPILER
SET(CMAKE_CXX_FLAGS_COVERAGE
"-g -O0 --coverage -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
SET(CMAKE_C_FLAGS_COVERAGE
"-g -O0 --coverage -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C compiler during coverage builds."
FORCE )
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used for linking binaries during coverage builds."
FORCE )
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE )
MARK_AS_ADVANCED(
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
IF ( NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "Coverage"))
MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
# Param _targetname The name of new the custom make target
# Param _testrunner The name of the target which runs the tests.
# MUST return ZERO always, even on errors.
# If not, no coverage report will be created!
# Param _outputname lcov output is generated as _outputname.info
# HTML report is generated in _outputname/index.html
# Optional fourth parameter is passed as arguments to _testrunner
# Pass them in list form, e.g.: "-j;2" for -j 2
FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
IF(NOT LCOV_PATH)
MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
ENDIF() # NOT LCOV_PATH
IF(NOT GENHTML_PATH)
MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
ENDIF() # NOT GENHTML_PATH
SET(coverage_info "${CMAKE_BINARY_DIR}/${_outputname}.info")
SET(coverage_cleaned "${coverage_info}.cleaned")
SEPARATE_ARGUMENTS(test_command UNIX_COMMAND "${_testrunner}")
# COMMAND ${test_command}
# COMMAND ${test_command}
#set(list_var "${ARGN}")
# set(list_var "${ARGV3}")
# message("*************** ARGC=${ARGC}")
# set(argi "4")
# message("*************** ARGV4=${ARGV${argi}}")
# foreach(loop_var IN LISTS list_var)
# message("****************** ${loop_var}")
# endforeach()
# get varargs one by one
set(ARGI "3")
set(FULL_TEST_COMMAND "")
while(ARGI LESS ARGC)
# replace ";" by " " in ARGVi
set(TEST_COMMAND_ARGS "")
# foreach(OPT ${ARGV${ARGI}})
# set(TEST_COMMAND_ARGS "${TEST_COMMAND_ARGS} ${OPT}")
# endforeach()
list(LENGTH FULL_TEST_COMMAND COMMAND_COUNT)
if (COMMAND_COUNT GREATER 0)
set(FULL_TEST_COMMAND "${FULL_TEST_COMMAND}${CMAKE_CURRENT_BINARY_DIR}/${test_command};${ARGV${ARGI}}\;") #${TEST_COMMAND_ARGS}")
else()
set(FULL_TEST_COMMAND "${test_command};${ARGV${ARGI}}\;") #${TEST_COMMAND_ARGS}")
endif()
# next
math(EXPR ARGI "${ARGI}+1")
endwhile()
# message("================== ${test_command} ↑ ${_testrunner}")
# message("****************** ${FULL_TEST_COMMAND}")
# # find_program(TEST_COMMAND_PATH "${test_command}"
# PATHS ${CMAKE_BINARY_DIR})
# message("!!!!!!!!!!!!!!!!!! ${TEST_COMMAND_PATH} ${CMAKE_CURRENT_BINARY_DIR}")
# add this so that the return value is always 0
math(EXPR TEST_COUNT "${ARGC}-3")
set(FULL_TEST_COMMAND "${FULL_TEST_COMMAND}echo;Testing phase finished. ${TEST_COUNT} test done.")
# Setup target
ADD_CUSTOM_TARGET(${_targetname}
# Cleanup lcov
${LCOV_PATH} --directory . --zerocounters
# Run tests
COMMAND ${FULL_TEST_COMMAND}}
# Capturing lcov counters and generating report
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${coverage_info}
COMMAND ${LCOV_PATH} --remove ${coverage_info} 'tests/*' '/usr/*' --output-file ${coverage_cleaned}
COMMAND ${GENHTML_PATH} -o ${_outputname} ${coverage_cleaned}
COMMAND ${CMAKE_COMMAND} -E remove ${coverage_info} ${coverage_cleaned}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
)
# Show info where to find the report
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
COMMAND ;
COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
)
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
# Param _targetname The name of new the custom make target
# Param _testrunner The name of the target which runs the tests
# Param _outputname cobertura output is generated as _outputname.xml
# Optional fourth parameter is passed as arguments to _testrunner
# Pass them in list form, e.g.: "-j;2" for -j 2
FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
IF(NOT PYTHON_EXECUTABLE)
MESSAGE(FATAL_ERROR "Python not found! Aborting...")
ENDIF() # NOT PYTHON_EXECUTABLE
IF(NOT GCOVR_PATH)
MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
ENDIF() # NOT GCOVR_PATH
ADD_CUSTOM_TARGET(${_targetname}
# Run tests
${_testrunner} ${ARGV3}
# Running gcovr
COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Running gcovr to produce Cobertura code coverage report."
)
# Show info where to find the report
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
COMMAND ;
COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
)
ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA
# This is an additional CMake modules published on github by Lars Bilke.
# see https://github.com/bilke/cmake-modules for more scripts.
#
# TODO include this to debian/copyright:
# ---
# Files: sdk/cmake/modules/macros/CodeCoverage.cmake
# Copyright: © 2012 - 2017, Lars Bilke
# License: BSD-3-clause
# Comment: see original sources at https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake
# ---
#
# Copyright (c) 2012 - 2017, Lars Bilke
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# CHANGES:
#
# 2012-01-31, Lars Bilke
# - Enable Code Coverage
#
# 2013-09-17, Joakim Söderberg
# - Added support for Clang.
# - Some additional usage instructions.
#
# 2016-02-03, Lars Bilke
# - Refactored functions to use named parameters
#
# 2017-06-02, Lars Bilke
# - Merged with modified version from github.com/ufz/ogs
#
#
# USAGE:
#
# 1. Copy this file into your cmake modules path.
#
# 2. Add the following line to your CMakeLists.txt:
# include(CodeCoverage)
#
# 3. Append necessary compiler flags:
# APPEND_COVERAGE_COMPILER_FLAGS()
#
# 4. If you need to exclude additional directories from the report, specify them
# using the COVERAGE_EXCLUDES variable before calling SETUP_TARGET_FOR_COVERAGE.
# Example:
# set(COVERAGE_EXCLUDES 'dir1/*' 'dir2/*')
#
# 5. Use the functions described below to create a custom make target which
# runs your test executable and produces a code coverage report.
#
# 6. Build a Debug build:
# cmake -DCMAKE_BUILD_TYPE=Debug ..
# make
# make my_coverage_target
#
include(CMakeParseArguments)
# Check prereqs
find_program( GCOV_PATH gcov )
find_program( LCOV_PATH lcov )
find_program( GENHTML_PATH genhtml )
find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
find_program( SIMPLE_PYTHON_EXECUTABLE python )
if(NOT GCOV_PATH)
message(FATAL_ERROR "gcov not found! Aborting...")
endif() # NOT GCOV_PATH
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
endif()
elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
endif()
set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
CACHE INTERNAL "")
set(CMAKE_CXX_FLAGS_COVERAGE
${COVERAGE_COMPILER_FLAGS}
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
set(CMAKE_C_FLAGS_COVERAGE
${COVERAGE_COMPILER_FLAGS}
CACHE STRING "Flags used by the C compiler during coverage builds."
FORCE )
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used for linking binaries during coverage builds."
FORCE )
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE )
mark_as_advanced(
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
link_libraries(gcov)
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()
# Defines a target for running and collection code coverage information
# Builds dependencies, runs the given executable and outputs reports.
# NOTE! The executable should always have a ZERO as exit code otherwise
# the coverage generation will not complete.
#
# SETUP_TARGET_FOR_COVERAGE(
# NAME testrunner_coverage # New target name
# EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
# DEPENDENCIES testrunner # Dependencies to build first
# )
function(SETUP_TARGET_FOR_COVERAGE)
set(options NONE)
set(oneValueArgs NAME)
set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT LCOV_PATH)
message(FATAL_ERROR "lcov not found! Aborting...")
endif() # NOT LCOV_PATH
if(NOT GENHTML_PATH)
message(FATAL_ERROR "genhtml not found! Aborting...")
endif() # NOT GENHTML_PATH
# Setup target
add_custom_target(${Coverage_NAME}
# Cleanup lcov
COMMAND ${LCOV_PATH} --directory . --zerocounters
# Create baseline to make sure untouched files show up in the report
COMMAND ${LCOV_PATH} -c -i -d . -o ${Coverage_NAME}.base
# Run tests
COMMAND ${Coverage_EXECUTABLE}
# Capturing lcov counters and generating report
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${Coverage_NAME}.info
# add baseline counters
COMMAND ${LCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.info --output-file ${Coverage_NAME}.total
COMMAND ${LCOV_PATH} --remove ${Coverage_NAME}.total ${COVERAGE_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
COMMAND ${GENHTML_PATH} --legend -o ${Coverage_NAME} ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
COMMAND ${CMAKE_COMMAND} -E remove ${Coverage_NAME}.base ${Coverage_NAME}.info ${Coverage_NAME}.total ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
DEPENDS ${Coverage_DEPENDENCIES}
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
)
# Show info where to find the report
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
COMMAND ;
COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
)
endfunction() # SETUP_TARGET_FOR_COVERAGE
# Defines a target for running and collection code coverage information
# Builds dependencies, runs the given executable and outputs reports.
# NOTE! The executable should always have a ZERO as exit code otherwise
# the coverage generation will not complete.
#
# SETUP_TARGET_FOR_COVERAGE_COBERTURA(
# NAME ctest_coverage # New target name
# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
# DEPENDENCIES executable_target # Dependencies to build first
# )
function(SETUP_TARGET_FOR_COVERAGE_COBERTURA)
set(options NONE)
set(oneValueArgs NAME)
set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})