clangformat-cmake icon indicating copy to clipboard operation
clangformat-cmake copied to clipboard

A simple CMake module for clang-format support

language.badge license.badge

ClangFormat.cmake module

ClangFormat.cmake is a simple CMake module for clang-format support.

Requirements

The module requires CMake 3.0 or higher and some version of clang-format installed.

Integration

  1. 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)
      
  2. Include the module:

    # CMakeLists.txt
    
    include(ClangFormat)
    
  3. Setup the module:

    # CMakeLists.txt
    
    clangformat_setup(
      src/hello.hpp
      src/hello.cpp
    )
    

    or:

    # CMakeLists.txt
    
    target_clangformat_setup(sometarget)
    

Usage

  1. Generate the build system:

    $ cmake -S . -Bbuild
    
  2. Format sources anytime using clangformat target:

    $ cmake --build build --target clangformat
    

    or:

    $ cd build
    $ make clangformat
    

Example

See an example here.