83 lines
2.7 KiB
CMake
83 lines
2.7 KiB
CMake
set(PROJECT_NAME ${PROJECT_NAME} PARENT_SCOPE )
|
|
# GoogleTest requires at least C++14
|
|
set(CMAKE_CXX_STANDARD 14)
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
|
|
# Get GoogleTest
|
|
include(FetchContent)
|
|
FetchContent_Declare(
|
|
googletest
|
|
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
|
)
|
|
# For Windows: Prevent overriding the parent project's compiler/linker settings
|
|
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
|
FetchContent_MakeAvailable(googletest)
|
|
|
|
add_subdirectory(src)
|
|
add_executable(tests ${TESTS})
|
|
|
|
# Link test executable against gtest & gtest_main
|
|
target_link_libraries(
|
|
tests
|
|
GTest::gtest_main
|
|
)
|
|
|
|
target_include_directories(tests PRIVATE
|
|
${CMAKE_SOURCE_DIR}/src
|
|
)
|
|
|
|
# Discover tests
|
|
include(GoogleTest)
|
|
gtest_discover_tests(tests)
|
|
add_dependencies(tests ${PROJECT_NAME})
|
|
|
|
# The following section is inspired by https://github.com/cmake-modules/lcov
|
|
if(ENABLE_COVERAGE)
|
|
message("Test coverage enabled")
|
|
# set(exclude_dir "*/tests/* */_deps/* /usr/include/c++/11/**/* /usr/include/c++/**/*")
|
|
# set(exclude_dir "*/tests/* */_deps/* /usr/include/c++/11/tuple /usr/include/c++/11/**/*")
|
|
|
|
# Check for lcov, gcov and genhtml
|
|
find_program(GCOV gcov)
|
|
if (NOT GCOV)
|
|
message(WARNING "gcov not found")
|
|
endif()
|
|
find_program(LCOV lcov)
|
|
if (NOT LCOV)
|
|
message(WARNING "lcov not found")
|
|
endif()
|
|
find_program(GENHTML genhtml)
|
|
if (NOT GENHTML)
|
|
message(WARNING "genhtml not found")
|
|
endif()
|
|
|
|
if (GCOV AND LCOV AND GENHTML)
|
|
# Set C compiler flags
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
|
|
# Set C++ compiler flags
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
|
|
|
|
set(covname cov.info)
|
|
add_custom_target(coverage DEPENDS ${covname})
|
|
add_dependencies(coverage tests ${PROJECT_NAME})
|
|
add_custom_command(
|
|
OUTPUT ${covname}
|
|
COMMAND rm -rf **/coverage
|
|
COMMAND ${LCOV} -c -o ${covname} -d ${CMAKE_BINARY_DIR}/tests/CMakeFiles/tests.dir/ -b . --gcov-tool ${GCOV}
|
|
COMMAND ${LCOV} -r ${covname} -o ${covname} "*/tests/*" "*/_deps/**/*" "/usr/include/c++/**/*" "/usr/include/c++/11/**/*"
|
|
COMMAND ${LCOV} -l ${covname}
|
|
COMMAND ${GENHTML} ${covname} -output coverage
|
|
COMMAND ${LCOV} -l ${covname} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total
|
|
COMMAND rm -rf CMakeFiles/tests.dir/src/*.gcda CMakeFiles/tests.dir/src/*.gcno
|
|
COMMAND rm -f ${covname}
|
|
)
|
|
set_directory_properties(PROPERTIES
|
|
ADDITIONAL_CLEAN_FILES ${covname}
|
|
)
|
|
set_directory_properties(PROPERTIES
|
|
ADDITIONAL_CLEAN_FILES coverage/
|
|
)
|
|
else()
|
|
message(WARNING "Cannot enable coverage. Missing the required tools")
|
|
endif()
|
|
endif()
|