]> git.ipfire.org Git - thirdparty/squid.git/blame - scripts/source-maintenance.sh
Source Format Enforcement (#532)
[thirdparty/squid.git] / scripts / source-maintenance.sh
CommitLineData
d743fb5d 1#!/bin/sh
a151895d 2#
77b1029d 3## Copyright (C) 1996-2020 The Squid Software Foundation and contributors
a151895d
AJ
4##
5## Squid software is distributed under GPLv2+ license and includes
6## contributions from numerous individuals and organizations.
7## Please see the COPYING and CONTRIBUTORS files for details.
8##
9
6f98e675 10#
b510f3a1 11# This script contains the code run to perform automatic source maintenance
a151895d 12# on Squid
b510f3a1
AJ
13#
14
15## Source Code Format Enforcement
16#
6f98e675
AJ
17# A checker to recursively reformat all source files: .h .c .cc .cci
18# using a custom astyle formatter and to use MD5 to validate that
19# the formatter has not altered the code syntax.
20#
21# If code alteration takes place the process is halted for manual intervention.
22#
23
6adea170
AJ
24# On squid-cache.org we have to use the python scripted md5sum
25HOST=`hostname`
26if test "$HOST" = "squid-cache.org" ; then
f437b706 27 MD5="md5"
6adea170
AJ
28else
29 MD5="md5sum"
30fi
31
f77fbf5b 32ASVER=`astyle --version 2>&1 | grep -o -E "[0-9.]+"`
2ab61c3a
SM
33if test "${ASVER}" != "2.04" ; then
34 echo "Astyle version problem. You have ${ASVER} instead of 2.04"
11097007 35 ASVER=""
f77fbf5b
AJ
36else
37 echo "Found astyle ${ASVER}. Formatting..."
38fi
39
e819858c 40COPYRIGHT_YEARS=`date +"1996-%Y"`
114c8217 41echo "s/1996-2[0-9]+ The Squid Software Foundation and contributors/${COPYRIGHT_YEARS} The Squid Software Foundation and contributors/g" >>boilerplate_fix.sed
e819858c 42
e555fec8
AJ
43srcformat ()
44{
605f2c3e
AJ
45#
46# Scan for incorrect use of #ifdef/#ifndef
47#
114c8217 48git grep "ifn?def .*_SQUID_" |
1a8e2e1c 49 grep -v -E "_H$" |
114c8217
AJ
50 grep -v "scripts/source-maintenance.sh" |
51 while read f; do echo "PROBLEM?: ${f}"; done
605f2c3e
AJ
52
53#
54# Scan for file-specific actions
55#
114c8217 56for FILENAME in `git ls-files`; do
e9549c27 57 skip_copyright_check=""
6f98e675 58
114c8217
AJ
59 # skip subdirectories, git ls-files is recursive
60 test -d $FILENAME && continue
61
d743fb5d
AJ
62 case ${FILENAME} in
63
64 *.h|*.c|*.cc|*.cci)
65
d090e020
AJ
66 #
67 # Code Style formatting maintenance
68 #
11097007 69 if test "${ASVER}"; then
114c8217 70 ./scripts/formater.pl ${FILENAME}
f77fbf5b
AJ
71 if test -e $FILENAME -a -e "$FILENAME.astylebak"; then
72 md51=`cat $FILENAME| tr -d "\n \t\r" | $MD5`;
73 md52=`cat $FILENAME.astylebak| tr -d "\n \t\r" | $MD5`;
74
0278ba4d 75 if test "$md51" != "$md52"; then
114c8217 76 echo "ERROR: File $FILENAME not formating well";
f77fbf5b
AJ
77 mv $FILENAME $FILENAME.astylebad
78 mv $FILENAME.astylebak $FILENAME
114c8217 79 git checkout -- ${FILENAME}
f77fbf5b
AJ
80 else
81 rm -f $FILENAME.astylebak
82 fi
83 fi
84 fi
d090e020 85
114c8217 86 ./scripts/sort-includes.pl ${FILENAME} >${FILENAME}.sorted
987ca90e
AJ
87 if test -e ${FILENAME} -a -e "${FILENAME}.sorted"; then
88 md51=`cat ${FILENAME}| tr -d "\n \t\r" | $MD5`;
89 md52=`cat ${FILENAME}.sorted| tr -d "\n \t\r" | $MD5`;
90
91 if test "$md51" != "$md52" ; then
114c8217 92 echo "NOTICE: File ${FILENAME} changed #include order"
987ca90e
AJ
93 fi
94 mv ${FILENAME}.sorted ${FILENAME}
95 fi
96
536e999d 97 #
582c2af2 98 # REQUIRE squid.h first #include
536e999d
AJ
99 #
100 case ${FILENAME} in
114c8217
AJ
101 src/cf_gen.cc)
102 # ignore, this is a build tool.
103 ;;
536e999d
AJ
104 *.c|*.cc)
105 FI=`grep "#include" ${FILENAME} | head -1`;
582c2af2 106 if test "${FI}" != "#include \"squid.h\"" -a "${FILENAME}" != "cf_gen.cc"; then
114c8217 107 echo "ERROR: ${FILENAME} does not include squid.h first!"
536e999d
AJ
108 fi
109 ;;
110 *.h|*.cci)
f7f3304a 111 FI=`grep "#include \"squid.h\"" ${FILENAME}`;
582c2af2 112 if test "x${FI}" != "x" ; then
114c8217 113 echo "ERROR: ${FILENAME} duplicate include of squid.h"
536e999d
AJ
114 fi
115 ;;
116 esac
117
24b30fdc
EQ
118 #
119 # If a file includes openssl headers, then it must include compat/openssl.h
120 #
14bf2426
AJ
121 if test "${FILENAME}" != "compat/openssl.h"; then
122 FA=`grep "#include.*openssl/" "${FILENAME}" 2>/dev/null | head -1`;
123 FB=`grep '#include.*compat/openssl[.]h' "${FILENAME}" 2>/dev/null | head -1`;
124 if test "x${FA}" != "x" -a "x${FB}" = "x"; then
125 echo "ERROR: ${FILENAME} includes openssl headers without including \"compat/openssl.h\""
126 fi
24b30fdc
EQ
127 fi
128
eb13c21e
AJ
129 #
130 # forward.h means different things to Squid code depending on the path
131 # require the full path is explicit for every include
132 #
133 FI=`grep "#include \"forward.h\"" ${FILENAME}`;
134 if test "x${FI}" != "x" ; then
114c8217 135 echo "ERROR: ${FILENAME} contains reference to forward.h without path"
eb13c21e
AJ
136 fi
137
b8889258
AJ
138 #
139 # detect functions unsafe for use within Squid.
86c63190
AJ
140 # strdup() - only allowed in compat/xstring.h which defines a safe replacement.
141 # sprintf() - not allowed anywhere.
b8889258 142 #
86c63190 143 STRDUP=`grep -e "[^x]strdup(" ${FILENAME}`;
14bf2426 144 if test "x${STRDUP}" != "x" -a "${FILENAME}" != "compat/xstring.h"; then
114c8217 145 echo "ERROR: ${FILENAME} contains unprotected use of strdup()"
b8889258 146 fi
86c63190 147 SPRINTF=`grep -e "[^v]sprintf(" ${FILENAME}`;
b8889258 148 if test "x${SPRINTF}" != "x" ; then
114c8217 149 echo "ERROR: ${FILENAME} contains unsafe use of sprintf()"
b8889258
AJ
150 fi
151
d090e020
AJ
152 #
153 # DEBUG Section list maintenance
154 #
114c8217 155 grep " DEBUG: section" <${FILENAME} | sed -e 's/ \* DEBUG: //' -e 's%/\* DEBUG: %%' -e 's% \*/%%' | sort -u >>doc/debug-sections.tmp
d090e020
AJ
156
157 #
158 # File permissions maintenance.
159 #
160 chmod 644 ${FILENAME}
161 ;;
162
163 *.pl|*.sh)
164 #
165 # File permissions maintenance.
166 #
167 chmod 755 ${FILENAME}
8cd09bae
HN
168 ;;
169
170 Makefile.am)
171
ddf5d6db 172 perl -p -e 's/@([A-Z0-9_]+)@/\$($1)/g' <${FILENAME} >${FILENAME}.styled
8cd09bae
HN
173 mv ${FILENAME}.styled ${FILENAME}
174 ;;
175
e758499d 176 ChangeLog|CREDITS|CONTRIBUTORS|COPYING|*.list|*.png|*.po|*.pot|rfcs/|*.txt|test-suite/squidconf/empty|.bzrignore)
e9549c27
AJ
177 # we do not enforce copyright blurbs in:
178 #
e758499d
AJ
179 # Squid Project contributor attribution file
180 # third-party copyright attribution file
e9549c27
AJ
181 # images,
182 # translation PO/POT
183 # auto-generated .list files,
e758499d 184 # license documentation files
e9549c27 185 # (imported) plain-text documentation files and ChangeLogs
e758499d 186 # VCS internal files
e9549c27
AJ
187 #
188 skip_copyright_check=1
189 ;;
d743fb5d 190 esac
6f98e675 191
5fbc3e1d 192 # check for Foundation copyright blurb
114c8217 193 if test -f ${FILENAME} -a "x$skip_copyright_check" = "x"; then
e819858c 194 BLURB=`grep -o "${COPYRIGHT_YEARS} The Squid Software Foundation and contributors" ${FILENAME}`;
5fbc3e1d 195 if test "x${BLURB}" = "x"; then
e819858c
SM
196 BOILER=`grep -o -E "1996-2[0-9]+ The Squid Software Foundation and contributors" ${FILENAME}`;
197 if test "x${BOILER}" != "x"; then
114c8217
AJ
198 echo "UPDATE COPYRIGHT for ${FILENAME}"
199 sed --in-place -r -f boilerplate_fix.sed ${FILENAME}
e819858c 200 else
114c8217 201 echo "CHECK COPYRIGHT for ${FILENAME}"
e819858c 202 fi
5fbc3e1d
AJ
203 fi
204 fi
205
6f98e675 206done
e555fec8
AJ
207}
208
598c5a44 209# Build XPROF types file from current sources
e9549c27
AJ
210(
211cat scripts/boilerplate.h
212echo "#ifndef _PROFILER_XPROF_TYPE_H_"
213echo "#define _PROFILER_XPROF_TYPE_H_"
214echo "/* AUTO-GENERATED FILE */"
215echo "#if USE_XPROF_STATS"
216echo "typedef enum {"
217echo " XPROF_PROF_UNACCOUNTED,"
218grep -R -h "PROF_start.*" ./* | grep -v probename | sed -e 's/ //g; s/PROF_start(/XPROF_/; s/);/,/' | sort -u
219echo " XPROF_LAST } xprof_type;"
220echo "#endif"
221echo "#endif"
114c8217
AJ
222) >lib/profiler/list
223mv lib/profiler/list lib/profiler/xprof_type.h
598c5a44 224
46002cc1 225# Build icons install include from current icons available
54966be3 226(
e9549c27 227sed -e 's%\ \*%##%' -e 's%/\*%##%' -e 's%##/%##%' <scripts/boilerplate.h
54966be3 228echo -n "ICONS="
114c8217 229for f in `ls -1 icons/silk/* | sort -u`
d78c092d 230do
54966be3
AJ
231 echo " \\"
232 echo -n " ${f}"
d78c092d 233done
54966be3 234echo " "
114c8217 235)| sed s%icons/%%g >icons/icon.list
d78c092d 236
46002cc1
AJ
237# Build templates install include from current templates available
238(
e9549c27 239sed -e 's%\ \*%##%' -e 's%/\*%##%' -e 's%##/%##%' <scripts/boilerplate.h
46002cc1 240echo -n "ERROR_TEMPLATES="
114c8217 241for f in `ls -1 errors/templates/ERR_* | sort -u`
46002cc1
AJ
242do
243 echo " \\"
244 echo -n " ${f}"
245done
246echo " "
114c8217 247)| sed s%errors/%%g >errors/template.list
46002cc1
AJ
248
249# Build errors translation install include from current .PO available
250(
e9549c27 251sed -e 's%\ \*%##%' -e 's%/\*%##%' -e 's%##/%##%' <scripts/boilerplate.h
46002cc1 252echo -n "TRANSLATE_LANGUAGES="
114c8217 253for f in `ls -1 errors/*.po | sort -u`
46002cc1
AJ
254do
255 echo " \\"
256 echo -n " ${f}"
257done
258echo " "
114c8217 259)| sed s%errors/%%g | sed s%\.po%\.lang%g >errors/language.list
46002cc1
AJ
260
261# Build manuals translation install include from current .PO available
262(
e9549c27 263sed -e 's%\ \*%##%' -e 's%/\*%##%' -e 's%##/%##%' <scripts/boilerplate.h
46002cc1 264echo -n "TRANSLATE_LANGUAGES="
114c8217 265for f in `ls -1 doc/manuals/*.po | sort -u`
46002cc1
AJ
266do
267 echo " \\"
268 echo -n " ${f}"
269done
270echo " "
114c8217 271)| sed s%doc/manuals/%%g | sed s%\.po%\.lang%g >doc/manuals/language.list
46002cc1 272
ee4478ed
AJ
273# Build STUB framework include from current stub_* available
274(
e9549c27 275sed -e 's%\ \*%##%' -e 's%/\*%##%' -e 's%##/%##%' <scripts/boilerplate.h
1c7a7cd5 276echo -n "STUB_SOURCE= tests/STUB.h"
114c8217 277for f in `ls -1 src/tests/stub_*.cc | sort -u`
ee4478ed
AJ
278do
279 echo " \\"
280 echo -n " ${f}"
281done
282echo " "
114c8217 283)| sed s%src/%%g >src/tests/Stub.list
ee4478ed 284
3d50beac
FC
285# Build the GPERF generated content
286make -C src/http gperf-files
287
598c5a44 288# Run formating
114c8217 289echo "" >doc/debug-sections.tmp
e555fec8 290srcformat || exit 1
114c8217
AJ
291sort -u <doc/debug-sections.tmp | sort -n >doc/debug-sections.tmp2
292cat scripts/boilerplate.h doc/debug-sections.tmp2 >doc/debug-sections.txt
293rm doc/debug-sections.tmp doc/debug-sections.tmp2
294rm boilerplate_fix.sed