ClangFormat.cmake is a simple CMake module for clang-format support.
The module requires CMake 3.0 or higher and some version of clang-format installed.
-
Obtain the module and add it into your project's CMake modules path:
-
Copy approach:
$ wget https://raw.githubusercontent.com/zemasoft/clangformat-cmake/master/cmake/ClangFormat.cmake -P cmake
# CMakeLists.txt list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
-
Submodule approach:
$ git submodule add https://github.com/zemasoft/clangformat-cmake
# CMakeLists.txt list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/clangformat-cmake/cmake)
-
-
Include the module:
# CMakeLists.txt include(ClangFormat)
-
Setup the module:
# CMakeLists.txt clangformat_setup( src/hello.hpp src/hello.cpp )
or:
# CMakeLists.txt target_clangformat_setup(sometarget)
-
Generate the build system:
$ cmake . -Bbuild
-
Format sources anytime using
clangformat
target:$ cmake --build build --target clangformat
or:
$ cd build $ make clangformat
See an example here.