Want to see Parasoft in action? Sign up for our Monthly Demos! See Demos & Events >>

X
BLOG

Using Parasoft C/C++test With CMake for Static Analysis

Using Parasoft C/C++test With CMake for Static Analysis Reading Time: 4 minutes

CMake is one of the most popular tools for building, testing, and packaging software. It simplifies managing the build process for sizable projects in a big way. The Parasoft C/C++test 2020.1 release introduces a collection of enhancements that streamline the integration with CMake-based projects.

We developed these enhancements during the rollout of our testing solution for one of the biggest and well-known automotive manufacturers.

Our challenge was to introduce static analysis and unit testing capabilities into large and complex projects that are based on the adaptive AUTOSAR platform. These projects were composed of multiple static and dynamic libraries and many executables.

In this blog post, I’ll explain how to integrate Parasoft C/C++test static analysis capabilities into CMake-based projects. Stay tuned for my next post where I’ll explain how to do the same for unit testing.

Integrating Static Analysis Into CMake-Based Projects

To perform static code analysis, tools like Parasoft C/C++test require information about the project build. Generally, all that’s needed are compilation command lines to invoke the files that are intended for analysis.

With Parasoft C/C++test, there are different ways to integrate into the builds. Different build systems may require different approaches. With CMake-based projects, there are great options available to make static analysis effortless and require minimal changes to the process. It makes for smooth integration with C/C++test.

CMake scripts can be easily configured to generate compile-time information as a JSON file. You usually get one JSON file per build. This JSON file contains all that the static analyzer needs to perform the code scan. The JSON file with compilation command lines is then created at the time of the build script generation. The C/C++test 2020.1 release understands this format and can use it directly as an input to performing static analysis.

How to Run Static Analysis on CMake-Based Projects

The procedure to run the static analysis on CMake-based projects involves the following steps:

  1. Enable generation of the JSON file with compile commands. This is a one-time activity.
  2. Generate your build scripts. This will include the compile-info JSON file generation.
  3. Run static analysis by feeding the compile-info from the JSON file as an input to the static analyzer.

That’s it!

An Example of Static Analysis on CMake-Based Projects

Let’s walk through an example of running static analysis on CMake-based projects. If you haven’t already done so, please install the standard edition of C++test 2020.1

Follow These Steps

After installation, go to the installation directory and into the examples subfolder. Make a copy of the Timer example and enter the Timer directory. (These steps use Linux as the development platform.)

You should see the following content:

Using Parasoft C/C++test With CMake for Static Analysis

This example is prepared to be built with a “static” makefile that’s located in the main directory or using the CMake generated build scripts.

Create a build folder inside the Timer directory by using cd to it. Call the build scripts generation using CMake.

Using Parasoft C/C++test With CMake for Static Analysis

If we list the content of the build directory after generation, we will see that the compile_commands.json file was generated.

For the next example project, the generation of compile-info JSON files has already been enabled.

Using Parasoft C/C++test With CMake for Static Analysis

We’re now ready to run the static analysis using the generated compile_commands.json file. A great advantage is that we can append the .json file directly into the command line interface of the Parasoft C/C++test standard product:

Using Parasoft C/C++test With CMake for Static Analysis

That’s it. The static analysis is performed and the results are returned.

Using Parasoft C/C++test With CMake for Static Analysis

This integration approach can be used for CI or locally at the developer desktop. The only modification that might be required is enabling the generation of the compile_command.json file.

How to Enable the Generation File in Your Project

If you don’t have the generation of the compile_command.json file enabled in your project, you can do it in one of two ways.

Option 1

Set the CMAKE_EXPORT_COMPILE_COMMANDS options to ON in the CMakeLists.txt file.

Example

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Option 2

Add it to the command-line invocation of the cmake when generating the build scripts.

Example

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 <path to src>

That’s all there is to integrate static analysis into CMake-based projects. Next, I’ll cover unit testing with CMake. Blog post coming soon.

Using Parasoft C/C++test With CMake for Static Analysis

Written by

Miroslaw Zielinski

Product Manager for Parasoft's embedded testing solutions, Miroslaw's specialties include C/C++, RTOSes, static code analysis, unit testing, managing software quality for safety critical applications, and software compliance to safety standards.

将最新的软件测试新闻和资源发送到您的电子邮箱。