使用CMake将* .gcno和* .gcda文件添加到make clean规则中

时间:2014-09-21 20:33:45

标签: makefile cmake lcov test-coverage

我的项目结构与此类似:

├── CMakeLists.txt
├── src
│   ├── logic.cpp
│   └── logic.h
└── test
    ├── CMakeLists.txt
    └── logic_test.cpp

CMakeLists.txt文件是:

cmake_minimum_required (VERSION 2.8)
project (Logic)
set (Logic_SOURCES ${PROJECT_SOURCE_DIR}/src/logic.cpp)
include_directories (${PROJECT_SOURCE_DIR}/src)
add_library (logic SHARED ${Logic_SOURCES})
add_subdirectory (test)

CMakeLists.txt用于测试:

find_package (GTest)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -march=native -mtune=native -fprofile-arcs -ftest-coverage")
set (CMAKE_EXE_LINKER_FLAGS "-fprofile-arcs -ftest-coverage")
set (Test_SOURCES ${Logic_SOURCES} ${PROJECT_SOURCE_DIR}/test/logic_test.cpp)
add_executable (logic_test ${Test_SOURCES})
target_link_libraries (${TestName} gtest gtest_main gcov pthread)

为了处理测试覆盖率报告,我已将自定义目标添加到test/CMakeLists.txt

set (Coverage_REPORT ${PROJECT_BINARY_DIR}/coverage.info)
set (Coverage_DIR    ${PROJECT_BINARY_DIR}/coverage)
add_custom_command (
    OUTPUT  ${Coverage_REPORT}
    COMMAND lcov -q -c -f -b . -d ${PROJECT_BINARY_DIR}/test -o ${Coverage_REPORT}
    COMMAND lcov -e ${Coverage_REPORT} '${PROJECT_SOURCE_DIR}/src/*' -o ${Coverage_REPORT}
    COMMAND genhtml ${Coverage_REPORT} --legend --demangle-cpp -f -q -o ${Coverage_DIR}
    DEPENDS logic_test
)
add_custom_target (coverage DEPENDS ${Coverage_REPORT})

所有这些代码都正常并且符合预期。工作流程如下所示:

mkdir build
cd build
cmake ..
make
./test/logictest
make coverage

但是现在我想在make clean规则中添加测试覆盖工件。我尝试将此代码添加到test/CMakeLists.txt

