File: CodeCoverage.cmake

package info (click to toggle)
therion 6.3.4-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,544 kB
  • sloc: cpp: 195,273; tcl: 19,779; ansic: 8,434; perl: 1,895; makefile: 1,281; python: 255; asm: 219; sh: 104
file content (48 lines) | stat: -rw-r--r-- 1,718 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#
# Generate code coverage report for unit tests.
#
# How to use:
#   1. Use Clang compiler.
#   2. Set already configured build folder as a working directory.
#   3. Enable code coverage: cmake -DENABLE_CODE_COVERAGE=ON .
#   4. Build the project:    cmake --build .
#   5. Run unit tests:       cmake --build . --target test
#   6. Generate report:      cmake --build . --target ccov-report
#   7. View HTML report at ./coverage/html/index.html.
#

# interface library for setting compiler and linker flags
add_library(code-coverage INTERFACE)

if (ENABLE_CODE_COVERAGE)
    if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        message(FATAL_ERROR "Code coverage requires Clang compiler.")
    endif()

    set(COVERAGE_FLAGS -fprofile-instr-generate -fcoverage-mapping)
    target_compile_options(code-coverage INTERFACE ${COVERAGE_FLAGS})
    target_link_options(code-coverage INTERFACE ${COVERAGE_FLAGS})
endif()

set(COVERAGE_FOLDER ${CMAKE_BINARY_DIR}/coverage)

add_custom_target(ccov-report
    # process raw coverage profile
    COMMAND llvm-profdata merge -sparse ${COVERAGE_FOLDER}/utest.profraw -o ${COVERAGE_FOLDER}/utest.profdata
    # generate HTML report
    COMMAND llvm-cov show 
        $<TARGET_FILE:utest>
        -instr-profile=${COVERAGE_FOLDER}/utest.profdata
        -output-dir=${COVERAGE_FOLDER}/html
        -format=html
        -show-line-counts-or-regions
        -show-expansions
        -Xdemangler=c++filt
    # Generate console summary
    COMMAND llvm-cov report 
        $<TARGET_FILE:utest> 
        -instr-profile=${COVERAGE_FOLDER}/utest.profdata
        -show-region-summary=false
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMENT "Generating coverage report for utest"
)