CI: make clang-format wrangling more flexible

Various clang-format versions have different defaults and
don't understand the same options, so adjust to having
files per-formatting-version to patch things up.
This commit is contained in:
Adriaan de Groot 2021-06-04 14:47:26 +02:00
parent 236bd0eb96
commit f3c57723df

View File

@ -5,12 +5,7 @@
# SPDX-License-Identifier: BSD-2-Clause # SPDX-License-Identifier: BSD-2-Clause
# #
# Calls astyle with settings matching Calamares coding style # Calls astyle with settings matching Calamares coding style
# Requires astyle >= 2.04 and clang-format-7 -8 or -9 # Requires astyle >= 2.04 and clang-format-8 or later
#
# Clang-format-10 is **not** supported, since it changes a default
# that re-introduces a space into empty function bodies; this
# can be turned off with a style setting, but that breaks
# older format versions which don't recognize the setting.
# #
# You can pass in directory names, in which case the files # You can pass in directory names, in which case the files
# in that directory (NOT below it) are processed. # in that directory (NOT below it) are processed.
@ -20,9 +15,16 @@ LC_ALL=C
LC_NUMERIC=C LC_NUMERIC=C
export LANG LC_ALL LC_NUMERIC export LANG LC_ALL LC_NUMERIC
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 ; }
test -f "$TOPDIR/.clang-format.base" || { echo "! No .clang-format support files in $TOPDIR" ; exit 1 ; }
AS=$( which astyle ) AS=$( which astyle )
CF_VERSIONS="clang-format-8 clang-format80 clang-format90 clang-format-9.0.1 clang-format" # Allow specifying CF_VERSIONS outside already
CF_VERSIONS="$CF_VERSIONS clang-format-8 clang-format80 clang-format90 clang-format-9.0.1 clang-format"
for _cf in $CF_VERSIONS for _cf in $CF_VERSIONS
do do
# Not an error if this particular clang-format isn't found # Not an error if this particular clang-format isn't found
@ -35,19 +37,45 @@ test -n "$CF" || { echo "! No clang-format ($CF_VERSIONS) found in PATH"; exit 1
test -x "$AS" || { echo "! $AS is not executable."; exit 1 ; } test -x "$AS" || { echo "! $AS is not executable."; exit 1 ; }
test -x "$CF" || { echo "! $CF is not executable."; exit 1 ; } test -x "$CF" || { echo "! $CF is not executable."; exit 1 ; }
unmangle_clang_format="" ### CLANG-FORMAT-WRANGLING
#
# Version 7 and earlier doesn't understand all the options we would like
# Version 8 is ok
# Version 9 is ok
# Later versions change some defaults so need extra wrangling.
# .. there are extra files that are appended to the settings, per
# .. clang-format version.
format_version=`"$CF" --version | tr -dc '[^.0-9]' | cut -d . -f 1` format_version=`"$CF" --version | tr -dc '[^.0-9]' | cut -d . -f 1`
if expr "$format_version" '<' 8 > /dev/null ; then case "$format_version" in
[0-7] )
echo "! Clang-format version 8+ required" echo "! Clang-format version 8+ required"
exit 1 exit 1
fi ;;
if expr "$format_version" '<' 10 > /dev/null ; then [89] )
: :
else ;;
unmangle_clang_format=$( dirname $0 )/../.clang-format 10 )
echo "SpaceInEmptyBlock: false" >> "$unmangle_clang_format" extra_settings="10"
fi ;;
11 )
extra_settings="10 11"
;;
* )
echo "! Clang-format version '$format_version' unknown."
exit 1
;;
esac
_fmt="$TOPDIR/.clang-format"
cp "$_fmt.base" "$_fmt"
for f in "$extra_settings" ; do
test -f "$_fmt.$f" && cat "$_fmt.$f" >> "$_fmt"
done
### FILE PROCESSING
#
#
set -e set -e
any_dirs=no any_dirs=no
@ -59,7 +87,7 @@ done
style_some() style_some()
{ {
if test -n "$*" ; then if test -n "$*" ; then
$AS --options=$(dirname $0)/astylerc --quiet "$@" $AS --options=$BASEDIR/astylerc --quiet "$@"
$CF -i -style=file "$@" $CF -i -style=file "$@"
fi fi
} }
@ -77,6 +105,7 @@ else
style_some "$@" style_some "$@"
fi fi
if test -n "$unmangle_clang_format" ; then ### CLANG-FORMAT-WRANGLING
sed -i.bak '/^SpaceInEmptyBlock/d' "$unmangle_clang_format" #
fi # Restore the original .clang-format
cp "$_fmt.base" "$_fmt"