How to use cpplint code style checking with CMake?

7.3k Views Asked by At

The only online resources I have found are the CMake documentation on CMAKE_<LANG>_CPPLINT (link here) and this example (link here), but I cannot figure out how to actually use it inside a CMakeLists.txt file. I tried the example provided, but I can't make it work. FYI, I installed cpplint as explained here.

As of now, I can run the cpplint python script inside CMakeLists.txt using this CMake command:

execute_process(COMMAND cpplint path/To/File/To/Analyse.cpp)

However, I am pretty sure that this is not the right way to do this.

4

There are 4 best solutions below

0
On BEST ANSWER

Recommended way to use static analysis tools with CMake was presented in Daniel Pffeifer's "Effective Cmake" (https://www.youtube.com/watch?v=rLopVhns4Zs&amp=&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:

  1. Errors count will not get accumulated (because cpplint is invoked for each file separately).
  2. 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)).

1
On

I failed to use CMAKE_<LANG>_CPPLINT to check code style.

I make it by using add_custom_target.

  1. download cpplint.py

  2. then download cpplint.cmake or write yourselt.

Suppose that there is a source code directory named src in your project, code those statements into your CMakeLists.txt.

aux_source_directory(${CMAKE_SOURCE_DIR}/src src)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}) #I put cpplint.cmake in $CMAKE_SOURCE_DIR
include(cpplint)

add_style_check_target(phoenix-cpplint "${src}") 

Note:

  1. you should pass the whole list, so use "${src}" instead of ${src}.

  2. By default nothing depends on the custom target, see add_custom_target.

If there's still some problem, debug your CMakeLists.txt.

0
On

I have been struggling with the same problem.

I tried it with CMake 3.10.2 and the comment by user2449761 is still true. Using set(CMAKE_CXX_CPPLINT "cpplint") still does not check any header files.

The answer by kgbook does not work anymore, since aux_source_directory does not list the header files. You can, however, use

get_target_property(src staticcodecheck SOURCES)

That will give you all the non-system headers. The rest can be kept the same. As for running cpplint at a specific time, you might try

add_custom_command(TARGET ${TARGET}
    PRE_BUILD
...

That will replace add_custom_target(${TARGET_NAME}... in his cpplint.cmake.

Hope this helps.

0
On

The following is how I am running cpplint on all files in the src directory for a project.

file(GLOB_RECURSE SRC_FILES "${PROJECT_SOURCE_DIR}/src/**/*")
add_custom_command(TARGET target PRE_BUILD COMMAND cpplint ${SRC_FILES})

This runs every time, it fails the build when there are cpplint issues, and it runs on all files in the src directory. You may also want to consider adding cpplint specific arguments to the command, such as --quiet or --extensions for example.