Besides the compile_commands.json mode, you can work with PVS-Studio in CMake with a special CMake module. This mode allows you to integrate the analyzer into a CMake-based project in a deeper way. For example, you can set specific goals for the analysis. Thus, you can check only those parts that you need, not the whole project.
Make sure you entered the license key (Linux/macOS section). Otherwise, the analysis won't run. You can read more about entering the license here.
The easiest and recommended way to add a module to the project is to use FetchContent for automatic load. You can do it like this:
include(FetchContent)
FetchContent_Declare(
PVS_CMakeModule
GIT_REPOSITORY "https://github.com/viva64/pvs-studio-cmake-module.git"
GIT_TAG "master"
)
FetchContent_MakeAvailable(PVS_CMakeModule)
include("${pvs_cmakemodule_SOURCE_DIR}/PVS-Studio.cmake")
Such code will load the Git repository with the module into the generated cache folder. Thus, you'll integrate the analyzer into your project. Note: master is the latest version. If you have problems with it, try to take the latest release tag of the current analyzer's version.
You can load the file with the PVS-Studio.cmake module yourself if you don't want to have unnecessary dependencies on FetchContent.
Don't forget to update the module with the release of a new analyzer's version to avoid problems in its work. In the GIT_TAG parameter, you can specify the master branch to always use the latest version of the module.
To run the analyzer, CMake module adds a separate target for the build. When you run the build of this target, the analysis will run with the parameters that you specified when adding this target. To add the analysis target, use the pvs_studio_add_target command. For example:
cmake_minimum_required(VERSION 3.5)
project(pvs-studio-cmake-example CXX)
add_executable(example main.cpp)
# Optional:
# include(FetchContent)
# FetchContent_Declare(....)
# FetchContent_MakeAvailable(....)
include(PVS-Studio.cmake)
pvs_studio_add_target(TARGET example.analyze ALL
OUTPUT FORMAT json
ANALYZE example
MODE GA:1,2
LOG target.err
ARGS -e /path/to/exclude-path)
This small CMake file contains one target to build the executable file and one target to run the analysis. Let's look at the parameters of the pvs_studio_add_target command:
To exclude files from analysis, use the ARGS option by passing the paths through the -e (‑‑exclude-path) flag, as shown in the example above. You can specify absolute, relative paths, or a search mask (glob). Note that relative paths will be expanded in relation to the build directory. This approach allows you, for example, to exclude third-party libraries from the analysis.
To start the analysis, build the target added to pvs_studio_add_target. For example, this is how the analysis run looks like for the example above:
cmake --build <path-to-cache-dir> --target example.analyze
Before the run, all the targets specified for analysis in the ANALYZE parameter are built.
Here you can find examples of the PVS-Studio integration in CMake.