Skip to content

Commit 19df331

Browse files
committed
add coverage target in cmake
1 parent b530b7f commit 19df331

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ STRING (REGEX MATCH "#define MSGPACK_VERSION_REVISION *([0-9a-zA-Z_]*)" NULL_OUT
1616
SET (VERSION_REVISION ${CMAKE_MATCH_1})
1717
SET (VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION})
1818

19+
LIST (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
1920
SET (prefix ${CMAKE_INSTALL_PREFIX})
2021
SET (exec_prefix "\${prefix}")
2122
SET (libdir "\${exec_prefix}/lib")
@@ -152,6 +153,7 @@ FIND_PACKAGE (ZLIB)
152153
FIND_PACKAGE (Threads)
153154
IF (GTEST_FOUND AND ZLIB_FOUND AND THREADS_FOUND AND NOT "${MSGPACK_FUZZ_REGRESSION}" STREQUAL "ON")
154155
OPTION (MSGPACK_BUILD_TESTS "Build msgpack tests." ON)
156+
OPTION (MSGPACK_GEN_COVERAGE "Enable running gcov to get a test coverage report." OFF)
155157
ENDIF ()
156158

157159
IF (DEFINED BUILD_SHARED_LIBS)
@@ -277,6 +279,21 @@ IF ("${MSGPACK_FUZZ_REGRESSION}" STREQUAL "ON" AND "${CMAKE_CXX_COMPILER_ID}" ST
277279
SET (MSGPACK_BUILD_EXAMPLES OFF)
278280
ENDIF ()
279281

282+
IF (MSGPACK_GEN_COVERAGE)
283+
IF (NOT MSGPACK_BUILD_TESTS)
284+
MESSAGE(FATAL_ERROR "Coverage requires -DMSGPACK_BUILD_TESTS=ON")
285+
ENDIF ()
286+
STRING(TOUPPER "${CMAKE_BUILD_TYPE}" UPPER_CMAKE_BUILD_TYPE)
287+
IF (NOT "${UPPER_CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
288+
MESSAGE(FATAL_ERROR "Coverage requires -DCMAKE_BUILD_TYPE=Debug")
289+
ENDIF ()
290+
291+
INCLUDE(CodeCoverage)
292+
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_FLAGS}")
293+
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_FLAGS}")
294+
295+
SETUP_TARGET_FOR_COVERAGE(coverage make coverage test)
296+
ENDIF ()
280297

281298
IF (MSGPACK_BUILD_TESTS)
282299
ENABLE_TESTING ()

cmake/CodeCoverage.cmake

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Check prereqs
2+
FIND_PROGRAM(GCOV_PATH gcov)
3+
FIND_PROGRAM(LCOV_PATH lcov)
4+
FIND_PROGRAM(GENHTML_PATH genhtml)
5+
6+
IF(NOT GCOV_PATH)
7+
MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
8+
ENDIF()
9+
10+
IF(NOT CMAKE_COMPILER_IS_GNUCC)
11+
# Clang version 3.0.0 and greater now supports gcov as well.
12+
MESSAGE(STATUS "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
13+
IF(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" AND NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
14+
MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
15+
ENDIF()
16+
ENDIF()
17+
18+
SET(COVERAGE_FLAGS "-g -O0 --coverage")
19+
20+
FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
21+
22+
IF(NOT LCOV_PATH)
23+
MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
24+
ENDIF()
25+
26+
IF(NOT GENHTML_PATH)
27+
MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
28+
ENDIF()
29+
30+
# Setup target
31+
ADD_CUSTOM_TARGET(${_targetname}
32+
33+
# Cleanup lcov
34+
${LCOV_PATH} --directory . --zerocounters
35+
36+
# Run tests
37+
COMMAND ${_testrunner} ${ARGV3}
38+
39+
# Capturing lcov counters and generating report
40+
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info --base-directory ${CMAKE_SOURCE_DIR} --no-external --quiet
41+
COMMAND ${LCOV_PATH} --remove ${_outputname}.info '*/test/*' '*/fuzz/*' --output-file ${_outputname}.info.cleaned --quiet
42+
COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned --prefix ${CMAKE_SOURCE_DIR}
43+
# COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
44+
45+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
46+
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
47+
)
48+
49+
# Show info where to find the report
50+
ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
51+
COMMAND ;
52+
COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
53+
)
54+
55+
ENDFUNCTION()

0 commit comments

Comments
 (0)