Debian Codegraph @

ci/calamaresstyle

raw
1 #!/bin/sh
2 #
3 # SPDX-FileCopyrightText: 2014 Aurélien Gâteau <[email protected]>
4 # SPDX-FileCopyrightText: 2019 Adriaan de Groot <[email protected]>
5 # SPDX-License-Identifier: BSD-2-Clause
6 #
7 # Apply Calamares-style formatting to sources. Requires clang-format-15.
8 #
9 # You can pass in directory names, in which case the files
10 # in that directory (NOT below it) are processed.
11 #
12 # If the environment variable CLANG_FORMAT is set to a (full path) and
13 # that path is executable, it will be used if possible.
14 #
15 LANG=C
16 LC_ALL=C
17 LC_NUMERIC=C
18 export LANG LC_ALL LC_NUMERIC
19
20 BASEDIR=$(dirname $0)
21 TOPDIR=$( cd $BASEDIR/.. && pwd -P )
22 test -d "$BASEDIR" || { echo "! Could not determine base for $0" ; exit 1 ; }
23 test -d "$TOPDIR" || { echo "! Cound not determine top-level source dir" ; exit 1 ; }
24 test -f "$TOPDIR/.clang-format" || { echo "! No .clang-format support files in $TOPDIR" ; exit 1 ; }
25
26 # Start with CLANG_FORMAT, if it is specified
27 CF_VERSIONS=""
28 if test -n "$CLANG_FORMAT" && test -x "$CLANG_FORMAT" ; then
29 CF_VERSIONS="$CLANG_FORMAT"
30 fi
31 # And a bunch of other potential known versions of clang-format, newest first
32 CF_VERSIONS="$CF_VERSIONS clang-format-17"
33 CF_VERSIONS="$CF_VERSIONS clang-format-16 clang-format-16.0.6 "
34 CF_VERSIONS="$CF_VERSIONS clang-format15 clang-format-15 clang-format-15.0.7"
35 # Generic name of clang-format
36 CF_VERSIONS="$CF_VERSIONS clang-format"
37 for _cf in $CF_VERSIONS
38 do
39 # Not an error if this particular clang-format isn't found
40 CF=$( which $_cf 2> /dev/null || true )
41 test -n "$CF" && break
42 done
43
44 test -n "$CF" || { echo "! No clang-format ($CF_VERSIONS) found in PATH"; exit 1 ; }
45 test -x "$CF" || { echo "! $CF is not executable."; exit 1 ; }
46
47 ### CLANG-FORMAT-WRANGLING
48 #
49 # Version 7 and earlier doesn't understand all the options we would like.
50 # Version 12 handled lambdas nicely and was the norm for Calamares 3.2.
51 # Version 13 was also ok.
52 # Version 14 behaves differently with short-functions-in-class,
53 # spreading functions out that 13 keeps on one line. To avoid
54 # ping-pong commits, forbid 14.
55 # Version 15 is available on recent-ish Ubuntus and FreeBSD, pick it.
56 # It also supports inserting braces, which is the one thing we kept
57 # astyle around for.
58 # Version 16 is available on openSUSE and is ok as well.
59 # Version 17 is available on FreeBSD and KaOS and is ok as well.
60
61 format_version=`"$CF" --version | tr -dc '[^.0-9]' | cut -d . -f 1`
62 case "$format_version" in
63 15|16|17 )
64 :
65 ;;
66 * )
67 echo "! Clang-format version '$format_version' unsupported, versions 15-17 are ok."
68 exit 1
69 ;;
70 esac
71
72
73 ### FILE PROCESSING
74 #
75 #
76 set -e
77
78 any_dirs=no
79 for d in "$@"
80 do
81 test -d "$d" && any_dirs=yes
82 done
83
84 style_some()
85 {
86 if test -n "$*" ; then
87 $CF -i -style=file "$@"
88 fi
89 }
90
91 if test "x$any_dirs" = "xyes" ; then
92 for d in "$@"
93 do
94 if test -d "$d" ; then
95 style_some $( find "$d" -maxdepth 1 -type f -name '*.cpp' -o -name '*.h' )
96 else
97 style_some "$d"
98 fi
99 done
100 else
101 style_some "$@"
102 fi