misc: fix checkstyle on fedora 33/31633/8
authorRay Kinsella <mdr@ashroe.eu>
Fri, 12 Mar 2021 15:57:29 +0000 (15:57 +0000)
committerDamjan Marion <dmarion@me.com>
Tue, 16 Mar 2021 21:36:28 +0000 (21:36 +0000)
The fedora clang-format command helpfully does not include the version
suffix, and places clang-format-diff in /usr/share/clang.

Have summited #1939018 in Fedora, to fix upstream.
https://bugzilla.redhat.com/show_bug.cgi?id=1939018

Until then ...

Type: fix

Signed-off-by: Ray Kinsella <mdr@ashroe.eu>
Change-Id: Ibceae0fc15e7461c24288ee04f4d28943f889c36

Makefile
extras/scripts/checkstyle.sh

index 149af03..05a912c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -620,9 +620,6 @@ compdb:
 
 .PHONY: checkstyle
 checkstyle: checkfeaturelist
-ifeq ($(shell which clang-format-10),)
-       @sudo apt-get install -y clang-format-10
-endif
        @extras/scripts/checkstyle.sh
 
 .PHONY: checkstyle-commit
index ab0e6e4..d7587cb 100755 (executable)
 
 set -eEo pipefail
 
+CLANG_FORMAT_VER_REGEX='([0-9]+)\.[0-9]+\.[0-9]+'
+CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py"
+
 CLANG_FORMAT_VER=10
 GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
 CLANG_FORMAT_DIFF_ARGS="-style file -p1"
 SUFFIX="-${CLANG_FORMAT_VER}"
 
-clang-format${SUFFIX} --version
+# Attempt to find clang-format to confirm Clang version.
+if command -v clang-format${SUFFIX} &> /dev/null;
+then
+    CLANG_FORMAT=clang-format${SUFFIX}
+elif command -v clang-format &> /dev/null;
+then
+    CLANG_FORMAT=clang-format
+fi
+
+CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version)
+echo $CLANG_FORMAT_VERSION
+
+# Confirm that Clang is the expected version.
+if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]];
+then
+    echo "*******************************************************************"
+    echo "* CHECKSTYLE VERSION REGEX CHECK FAILED"
+    echo "* $CLANG_FORMAT_VERSION"
+    echo "*******************************************************************"
+    exit 1
+fi
+
+if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]];
+then
+    echo "*******************************************************************"
+    echo "* CHECKSTYLE VERSION CHECK FAILED"
+    echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}"
+    echo "*******************************************************************"
+    exit 1
+fi
+
+# Attempt to find clang-format-diff.
+if command -v clang-format-diff${SUFFIX} &> /dev/null;
+then
+    CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX}
+elif command -v clang-format-diff &> /dev/null;
+then
+    CLANG_FORMAT=clang-format-diff
+elif [ ! -f $CLANG_FORMAT_DIFF ] ;
+then
+    echo "*******************************************************************"
+    echo "* CHECKSTYLE FAILED"
+    echo "* Could not locate the clang-format-diff script"
+    echo "*******************************************************************"
+    exit 1
+fi
 
 in=$(mktemp)
 git diff ${GIT_DIFF_ARGS} ':!*.patch' > ${in}
@@ -39,7 +87,7 @@ if [ ${line_count} -gt 0 ] ; then
 fi
 
 if [ "${1}" == "--fix" ]; then
-  cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} -i
+  cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i
   filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
   git status ${filelist}
   rm ${in}
@@ -61,7 +109,7 @@ fi
 
 out=$(mktemp)
 
-cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
+cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
 rm ${in}
 
 line_count=$(cat ${out} | wc -l)