Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #301 from dnp3/coverage
Browse files Browse the repository at this point in the history
Add code coverage support
  • Loading branch information
jadamcrain authored Jun 7, 2019
2 parents 71f6e0f + 4d902c9 commit dce14c4
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 98 deletions.
17 changes: 11 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ version: 2
aliases:
- run: &install_deps_gcc
name: Install dependencies
command: apt-get update && apt-get install -y git cmake g++ libssl-dev #default-jdk maven
command: apt-get update && apt-get install -y git cmake g++ libssl-dev lcov curl #default-jdk maven
- run: &install_deps_clang
name: Install dependencies
command: apt-get update && apt-get install -y git cmake clang libc++-dev libc++abi-dev libssl-dev #default-jdk maven
command: apt-get update && apt-get install -y git cmake clang libc++-dev libc++abi-dev libssl-dev lcov curl #default-jdk maven
- run: &print_versions_gcc
name: Print versions
command: g++ -v && cmake -version && openssl version #&& java --version
Expand All @@ -23,6 +23,10 @@ aliases:
name: Test
command: ctest -VV .
working_directory: build
- run: &coverage
name: Code coverage
command: cmake --build . --target coverage && bash <(curl -s https://codecov.io/bash) -f coverage.info.cleaned
working_directory: build
- run: &build_java
# I was not able to set java.library.path to the build directory, so we just
# install the target to a system directory.
Expand Down Expand Up @@ -52,10 +56,11 @@ jobs:
#- run: *init_submodules
- run:
name: Configure
command: cmake -DCMAKE_BUILD_TYPE=Debug -DDNP3_EXAMPLES=ON -DDNP3_TESTS=ON -DDNP3_FUZZING=ON -DDNP3_TLS=ON ..
command: cmake -DCMAKE_BUILD_TYPE=Debug -DDNP3_EVERYTHING=ON -DDNP3_COVERAGE=ON ..
working_directory: build
- run: *build
- run: *test
- run: *coverage
#- run: *build_java
#- run: *test_java
#- run: *package
Expand All @@ -70,7 +75,7 @@ jobs:
#- run: *init_submodules
- run:
name: Configure
command: cmake -DCMAKE_BUILD_TYPE=Release -DDNP3_EXAMPLES=ON -DDNP3_TESTS=ON -DDNP3_FUZZING=ON -DDNP3_TLS=ON ..
command: cmake -DCMAKE_BUILD_TYPE=Release -DDNP3_EVERYTHING=ON ..
working_directory: build
- run: *build
- run: *test
Expand All @@ -88,7 +93,7 @@ jobs:
#- run: *init_submodules
- run:
name: Configure
command: cmake -DCMAKE_BUILD_TYPE=Debug -DDNP3_EXAMPLES=ON -DDNP3_TESTS=ON -DDNP3_FUZZING=ON -DDNP3_TLS=ON ..
command: cmake -DCMAKE_BUILD_TYPE=Debug -DDNP3_EVERYTHING=ON ..
working_directory: build
- run: *build
- run: *test
Expand All @@ -106,7 +111,7 @@ jobs:
#- run: *init_submodules
- run:
name: Configure
command: cmake -DCMAKE_BUILD_TYPE=Release -DDNP3_EXAMPLES=ON -DDNP3_TESTS=ON -DDNP3_FUZZING=ON -DDNP3_TLS=ON ..
command: cmake -DCMAKE_BUILD_TYPE=Release -DDNP3_EVERYTHING=ON ..
working_directory: build
- run: *build
- run: *test
Expand Down
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ option(DNP3_TLS "Build TLS client/server support (requires OpenSSL)" OFF)
option(DNP3_TESTS "Build unit and integration tests" OFF)
option(DNP3_EXAMPLES "Build example applications" OFF)
option(DNP3_FUZZING "Build Google OSS-Fuzz targets" OFF)
option(DNP3_COVERAGE "Enable code coverage target" OFF)
if(WIN32)
option(DNP3_DOTNET "Build the .NET bindings" OFF)
set(DNP3_DOTNET_FRAMEWORK_VERSION "v4.5" CACHE STRING "The target .NET framework version for the .NET components")
Expand Down Expand Up @@ -53,6 +54,12 @@ if(DNP3_TESTS)
include(./deps/catch.cmake)
endif()

# Set coverage flags if necessary
if(DNP3_COVERAGE)
include(./cmake/CodeCoverage.cmake)
add_coverage_flags()
endif()

# Library
add_subdirectory(./cpp/lib)

Expand All @@ -64,6 +71,14 @@ if(DNP3_TESTS)
add_subdirectory(./cpp/tests/unit)
add_subdirectory(./cpp/tests/asiotests)
add_subdirectory(./cpp/tests/integration)

if(DNP3_COVERAGE)
define_coverage_target(
NAME coverage
DIRECTORY cpp/lib
TARGETS unittests asiotests integrationtests
)
endif()
endif()

# Examples
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ sessions or huge device simulations. It also embeds with a small footprint on Li
Build status
============

| Branch | Ubuntu 18.04 | Windows |
| ------- | ------------ | ------- |
| master | [![CircleCI](https://circleci.com/gh/dnp3/opendnp3/tree/master.svg?style=svg)](https://circleci.com/gh/dnp3/opendnp3/tree/master) | [![AppVeyor](https://ci.appveyor.com/api/projects/status/971s9e53c9uoge4k/branch/master?svg=true)](https://ci.appveyor.com/project/dnp3/dnp3?branch=master) |
| develop | [![CircleCI](https://circleci.com/gh/dnp3/opendnp3/tree/develop.svg?style=svg)](https://circleci.com/gh/dnp3/opendnp3/tree/develop) | [![AppVeyor](https://ci.appveyor.com/api/projects/status/971s9e53c9uoge4k/branch/develop?svg=true)](https://ci.appveyor.com/project/dnp3/dnp3?branch=develop) |
| Branch | Ubuntu 18.04 | Windows | Code coverage |
| ------- | ------------ | ------- | ------------- |
| master | [![CircleCI](https://circleci.com/gh/dnp3/opendnp3/tree/master.svg?style=svg)](https://circleci.com/gh/dnp3/opendnp3/tree/master) | [![AppVeyor](https://ci.appveyor.com/api/projects/status/971s9e53c9uoge4k/branch/master?svg=true)](https://ci.appveyor.com/project/dnp3/dnp3?branch=master) | [![Codecov](https://codecov.io/gh/dnp3/opendnp3/branch/master/graph/badge.svg)](https://codecov.io/gh/dnp3/opendnp3/branch/master) |
| develop | [![CircleCI](https://circleci.com/gh/dnp3/opendnp3/tree/develop.svg?style=svg)](https://circleci.com/gh/dnp3/opendnp3/tree/develop) | [![AppVeyor](https://ci.appveyor.com/api/projects/status/971s9e53c9uoge4k/branch/develop?svg=true)](https://ci.appveyor.com/project/dnp3/dnp3?branch=develop) | [![Codecov](https://codecov.io/gh/dnp3/opendnp3/branch/develop/graph/badge.svg)](https://codecov.io/gh/dnp3/opendnp3/branch/develop) |


Documentation
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ install:
build_script:
- cmd: MKDIR build
- cmd: CD build
- cmd: cmake .. -DCMAKE_BUILD_TYPE=Release -DDNP3_TLS=ON -DDNP3_TESTS=ON -DDNP3_EXAMPLES=ON -DDNP3_FUZZING=ON -DDNP3_DOTNET=ON
- cmd: cmake .. -DCMAKE_BUILD_TYPE=Release -DNP3_EVERYTHING=ON -DDNP3_DOTNET=ON
- cmd: cmake --build . --config Release
after_build:
- ps: >-
Expand Down
170 changes: 170 additions & 0 deletions cmake/CodeCoverage.cmake
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()
24 changes: 0 additions & 24 deletions cmake/inc/msvc.cmake

This file was deleted.

53 changes: 0 additions & 53 deletions cmake/inc/posix.cmake

This file was deleted.

6 changes: 0 additions & 6 deletions cmake/settings.cmake

This file was deleted.

1 change: 0 additions & 1 deletion cpp/examples/decoder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* limitations under the License.
*/

#include <ser4cpp/container/Buffer.h>
#include <log4cpp/Logger.h>

#include <opendnp3/ConsoleLogger.h>
Expand Down
Loading

0 comments on commit dce14c4

Please sign in to comment.