Clang.cmake is a cmake module for clang-format, clang-tidy and include-what-you-use.
The module requires CMake 3.0 or higher and some version of clang-format clang-tidy and iwyu installed.
-
Obtain the module and add it into your project's CMake modules path:
-
Submodule approach:
$ git submodule add https://github.com/erezamihud/clang-cmake
# CMakeLists.txt list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/clang-cmake/cmake)
-
-
Include the modules:
# CMakeLists.txt include(ClangFormat) include(ClangTidy) include(IWYU)
-
Setup the module:
# CMakeLists.txt clangformat_setup( src/hello.hpp src/hello.cpp ) clangtidy_setup( src/hello.hpp src/hello.cpp ) iwyu_setup( src/hello.hpp src/hello.cpp )
or to get the sources from existing targets:
# CMakeLists.txt target_clangformat_setup(sometarget) target_clangtidy_setup(sometarget) target_iwyu_setup(sometarget)
To setup all the things for a specific target:
target_setup(sometarget)
The module add a few custom commands:
check
- run all of the target with warnings as errors (as you would in ci) Do cmake --build build --target help to look at everything else. To add iwyu mapping files use the variable IWYU_IMP is a list, that should have all of the mapping files
-
Generate the build system:
$ cmake -S . -Bbuild
-
Run things:
$ cmake --build build --target format
- In iwyu python scripts I added code to make sure that when exit code 2 is received the script exit with exit code 0 (code 2 in iwyu states that everything worked well)
See an example here.