From 561babaa74a7c8cc19a07fabeb06a2ac87fc877a Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Fri, 20 Mar 2020 15:38:29 +0100 Subject: [PATCH] build: add coverage information --- CMakeLists.txt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 040d1613..e32bb538 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ endif() option(LIBMBUS_BUILD_EXAMPLES "build examples" OFF) option(LIBMBUS_BUILD_TESTS "build tests" OFF) +option(LIBMBUS_ENABLE_COVERAGE "Build with coverage support" ON) +option(LIBMBUS_RUN_CLANG_TIDY "Use Clang-Tidy for static analysis" OFF) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -42,6 +44,35 @@ if(LIBMBUS_RUN_CLANG_TIDY) endif() endif(LIBMBUS_RUN_CLANG_TIDY) +if(LIBMBUS_ENABLE_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") + # https://discuss.circleci.com/t/problems-using-lcov-gcov-for-c-code/13898 + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + # using Clang + message(STATUS "Not doing coverage...") + elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + # using GCC + message(STATUS "Building with code coverage...") + set(CMAKE_BUILD_TYPE DEBUG) + set(CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} -fPIC -ggdb3 -O0 --coverage -fprofile-arcs -ftest-coverage" + ) + set(CMAKE_C_FLAGS + "${CMAKE_C_FLAGS} -fPIC -ggdb3 -O0 --coverage -fprofile-arcs -ftest-coverage " + ) + set(CMAKE_EXE_LINKER_FLAGS + "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage ") + set(CMAKE_SHARED_LINKER_FLAGS + "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage ") + link_libraries(-lgcov) + endif() +endif() + include(CheckIncludeFile) check_include_file(dlfcn.h HAVE_DLFCN_H)