From ef5e16f9b668202b0688e65f83dd11f8bfe9868d Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Wed, 29 Nov 2017 17:26:29 +0100
Subject: [PATCH] run astyle over all sources when executing make

---
 CMakeLists.txt                                |  1 +
 CMakeModules/astyle.cmake                     | 31 +++++++++++--------
 CMakeModules/testing_cpp.cmake                |  2 --
 common/cpp/CMakeLists.txt                     |  3 --
 producer/api/CMakeLists.txt                   |  3 --
 .../inotify-event-detector-cpp/CMakeLists.txt |  3 --
 6 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a9364a549..639dee25c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,6 +8,7 @@ option(BUILD_DOCS "Uses doxygen to build the documentaion" OFF)
 
 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules/)
 
+# format sources
 include(astyle)
 
 include(testing_cpp)
diff --git a/CMakeModules/astyle.cmake b/CMakeModules/astyle.cmake
index a6ad6663b..abb7dc9be 100644
--- a/CMakeModules/astyle.cmake
+++ b/CMakeModules/astyle.cmake
@@ -1,14 +1,19 @@
-function(astyle target source_files)
-    find_program(ASTYLE_EXECUTABLE astyle)
-    if(ASTYLE_EXECUTABLE)
-        message(STATUS "Found astyle, using astyle to format code of target ${target}.")
-        add_custom_command(
-                TARGET ${target} PRE_BUILD
-                COMMAND
-                ${ASTYLE_EXECUTABLE} -n --style=1tbs --indent-namespaces --indent-preproc-block ${source_files}
-                WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} VERBATIM
+find_program(ASTYLE_EXECUTABLE astyle)
+
+add_custom_target(ASTYLE)
+
+if(ASTYLE_EXECUTABLE)
+	message(STATUS "Found astyle, using astyle to format code of target ${target}.")
+	add_custom_target(astyle ALL
+		COMMAND
+		${ASTYLE_EXECUTABLE} -i
+			--exclude=${PROJECT_BINARY_DIR}
+			--recursive -n --style=google 
+			"${PROJECT_SOURCE_DIR}/*.cpp" "${PROJECT_SOURCE_DIR}/*.h"
+		WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+		VERBATIM	
         )
-    else()
-        message(WARNING "Unable to find astyle. Skipping code formatting for ${target}")
-    endif()
-endfunction()
+else()
+	message(WARNING "Unable to find astyle. Code formatting willbe skipped")
+endif()
+
diff --git a/CMakeModules/testing_cpp.cmake b/CMakeModules/testing_cpp.cmake
index e476ee067..eeec49167 100644
--- a/CMakeModules/testing_cpp.cmake
+++ b/CMakeModules/testing_cpp.cmake
@@ -12,8 +12,6 @@ function(gtest target test_source_files test_libraries)
 	target_link_libraries(test-${target} ${test_libraries})
 	add_test(NAME test-${target} COMMAND test-${target})
 	
-	astyle(test-${target} ${test_source_files})
-
 	message(STATUS "Added test 'test-${target}'")
         
 	if(CMAKE_COMPILER_IS_GNUCXX)
diff --git a/common/cpp/CMakeLists.txt b/common/cpp/CMakeLists.txt
index a291827b8..8a24c3acc 100644
--- a/common/cpp/CMakeLists.txt
+++ b/common/cpp/CMakeLists.txt
@@ -9,9 +9,6 @@ add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
 target_include_directories(${TARGET_NAME} PUBLIC include)
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
 
-astyle(${TARGET_NAME} ${SOURCE_FILES})
-
-
 ################################
 # Testing
 ################################
diff --git a/producer/api/CMakeLists.txt b/producer/api/CMakeLists.txt
index 97a0876ee..40d5bc8d8 100644
--- a/producer/api/CMakeLists.txt
+++ b/producer/api/CMakeLists.txt
@@ -10,9 +10,6 @@ target_include_directories(${TARGET_NAME} PUBLIC include)
 target_link_libraries(${TARGET_NAME} common)
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
 
-astyle(${TARGET_NAME} ${SOURCE_FILES})
-
-
 ################################
 # Testing
 ################################
diff --git a/producer/inotify-event-detector-cpp/CMakeLists.txt b/producer/inotify-event-detector-cpp/CMakeLists.txt
index f940663aa..dcb6da543 100644
--- a/producer/inotify-event-detector-cpp/CMakeLists.txt
+++ b/producer/inotify-event-detector-cpp/CMakeLists.txt
@@ -10,9 +10,6 @@ target_include_directories(${TARGET_NAME} PRIVATE include)
 target_link_libraries(${TARGET_NAME} common producer-api)
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
 
-astyle(${TARGET_NAME} ${SOURCE_FILES})
-
-
 ################################
 # Testing
 ################################
-- 
GitLab