build FEATURE source formatting using uncrustify
diff --git a/CMakeModules/FindUncrustify.cmake b/CMakeModules/FindUncrustify.cmake
new file mode 100644
index 0000000..3b013e8
--- /dev/null
+++ b/CMakeModules/FindUncrustify.cmake
@@ -0,0 +1,21 @@
+# - Find uncrustify
+# Find the uncrustify binary.
+#
+# UNCRUSTIFY - path ot the binary
+# UNCRUSTIFY_VERSION - found version
+# UNCRUSTIFY_FOUND - True if uncrustify found.
+include(FindPackageHandleStandardArgs)
+
+find_program(UNCRUSTIFY uncrustify)
+if(UNCRUSTIFY)
+ execute_process(COMMAND ${UNCRUSTIFY} --version OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE VERSION)
+ string(FIND ${VERSION} "-" START_IDX)
+ math(EXPR START_IDX "${START_IDX} + 1")
+ string(SUBSTRING "${VERSION}" ${START_IDX} -1 VERSION)
+
+ string(FIND ${VERSION} "-" LEN)
+ string(SUBSTRING "${VERSION}" 0 ${LEN} UNCRUSTIFY_VERSION)
+endif()
+
+# Handle the QUIETLY and REQUIRED arguments and set UNCRUSTIFY_FOUND to TRUE if all listed variables are TRUE.
+find_package_handle_standard_args(Uncrustify REQUIRED_VARS UNCRUSTIFY VERSION_VAR UNCRUSTIFY_VERSION)
diff --git a/CMakeModules/SourceFormat.cmake b/CMakeModules/SourceFormat.cmake
new file mode 100644
index 0000000..76132a4
--- /dev/null
+++ b/CMakeModules/SourceFormat.cmake
@@ -0,0 +1,32 @@
+# format source files with uncrustify
+
+# check that format checking is available - always use before SOURCE_FORMAT
+macro(SOURCE_FORMAT_ENABLE)
+ find_package(Uncrustify 0.71)
+ if(UNCRUSTIFY_FOUND)
+ set(SOURCE_FORMAT_ENABLED TRUE)
+ else()
+ set(SOURCE_FORMAT_ENABLED FALSE)
+ endif()
+endmacro()
+
+# files are expected to be a list and relative paths are resolved wtih respect to CMAKE_SOURCE DIR
+macro(SOURCE_FORMAT)
+ if(NOT ${ARGC})
+ message(FATAL_ERROR "source_format() needs a list of files to format!")
+ endif()
+
+ if(SOURCE_FORMAT_ENABLED)
+ add_custom_target(format
+ COMMAND ${UNCRUSTIFY} -c ${CMAKE_SOURCE_DIR}/uncrustify.cfg --no-backup --replace ${ARGN}
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+ COMMENT "Formating sources with ${UNCRUSTIFY} ...")
+
+ add_custom_target(format-check
+ COMMAND ${UNCRUSTIFY} -c ${CMAKE_SOURCE_DIR}/uncrustify.cfg --check ${ARGN}
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+ COMMENT "Checking format of the sources with ${UNCRUSTIFY} ...")
+
+ set(SOURCE_FORMAT_ENABLED TRUE)
+ endif()
+endmacro()