Introduction

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.

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.

  1. Move the WORKSPACE.bazel and BUILD.bazel files located in the integration/bazel directory to the root of the C/C++test installation.
  2. Open the Bazel WORKSPACE file at the root of your project.
  3. 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:

  1. 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")
  2. Create a list of targets which will be part of the BDF:

    targets = [ "hello-world" ]
  3. 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.

  4. Save the BUILD file.

Generating BDF File

  1. 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. The integration will also generate the files necessary for C/C++test Professional to be able to open your project as a C/C++test project.

  2. Import the project into your Eclipse workspace and run analysis:

    cpptestcli -data /path/to/workspace -import . -config "builtin://Recommended Rules"
  • 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. 

  1. 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"
    )
  2. 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. 

  1. 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.
  2. Execute instrumented binary:

    ./bazel-bin/hello-world.elf
  3. Import the project into your Eclipse workspace and generate coverage report:

    cpptestcli -data /path/to/workspace -import . -config "builtin://Load Application Coverage" -report reports

C/C++test Configuration Options

OptionDescription
--@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.

--@cpptest//:mcdc-coverage

Enables MC/DC coverage.

--@cpptest//:optimized-line-coverage

Enables optimized line coverage.

--@cpptest//:function-coverage

Enables function coverage.

--@cpptest//:optimized-function-coverage

Enables optimized function coverage.

--@cpptest//:statement-coverage

Enables statement coverage.

--@cpptest//:optimized-statement-coverage

Enables optimized statement coverage.

--@cpptest//:block-coverage

Enables block coverage.

--@cpptest//:optimized-block-coverage

Enables optimized block coverage.

--@cpptest//:simple-condition-coverage

Enables simple condition coverage.

--@cpptest//:optimized-simple-condition-coverage

Enables optimized simple condition coverage.

--@cpptest//:path-coverage

Enables path coverage.

--@cpptest//:decision-coverage

Enables decision coverage.

--@cpptest//:optimized-decision-coverage

Enables optimized decision coverage.

--@cpptest//:call-coverage

Enables call coverage.

--@cpptest//:optimized-call-coverage

Enables optimized call coverage.

--@cpptest//:template-coverage

Enables template coverage.

  • No labels