├── LICENSE_1_0.txt ├── cmake └── ClangFormat.cmake └── README.md /LICENSE_1_0.txt: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /cmake/ClangFormat.cmake: -------------------------------------------------------------------------------- 1 | # Copyright Tomas Zeman 2019-2020. 2 | # Distributed under the Boost Software License, Version 1.0. 3 | # (See accompanying file LICENSE_1_0.txt or copy at 4 | # http://www.boost.org/LICENSE_1_0.txt) 5 | 6 | function(prefix_clangformat_setup prefix) 7 | if(NOT CLANGFORMAT_EXECUTABLE) 8 | set(CLANGFORMAT_EXECUTABLE clang-format) 9 | endif() 10 | 11 | if(NOT EXISTS ${CLANGFORMAT_EXECUTABLE}) 12 | find_program(clangformat_executable_tmp ${CLANGFORMAT_EXECUTABLE}) 13 | if(clangformat_executable_tmp) 14 | set(CLANGFORMAT_EXECUTABLE ${clangformat_executable_tmp}) 15 | unset(clangformat_executable_tmp) 16 | else() 17 | message(FATAL_ERROR "ClangFormat: ${CLANGFORMAT_EXECUTABLE} not found! Aborting") 18 | endif() 19 | endif() 20 | 21 | foreach(clangformat_source ${ARGN}) 22 | get_filename_component(clangformat_source ${clangformat_source} ABSOLUTE) 23 | list(APPEND clangformat_sources ${clangformat_source}) 24 | endforeach() 25 | 26 | add_custom_target(${prefix}_clangformat 27 | COMMAND 28 | ${CLANGFORMAT_EXECUTABLE} 29 | -style=file 30 | -i 31 | ${clangformat_sources} 32 | WORKING_DIRECTORY 33 | ${CMAKE_SOURCE_DIR} 34 | COMMENT 35 | "Formatting ${prefix} with ${CLANGFORMAT_EXECUTABLE} ..." 36 | ) 37 | 38 | if(TARGET clangformat) 39 | add_dependencies(clangformat ${prefix}_clangformat) 40 | else() 41 | add_custom_target(clangformat DEPENDS ${prefix}_clangformat) 42 | endif() 43 | endfunction() 44 | 45 | function(clangformat_setup) 46 | prefix_clangformat_setup(${PROJECT_NAME} ${ARGN}) 47 | endfunction() 48 | 49 | function(target_clangformat_setup target) 50 | get_target_property(target_sources ${target} SOURCES) 51 | prefix_clangformat_setup(${target} ${target_sources}) 52 | endfunction() 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![language.badge]][language.url] [![license.badge]][license.url] 2 | 3 | # ClangFormat.cmake module 4 | 5 | ClangFormat.cmake is a simple CMake module for clang-format support. 6 | 7 | ## Requirements 8 | 9 | The module requires CMake 3.0 or higher and some version of clang-format 10 | installed. 11 | 12 | ## Integration 13 | 14 | 1. Obtain the module and add it into your project's CMake modules path: 15 | 16 | * Copy approach: 17 | 18 | ```bash 19 | $ wget https://raw.githubusercontent.com/zemasoft/clangformat-cmake/master/cmake/ClangFormat.cmake -P cmake 20 | ``` 21 | 22 | ```cmake 23 | # CMakeLists.txt 24 | 25 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) 26 | ``` 27 | 28 | * Submodule approach: 29 | 30 | ```bash 31 | $ git submodule add https://github.com/zemasoft/clangformat-cmake 32 | ``` 33 | 34 | ```cmake 35 | # CMakeLists.txt 36 | 37 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/clangformat-cmake/cmake) 38 | ``` 39 | 40 | 2. Include the module: 41 | 42 | ```cmake 43 | # CMakeLists.txt 44 | 45 | include(ClangFormat) 46 | ``` 47 | 48 | 3. Setup the module: 49 | 50 | ```cmake 51 | # CMakeLists.txt 52 | 53 | clangformat_setup( 54 | src/hello.hpp 55 | src/hello.cpp 56 | ) 57 | ``` 58 | 59 | or: 60 | 61 | ```cmake 62 | # CMakeLists.txt 63 | 64 | target_clangformat_setup(sometarget) 65 | ``` 66 | 67 | ## Usage 68 | 69 | 1. Generate the build system: 70 | 71 | ```bash 72 | $ cmake -S . -Bbuild 73 | ``` 74 | 75 | 2. Format sources anytime using `clangformat` target: 76 | 77 | ```bash 78 | $ cmake --build build --target clangformat 79 | ``` 80 | 81 | or: 82 | 83 | ```bash 84 | $ cd build 85 | $ make clangformat 86 | ``` 87 | 88 | ## Example 89 | 90 | See an example [here](https://github.com/zemasoft/clangformat-cmake-example). 91 | 92 | [language.url]: https://cmake.org/ 93 | [language.badge]: https://img.shields.io/badge/language-CMake-blue.svg 94 | 95 | [license.url]: http://www.boost.org/LICENSE_1_0.txt 96 | [license.badge]: https://img.shields.io/badge/license-Boost%201.0-blue.svg 97 | --------------------------------------------------------------------------------