Skip to content

Commit 4121fa7

Browse files
authored
Merge pull request #224 from thewtex/clang-format-hook
ENH: Add clang-format pre-commit hook
2 parents ea1de65 + eb06310 commit 4121fa7

File tree

6 files changed

+398
-1
lines changed

6 files changed

+398
-1
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Custom attribute to mark sources as using our C++/C code style.
2-
[attr]our-c-style whitespace=tab-in-indent,no-lf-at-eof hooks.style=KWStyle,clangformat
2+
[attr]our-c-style whitespace=tab-in-indent,no-lf-at-eof hooks.style=clangformat
33

44
*.c our-c-style
55
*.h our-c-style

Superbuild/External-ClangFormat.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
include(ExternalProject)
2+
3+
if(WIN32)
4+
set(exe .exe)
5+
endif()
6+
set(CLANG_FORMAT_EXECUTABLE ${CMAKE_CURRENT_BINARY_DIR}/ClangFormat-prefix/src/ClangFormat/clang-format${exe})
7+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/ITKClangFormatConfig.cmake.in"
8+
"${CMAKE_CURRENT_BINARY_DIR}/ITKClangFormatConfig.cmake" @ONLY)
9+
10+
set(_clang_format_hash )
11+
set(_clang_format_url )
12+
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
13+
set(_clang_format_hash
14+
b14de32036c48f6c62998e2ebab509e71a0ae71464acb4616484e3a6eb941e1d9fac38559f5d27ea0cbbb512d590279ffb3015fae17779229e1090c2763ebcf3)
15+
set(_clang_format_url "https://data.kitware.com/api/v1/file/hashsum/sha512/${_clang_format_hash}/download")
16+
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64" AND (NOT CMAKE_HOST_SYSTEM_VERSION VERSION_LESS "13.0.0"))
17+
set(_clang_format_hash
18+
97460f9eef556a27592ccd99d8fc894554e5b3196326df4e33bfcdecfcb7eda2b5c7488008abc4dd923d607f2cb47d61567b1da99df60f31f719195118c117a9)
19+
set(_clang_format_url "https://data.kitware.com/api/v1/file/hashsum/sha512/${_clang_format_hash}/download")
20+
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64")
21+
set(_clang_format_hash
22+
e96dd15938fd9b1c57028a519189f138397774eb6b66971d114300d2a80248adda9f74b192985a3c91c7de52c4dbe21800bc6b3cc8201c4985fc39ecfc64fdbe)
23+
set(_clang_format_url "https://data.kitware.com/api/v1/file/hashsum/sha512/${_clang_format_hash}/download")
24+
else()
25+
# If desired, a compatible clang-format can be provided manually with
26+
# `git config clangFormat.binary /path/to/clang-format`.
27+
endif()
28+
29+
if(NOT TARGET ClangFormat AND _clang_format_hash)
30+
ExternalProject_add(ClangFormat
31+
URL ${_clang_format_url}
32+
URL_HASH SHA512=${_clang_format_hash}
33+
CONFIGURE_COMMAND ""
34+
BUILD_COMMAND ""
35+
LOG_DOWNLOAD 1
36+
LOG_UPDATE 0
37+
LOG_CONFIGURE 0
38+
LOG_BUILD 0
39+
LOG_TEST 0
40+
LOG_INSTALL 0
41+
INSTALL_COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/ITKClangFormatConfig.cmake
42+
)
43+
endif()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set(ITKExamples_SOURCE_DIR "@ITKExamples_SOURCE_DIR@")
2+
set(CMAKE_SOURCE_DIR "@CMAKE_SOURCE_DIR@")
3+
set(CLANG_FORMAT_EXECUTABLE "@CLANG_FORMAT_EXECUTABLE@")
4+
5+
set(WORKING_DIR "")
6+
if(ITKExamples_SOURCE_DIR)
7+
set(WORKING_DIR "${ITKExamples_SOURCE_DIR}/..")
8+
else()
9+
set(WORKING_DIR "${CMAKE_SOURCE_DIR}/..")
10+
endif()
11+
12+
find_package(Git)
13+
if(GIT_FOUND AND EXISTS "${WORKING_DIR}/.git/config")
14+
execute_process(COMMAND ${GIT_EXECUTABLE} config clangFormat.binary
15+
"${CLANG_FORMAT_EXECUTABLE}"
16+
WORKING_DIRECTORY ${WORKING_DIR})
17+
endif()

Superbuild/Superbuild.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,19 @@ ExternalProject_Add(ITKExamples
119119
-DExternalData_OBJECT_STORES:STRING=${ExternalData_OBJECT_STORES}
120120
INSTALL_COMMAND ""
121121
)
122+
123+
# Download and configure clang-format for style uniformity during development.
124+
#
125+
# This code helps download a version of clang-format binaries consistent with
126+
# ITK's clang-format configuration. Git configuration is added during
127+
# development to help automatically apply the formatting.
128+
#
129+
# Globally, the ITK/.clang-format file provides the base style configuration.
130+
# For more information on ITK coding style, see the ITK Coding Style Guide in
131+
# the ITK Software Guide.
132+
option(ITKExamples_USE_CLANG_FORMAT "Enable the use of clang-format for coding style formatting." ${BUILD_TESTING})
133+
mark_as_advanced(ITKExamples_USE_CLANG_FORMAT)
134+
135+
if(BUILD_TESTING AND ITKExamples_USE_CLANG_FORMAT AND NOT CMAKE_CROSSCOMPILING)
136+
include(${CMAKE_SOURCE_DIR}/External-ClangFormat.cmake)
137+
endif()

Utilities/Hooks/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ if test $lastSetupForDevelopment -lt $SetupForDevelopment_VERSION; then
4242
\"$top_level_dir/Utilities/SetupForDevelopment.sh\"
4343
"
4444
fi
45+
46+
#-----------------------------------------------------------------------------
47+
# Style hooks.
48+
. "${BASH_SOURCE%/*}/pre-commit-style.bash"

0 commit comments

Comments
 (0)