geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbarr...@apache.org
Subject [geode-native] 01/06: GEODE-5847: Integrate clang-format into CMake.
Date Sat, 13 Oct 2018 22:10:50 GMT
This is an automated email from the ASF dual-hosted git repository.

jbarrett pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-native.git

commit 1b2e8aa39dea874a550fccb86a5899bd382608df
Author: Jacob Barrett <jbarrett@pivotal.io>
AuthorDate: Wed Oct 10 09:55:25 2018 -0700

    GEODE-5847: Integrate clang-format into CMake.
    
    * Formats all C++ sources on compile if they have changed.
---
 CMakeLists.txt                                     |  2 +
 cmake/ClangFormat.cmake                            | 66 ++++++++++++++++++++++
 .../CMakeLists.txt => cmake/FindClangFormat.cmake  | 34 ++++-------
 cppcache/integration-test-2/CMakeLists.txt         |  3 +
 cppcache/integration-test/CMakeLists.txt           | 27 +++++----
 cppcache/src/version.h.in                          |  4 ++
 cppcache/static/CMakeLists.txt                     |  2 +
 cppcache/test/CMakeLists.txt                       |  2 +
 cryptoimpl/CMakeLists.txt                          |  2 +
 dhimpl/CMakeLists.txt                              |  2 +
 templates/security/CMakeLists.txt                  |  2 +
 tests/cpp/fwklib/CMakeLists.txt                    |  2 +
 tests/cpp/security/CMakeLists.txt                  |  2 +
 tests/cpp/testobject/CMakeLists.txt                |  2 +
 14 files changed, 119 insertions(+), 33 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a8a556f..6f50ecb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,6 +25,8 @@ if (USE_PCH)
   include(cotire)
 endif()
 
+include(ClangFormat)
+
 if(CMAKE_GENERATOR MATCHES Win64*)
 
   if ((CMAKE_GENERATOR MATCHES "Visual Studio") AND (CMAKE_GENERATOR_TOOLSET STREQUAL ""))
diff --git a/cmake/ClangFormat.cmake b/cmake/ClangFormat.cmake
new file mode 100644
index 0000000..fb93b79
--- /dev/null
+++ b/cmake/ClangFormat.cmake
@@ -0,0 +1,66 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set(ClangFormat_FLAGS "-style=file;-fallback-style=Google" CACHE STRING "Flags sent to clang-format")
+
+find_package(ClangFormat QUIET)
+
+function(add_clangformat _target)
+  if (ClangFormat_FOUND)
+    if (NOT TARGET ${_target})
+      message(FATAL_ERROR "add_clangformat should only be called on targets (got " ${_target}
")")
+    endif ()
+
+    # figure out which sources this should be applied to
+    get_target_property(_target_SOURCES ${_target} SOURCES)
+    get_target_property(_target_BINARY_DIR ${_target} BINARY_DIR)
+    get_target_property(_target_SOURCE_DIR ${_target} SOURCE_DIR)
+
+    set(_clangformat "${_target}-clangformat")
+    set(_clangformat_SOURCES "")
+    foreach (_source ${_target_SOURCES})
+      if (NOT TARGET ${_source})
+        get_source_file_property(_source_LOCATION "${_source}" LOCATION)
+        get_source_file_property(_source_GENERATED "${_source}" GENERATED)
+
+        if(_source_GENERATED)
+          break()
+        endif()
+
+        file(RELATIVE_PATH _source_RELATIVE_PATH ${_target_SOURCE_DIR} ${_source_LOCATION})
+        string(REPLACE ".." "__" _format_file "${_target_BINARY_DIR}/CMakeFiles/${_clangformat}.dir/${_source_RELATIVE_PATH}.format")
+        get_filename_component(_format_DIRECTORY ${_format_file} DIRECTORY)
+        file(MAKE_DIRECTORY ${_format_DIRECTORY})
+
+        add_custom_command(OUTPUT ${_format_file}
+            DEPENDS ${_source}
+            COMMENT "Clang-Format ${_source}"
+            COMMAND ${ClangFormat_EXECUTABLE} ${ClangFormat_FLAGS} -i ${_source_LOCATION}
+            COMMAND ${CMAKE_COMMAND} -E touch ${_format_file})
+
+        list(APPEND _clangformat_SOURCES ${_format_file})
+      endif ()
+    endforeach ()
+
+    if (_clangformat_SOURCES)
+      add_custom_target(${_clangformat}
+          SOURCES ${_clangformat_SOURCES}
+          COMMENT "Clang-Format for target ${_target}")
+
+      add_dependencies(${_target} ${_clangformat})
+    endif ()
+
+  endif ()
+endfunction()
diff --git a/dhimpl/CMakeLists.txt b/cmake/FindClangFormat.cmake
similarity index 61%
copy from dhimpl/CMakeLists.txt
copy to cmake/FindClangFormat.cmake
index 0ce8239..b06fc5c 100644
--- a/dhimpl/CMakeLists.txt
+++ b/cmake/FindClangFormat.cmake
@@ -4,37 +4,25 @@
 # The ASF licenses this file to You under the Apache License, Version 2.0
 # (the "License"); you may not use this file except in compliance with
 # the License.  You may obtain a copy of the License at
