See what API testing solution came out on top in the GigaOm Radar Report. Get your free analyst report >>

See what API testing solution came out on top in the GigaOm Radar Report. Get your free analyst report >>
Jump to Section
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
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 C/C++test static analysis capabilities into CMake-based projects. You can do the same for unit testing.
To perform static code analysis, tools like 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 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.
The procedure to run the static analysis on CMake-based projects involves the following steps:
That’s it!
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.
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.
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.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
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.