Debian Codegraph @

tools/cmake-format

raw
1 #!/bin/sh
2 #
3 # cmake-format - a simple cmake formatter
4 #
5 # Copyright (c) 2019,2021 Ralf Habacker <[email protected]>
6 #
7 # SPDX-License-Identifier: BSD-3-Clause
8
9 if test -z "$1"; then
10 echo "format cmake files by Ralf Habacker"
11 echo
12 echo "Usage: $0 --all | --end-args | --indents | --keyword-case | --keyword-spaces | --tabs | --trailing-spaces [<source-dir>]"
13 echo
14 echo "command line parameter:"
15 echo " --all all above"
16 echo " --end-args remove obsolete parameter list from end... cmake commands"
17 echo " --indents fix indents (requires keyword-case)"
18 echo " --keyword-case make all cmake keywords lowercase"
19 echo " --keyword-spaces remove spaces between keyword and opening bracket"
20 echo " --tabs replace tabs by 4 spaces"
21 echo " --trailing-spaces remove trailing spaces"
22 echo " --check-indents check indents"
23 echo " [<source-dir>] specify root dir to perform the requested action (optional)"
24 echo " If not specified, dbus source dir root is used"
25 exit 1
26 fi
27
28 # only apply to cmake keywords
29 KEYWORDS=$(cmake --help-command-list)
30
31 # do not search in comments
32 range="/^[ ]*#/!"
33
34 # use 4 spaces
35 indent="\ \ \ \ "
36
37 # remove tabs
38 exp="${range}s/\t/${indent}/g"
39 expt=$exp
40
41 # remove trailing spaces
42 exp="${range}s/[ \t]*$//"
43 expts=$exp
44
45 # fix indents
46 exp=
47 for j in $(echo $KEYWORDS); do
48 exp="$exp;${range}s/^ \{1,3\}$j(/${indent}$j(/g"
49 exp="$exp;${range}s/^ \{5,7\}$j(/${indent}${indent}$j(/g"
50 exp="$exp;${range}s/^ \{9,11\}$j(/${indent}${indent}${indent}$j(/g"
51 done
52 expki=$exp
53
54 # convert upper case keywords to lower case
55 exp=
56 for j in $(echo $KEYWORDS); do
57 u=$(echo $j | sed 's/.*/\U&/')
58 exp="$exp;${range}s/$u[ \t]*(/$j(/g"
59 done
60 expku=$exp
61
62 # remove spaces after keywords and (
63 exp=
64 for j in $(echo $KEYWORDS); do
65 exp="$exp;${range}s/$j[ \t]\+(/$j(/g"
66 done
67 expks=$exp
68
69 # remove obsolete arguments from end...()
70 exp=
71 for j in $(echo $KEYWORDS); do
72 e=$(echo $j | grep '^end')
73 if test -n "$e"; then
74 exp="$exp;${range}s/$j(.*)/$j()/g"
75 fi
76 done
77 expke=$exp
78 func=
79
80 if test "$1" == "--all"; then
81 exp="${expt};${expts}${expku}${expki}${expks}${expke}"
82 func=$1
83 shift
84 elif test "$1" == "--end-args"; then
85 exp=$expke
86 func=$1
87 shift
88 elif test "$1" == "--indents"; then
89 exp=$expki
90 func=$1
91 shift
92 elif test "$1" == "--keyword-case"; then
93 exp=$expku
94 func=$1
95 shift
96 elif test "$1" == "--keyword-spaces"; then
97 exp=$expks
98 func=$1
99 shift
100 elif test "$1" == "--tabs"; then
101 exp=$expt
102 func=$1
103 shift
104 elif test "$1" == "--trailing-spaces"; then
105 exp=$expts
106 func=$1
107 shift
108 elif test "$1" == "--check-indents"; then
109 echo "locations with unusual indention level changes, please inspect"
110 func=$1
111 shift
112 fi
113
114 # setup dir to apply
115 if test -n "$1"; then
116 root=$1
117 else
118 s=$(dirname $0)
119 root=$(realpath $s/..)
120 fi
121
122 #echo $exp
123 #echo $root
124
125 # script for checking indents
126 awk='BEGIN { debug=0; indent=0 }
127 $0 ~ /^ {0}/ && $0 !~ /^$/{ indent=0; }
128 $0 ~ /^ {4}/ { indent=1; }
129 $0 ~ /^ {8}/ { indent=2; }
130 $0 ~ /^ {12}/ { indent=3; }
131 debug == 1 { print FILENAME "[" NR "]:" indent " " oldindent ": " $0; }
132 { if (indent - oldindent > 1) print FILENAME ":" NR ":" $0; }
133 { oldindent = indent;}
134 '
135
136 # apply to cmake related files
137 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
138 # apply style
139 if ! test "$func" = "--check-indents"; then
140 sed -i "$exp" $i
141 else
142 gawk "$awk" $i
143 fi
144 done