This section describes how to integrate C/C++test CT with a Bazel build for running static analysis and code coverage.
A clean installation of C/C++test CT will not immediately work as a Bazel local repository. Follow the procedure below to add a C/C++test CT installation as a Bazel local repository.
integration/bazel
directory to the root of the C/C++test CT installation.cd <INSTALL_DIR> mv integration/bazel/WORKSPACE.bazel . mv integration/bazel/BUILD.bazel . |
integration/bazel
directory.Register the C/C++test CT installation as a Bazel local repository in your WORKSPACE file.
local_repository(name = "cpptest", path = "<INSTALL_DIR>") |
The following example will be used in the subsequent procedures:
cc_binary( name = "hello-world", srcs = ["hello-world.cc"], deps = [ ":hello-greet", ], ) |
Follow the procedure below to generate a test binary with code coverage instrumentation.
Run the "@cpptest//:coverage" rule to generate the instrumented binary.
bazel run @cpptest//:coverage --@cpptest//:target=//:hello-world --@cpptest//:compiler-config=gcc_10-64 --@cpptest//:line-coverage |
Execute the instrumented binary.
bazel-bin/hello-world.elf |
Generate coverage data files.
cpptestcov compute -map .cpptest -clog cpptest_results.clog -out .coverage |
Generate a coverage report.
cpptestcov report text .coverage |
Note: When generating coverage data for a Bazel project, C/C++test CT will resolve symbolic links in the file paths. To keep the symbolic links unresolved, define the following environment variable before generating the coverage data files.
export CPPTEST_COVERAGE_SRC_ROOT_RESOLVE_SYMLINKS=false |
You can configure the coverage instrumentation tool (cpptestcc) using a .psrc configuration file.
Copy the example .psrc file to the project.
cp <INSTALL_DIR>/integration/bazel/cpptestcc-bazel.psrc <PROJECT_ROOT>/cpptestcc-bazel.psrc |
cpptestcc -help |
Define the "cpptestcc-bazel-psrc" filegroup in your BUILD.bazel file that refers to the cpptestcc-bazel.psrc file.
filegroup(name = "cpptestcc-bazel-psrc", srcs = ["cpptestcc-bazel.psrc"], visibility = ["//visibility:public"]) |
Use the "--@cpptest//:psrc_file" option with the "cpptestcc-bazel-psrc" filegroup when executing the "@cpptest//:coverage" rule.
bazel run @cpptest//:coverage --@cpptest//:target=//:hello-world --@cpptest//:psrc_file=//:cpptestcc-bazel-psrc |
Option | Description |
---|---|
--@cpptest//:target=<TARGET> | Specifies the target rule to be analyzed. A project rule of the type "cc_*" or a type derived from it should be specified. |
--@cpptest//:psrc_file=<CUSTOM_PSRC_FILEGROUP> | Specifies the filegroup for the .psrc configuration file; see Configuring the Coverage Instrumentation Tool (cpptestcc). |
Option | Description |
---|---|
--@cpptest//:compiler-config=<COMPILER_CONFIGURATION_NAME> | Specifies the compiler configuration. |
| Enables specific coverage metric(s). This is mandatory unless it is specified in the .psrc file; see Configuring the Coverage Instrumentation Tool (cpptestcc). |
--@cpptest//:verbose | Enables verbose console output for all stages of cpptestcc instrumentation and compilation. |
--@cpptest//:quiet | Suppresses all console text output from the cpptest engine during execution. |
This example uses C/C++test CT coverage plugin for Bazel.
<INSTALL_DIR>/integration/bazel/WORKSPACE.bazel
and <INSTALL_DIR>/integration/bazel/BUILD.bazel
into the C/C++test CT installation directory. integration/bazel
directory.<INSTALL_DIR>/examples/CovApplication
bazel run --config=cpptest_coverage |
bazel-bin/Timer.elf |
.coverage
.cpptestcov compute -map .cpptest -clog cpptest_results.clog -out .coverage |
.coverage
.cpptestcov report text .coverage |
See <INSTALL_DIR>/integration/bazel/README.txt
for more details.