Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DLR-AMR
GitHub Repository: DLR-AMR/t8code
Path: blob/main/scripts/t8indent.sh
900 views
1
#! /bin/bash
2
3
# This file is part of t8code.
4
# t8code is a C library to manage a collection (a forest) of multiple
5
# connected adaptive space-trees of general element classes in parallel.
6
#
7
# Copyright (C) 2023 the developers
8
#
9
# t8code is free software; you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation; either version 2 of the License, or
12
# (at your option) any later version.
13
#
14
# t8code is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License
20
# along with t8code; if not, write to the Free Software Foundation, Inc.,
21
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23
# We use clang-format to indent our code. There are different base styles
24
# available, but we use a modification. The options for this modification
25
# are located in the .clang-format file.
26
# We have to use the -i argument, so that clang-format directly alters the
27
# files instead of printing the changes to stdout. The --style=file
28
# arguments tells clang-format to look for a *.clang-format file.
29
#
30
# If you call this script with "NO_CHANGE" as first argument it will run
31
# in dry-mode, not changing the file contents.
32
FORMAT_OPTIONS="--Werror -i --style=file"
33
34
# Required version of the clang format program.
35
REQUIRED_VERSION_MAJOR="17"
36
REQUIRED_VERSION_MINOR="0"
37
REQUIRED_VERSION_STRING="${REQUIRED_VERSION_MAJOR}.${REQUIRED_VERSION_MINOR}"
38
39
FORMAT=`which clang-format 2> /dev/null`
40
41
# Check if ${FORMAT} exists
42
if [ -z "$FORMAT" ]
43
then
44
# Exit if the indentation script was not found
45
echo "ERROR: clang-format not found."
46
echo "Please install clang-format version ${REQUIRED_VERSION_STRING}."
47
echo "See https://github.com/ssciwr/clang-format-wheel"
48
exit 1
49
fi
50
51
# Get indentation script version and split it into major, minor, patch
52
CLANG_VERSION_STRING=`$FORMAT --version`
53
54
[[ $CLANG_VERSION_STRING =~ version[[:space:]]+([^[:space:]]+) ]] &&
55
VERSION="${BASH_REMATCH[1]}"
56
MAJOR=`echo $VERSION | cut -d. -f1`
57
MINOR=`echo $VERSION | cut -d. -f2`
58
PATCH=`echo $VERSION | cut -d. -f3`
59
60
# Check if version matches required version
61
if [[ "$MAJOR" != "$REQUIRED_VERSION_MAJOR" || $MINOR != "$REQUIRED_VERSION_MINOR" ]]; then
62
echo "Please install clang-format version $REQUIRED_VERSION_STRING"
63
exit 1
64
fi
65
66
#
67
# Parsing of input files and throwing out files to be ignored
68
#
69
# Read all lines from the IGNORE_FILE
70
# that are not empty and are not comments (i.e. start with '#').
71
# Determine base directory of git repo
72
GIT_REPO_PATH=$(git rev-parse --show-toplevel)
73
74
IGNORE_FILE=${GIT_REPO_PATH}/scripts/t8indent_ignore.sh
75
files_to_ignore=()
76
while read line; do
77
if [[ ${line:0:1} != "#" ]] && [[ $line != "" ]]
78
then
79
files_to_ignore+=("$line")
80
fi
81
done <$IGNORE_FILE
82
83
#
84
# Check if first argument is "NO_CHANGE", if so
85
# the file content is not changed.
86
#
87
OUTFILE_OPTION=
88
NO_CHANGE=FALSE
89
if [[ $1 == "NO_CHANGE" ]]
90
then
91
shift # Removes first argument from $@ list
92
NO_CHANGE=TRUE
93
FORMAT_OPTIONS="${FORMAT_OPTIONS} --dry-run"
94
fi
95
96
97
# Iterate over all arguments and throw
98
# away those filenames that we should ignore.
99
# Also check if suffix is ".c" ".cxx" ".h" or ".hxx"
100
for arg in "$@"
101
do
102
FILE_SUFFIX="${arg##*.}"
103
if ! [ $FILE_SUFFIX = "c" -o $FILE_SUFFIX = "h" -o $FILE_SUFFIX = "cxx" -o $FILE_SUFFIX = "hxx" ]
104
then
105
echo "ERROR: File "$arg" does not have valid suffix (.c .h .cxx .hxx)."
106
exit 1
107
fi
108
109
ignore_arg=0
110
# Iterate over each ignore filename
111
for ignore_file in "${files_to_ignore[@]}"
112
do
113
echo Checking "$arg" against "${GIT_REPO_PATH}/$ignore_file"
114
if [[ "$arg" -ef "${GIT_REPO_PATH}/$ignore_file" ]]
115
then
116
# arg matches and will be ignored
117
echo The file \"$arg\" will be ignored by indentation as specified in \"$IGNORE_FILE\".
118
ignore_arg=1
119
fi
120
done
121
# Now add all non-ignored files to a new argument array
122
if [[ $ignore_arg == 0 ]]
123
then
124
newargs+=("$arg")
125
fi
126
done
127
128
#
129
# Do the actual indentation.
130
# If NO_CHANGE was set, this does a dry run and we additionally print stderr to stdout.
131
#
132
if [[ $NO_CHANGE == "TRUE" ]]
133
then
134
$FORMAT $FORMAT_OPTIONS ${newargs[@]} 2>&1
135
status=$?
136
else
137
$FORMAT $FORMAT_OPTIONS ${newargs[@]}
138
status=$?
139
fi
140
141
# If the file content was not changed, the return
142
# value determines whether or not the file was
143
# indented.
144
if [[ $NO_CHANGE == "TRUE" ]]
145
then
146
exit $status
147
fi
148
149