简体   繁体   English

如何在TravisCI中为带有CMake项目的C ++正确配置CodeCov?

[英]How to properly configure CodeCov for a C++ w/ CMake project in TravisCI?

I was trying to configure CodeCov on my GitHub repo with TravisCI. 我试图使用TravisCI在GitHub存储库上配置CodeCov。 Since my repo is in C++ and I used CMake already, I essentially copy-pasted the after_success label of this example into my .travis.yml file: 由于我的存储库是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"

I pushed a custom .codecov.yml file to provide my token and notification settings, but the after_success is just the one suggested in the example. 我推送了一个自定义.codecov.yml文件以提供令牌和通知设置,但是after_success只是示例中建议的一个。 I'm really not familiar with lcov , so I have no idea whether something else is missing or what those calls are for. 我真的不太熟悉lcov ,所以我不知道是否还有其他缺失或这些要求是什么。

Looking at CodeCov's documentation I couldn't find what I might be missing but my coverage reports are empty because lcov can't find some mysterious .gcda file (I guess). 查看CodeCov的文档,我找不到我可能会缺少的内容,但是我的覆盖率报告为空,因为lcov找不到一些神秘的.gcda文件(我想)。 CodeCov's docs don't mention that anywhere so I guess I'm missing an important step in my configuration (or perhaps the file can't be found for some reason...?) CodeCov的文档在任何地方都没有提到它,所以我想我在配置中缺少了重要的一步(或者可能由于某种原因找不到文件...?)

This is the output of TravisCI's after_success stage: 这是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/********

The problem was with Cmake compiler and linker flags. 问题出在Cmake编译器和链接器标志上。 In order for GCov (part of GCC, and used by lcov ) to provide profiling information and coverage tests some flags have to be set. 为了使GCov(GCC的一部分,由lcov )提供性能分析信息和覆盖率测试,必须设置一些标志。

From Code Coverage for C Unit Tests : C单元测试的代码覆盖率

Specifically, you need the compiler to add profiling information to the object code, which is done by adding the flags -fprofile-arcs and -ftest-coverage to your compile command. 具体来说,您需要编译器向目标代码中添加分析信息,这是通过在编译命令中添加标志-fprofile-arcs-ftest-coverage来完成的。

The first flag adds logic to the object code to generate generic profiling information. 第一个标志将逻辑添加到目标代码以生成通用配置信息。 This is information about how often each basic block is executed. 这是有关每个基本块执行频率的信息。 When your program is run, all of this information will be saved to a new file with the extension .da, next to the .o file. 当您的程序运行时,所有这些信息将被保存到.o文件旁边的扩展名为.da的新文件中。 The data in this file isn't coverage specific, but it will be used by gcov. 该文件中的数据不是特定于覆盖率的,但是它将由gcov使用。

The second flag that you passed to GCC, -ftest-coverage , is also going to add logic to your object files. 传递给GCC的第二个标志-ftest-coverage也将为目标文件添加逻辑。 This time, the goal is to output the coverage-specific information. 这次,目标是输出特定于覆盖范围的信息。 There are two files that will be generated, a .bb and a .bbg. 将生成两个文件,.bb和.bbg。 The .bb file is a simple mapping file from basic blocks to line numbers. .bb文件是从基本块到行号的简单映射文件。 The .bbg file lists each of the arcs in the corresponding source file that were run when executing the application. .bbg文件列出了在执行应用程序时运行的相应源文件中的每个弧。 This data is used by gcov to reconstruct the actual program flow graph, from which all basic block and arc execution counts can be computed. gcov使用此数据来重建实际的程序流程图,从中可以计算出所有基本的程序段和弧执行计数。

Also, the sources need to be linked with -lgcov --coverage . 同样,源需要与-lgcov --coverage链接。 In my case, since I'm using Cmake I needed to specify these with the set_target_properties function: 就我而言,由于我使用的是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"
)

Finally, since you normally don't want to include your unit tests in your coverage report, you will not define the compiler flags for them. 最后,由于通常不希望将单元测试包含在覆盖率报告中,因此不会为它们定义编译器标志。 Note, however, that if you link your unit tests with libraries that were compiled with gcov options, you'll still need to add the linker flags. 但是请注意,如果将单元测试与使用gcov选项编译的库链接,则仍然需要添加链接器标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM