cmake clang-tidy (or other script) as custom target

fuji picture fuji · Aug 29, 2015 · Viewed 11.8k times · Source

I am trying to create a custom cmake target for clang-tidy, to lint my project. The source folder looks something like this:

src/scripts/run-clang-tidy.py
src/.clang-tidy
src/...

So far my plan was to copy both these files to the build directory with a custom command:

add_custom_command(
    OUTPUT run-clang-tidy.py .clang-tidy
    COMMAND cp ${CMAKE_SOURCE_DIR}/scripts/run-clang-tidy.py ${CMAKE_SOURCE_DIR}/.clang-tidy ${CMAKE_CURRENT_BINARY_DIR})

I now want to call run-clang-tidy.py in the build directory (which should be the working directory), with a custom target, so that I can just call:

make lint

Which should run the checks specified in .clang-tidy.

For this script to work, it also needs the CMAKE_EXPORT_COMPILE_COMMANDS option. I try to set it with the following command, but it does not recognize it:

add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)

How would the call to add_custom_target look like?

Answer

Alexander Shukaev picture Alexander Shukaev · Aug 11, 2016

Since CMake 3.6, native integration of clang-tidy is implemented [1, 2]. Mechanics are similar to include-what-you-use integration that was there since CMake 3.3 [3].