[英]How to properly configure CodeCov for a C++ w/ CMake project in TravisCI?
我试图使用TravisCI在GitHub存储库上配置CodeCov。 由于我的存储库是C ++,并且已经使用了CMake,因此我实际上将本示例的after_success
标签复制粘贴到了.travis.yml
文件中:
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
文件以提供令牌和通知设置,但是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
)提供性能分析信息和覆盖率测试,必须设置一些标志。
从C单元测试的代码覆盖率 :
具体来说,您需要编译器向目标代码中添加分析信息,这是通过在编译命令中添加标志
-fprofile-arcs
和-ftest-coverage
来完成的。第一个标志将逻辑添加到目标代码以生成通用配置信息。 这是有关每个基本块执行频率的信息。 当您的程序运行时,所有这些信息将被保存到.o文件旁边的扩展名为.da的新文件中。 该文件中的数据不是特定于覆盖率的,但是它将由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选项编译的库链接,则仍然需要添加链接器标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.