Want to see Parasoft in action? Sign up for our Monthly Demos! See Demos & Events >>
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.
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.
The procedure to run the static analysis on CMake-based projects involves the following steps:
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
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 CI or locally at the developer desktop. The only modification that might be required is enabling the generation of the compile_command.json file.
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. Next, I’ll cover unit testing with CMake. Blog post coming soon.
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.