To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
How to integrate PVS-Studio into CMake …
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Diagnosis of micro-optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C#)
Problems related to code analyzer
Additional information
Contents

How to integrate PVS-Studio into CMake with a CMake module

Apr 06 2022

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.

Before you start

Make sure you entered the license key (Linux/macOS section). Otherwise, the analysis won't run. CMake module works with the pvs-studio-analyzer / CompilerCommandsAnalyzer.exe utility. So, the .lic file is used to activate the license. If you store the .lic file separately, call the pvs_studio_add_target command and enter the path to it via the LICENSE parameter.

Attention Windows users: if you enter the license key in the plugin for Visual Studio, make sure you also enter the CompilerCommandsAnalyzer.exe credentials to activate the analyzer. This is necessary due to different mechanisms of working with the license.

How to add a module to the project

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.

How to configure 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)

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:

Target options

  • ALL — the analysis is started automatically when the all target is built. So, it will work with each project build;
  • TARGET — the name of the target created for the analysis. To run the analysis, just build this target;
  • ANALYZE — targets to analyze. To also analyze the dependencies of these targets, add the RECURSIVE flag;
  • RECURSIVE — recursively analyze targets dependencies;
  • COMPILE_COMMANDS — use compile_commands.json instead of specifying targets in the ANALYZE option. It works with CMAKE_EXPORT_COMPILE_COMMANDS and available only when you use Makefile or Ninja generators.

Output options

  • OUTPUT — prints the analyzer output into the build log;
  • LOG — the report file. If you don't specify it, the PVS-Studio.log file will be used in the directory with the CMake cache;
  • FORMAT — the report format. In this case, json is an error format with support for multi-file navigation. You can find a list of available formats here (section "Plog Converter utility");
  • MODE — enable diagnostic groups and their levels.

Analysis options

  • PLATFORM - platform name. Available options: win32, x64/win64, linux32, linux64, macos, arm (IAR Embedded Workbench), pic8 (MPLAB XC8), tms (Texas Instruments C6000);
  • PREPROCESSOR - preprocessed file format (clang/visualcpp/gcc);
  • LICENSE — path to the .lic file;
  • CONFIG — path to the .cfg file;
  • CFG_TEXT — contents of the .cfg file;
  • SUPPRESS_BASE — path to the suppress file in the .json format;
  • KEEP_COMBINED_PLOG — do not delete the combined .pvs.raw file for subsequent processing by the plog-converter utility.

Other options

  • DEPENDS — additional dependencies for the target;
  • SOURCES — list of source files for analysis;
  • BIN — path to pvs-studio-analyzer (macOS/Linux) or CompilerCommandsAnalyzer.exe (Windows);
  • CONVERTER — path to plog-converter (macOS/Linux) or HtmlGenerator.exe (Windows);
  • C_FLAGS — additional flags for the C compiler;
  • CXX_FLAGS — additional flags for the C++ compiler;
  • ARGS — additional arguments for pvs-studio-analyzer/CompilerCommandsAnalyzer.exe;
  • CONVERTER_ARGS — additional arguments for plog-converter/HtmlGenerator.exe .

How to start 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 example1.analyze

Before the run, all the targets specified for analysis in the ANALYZE parameter are built.

This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept