如何在TravisCI中为C++ w/CMake项目正确配置CodeCov?
我正试图在我的GitHub仓库上配置CodeCov和TravisCI。由于我的回购是在C++中,我使用的CMake了,我基本上是复制粘贴的this example的after_success
标签到我.travis.yml
文件:如何在TravisCI中为C++ w/CMake项目正确配置CodeCov?
after_success:
# Creating report
- cd ${TRAVIS_BUILD_DIR}
- lcov --directory . --capture --output-file coverage.info coverage info
- lcov --remove coverage.info '/usr/*' --output-file coverage.info
- lcov --list coverage.info
# Uploading report to CodeCov
- bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
我推定制.codecov.yml
文件提供了USB Key和通知设置,但after_success
只是该示例中建议的一个。我真的不熟悉lcov
,所以我不知道是否有其他东西丢失或这些电话是用于什么。
看着CodeCov的文档我找不到什么我可能会丢失,但我的覆盖报告是空的,因为lcov
无法找到某种神秘.gcda
文件(我猜)。 CodeCov的文档没有提到任何地方,所以我想我错过了我的配置中的重要一步(或者由于某些原因可能找不到文件...?)
这是TravisCI的输出after_success
阶段:
$ lcov --directory . --capture --output-file coverage.info
Capturing coverage data from .
Found gcov version: 4.8.4
Scanning . for .gcda files ...
geninfo: ERROR: no .gcda files found in .!
$ lcov --remove coverage.info '/usr/*' --output-file coverage.info
Reading tracefile coverage.info
lcov: ERROR: no valid records found in tracefile coverage.info
$ lcov --list coverage.info
Reading tracefile coverage.info
lcov: ERROR: no valid records found in tracefile coverage.info
$ bash <(curl -s https://codecov.io/bash) || \
echo "Codecov did not collect coverage reports"
_____ _
/____| | |
| | ___ __| | ___ ___ _____ __
| | /_ \/_` |/ _ \/ __/ _ \ \//
| |___| (_) | (_| | __/ (_| (_) \ V/
\_____\___/ \__,_|\___|\___\___/ \_/
Bash-8fda091
==> Travis CI detected.
project root: .
--> token set from env
...
==> Running gcov in . (disable via -X gcov)
==> Python coveragepy not found
==> Searching for coverage reports in:
+ .
-> Found 3 reports
==> Detecting git/mercurial file structure
==> Appending build variables
+ TRAVIS_OS_NAME
==> Reading reports
- Skipping empty file ./coverage.info
+ ./doc/****.zip bytes=337165 -----> That's not a report.
+ ./doc/****.pdf bytes=353614 -----> That's not a report.
==> Appending adjustments
http://docs.codecov.io/docs/fixing-reports
+ Found adjustments
==> Uploading reports
url: https://codecov.io
query: branch=codecov&commit=*****...
-> Pinging Codecov
-> Uploading to S3 https://codecov.s3.amazonaws.com
-> View reports at https://codecov.io/github/********
问题在于Cmake编译器和链接器标志。为了让GCov(GCC的一部分,并由lcov
使用)提供分析信息和覆盖测试,必须设置一些标志。
从Code Coverage for C Unit Tests:
具体来说,需要编译器添加分析信息,以目标代码,它是通过将标志
-fprofile-arcs
和-ftest-coverage
到你的编译命令来完成。第一个标志将逻辑添加到目标代码以生成通用分析信息。这是关于每个基本块被执行的频率的信息。当你的程序运行时,所有这些信息将被保存到一个新的文件中,扩展名为.da,位于.o文件旁边。该文件中的数据不是覆盖范围特定的,但它将被gcov使用。
您传递给GCC的第二个标志
-ftest-coverage
也将为您的对象文件添加逻辑。这一次,目标是输出特定于覆盖范围的信息。有两个文件将被生成,一个.bb和一个.bbg。 .bb文件是从基本块到行号的简单映射文件。 .bbg文件列出了执行应用程序时运行的相应源文件中的每个弧。这个数据被gcov用来重建实际的程序流程图,从中可以计算出所有的基本块和弧的执行计数。
此外,来源需要与-lgcov --coverage
连接。对我来说,因为我使用cmake我需要与set_target_properties
功能指定这些:
add_executable(dss-sim dss-sim.cpp)
target_link_libraries(dss-sim
list
of
my
static
libs
)
# The libs above also need to be compiled with the same flags.
set_target_properties(dss-sim
PROPERTIES
COMPILE_FLAGS "-ftest-coverage -fprofile-arcs"
LINK_FLAGS "-lgcov --coverage"
)
最后,因为你通常不希望在您的覆盖率报告你的单元测试,你会不为它们定义编译器标志。但是,请注意,如果您将单元测试与使用gcov选项编译的库链接,则仍然需要添加链接器标志。
GCC的较新版本可以使用 - 覆盖一切。请参阅https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html。 –