Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH] cmake: add code coverage support
@ 2023-07-25 13:52 Sergey Bronnikov via Tarantool-patches
  2023-07-26 15:02 ` Sergey Kaplun via Tarantool-patches
  2023-08-08  8:16 ` Sergey Bronnikov via Tarantool-patches
  0 siblings, 2 replies; 10+ messages in thread
From: Sergey Bronnikov via Tarantool-patches @ 2023-07-25 13:52 UTC (permalink / raw)
  To: tarantool-patches, Sergey Kaplun, max.kokryashkin

From: Sergey Bronnikov <sergeyb@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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-08-14  7:46 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-25 13:52 [Tarantool-patches] [PATCH] cmake: add code coverage support Sergey Bronnikov via Tarantool-patches
2023-07-26 15:02 ` Sergey Kaplun via Tarantool-patches
2023-07-27  8:42   ` Sergey Bronnikov via Tarantool-patches
2023-07-30 12:55     ` Maxim Kokryashkin via Tarantool-patches
2023-07-30 13:34     ` Maxim Kokryashkin via Tarantool-patches
2023-08-01 15:25       ` Sergey Bronnikov via Tarantool-patches
2023-08-08  8:16 ` Sergey Bronnikov via Tarantool-patches
2023-08-08 10:47   ` Sergey Kaplun via Tarantool-patches
2023-08-08 19:25     ` Sergey Bronnikov via Tarantool-patches
2023-08-14  7:46       ` Maxim Kokryashkin via Tarantool-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox