Introduction
This section describes how to integrate C/C++test with a Bazel build for running Static Analysis and Code Coverage.
Prerequisites
- C/C++test installed
- Bazel version 4.0+
- Linux only
Adding a C/C++test Installation as a Bazel Local Repository
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.
- Move the WORKSPACE.bazel and BUILD.bazel files located in the integration/bazel directory to the root of the C/C++test installation.
- Open the Bazel WORKSPACE file at the root of your project.
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")
Registering BDF Generation Rule
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.
- Save the BUILD file.
Generating BDF 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 theproject-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
- Generating a BDF will not produce any project artifacts, i.e. executables, objects, generated sources.
- The auto-generated code for the project will be located in the
bazel-build
subdirectory. bazel clean
will remove the contents of this directory and invalidate the BDF.- Rebuilding the target should resolve missing references within the BDF.
Registering Code Coverage Rule
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 )
Generating an Executable with Code Coverage
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
- The executable generated will be of the form "<TARGET_NAME>.elf" and will be located in the bazel-bin directory next to where the normal binary would be generated. In the above example it will be "hello-world.elf". (Note: The use of the file extension ".elf" is arbitrary.)
- The integration will not run the instrumented executable.
Execute instrumented binary:
./bazel-bin/hello-world.elf
Generate coverage report:
cpptestcli -config builtin://Coverage -module . -input cpptest_results.clog
C/C++test Configuration Options
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. |