summaryrefslogtreecommitdiff
path: root/tools/cmake-format
diff options
context:
space:
mode:
authorRalf Habacker <ralf.habacker@freenet.de>2019-01-10 10:11:46 +0100
committerRalf Habacker <ralf.habacker@freenet.de>2019-01-23 21:34:08 +0100
commitf855d0bb480f2d6f8fea0a888905bc04c87d5795 (patch)
tree5b8a4d9795d9b0beba8551e0ca9cc079dcaf0d6f /tools/cmake-format
parentfc205cb4e4bc3f50ac0a4669e4d4ce55b56c89df (diff)
downloaddbus-f855d0bb480f2d6f8fea0a888905bc04c87d5795.tar.gz
Add script for formatting cmake files
The prefered call order is tools/cmake-format --trailing-spaces # create git commit tools/cmake-format --tabs # create git commit tools/cmake-format --keyword-case # create git commit tools/cmake-format --keyword-spaces # create git commit tools/cmake-format --end-args # create git commit tools/cmake-format --indents # create git commit Signed-off-by: Ralf Habacker <ralf.habacker@freenet.de>
Diffstat (limited to 'tools/cmake-format')
-rwxr-xr-xtools/cmake-format109
1 files changed, 109 insertions, 0 deletions
diff --git a/tools/cmake-format b/tools/cmake-format
new file mode 100755
index 00000000..303f6745
--- /dev/null
+++ b/tools/cmake-format
@@ -0,0 +1,109 @@
+#!/bin/sh
+#
+# cmake-format - a simple cmake formatter
+#
+# Copyright (c) 2019 Ralf Habacker <ralf.habacker@freenet.de>
+#
+# SPDX-License-Identifier: BSD
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+#
+
+if test -z "$1"; then
+ echo "format cmake files by Ralf Habacker"
+ echo
+ echo "Usage: $0 --all | --end-args | --indents | --keyword-case | --keyword-spaces | --tabs | --trailing-spaces"
+ echo
+ echo "command line parameter:"
+ echo " --all all above"
+ echo " --end-args remove obsolete parameter list from end... cmake commands"
+ echo " --indents fix indents (requires keyword-case)"
+ echo " --keyword-case make all cmake keywords lowercase"
+ echo " --keyword-spaces remove spaces between keyword and opening bracket"
+ echo " --tabs replace tabs by 4 spaces"
+ echo " --trailing-spaces remove trailing spaces"
+ exit 1
+fi
+
+# only apply to cmake keywords
+KEYWORDS=$(cmake --help-command-list)
+
+# do not search in comments
+range="/^[ ]*#/!"
+
+# use 4 spaces
+indent="\ \ \ \ "
+
+# remove tabs
+exp="${range}s/\t/${indent}/g"
+expt=$exp
+
+# remove trailing spaces
+exp="${range}s/[ \t]*$//"
+expts=$exp
+
+# fix indents
+exp=
+for j in $(echo $KEYWORDS); do
+ exp="$exp;${range}s/^ \{1,3\}$j(/${indent}$j(/g"
+ exp="$exp;${range}s/^ \{5,7\}$j(/${indent}${indent}$j(/g"
+ exp="$exp;${range}s/^ \{9,11\}$j(/${indent}${indent}${indent}$j(/g"
+done
+expki=$exp
+
+# convert upper case keywords to lower case
+exp=
+for j in $(echo $KEYWORDS); do
+ u=$(echo $j | sed 's/.*/\U&/')
+ exp="$exp;${range}s/$u[ \t]*(/$j(/g"
+done
+expku=$exp
+
+# remove spaces after keywords and (
+exp=
+for j in $(echo $KEYWORDS); do
+ exp="$exp;${range}s/$j[ \t]\+(/$j(/g"
+done
+expks=$exp
+
+# remove obsolete arguments from end...()
+exp=
+for j in $(echo $KEYWORDS); do
+ e=$(echo $j | grep '^end')
+ if test -n "$e"; then
+ exp="$exp;${range}s/$j(.*)/$j()/g"
+ fi
+done
+expke=$exp
+
+if test "$1" == "--all"; then
+ exp="${expt}${expts}${expku}${expki}${expks}${expke}"
+elif test "$1" == "--end-args"; then
+ exp=$expke
+elif test "$1" == "--indents"; then
+ exp=$expki
+elif test "$1" == "--keyword-case"; then
+ exp=$expku
+elif test "$1" == "--keyword-spaces"; then
+ exp=$expks
+elif test "$1" == "--tabs"; then
+ exp=$expt
+elif test "$1" == "--trailing-spaces"; then
+ exp=$expts
+fi
+
+# setup dir to apply
+s=$(dirname $0)
+root=$(realpath $s/../cmake)
+
+#echo $exp
+#echo $root
+
+echo "locations with unusual indention level changes, please inspect"
+# apply to cmake related files
+for i in $(find $root -name 'CMakeLists.txt' -o -name '*.cmake' | grep -v README.cmake | grep -v config.h.cmake | grep -v bat.cmake | grep -v '/Find'); do
+ # apply style
+ sed -i "$exp" $i
+ # show unusual indention changes
+ gawk 'BEGIN { indent=0 } $0 ~ /^ {0}/ && $0 !~ /^$/{ indent=0; } $0 ~ /^ {4}/ { indent=1; } $0 ~ /^ {8}/ { indent=2; } $0 ~ /^ {12}/ { indent=3; } { if (indent - oldindent > 1) print FILENAME ":" NR ":" $0; oldindent=indent;}' $i
+done