2014-07-15 11:10:49 +02:00
|
|
|
#!/bin/sh
|
2019-06-11 12:51:33 +02:00
|
|
|
#
|
2020-08-21 23:19:41 +02:00
|
|
|
# SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
|
|
|
|
# SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
#
|
2023-10-17 21:44:33 +02:00
|
|
|
# Apply Calamares-style formatting to sources. Requires clang-format-15.
|
2019-06-11 12:51:33 +02:00
|
|
|
#
|
|
|
|
# You can pass in directory names, in which case the files
|
|
|
|
# in that directory (NOT below it) are processed.
|
|
|
|
#
|
2024-04-17 10:48:04 +02:00
|
|
|
# If the environment variable CLANG_FORMAT is set to a (full path) and
|
|
|
|
# that path is executable, it will be used if possible.
|
|
|
|
#
|
2020-03-03 14:21:43 +01:00
|
|
|
LANG=C
|
|
|
|
LC_ALL=C
|
|
|
|
LC_NUMERIC=C
|
|
|
|
export LANG LC_ALL LC_NUMERIC
|
|
|
|
|
2021-06-04 14:47:26 +02:00
|
|
|
BASEDIR=$(dirname $0)
|
|
|
|
TOPDIR=$( cd $BASEDIR/.. && pwd -P )
|
|
|
|
test -d "$BASEDIR" || { echo "! Could not determine base for $0" ; exit 1 ; }
|
|
|
|
test -d "$TOPDIR" || { echo "! Cound not determine top-level source dir" ; exit 1 ; }
|
2022-02-01 16:16:46 +01:00
|
|
|
test -f "$TOPDIR/.clang-format" || { echo "! No .clang-format support files in $TOPDIR" ; exit 1 ; }
|
2021-06-04 14:47:26 +02:00
|
|
|
|
2024-04-17 10:48:04 +02:00
|
|
|
# Start with CLANG_FORMAT, if it is specified
|
|
|
|
CF_VERSIONS=""
|
|
|
|
if test -n "$CLANG_FORMAT" && test -x "$CLANG_FORMAT" ; then
|
|
|
|
CF_VERSIONS="$CLANG_FORMAT"
|
|
|
|
fi
|
|
|
|
# And a bunch of other potential known versions of clang-format, newest first
|
|
|
|
CF_VERSIONS="$CF_VERSIONS clang-format-17"
|
|
|
|
CF_VERSIONS="$CF_VERSIONS clang-format-16 clang-format-16.0.6 "
|
|
|
|
CF_VERSIONS="$CF_VERSIONS clang-format15 clang-format-15 "
|
|
|
|
# Generic name of clang-format
|
|
|
|
CF_VERSIONS="$CF_VERSIONS clang-format"
|
2020-04-15 11:54:27 +02:00
|
|
|
for _cf in $CF_VERSIONS
|
2019-07-05 10:47:31 +02:00
|
|
|
do
|
|
|
|
# Not an error if this particular clang-format isn't found
|
2020-04-15 11:54:27 +02:00
|
|
|
CF=$( which $_cf 2> /dev/null || true )
|
2019-07-05 10:47:31 +02:00
|
|
|
test -n "$CF" && break
|
|
|
|
done
|
2019-06-11 12:51:33 +02:00
|
|
|
|
2020-04-15 11:54:27 +02:00
|
|
|
test -n "$CF" || { echo "! No clang-format ($CF_VERSIONS) found in PATH"; exit 1 ; }
|
2019-06-11 12:51:33 +02:00
|
|
|
test -x "$CF" || { echo "! $CF is not executable."; exit 1 ; }
|
|
|
|
|
2021-06-04 14:47:26 +02:00
|
|
|
### CLANG-FORMAT-WRANGLING
|
|
|
|
#
|
2023-10-17 21:44:33 +02:00
|
|
|
# Version 7 and earlier doesn't understand all the options we would like.
|
|
|
|
# Version 12 handled lambdas nicely and was the norm for Calamares 3.2.
|
|
|
|
# Version 13 was also ok.
|
2022-05-18 12:55:15 +02:00
|
|
|
# Version 14 behaves differently with short-functions-in-class,
|
|
|
|
# spreading functions out that 13 keeps on one line. To avoid
|
|
|
|
# ping-pong commits, forbid 14.
|
2023-10-17 21:44:33 +02:00
|
|
|
# Version 15 is available on recent-ish Ubuntus and FreeBSD, pick it.
|
|
|
|
# It also supports inserting braces, which is the one thing we kept
|
2023-10-21 16:22:37 +02:00
|
|
|
# astyle around for.
|
|
|
|
# Version 16 is available on openSUSE and is ok as well.
|
2023-10-24 14:55:24 +02:00
|
|
|
# Version 17 is available on FreeBSD and KaOS and is ok as well.
|
2021-06-04 14:47:26 +02:00
|
|
|
|
2021-03-16 16:06:46 +01:00
|
|
|
format_version=`"$CF" --version | tr -dc '[^.0-9]' | cut -d . -f 1`
|
2021-06-04 14:47:26 +02:00
|
|
|
case "$format_version" in
|
2023-10-24 14:55:24 +02:00
|
|
|
15|16|17 )
|
2021-06-04 14:47:26 +02:00
|
|
|
:
|
|
|
|
;;
|
|
|
|
* )
|
2023-10-24 14:55:24 +02:00
|
|
|
echo "! Clang-format version '$format_version' unsupported, versions 15-17 are ok."
|
2021-06-04 14:47:26 +02:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-06-17 13:53:40 +02:00
|
|
|
|
2021-06-04 14:47:26 +02:00
|
|
|
### FILE PROCESSING
|
|
|
|
#
|
|
|
|
#
|
2019-11-26 22:29:06 +01:00
|
|
|
set -e
|
|
|
|
|
2019-06-11 12:51:33 +02:00
|
|
|
any_dirs=no
|
|
|
|
for d in "$@"
|
|
|
|
do
|
|
|
|
test -d "$d" && any_dirs=yes
|
|
|
|
done
|
|
|
|
|
|
|
|
style_some()
|
|
|
|
{
|
2020-08-25 23:43:15 +02:00
|
|
|
if test -n "$*" ; then
|
|
|
|
$CF -i -style=file "$@"
|
|
|
|
fi
|
2019-06-11 12:51:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if test "x$any_dirs" = "xyes" ; then
|
|
|
|
for d in "$@"
|
|
|
|
do
|
2019-08-04 22:21:53 +02:00
|
|
|
if test -d "$d" ; then
|
2019-06-11 12:51:33 +02:00
|
|
|
style_some $( find "$d" -maxdepth 1 -type f -name '*.cpp' -o -name '*.h' )
|
|
|
|
else
|
|
|
|
style_some "$d"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
style_some "$@"
|
|
|
|
fi
|