This section describes how to integrate C/C++test with a Bazel build for running static analysis and code coverage.
Collecting code coverage for Bazel is currently not fully supported and remains an experimental feature. |
A clean install of C/C++test will not immediately work as a Bazel local repository. Follow the procedure below to add a C/C++test installation as a Bazel local repository.
Add the following to the Bazel WORKSPACE file (modify the path to reflect the root of the C/C++test installation):
local_repository(name = "cpptest", path = "/opt/parasoft/cpptest") |
For Bazel to perform BDF generation, a rule must be added to the BUILD file where the cc_library or cc_binary to be tested is located. For example, given the following Bazel BUILD file we want to generate a BDF for the "hello-world" cc_binary rule:
cc_library( name = "hello-greet", srcs = ["hello-greet.cc"], hdrs = ["hello-greet.h"], ) cc_binary( name = "hello-world", srcs = ["hello-world.cc"], deps = [ ":hello-greet", ], ) |
Follow the procedure below to register BDF generation with Bazel:
Include the Bazel integration code from C/C++test. To do this, add the following to the project BUILD file:
load("@cpptest//:integration/bazel/cpptest.bzl", "cpptest_generate_bdf") |
Create a list of targets which will be part of the BDF:
targets = [ "hello-world" ] |
Register the targets with a new BDF generation rule by adding the following rule declaration after targets:
cpptest_generate_bdf( name = "make_bdf", deps = targets, ) |
The cpptest_generate_bdf rule has now been registered in the BUILD file.
To run the BDF generation rule, enter something like the following (this example uses the gcc_10-64 compiler and the "hello-world" project from above):
bazel run //:make_bdf --@cpptest//:compiler-config=gcc_10-64 --@cpptest//:project-name=hello-world |
Customize the compiler-config
argument for your compiler and the project-name
argument for your project.
A BDF will be generated at the root of your project.
Run analysis with the generated BDF file:
cpptestcli -config "builtin://Recommended Rules" -module . -input hello-world.bdf -compiler gcc_10-64 |
|
To add code coverage to a target executable in your project's BUILD file, a new rule must be added to the file.
Modify the following line in the BUILD file:
load("@cpptest//:integration/bazel/cpptest.bzl", "cpptest_generate_bdf") |
to become:
load("@cpptest//:integration/bazel/cpptest.bzl", "cpptest_generate_bdf", "cpptest_code_coverage_executable" ) |
Add a new rule to the file:
cpptest_code_coverage_executable( name = "code_cov", # This can be any name deps = targets, # Reuse the list of targets from BDF generation ) |
Follow the procedure below to generate the instrumented binary.
Specify the line-coverage. To do this, add a new option to the Bazel run command. This will tell cpptestcc to instrument the executable with line coverage.
bazel run //:code_cov --@cpptest//:compiler-config=gcc_10-64 --@cpptest//:line-coverage |
|
Execute instrumented binary:
./bazel-bin/hello-world.elf |
Generate coverage report:
cpptestcli -config builtin://Coverage -module . -input cpptest_results.clog |
Option | Description |
---|---|
--@cpptest//:compiler-config=<COMPILER-CONFIGURATION-NAME> | Passes compiler configuration name to the underlying rules that make up the C/C++test Bazel integration. It is recommended to always specify it on the command line. By default, the compiler configuration is set to an invalid self documenting value. |
--@cpptest//:project-name=<NEW-PROJECT-NAME> | Assigns the given name to the BDF to be generated. If the WORKSPACE file explicitly specifies a name, then that name will always be used. If this option is not explicitly specified on the command line (and the WORKSPACE does not explicitly specify a name), then the folder containing the root of the workspace will be used. For example, if the path to the root of the workspace is /foo, then the project name will be "foo". |
--@cpptest//:line-coverage | Enables line coverage. |