This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
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 #301 from dnp3/coverage
Add code coverage support
- Loading branch information
Showing
10 changed files
with
204 additions
and
98 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
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
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,170 @@ | ||
# Copyright (c) 2012 - 2017, Lars Bilke | ||
# Copyright (c) 2019, Automatak, LLC | ||
# 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. | ||
|
||
include(CMakeParseArguments) | ||
|
||
# Check compiler support | ||
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") | ||
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3) | ||
message(FATAL_ERROR "For coverage support, clang version must be 3.0.0 or greater.") | ||
endif() | ||
elseif(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU") | ||
message(FATAL_ERROR "Compiler does not support gcov. Disable code coverage support.") | ||
endif() | ||
|
||
# Find dependencies | ||
find_program(GCOV_PATH gcov) | ||
find_program(LCOV_PATH NAMES lcov lcov.bat lcov.exe lcov.perl) | ||
find_program(GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat) | ||
|
||
if(NOT GCOV_PATH) | ||
message(FATAL_ERROR "gcov not found") | ||
endif() | ||
if(NOT LCOV_PATH) | ||
message(FATAL_ERROR "lcov not found") | ||
endif() | ||
if(NOT GENHTML_PATH) | ||
message(FATAL_ERROR "genhtml not found") | ||
endif() | ||
|
||
# Set cache variables for coverage | ||
set(COVERAGE_COMPILER_FLAGS "-g --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 ) | ||
|
||
# Call this function before registering any CMake target | ||
# that will affect the code coverage. | ||
function(add_coverage_flags) | ||
# Warning about non-debug build | ||
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
message(WARNING "Code coverage results with an optimised (non-debug) build may be misleading") | ||
endif() | ||
|
||
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") | ||
link_libraries(gcov) | ||
else() | ||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") | ||
endif() | ||
|
||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) | ||
message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}") | ||
endfunction() | ||
|
||
# Defines a target for running and collecting the 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. | ||
# | ||
# define_coverage_target( | ||
# NAME project_coverage # New target name | ||
# DIRECTORY cpp/lib # Directory to use | ||
# TARGETS unittest integrationtest # Targets to run | ||
# ) | ||
function(define_coverage_target) | ||
|
||
set(options NONE) | ||
set(oneValueArgs NAME DIRECTORY) | ||
set(multiValueArgs TARGETS) | ||
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) | ||
|
||
# Build list of targets | ||
set(run_commands) | ||
foreach(target ${Coverage_TARGETS}) | ||
if(NOT TARGET ${target}) | ||
message(FATAL_ERROR "${target} is not a target.") | ||
endif() | ||
|
||
list(APPEND run_commands COMMAND $<TARGET_FILE:${target}>) | ||
endforeach() | ||
|
||
# Setup target | ||
add_custom_target(${Coverage_NAME} | ||
|
||
# Cleanup lcov | ||
COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --directory ${Coverage_DIRECTORY} --zerocounters | ||
# Create baseline to make sure untouched files show up in the report | ||
COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --directory ${Coverage_DIRECTORY} --capture --initial --output-file ${Coverage_NAME}.base | ||
|
||
# Run tests | ||
${run_commands} | ||
|
||
# Capturing lcov counters and generating report | ||
COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --directory ${Coverage_DIRECTORY} --capture --output-file ${Coverage_NAME}.info | ||
# Add baseline counters | ||
COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.info --output-file ${Coverage_NAME}.total | ||
# Remove external stuff | ||
COMMAND ${LCOV_PATH} --gcov-tool ${GCOV_PATH} --extract ${Coverage_NAME}.total '*/${Coverage_DIRECTORY}/*' --output-file ${Coverage_NAME}.info.cleaned | ||
# Generate HTML report | ||
COMMAND ${GENHTML_PATH} -o ${Coverage_NAME} ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned | ||
|
||
WORKING_DIRECTORY ${PROJECT_BINARY_DIR} | ||
DEPENDS ${Coverage_TARGETS} | ||
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." | ||
) | ||
|
||
# Show where to find the lcov info report | ||
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD | ||
COMMAND ; | ||
COMMENT "lcov code coverage info report saved in ${Coverage_NAME}.info." | ||
) | ||
|
||
# Show info where to find the HTML 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() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.