Using Parasoft C/C++test With CMake for Static Analysis
By Miroslaw Zielinski
April 10, 2023
4 min read
You may have been using CMake in your static analysis. But have you tried using Parasoft C/C++ test with CMake? Here's a breakdown of how to use C/C++test to run static analysis on CMake-based projects.
Jump to Section
CMake is one of the most popular tools for building, testing, and packaging software. Parasoft C/C++test streamlines the integration with CMake-based projects by simplifying the build management process. The bigger the project, the bigger the impact.
The Parasoft team developed these enhancements during a rollout of our testing solution for a large, well-known automotive manufacturer.
Our goal 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. You can 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 build process. 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 workflow. 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. C/C++test 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:
- Enable generation of the JSON file with compile commands. This is a one-time activity.
- Generate your build scripts. This will include the compile-info JSON file generation.
- Run static analysis by feeding the compile-info from the JSON file as an input to the static analyzer.
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, install the standard edition of C++test.
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:
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.
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.
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:
That’s it. The static analysis is performed and the results are returned.
This integration approach can be used for continuous integration (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.
Set the CMAKE_EXPORT_COMPILE_COMMANDS options to ON in the CMakeLists.txt file.
Add it to the command-line invocation of the cmake when generating the build scripts.
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 <path to src>
That’s all there is to integrate static analysis into CMake-based projects.