Detailed guide on using gcov with CMake/CDash?

前端 未结 4 2051
后悔当初
后悔当初 2020-12-02 07:50

I\'m using CMake with my project and set up a cdash server for continuous/nightly building. Everything works well and by setting up a crontab, we have hourly/nightly build/t

4条回答
  •  猫巷女王i
    2020-12-02 08:20

    This answer is the same as @rcomblen's answer but its a bit outdated so I'll share my solution. Here's what I did:

    1. created a toy project
    (base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ tree -L 2
    .
    ├── CMakeLists.txt
    ├── cmake
    │   └── CodeCoverage.cmake
    ├── src
    │   ├── domath.cpp
    │   ├── domath.h
    │   └── testdomath.cpp
    └── third_party
        └── googletest
    

    Where

    // src/domath.h
    
    #ifndef COVERAGETEST_DOMATH_H
    #define COVERAGETEST_DOMATH_H
    
    
    class domath {
    
    public:
        int add(int a, int b);
    };
    
    
    #endif //COVERAGETEST_DOMATH_H
    

    and

    // src/domath.cpp
    
    #include "domath.h"
    
    int domath::add(int a, int b) {
        return a + b;
    }
    

    and

    // src/testdomath.cpp
    #include "gtest/gtest.h"
    #include "domath.h"
    
    
    TEST(DoMathTests, testAdd){
        domath m;
        int actual = m.add(4, 6);
        ASSERT_EQ(10, actual);
    }
    
    1. download googletest and put it under third party directory
    2. copy the gist so kindly shared by the other answers on this thread into cmake/CodeCoverage.cmake
    3. install gcovr. This step is vital, because the other answers on this thread no longer work with the version of gcovr that I already had:
    (base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ pip install gcovr
    (base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ gcovr --version
    gcovr 4.2
    (base) ciaran@DESKTOP-K0APGUV:/mnt/d/CoverageTest$ which gcovr
    /home/ciaran/miniconda3/bin/gcovr
    

    Note, we need the output of which gcovr for the cmake script. 4) Write a cmake script that create a library, a test executable and use the CodeCoverage.cmake module:

    cmake_minimum_required(VERSION 3.15)
    project(CoverageTest)
    
    set(CMAKE_CXX_STANDARD 14)
    
    # setup googletest
    add_subdirectory(third_party/googletest)
    
    # create our library
    add_library(DoMath STATIC src/domath.h src/domath.cpp)
    add_dependencies(DoMath gtest gtest_main)
    
    # create the test executable
    add_executable(TestDoMath src/testdomath.cpp)
    target_include_directories(TestDoMath PRIVATE third_party/googletest/googletest)
    target_link_libraries(TestDoMath PRIVATE
            DoMath gtest gtest_main)
    add_dependencies(TestDoMath DoMath gtest gtest_main)
    
    # now for coverage bits
    set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
    if (CMAKE_COMPILER_IS_GNUCXX)
        include(CodeCoverage)
        append_coverage_compiler_flags()
    
        # we need to turn off optimization for non-skewed coverage reports
        set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -O0")
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
    
        # optional excludes - None needed here
        #    set(COVERAGE_EXCLUDES)
    
        # importantly, set the path to the gcovr executable that you downladed
        set(GCOVR_PATH "/home/ciaran/miniconda3/bin/gcovr")
        # Works
        setup_target_for_coverage_gcovr_xml(
                NAME TestDoMathCoverageXml
                EXECUTABLE TestDoMath
                DEPENDENCIES TestDoMath DoMath
        )
        # Works
        setup_target_for_coverage_gcovr_html(
                NAME TestDoMathCoverageHtml
                EXECUTABLE TestDoMath
                DEPENDENCIES TestDoMath DoMath
        )
        # This one did not work for me:
        setup_target_for_coverage_lcov(
                NAME TestDoMathCoverageLcov
                EXECUTABLE TestDoMath
                DEPENDENCIES TestDoMath DoMath
        )
    endif ()
    

    And thats it. Now just build the new targets.

    Good luck.

提交回复
热议问题