File: detect-coverage.cmake

package info (click to toggle)
node-yarnpkg 4.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,752 kB
  • sloc: javascript: 38,953; ansic: 26,035; cpp: 7,247; sh: 2,829; makefile: 724; perl: 493
file content (46 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download | duplicates (4)
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
# detect-coverage.cmake -- Detect supported compiler coverage flags
# Licensed under the Zlib license, see LICENSE.md for details

macro(add_code_coverage)
    # Check for -coverage flag support for Clang/GCC
    if(CMAKE_VERSION VERSION_LESS 3.14)
        set(CMAKE_REQUIRED_LIBRARIES -lgcov)
    else()
        set(CMAKE_REQUIRED_LINK_OPTIONS -coverage)
    endif()
    check_c_compiler_flag(-coverage HAVE_COVERAGE)
    set(CMAKE_REQUIRED_LIBRARIES)
    set(CMAKE_REQUIRED_LINK_OPTIONS)

    if(HAVE_COVERAGE)
        add_compile_options(-coverage)
        add_link_options(-coverage)
        message(STATUS "Code coverage enabled using: -coverage")
    else()
        # Some versions of GCC don't support -coverage shorthand
        if(CMAKE_VERSION VERSION_LESS 3.14)
            set(CMAKE_REQUIRED_LIBRARIES -lgcov)
        else()
            set(CMAKE_REQUIRED_LINK_OPTIONS -lgcov -fprofile-arcs)
        endif()
        check_c_compiler_flag("-ftest-coverage -fprofile-arcs -fprofile-values" HAVE_TEST_COVERAGE)
        set(CMAKE_REQUIRED_LIBRARIES)
        set(CMAKE_REQUIRED_LINK_OPTIONS)

        if(HAVE_TEST_COVERAGE)
            add_compile_options(-ftest-coverage -fprofile-arcs -fprofile-values)
            add_link_options(-lgcov -fprofile-arcs)
            message(STATUS "Code coverage enabled using: -ftest-coverage")
        else()
            message(WARNING "Compiler does not support code coverage")
            set(WITH_CODE_COVERAGE OFF)
        endif()
    endif()

    # Set optimization level to zero for code coverage builds
    if (WITH_CODE_COVERAGE)
        # Use CMake compiler flag variables due to add_compile_options failure on Windows GCC
        set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS}")
        set(CMAKE_CXX_FLAGS "-O0 ${CMAKE_CXX_FLAGS}")
    endif()
endmacro()