[Tarantool-patches] [PATCH 3/5] build: add clang-format rules

Kirill Yukhin kyukhin at tarantool.org
Fri Oct 30 14:43:50 MSK 2020


This patch introduces support of `make format-set`
and `make format-check` commands which invoes Clang
v'11 formatter.
---
 CMakeLists.txt | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 512f50e..cd72e15 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,11 +20,13 @@ include(FindPackageMessage)
 include(ExternalProject)
 
 find_program(ECHO echo)
-find_program(CAT cat)
 find_program(BASH bash)
+find_program(CAT cat)
+find_program(CLANG_FORMAT clang-format-11)
+find_program(CTAGS ctags)
+find_program(FIND find)
 find_program(GIT git)
 find_program(LD ld)
-find_program(CTAGS ctags)
 find_program(LUACHECK luacheck ENV PATH)
 
 # Define PACKAGE macro in tarantool/config.h
@@ -164,6 +166,38 @@ add_custom_command(TARGET luacheck
     COMMENT "Perform static analysis of Lua code"
 )
 
+if(CLANG_FORMAT)
+    #
+    # Enable 'make format-set' target.
+    #
+    add_custom_target(format-set
+	COMMAND ${FIND} "${PROJECT_SOURCE_DIR}/src/box"
+	        -iname "*.h" -o -iname "*.c" -o -iname "*.cc" |grep -v sql
+		|xargs ${CLANG_FORMAT} -i
+	COMMENT "Perform code style update w/ clang-format over code base"
+    )
+
+    #
+    # Enable 'make format-check' target.
+    #
+    add_custom_target(format-check
+	COMMAND ${FIND} "${PROJECT_SOURCE_DIR}/src/box"
+                -iname "*.h" -o -iname "*.c" -o -iname "*.cc"
+	        |grep -v sql
+	        |xargs ${CLANG_FORMAT} -output-replacements-xml
+	        |tee
+	        ${CMAKE_BINARY_DIR}/check_clang_format_file.txt |
+                grep -c "replacement " | tr -d "[:cntrl:]" && echo
+                " replacements necessary"
+        COMMAND ! grep -c "replacement "
+                ${CMAKE_BINARY_DIR}/check_clang_format_file.txt >
+                /dev/null
+	COMMENT "Check code style w/ clang-format over code base"
+    )
+else()
+    message(WARNING "Clang formatter v11 (clang-format-11) wasn't found")
+endif()
+
 if (WITH_JEPSEN)
     ExternalProject_Add(
         jepsen-tests
-- 
1.8.3.1



More information about the Tarantool-patches mailing list