[Tarantool-patches] [PATCH] cmake: add code coverage support
Sergey Bronnikov
estetus at gmail.com
Tue Jul 25 16:52:24 MSK 2023
From: Sergey Bronnikov <sergeyb at tarantool.org>
The patch adds building code coverage report using gcovr [1] and gcov.
gcovr is a better version of lcov, see [2]. CMake target LuaJIT-coverage
executes regression tests, proccess *.gcno and *.gcda files with gcov,
builds detailed HTML report and prints summary about code coverage.
```
$ cmake -S . -B build -DENABLE_COVERAGE=ON
$ cmake --build build --parallel
$ cmake --build build --target LuaJIT-coverage
<snipped>
lines: 84.1% (26056 out of 30997)
functions: 88.8% (2055 out of 2314)
branches: 71.5% (14801 out of 20703)
```
1. https://gcovr.com/
2. https://gcovr.com/en/stable/faq.html#what-is-the-difference-between-lcov-and-gcovr
---
CMakeLists.txt | 9 ++++++
test/CMakeLists.txt | 40 +++++++++++++++++++++++++++
test/tarantool-c-tests/CMakeLists.txt | 2 +-
3 files changed, 50 insertions(+), 1 deletion(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6ef24bba..8bc63b90 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -116,6 +116,15 @@ if(LUAJIT_ENABLE_WARNINGS)
)
endif()
+set(ENABLE_COVERAGE_DEFAULT OFF)
+option(ENABLE_COVERAGE "Enable integration with gcovr, a code coverage program" ${ENABLE_COVERAGE_DEFAULT})
+if (ENABLE_COVERAGE)
+ AppendFlags(CMAKE_C_FLAGS
+ -fprofile-arcs
+ -ftest-coverage
+ )
+endif()
+
# Auxiliary flags for main targets (libraries, binaries).
AppendFlags(TARGET_C_FLAGS
-D_FILE_OFFSET_BITS=64
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 47296a22..9b0f11d8 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -59,6 +59,44 @@ add_custom_target(${PROJECT_NAME}-test DEPENDS
tarantool-tests
)
+find_program(GCOVR gcovr)
+find_program(GCOV gcov)
+set(COVERAGE_HTML_REPORT "luajit-coverage.html")
+set(COVERAGE_JSON_REPORT "luajit-coverage.json")
+if(ENABLE_COVERAGE)
+ if(NOT GCOVR OR NOT GCOV)
+ add_custom_target(${PROJECT_NAME}-coverage)
+ add_custom_command(TARGET ${PROJECT_NAME}-coverage
+ COMMENT "Either `gcovr' or `gcov` not found, so ${PROJECT_NAME}-coverage target is dummy"
+ )
+ return()
+ endif()
+
+ add_custom_target(${PROJECT_NAME}-coverage)
+ add_custom_command(TARGET ${PROJECT_NAME}-coverage
+ COMMENT "Building coverage report"
+ COMMAND
+ ${GCOVR}
+ # See https://gcovr.com/en/stable/guide/configuration.html
+ --root ${PROJECT_SOURCE_DIR}
+ --filter ${PROJECT_SOURCE_DIR}/src
+ --print-summary
+ --output ${COVERAGE_HTML_REPORT}
+ --coveralls ${COVERAGE_JSON_REPORT}
+ --html
+ --html-title "LuaJIT Code Coverage Report"
+ --html-details
+ --sort-percentage
+ --branches
+ --decisions
+ --verbose
+ -j ${CMAKE_BUILD_PARALLEL_LEVEL}
+ WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+ )
+ message(STATUS "Code coverage HTML report: ${COVERAGE_HTML_REPORT}")
+ message(STATUS "Code coverage JSON report: ${COVERAGE_JSON_REPORT}")
+endif(ENABLE_COVERAGE)
+
if(LUAJIT_USE_TEST)
if(POLICY CMP0037)
if(CMAKE_VERSION VERSION_LESS 3.11)
@@ -76,4 +114,6 @@ if(LUAJIT_USE_TEST)
${PROJECT_NAME}-test
${PROJECT_NAME}-luacheck
)
+
+ add_dependencies(${PROJECT_NAME}-coverage ${PROJECT_NAME}-test)
endif()
diff --git a/test/tarantool-c-tests/CMakeLists.txt b/test/tarantool-c-tests/CMakeLists.txt
index 17255345..8cd76b44 100644
--- a/test/tarantool-c-tests/CMakeLists.txt
+++ b/test/tarantool-c-tests/CMakeLists.txt
@@ -45,7 +45,7 @@ foreach(test_source ${tests})
OUTPUT_NAME "${exe}${C_TEST_SUFFIX}"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
- target_link_libraries(${exe} libtest ${LUAJIT_LIBRARY})
+ target_link_libraries(${exe} libtest ${LUAJIT_LIBRARY} --coverage)
LIST(APPEND TESTS_COMPILED ${exe})
endforeach()
--
2.34.1
More information about the Tarantool-patches
mailing list