file (GLOB_RECURSE Test_GCNOS ${PROJECT_BINARY_DIR}/*.gcno)
file (GLOB_RECURSE Test_GCDAS ${PROJECT_BINARY_DIR}/*.gcda)
list (APPEND Test_COVERAGE_DATA "${Coverage_REPORT}")
list (APPEND Test_COVERAGE_DATA "${Coverage_DIR}")
list (APPEND Test_COVERAGE_DATA "${Coverage_GCNO}")
list (APPEND Test_COVERAGE_DATA "${Coverage_GCDA}")
set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${Test_COVERAGE_DATA}")

但是这种方法不能按预期工作(对我而言)。当调用cmake .. artifatcs尚不存在时,变量Coverage_DATA为空,这需要在运行测试后调用cmake ..。这看起来很难看(对我而言)。

所以我的问题是:如何将测试覆盖工件添加到make clean规则?

2 个答案:

答案 0 :(得分:2)

我编写了一个代码覆盖率cmake脚本,如果您正在进行一个开源项目,它可以与http://coveralls.io/一起使用:

https://github.com/JoakimSoderberg/coveralls-cmake https://github.com/JoakimSoderberg/coveralls-cmake-example

但是,如果您想要一个只在本地生成coverage数据的脚本,那么就有这个项目:

https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake

我解决了清理覆盖率数据的问题,总是先在我的覆盖目标中删除它,然后再生成新的覆盖率数据。它不是一个干净的解决方案,但工作正常:

file(REMOVE_RECURSE ${PROJECT_BINARY_DIR}/*.gcda)

(我几乎没有使用make clean,而是使用多个构建目录和偶尔使用rm -rf *。在我看来,make clean的整个概念都被打破了,将构建和源代码分开使得事情更加清晰)

答案 1 :(得分:1)

它无法被称为优雅的解决方案,但在我看来它比cmake召回更好。 我尝试过创建宏来确定源文件列表中的coverage文件(*.gcno*.gcda文件的名称):

macro (determine_coverage_data Sources TestName Artifacts Suffix)
set (CoverageDirectory "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${TestName}.dir")
foreach (File ${Sources})
    string (REGEX MATCH "^${CMAKE_CURRENT_SOURCE_DIR}*" Directory "${File}")
    if    (Directory STREQUAL CMAKE_CURRENT_SOURCE_DIR)
        string (REGEX REPLACE "^${CMAKE_CURRENT_SOURCE_DIR}*" "${CoverageDirectory}" File "${File}")
    else  (Directory STREQUAL CMAKE_CURRENT_SOURCE_DIR)
        string (REGEX REPLACE "/" ";" A "${CMAKE_CURRENT_SOURCE_DIR}")
        string (REGEX REPLACE "/" ";" B "${File}")
        list (LENGTH A DeepDirectory)
        list (LENGTH B DeepFile)
        set (File "${CoverageDirectory}")
        set (I 1)
        while    (I LESS DeepDirectory)
            list (GET A ${I} AI)
            list (GET B ${I} BI)
            if    (AI STREQUAL BI)
                math (EXPR I "${I} + 1")
            else  (AI STREQUAL BI)
                math (EXPR DeepDiff "${DeepFile} - ${I} - 1")
                while    (DeepDiff GREATER 0)
                    set (File "${File}/__")
                    math (EXPR DeepDiff "${DeepDiff} - 1")
                endwhile (DeepDiff GREATER 0)
                while    (I LESS DeepFile)
                    list (GET B ${I} BI)
                    set (File "${File}/${BI}")
                    math (EXPR I "${I} + 1")
                endwhile (I LESS DeepFile)
            endif (AI STREQUAL BI)
        endwhile (I LESS DeepDirectory)
    endif (Directory STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    set (${Artifacts} ${${Artifacts}} "${File}${Suffix}")
endforeach (File)
endmacro (determine_coverage_data)

所以完整的解决方案如下:

├── cmake
│   └── UseGCov.cmake
├── CMakeLists.txt
├── src
│   ├── logic.cpp
│   └── logic.h
└── test
    ├── CMakeLists.txt
    └── logic_test.cpp

CMakeLists.txt文件是:

cmake_minimum_required (VERSION 2.8)
project (Logic)
set (Logic_SOURCES ${PROJECT_SOURCE_DIR}/src/logic.cpp)
# Include macro.
include ("${PROJECT_SOURCE_DIR}/cmake/UseGCov.cmake")
include_directories (${PROJECT_SOURCE_DIR}/src)
add_library (logic SHARED ${Logic_SOURCES})
add_subdirectory (test)

CMakeLists.txt用于测试:

find_package (GTest)

set (Test_SOURCES ${Logic_SOURCES} ${PROJECT_SOURCE_DIR}/test/logic_test.cpp)
set (TestName logic_test)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -march=native -mtune=native -fprofile-arcs -ftest-coverage")
set (CMAKE_EXE_LINKER_FLAGS "-fprofile-arcs -ftest-coverage")

# Call macro.
determine_coverage_data ("${Test_SOURCES}" "${TestName}" Test_GCNOS ".gcno")
determine_coverage_data ("${Test_SOURCES}" "${TestName}" Test_GCDAS ".gcda")

set (Coverage_REPORT ${PROJECT_BINARY_DIR}/coverage.info)
set (Coverage_DIR    ${PROJECT_BINARY_DIR}/coverage)
add_custom_command (
    OUTPUT  ${Coverage_REPORT}
    COMMAND lcov -q -c -f -b . -d ${PROJECT_BINARY_DIR}/test -o ${Coverage_REPORT}
    COMMAND lcov -e ${Coverage_REPORT} '${PROJECT_SOURCE_DIR}/src/*' -o ${Coverage_REPORT}
    COMMAND genhtml ${Coverage_REPORT} --legend --demangle-cpp -f -q -o ${Coverage_DIR}
    DEPENDS logic_test
)
add_custom_target (coverage DEPENDS ${Coverage_REPORT})

set (Test_COVERAGE_DATA ${Coverage_REPORT} ${Coverage_DIR} ${Test_GCNOS} ${Test_GCDAS})
set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${Test_COVERAGE_DATA}")
add_executable (${TestName} ${Test_SOURCES})
target_link_libraries (${TestName} gtest gtest_main gcov pthread)