-# 
+#
 #      http://www.apache.org/licenses/LICENSE-2.0
-# 
+#
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-cmake_minimum_required(VERSION 3.10)
-project(DHImpl LANGUAGES CXX)
+include(FindPackageHandleStandardArgs)
 
-file(GLOB_RECURSE SOURCES "*.cpp")
+set(ClangFormat_EXECUTABLE "" CACHE FILEPATH "Path to clang-format executable.")
 
-add_library(DHImpl SHARED ${SOURCES})
-
-set_target_properties(DHImpl PROPERTIES
-  FOLDER cpp/test/integration)
-
-include(GenerateExportHeader)
-generate_export_header(DHImpl)
-
-target_include_directories(DHImpl
-  PUBLIC
-	$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
+find_program(ClangFormat_EXECUTABLE
+  NAMES clang-format
+  DOC "clang-format executable"
+)
+mark_as_advanced(ClangFormat_EXECUTABLE)
 
-target_link_libraries(DHImpl
-  PUBLIC 
-    apache-geode
-    crypto
-    c++11
-  PRIVATE
-    _WarningsAsError
+find_package_handle_standard_args(ClangFormat
+  REQUIRED_VARS ClangFormat_EXECUTABLE
 )
diff --git a/cppcache/integration-test-2/CMakeLists.txt b/cppcache/integration-test-2/CMakeLists.txt
index 09d4741..422d393 100644
--- a/cppcache/integration-test-2/CMakeLists.txt
+++ b/cppcache/integration-test-2/CMakeLists.txt
@@ -77,6 +77,9 @@ endif()
 
 set_target_properties(integration-test-2 PROPERTIES
 	FOLDER cpp/test/integration)
+
+add_clangformat(integration-test-2)
+
 enable_testing()
 include(GoogleTest)
 gtest_discover_tests(integration-test-2)
\ No newline at end of file
diff --git a/cppcache/integration-test/CMakeLists.txt b/cppcache/integration-test/CMakeLists.txt
index f6eccc5..e2ea203 100644
--- a/cppcache/integration-test/CMakeLists.txt
+++ b/cppcache/integration-test/CMakeLists.txt
@@ -16,13 +16,14 @@
 cmake_minimum_required(VERSION 3.10)
 project(nativeclient.tests.cppcache LANGUAGES CXX)
 
-set(TEST_UTILS_LIB test-cppcache-utils)
-add_library(${TEST_UTILS_LIB} STATIC
+add_library(test-cppcache-utils STATIC
   fw_dunit.cpp
   BBNamingContext.cpp
   CacheHelper.cpp
-  CacheableWrapper.cpp)
-target_link_libraries(${TEST_UTILS_LIB}
+  CacheableWrapper.cpp
+)
+
+target_link_libraries(test-cppcache-utils
   PRIVATE
     ACE
     _WarningsAsError
@@ -31,18 +32,20 @@ target_link_libraries(${TEST_UTILS_LIB}
     framework
 )
 
-set_target_properties(${TEST_UTILS_LIB} PROPERTIES
+set_target_properties(test-cppcache-utils PROPERTIES
 	FOLDER cpp/test/integration
 )
 
-target_compile_definitions(${TEST_UTILS_LIB}
+target_compile_definitions(test-cppcache-utils
   PRIVATE
-    $<MAKE_C_IDENTIFIER:${TEST_UTILS_LIB}>_static=1
+    $<MAKE_C_IDENTIFIER:test-cppcache-utils>_static=1
 )
-target_include_directories(${TEST_UTILS_LIB}
+target_include_directories(test-cppcache-utils
   PRIVATE
     $<TARGET_PROPERTY:apache-geode,SOURCE_DIR>/../src
 )
+add_clangformat(test-cppcache-utils)
+
 
 add_library(unit_test_callbacks SHARED LibraryCallbacks.cpp)
 target_link_libraries(unit_test_callbacks
@@ -57,7 +60,9 @@ target_include_directories(unit_test_callbacks
     $<TARGET_PROPERTY:apache-geode,SOURCE_DIR>/../src
 )
 
- set_target_properties(unit_test_callbacks PROPERTIES FOLDER cpp/test/integration)
+set_target_properties(unit_test_callbacks PROPERTIES FOLDER cpp/test/integration)
+
+add_clangformat(unit_test_callbacks)
 
 
 enable_testing()
@@ -92,7 +97,7 @@ foreach(FILE ${SOURCES})
   target_link_libraries(${TEST}
     PRIVATE
       ACE
-      ${TEST_UTILS_LIB}
+      test-cppcache-utils
       _WarningsAsError
       _CppCodeCoverage
       internal
@@ -118,6 +123,8 @@ foreach(FILE ${SOURCES})
   # Some tests depend on these library
   add_dependencies(${TEST} securityImpl cryptoImpl DHImpl SqLiteImpl)
 
+  add_clangformat(${TEST})
+
   set(TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}/.tests/${TEST})
 
   math(EXPR PORT "${PORT} + 1")
diff --git a/cppcache/src/version.h.in b/cppcache/src/version.h.in
index 12c1da1..6eb387b 100644
--- a/cppcache/src/version.h.in
+++ b/cppcache/src/version.h.in
@@ -15,8 +15,12 @@
  * limitations under the License.
  */
 
+// clang-format off
+
 #define PRODUCT_VERSION "@PRODUCT_VERSION@"
 #define PRODUCT_BUILDDATE "@PRODUCT_BUILDDATE@"
 #define PRODUCT_SOURCE_REVISION "@PRODUCT_SOURCE_REVISION@"
 #define PRODUCT_SOURCE_REPOSITORY "@PRODUCT_SOURCE_REPOSITORY@"
 #define PRODUCT_VERSION_RC @PRODUCT_VERSION_RC@
+
+// clang-format on
diff --git a/cppcache/static/CMakeLists.txt b/cppcache/static/CMakeLists.txt
index 373cffd..b387871 100644
--- a/cppcache/static/CMakeLists.txt
+++ b/cppcache/static/CMakeLists.txt
@@ -63,3 +63,5 @@ else()
   set_target_properties(apache-geode-static PROPERTIES
     FOLDER cpp)
 endif()
+
+add_clangformat(apache-geode-static)
diff --git a/cppcache/test/CMakeLists.txt b/cppcache/test/CMakeLists.txt
index 594d6e0..1fa77c9 100644
--- a/cppcache/test/CMakeLists.txt
+++ b/cppcache/test/CMakeLists.txt
@@ -74,6 +74,8 @@ set_target_properties(apache-geode_unittests PROPERTIES
   FOLDER cpp/test/unit
   )
 
+add_clangformat(apache-geode_unittests)
+
 enable_testing()
 add_test(NAME ${PROJECT_NAME} COMMAND $<TARGET_FILE:${PROJECT_NAME}>)
 
diff --git a/cryptoimpl/CMakeLists.txt b/cryptoimpl/CMakeLists.txt
index c7028b2..61acaa3 100644
--- a/cryptoimpl/CMakeLists.txt
+++ b/cryptoimpl/CMakeLists.txt
@@ -47,6 +47,8 @@ target_link_libraries(cryptoImpl
    c++11
 )
 
+add_clangformat(cryptoImpl)
+
 if ("Windows" STREQUAL ${CMAKE_SYSTEM_NAME} )
   install(TARGETS cryptoImpl
     RUNTIME DESTINATION bin
diff --git a/dhimpl/CMakeLists.txt b/dhimpl/CMakeLists.txt
index 0ce8239..a5fe395 100644
--- a/dhimpl/CMakeLists.txt
+++ b/dhimpl/CMakeLists.txt
@@ -38,3 +38,5 @@ target_link_libraries(DHImpl
   PRIVATE
     _WarningsAsError
 )
+
+add_clangformat(DHImpl)
diff --git a/templates/security/CMakeLists.txt b/templates/security/CMakeLists.txt
index 1f91042..df57aa2 100644
--- a/templates/security/CMakeLists.txt
+++ b/templates/security/CMakeLists.txt
@@ -42,3 +42,5 @@ target_link_libraries(securityImpl
   PRIVATE
     _WarningsAsError
 )
+
+add_clangformat(securityImpl)
diff --git a/tests/cpp/fwklib/CMakeLists.txt b/tests/cpp/fwklib/CMakeLists.txt
index acb532b..f57c01e 100644
--- a/tests/cpp/fwklib/CMakeLists.txt
+++ b/tests/cpp/fwklib/CMakeLists.txt
@@ -65,5 +65,7 @@ target_link_libraries(framework
 	  _WarningsAsError
 )
 
+add_clangformat(framework)
+
 # For Visual Studio organization
 set_target_properties(framework PROPERTIES FOLDER cpp/test/integration)
diff --git a/tests/cpp/security/CMakeLists.txt b/tests/cpp/security/CMakeLists.txt
index 09ec04b..e82b470 100644
--- a/tests/cpp/security/CMakeLists.txt
+++ b/tests/cpp/security/CMakeLists.txt
@@ -52,5 +52,7 @@ target_link_libraries(security
 	_WarningsAsError
 )
 
+add_clangformat(security)
+
 # For Visual Studio organization
 set_target_properties(security PROPERTIES FOLDER cli/test/integration)
diff --git a/tests/cpp/testobject/CMakeLists.txt b/tests/cpp/testobject/CMakeLists.txt
index 15cdd81..eddd919 100644
--- a/tests/cpp/testobject/CMakeLists.txt
+++ b/tests/cpp/testobject/CMakeLists.txt
@@ -97,5 +97,7 @@ target_link_libraries(testobject
 	  _WarningsAsError
 )
 
+add_clangformat(testobject)
+
 # For Visual Studio organization
 set_target_properties(testobject PROPERTIES FOLDER test-common)


Mime
View raw message