我有类似这样结构的项目:
├── 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 ..
工件尚不存在时,因此变量Coverage_DATA
为空,这需要cmake ..
在运行测试后调用。这看起来很难看(对我来说)。
所以我的问题是:如何将测试覆盖工件添加到make clean
规则中?