diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..43301e18e --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +--- +Language: Cpp +BasedOnStyle: Google +ColumnLimit: 120 +SortIncludes: false diff --git a/CMakeLists.txt b/CMakeLists.txt index 23d36d5b7..afa52ef7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -539,6 +539,23 @@ if(EIGEN_BUILD_BTL) add_subdirectory(bench/btl EXCLUDE_FROM_ALL) endif() +find_package(CLANG_FORMAT 9 EXACT) +if(CLANG_FORMAT_FOUND) +set(FORMAT_SOURCES) +list(APPEND FORMAT_SUBDIRS blas bench demos "doc" Eigen include lapack scripts share unsupported test failtest) +foreach(DIR ${FORMAT_SUBDIRS}) + set(ABS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${DIR}) + file(GLOB_RECURSE ${DIR}_SOURCES ${ABS_DIR}/*.cc ${ABS_DIR}/*.h ${ABS_DIR}/*.cpp ${ABS_DIR}/*.hpp ${ABS_DIR}/*.c) + list(APPEND FORMAT_SOURCES ${${DIR}_SOURCES}) + endforeach() + file(GLOB FORMAT_SOURCES_WITHOUTENDING LIST_DIRECTORIES false ${CMAKE_CURRENT_SOURCE_DIR}/Eigen/* ${CMAKE_CURRENT_SOURCE_DIR}/Eigen/CXX11/* ${CMAKE_CURRENT_SOURCE_DIR}/unsupported/Eigen/* ${CMAKE_CURRENT_SOURCE_DIR}/unsupported/Eigen/CXX11/*) + list(FILTER FORMAT_SOURCES_WITHOUTENDING EXCLUDE REGEX ".*.txt$") + list (APPEND FORMAT_SOURCES ${FORMAT_SOURCES_WITHOUTENDING}) + add_custom_target(format + COMMAND ${CLANG_FORMAT_EXECUTABLE} -i -style=file ${FORMAT_SOURCES} + DEPENDS ${FORMAT_SOURCES}) +endif() + if(NOT WIN32) add_subdirectory(bench/spbench EXCLUDE_FROM_ALL) endif() @@ -577,6 +594,9 @@ if(PROJECT_IS_TOP_LEVEL) message(STATUS "check | Build and run the unit-tests. Read this page:") message(STATUS " | http://eigen.tuxfamily.org/index.php?title=Tests") endif() + if(CLANG_FORMAT_FOUND) + message(STATUS "format | Formats the source code according to .clang-format file") + endif() message(STATUS "blas | Build BLAS library (not the same thing as Eigen)") message(STATUS "uninstall| Remove files installed by the install target") message(STATUS "---------+--------------------------------------------------------------") diff --git a/cmake/FindCLANG_FORMAT.cmake b/cmake/FindCLANG_FORMAT.cmake new file mode 100644 index 000000000..e00f19fe6 --- /dev/null +++ b/cmake/FindCLANG_FORMAT.cmake @@ -0,0 +1,61 @@ + + +# Find clang-format +# +# CLANG_FORMAT_EXECUTABLE - Path to clang-format executable +# CLANG_FORMAT_FOUND - True if the clang-format executable was found. +# CLANG_FORMAT_VERSION - The version of clang-format found +# +# Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org) +# +# Licensed under the Mozilla Public 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 +# +# https://www.mozilla.org/en-US/MPL/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. +# + +find_program(CLANG_FORMAT_EXECUTABLE + NAMES + clang-format-9 + clang-format + clang-format-11 + clang-format-10 + clang-format-8 + clang-format-7 + + DOC "clang-format executable") +mark_as_advanced(CLANG_FORMAT_EXECUTABLE) + +# Extract version from command "clang-format -version" +if(CLANG_FORMAT_EXECUTABLE) + execute_process(COMMAND ${CLANG_FORMAT_EXECUTABLE} -version + OUTPUT_VARIABLE clang_format_version + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) + + if(clang_format_version MATCHES "^.*clang-format version .*") + # clang_format_version sample: "clang-format version 3.9.1-4ubuntu3~16.04.1 + # (tags/RELEASE_391/rc2)" + string(REGEX + REPLACE "^.*clang-format version ([.0-9]+).*" + "\\1" + CLANG_FORMAT_VERSION + "${clang_format_version}") + # CLANG_FORMAT_VERSION sample: "3.9.1" + else() + set(CLANG_FORMAT_VERSION 0.0) + endif() +else() + set(CLANG_FORMAT_VERSION 0.0) +endif() + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set CLANG_FORMAT_FOUND to TRUE +# if all listed variables are TRUE +find_package_handle_standard_args(CLANG_FORMAT REQUIRED_VARS CLANG_FORMAT_EXECUTABLE VERSION_VAR CLANG_FORMAT_VERSION)