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 for Linux (Windows is not supported)
  • Bazel version 4.0+

Adding a C/C++test Installation as a Bazel Local Repository

A clean installation 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.
    cd <INSTALL_DIR>
    mv integration/bazel/WORKSPACE.bazel .
    mv integration/bazel/BUILD.bazel .
    Note: Make sure the files are moved and no copy remains in the integration/bazel directory.
  2. Open the Bazel WORKSPACE file at the root of your project.
  3. Register the C/C++test installation as a Bazel local repository in your WORKSPACE file.

    local_repository(name = "cpptest", path = "<INSTALL_DIR>")

Example Build Target

The following example will be used in the subsequent procedures:

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":hello-greet",
    ],
)

Generating a BDF File

  1. Run the "@cpptest//:bdf" rule to generate a BDF file.

    bazel run @cpptest//:bdf --@cpptest//:target=//:hello-world --@cpptest//:compiler-config=gcc_10-64 --@cpptest//:project-name=HelloWorld

    A BDF file 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 the 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.

Generating an Executable with Code Coverage

Follow the procedure below to generate a test binary with code coverage instrumentation. 

  1. 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
    • The generated executable will be of the form "<TARGET_NAME>.elf" and will be located in the bazel-bin directory next to where a binary would typically be generated. In the above example, it will be "hello-world.elf". (The use of the file extension ".elf" is arbitrary.)
    • The integration will not run the instrumented executable.
  2. Execute the instrumented binary.

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

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

Note:

  • If you are experiencing issues with memory allocation, see What if memory is not allocated when using C/C++test with a Bazel build?.
  • Coverage for third party and external sources may not be displayed in the IDE.
  • When generating a coverage report for a Bazel project, C/C++test will resolve symbolic links in the file paths. To keep the symbolic links unresolved, define the following environment variable before generating the coverage report:
    export CPPTEST_COVERAGE_SRC_ROOT_RESOLVE_SYMLINKS=false

Configuring the Coverage Instrumentation Tool (cpptestcc)

You can configure the coverage instrumentation tool (cpptestcc) using a .psrc configuration file.

  1. Copy the example .psrc file to the project.

    cp <INSTALL_DIR>/integration/bazel/cpptestcc-bazel.psrc <PROJECT_ROOT>/cpptestcc-bazel.psrc
  2. Modify the contents of cpptestcc-bazel.psrc, as needed. For a list of options, see Command Line Reference for cpptestcc or run:
    cpptestcc -help
  3. 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"])
  4. 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

Configuration Options for C/C++test's Bazel Rules

Common Arguments

OptionDescription
--@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.
Default: None
Example: --@cpptest//:target=//main:hello-world

--@cpptest//:psrc_file=<CUSTOM_PSRC_FILEGROUP>

Specifies the filegroup for the .psrc configuration file; see Configuring the Coverage Instrumentation Tool (cpptestcc).
Default: None
Example: --@cpptest//:psrc_file=//:cpptestcc-bazel-psrc

Before using this option, be sure to define the filegroup that refers to the .psrc configuration file in your BUILD.bazel file. For example:
filegroup(name = "cpptestcc-bazel-psrc", srcs = ["cpptestcc-bazel.psrc"], visibility = ["//visibility:public"])
See the <INSTALL_DIR>/integration/bazel/cpptestcc-bazel.psrc file for more information.

"@cpptest//:bdf" Rule Arguments

OptionDescription
--@cpptest//:project-name=<NEW_PROJECT_NAME>

Changes the name of the resulting BDF file.
Default: "$(basename $(bazel info workspace))" - the name of the directory containing the WORKSPACE file.
Example: --@cpptest//:project-name="foo"
The running bdf target will produce the file "foo.bdf".

--@cpptest//:missing-bazel-dirs="external","<DIRS>"

Specifies the beginning of the path to directories found during parsing. This path will be prepended with "bazel-[project-name]" within the BDF.
Default: "external" (required)
Example: --@cpptest//:missing-bazel-dirs="external","another_includes_dir"
The path of any directories found during parsing that starts with "external" or "another_includes_dir" will be prepended with "bazel-[project-name]" within the BDF.

"@cpptest//:coverage" Rule Arguments

OptionDescription
--@cpptest//:compiler-config=<COMPILER_CONFIGURATION_NAME>

Specifies the compiler configuration.
This argument is mandatory unless the compiler configuration is specified in the .psrc file; see Configuring the Coverage Instrumentation Tool (cpptestcc).
Default: None
Example: --@cpptest//:compiler-config=clang_12_0

--@cpptest//:line-coverage
--@cpptest//:mcdc-coverage
--@cpptest//:optimized-line-coverage
--@cpptest//:function-coverage
--@cpptest//:optimized-function-coverage
--@cpptest//:statement-coverage
--@cpptest//:optimized-statement-coverage
--@cpptest//:block-coverage
--@cpptest//:optimized-block-coverage
--@cpptest//:simple-condition-coverage
--@cpptest//:optimized-simple-condition-coverage
--@cpptest//:path-coverage
--@cpptest//:decision-coverage
--@cpptest//:optimized-decision-coverage
--@cpptest//:call-coverage
--@cpptest//:optimized-call-coverage

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.
Default: False

--@cpptest//:quiet

Suppresses all console text output from the cpptest engine during execution.
Default: False

--@cpptest//:project-name=<NEW_PROJECT_NAME>

Changes the names of the Eclipse project files: .project and .parasoft.
Default: "$(basename $(bazel info workspace))" - the name of the directory containing the WORKSPACE file.
Example: --@cpptest//:project-name="foo"
The .project file in the top level directory will contain: "<name>foo</name>"
The .parasoft file in top level directory will contain: "project.path=/foo"
/foo//...  - configuration entries for the project

--@cpptest//:project-workspace=<PROJECT_WORKSPACE>

Provides the location of a temporary Eclipse workspace for analysis and reporting.
Default: /tmp
Example: --@cpptest//:project-workspace=/tmp/project_name


  • No labels