From 46d761b39b59f438e4301cfb3fd576ffed400cf5 Mon Sep 17 00:00:00 2001 From: Luca Guerra Date: Tue, 26 Mar 2024 15:48:06 +0000 Subject: [PATCH] fix(build): use ENABLE_COVERAGE for coverage, fix segfault Signed-off-by: Luca Guerra --- CMakeLists.txt | 1 + cmake/modules/CompilerFlags.cmake | 9 +++++---- test/libscap/CMakeLists.txt | 5 ----- userspace/libsinsp/test/CMakeLists.txt | 6 ------ 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48f10af32a..daa41d9378 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ option(BUILD_SHARED_LIBS "Build libscap and libsinsp as shared libraries" OFF) option(ENABLE_VM_TESTS "Enable driver sanity tests" OFF) option(USE_ASAN "Build with AddressSanitizer" OFF) option(USE_UBSAN "Build with UndefinedBehaviorSanitizer" OFF) +option(ENABLE_COVERAGE "Build with code coverage" OFF) option(UBSAN_HALT_ON_ERROR "Halt on error when building with UBSan" ON) if(${CMAKE_VERSION} VERSION_LESS "3.1.0" AND BUILD_SHARED_LIBS) diff --git a/cmake/modules/CompilerFlags.cmake b/cmake/modules/CompilerFlags.cmake index a67bbf36f7..5a5d956a42 100644 --- a/cmake/modules/CompilerFlags.cmake +++ b/cmake/modules/CompilerFlags.cmake @@ -32,7 +32,6 @@ if(NOT MSVC) set(FALCOSECURITY_LIBS_COMMON_FLAGS "-Wall -ggdb") set(FALCOSECURITY_LIBS_DEBUG_FLAGS "-Og -D_DEBUG") set(FALCOSECURITY_LIBS_RELEASE_FLAGS "-O3 -fno-strict-aliasing -DNDEBUG") - set(FALCOSECURITY_LIBS_COVERAGE_FLAGS "-O0 -fprofile-arcs -ftest-coverage") if(MINIMAL_BUILD) set(FALCOSECURITY_LIBS_COMMON_FLAGS "${FALCOSECURITY_LIBS_COMMON_FLAGS} -DMINIMAL_BUILD") @@ -68,6 +67,11 @@ if(NOT MSVC) endif() endif() + if(ENABLE_COVERAGE) + set(FALCOSECURITY_LIBS_USERSPACE_COMPILE_FLAGS "${FALCOSECURITY_LIBS_USERSPACE_COMPILE_FLAGS};--coverage") + set(FALCOSECURITY_LIBS_USERSPACE_LINK_FLAGS "${FALCOSECURITY_LIBS_USERSPACE_COMPILE_FLAGS};--coverage") + endif() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FALCOSECURITY_LIBS_COMMON_FLAGS}") # we need also `-std=c++17` here beacuse `set(CMAKE_CXX_STANDARD 17)` is not enough to enforce c++17 # with some Cmake versions: https://github.com/falcosecurity/libs/pull/950 @@ -79,9 +83,6 @@ if(NOT MSVC) set(CMAKE_C_FLAGS_RELEASE "${FALCOSECURITY_LIBS_RELEASE_FLAGS}") set(CMAKE_CXX_FLAGS_RELEASE "${FALCOSECURITY_LIBS_RELEASE_FLAGS}") - set(CMAKE_C_FLAGS_COVERAGE "${FALCOSECURITY_LIBS_COVERAGE_FLAGS}") - set(CMAKE_CXX_FLAGS_COVERAGE "${FALCOSECURITY_LIBS_COVERAGE_FLAGS}") - add_definitions(-D__STDC_FORMAT_MACROS) else() # MSVC diff --git a/test/libscap/CMakeLists.txt b/test/libscap/CMakeLists.txt index 0119ccbda2..e88603ebd0 100644 --- a/test/libscap/CMakeLists.txt +++ b/test/libscap/CMakeLists.txt @@ -48,11 +48,6 @@ set(LIBSCAP_TESTS_LIBRARIES scap ) -# Add `gcov` to needed libraries -if (CMAKE_BUILD_TYPE STREQUAL "Coverage") - list(APPEND LIBSCAP_TESTS_LIBRARIES gcov) -endif() - set(LIBSCAP_TESTS_DEPENDENCIES gtest scap diff --git a/userspace/libsinsp/test/CMakeLists.txt b/userspace/libsinsp/test/CMakeLists.txt index 85ef11a3ab..5ce91dfe7e 100644 --- a/userspace/libsinsp/test/CMakeLists.txt +++ b/userspace/libsinsp/test/CMakeLists.txt @@ -192,12 +192,6 @@ target_link_libraries(unit-test-libsinsp "${JSONCPP_LIB}" ) -if (CMAKE_BUILD_TYPE STREQUAL "Coverage") - target_link_libraries(unit-test-libsinsp - gcov - ) -endif() - # Add some additional include directories associated with `ADDITIONAL_SINSP_TESTS_SUITE` # `ADDITIONAL_SINSP_TESTS_INCLUDE_FOLDERS` is a list of include paths `;` separated if(ADDITIONAL_SINSP_TESTS_INCLUDE_FOLDERS)