Skip to content

Commit 0c30bb3

Browse files
committed
1 parent 06656da commit 0c30bb3

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ repos:
3838
name: Format C/C++ code using clang-format.
3939
language: system
4040
files: \.(c|cc|cxx|cpp|h|hpp|hxx)$
41-
entry: clang-format -i
42-
args: [--style=file]
41+
entry: bin/admin/clang-format.sh
42+
args: [--style=file -i]

bin/admin/clang-format.sh

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
# these are the versions of clang-format that are supported required
4+
# should be ordered from oldest to newest to make sure the newest is picked
5+
supported_clang_format_versions="16 17"
6+
preferred_clang_format_version="" # prefer most recent supported clang-format version
7+
for v in $supported_clang_format_versions; do
8+
preferred_clang_format_version=$v
9+
done
10+
11+
# append common locations of clang-format to PATH
12+
unameOut="$(uname -s)"
13+
case "${unameOut}" in
14+
Darwin*)
15+
extra_path=""
16+
# this prefers more recent versions
17+
for v in $supported_clang_format_versions; do
18+
extra_path=/opt/homebrew/opt/llvm@$v/bin:/opt/homebrew/opt/clang-format@$v/bin:$extra_path
19+
done
20+
# prepend paths
21+
export PATH=$extra_path:$PATH:/opt/homebrew/bin
22+
;;
23+
esac
24+
25+
path_to_clang_format=`which clang-format`
26+
have_supported_clang_format_version=0
27+
if [[ "X$path_to_clang_format" != "X" ]]; then
28+
29+
# check clang-format version
30+
clang_format_version=`clang-format --version | sed 's/.* version //' | awk -F'[.]' '{print $1}'`
31+
32+
#echo "supported_clang_format_versions=\"$supported_clang_format_versions\" clang_format_version=$clang_format_version"
33+
34+
# if found clang-format, but wrong version, check if docker is available
35+
for v in $supported_clang_format_versions; do
36+
if [[ $clang_format_version -eq $v ]]; then
37+
have_supported_clang_format_version=1
38+
break
39+
fi
40+
done
41+
fi
42+
43+
if [[ $have_supported_clang_format_version -eq 0 ]]; then
44+
echo "WARNING: found clang-format with unsupported version $clang_format_version (supported versions: $supported_clang_format_versions)"
45+
46+
# look for docker
47+
path_to_docker=`which docker`
48+
if [[ "X$path_to_docker" = "X" ]]; then
49+
echo "ERROR: docker is not found either, PATH=$PATH, install one of supported clang-format versions (any of these: $supported_clang_format_versions) or install docker"
50+
exit 1
51+
fi
52+
53+
# if docker up?
54+
docker info >/dev/null 2>&1
55+
if [[ $? -ne 0 ]]; then
56+
echo "ERROR: docker is found but not running, start it"
57+
exit 1
58+
fi
59+
60+
# use docker to run clang-format
61+
mount_path=$(readlink -f "$HOME")
62+
63+
# convert file names in the arguments to relative paths
64+
args=""
65+
for i in "$@"; do
66+
# skip options
67+
if [[ "$i" == -* ]]; then
68+
args="$args $i"
69+
continue
70+
fi
71+
abs_file_path=$(readlink -f "$i")
72+
if [[ "X$abs_file_path" = "X" ]]; then
73+
echo "ERROR: given file $i is not found"
74+
exit 1
75+
fi
76+
77+
dir=$(dirname $abs_file_path)
78+
file_path_relative_to_project_root=$(basename $abs_file_path)
79+
while [[ "$dir" != "$mount_path" && "$dir" != "/" ]]; do
80+
file_path_relative_to_project_root="$(basename $dir)/$file_path_relative_to_project_root"
81+
dir=$(dirname $dir)
82+
#echo "dir=$dir file_path_relative_to_project_root=$file_path_relative_to_project_root"
83+
done
84+
if [[ "$dir" == "/" ]]; then
85+
echo "ERROR: given file $i (absolute path $abs_file_path) is not under \$HOME=$mount_path, cannot use docker-based clang-format in this case"
86+
exit 1
87+
fi
88+
args="$args /hostHOME/$file_path_relative_to_project_root"
89+
done
90+
docker run --platform linux/x86_64 -v $mount_path:/hostHOME xianpengshen/clang-tools:$preferred_clang_format_version clang-format $args
91+
else
92+
#echo "found $path_to_clang_format with required version $clang_format_version"
93+
clang-format $*
94+
fi

0 commit comments

Comments
 (0)