Recommended way to use static analysis tools with CMake was presented in Daniel Pffeifer's "Effective Cmake" (https://www.youtube.com/watch?v=rLopVhns4Zs&=&t=77m13s).
You can either define it when calling cmake
, eg.:
cmake "-DCMAKE_CXX_CPPLINT=cpplint" ..
or put it into CMakeLists.txt
:
set(CMAKE_CXX_CPPLINT "cpplint")
Recommended option is the first one (we shouldn't define in a project what isn't a project requirement).
CMake will call cpplint
for each file it compiles. You can pass extra arguments after semicolon (e.g. -DCMAKE_CXX_CPPLINT=cpplint;--linelength=100
).
Downsides of this method:
- Errors count will not get accumulated (because
cpplint
is invoked for each file separately).
- It will not check header files (as opposed to what D. Pffeifer says in his presentation, include files are not being scanned by
cpplint
).
Note that you can use other static analysis tools the same way:
- Clan Tidy
"-DCMAKE_CXX_CLANG_TIDY=/usr/bin/clang-tidy-3.9;-checks=*"
- CppCheck
"-DCMAKE_CXX_CPPCHECK=/usr/bin/cppcheck;--std=c++11"
- IWYU
"-DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=/usr/bin/iwyu;--transitive_includes_only"
- LWYU
cmake -DCMAKE_LINK_WHAT_YOU_USE=TRUE
- clazy
Some of them will require "compilation database" (set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
).