# the top-level directory.
# Usage:
-# clean-includes [--git subjectprefix] file ...
+# clean-includes [--git subjectprefix] [--check-dup-head] file ...
# or
-# clean-includes [--git subjectprefix] --all
+# clean-includes [--git subjectprefix] [--check-dup-head] --all
#
# If the --git subjectprefix option is given, then after making
# the changes to the files this script will create a git commit
# with the subject line "subjectprefix: Clean up includes"
# and a boilerplate commit message.
#
+# If --check-dup-head is specified, additionally check for duplicate
+# header includes.
+#
# Using --all will cause clean-includes to run on the whole source
# tree (excluding certain directories which are known not to need
# handling).
# However some caution is required regarding files that might be part
# of the guest agent or standalone tests.
-# for i in `git ls-tree --name-only HEAD` ; do test -f $i && \
+# for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \
# grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
# echo $i ; done
GIT=no
+DUPHEAD=no
# Extended regular expression defining files to ignore when using --all
-XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios|disas/libvixl)'
-
-if [ $# -ne 0 ] && [ "$1" = "--git" ]; then
- if [ $# -eq 1 ]; then
- echo "--git option requires an argument"
- exit 1
- fi
- GITSUBJ="$2"
- GIT=yes
- shift
- shift
-fi
+XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios)'
+
+while true
+do
+ case $1 in
+ "--git")
+ if [ $# -eq 1 ]; then
+ echo "--git option requires an argument"
+ exit 1
+ fi
+ GITSUBJ="$2"
+ GIT=yes
+ shift
+ shift
+ ;;
+ "--check-dup-head")
+ DUPHEAD=yes
+ shift
+ ;;
+ "--")
+ shift
+ break
+ ;;
+ *)
+ break
+ ;;
+ esac
+done
if [ $# -eq 0 ]; then
- echo "Usage: clean-includes [--git subjectprefix] [--all | foo.c ...]"
+ echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]"
echo "(modifies the files in place)"
exit 1
fi
)
EOT
-
+files=
for f in "$@"; do
+ if [ -L "$f" ]; then
+ echo "SKIPPING $f (symbolic link)"
+ continue
+ fi
case "$f" in
- *.inc.c)
+ *.c.inc)
# These aren't standalone C source files
echo "SKIPPING $f (not a standalone source file)"
continue
;;
*include/qemu/osdep.h | \
*include/qemu/compiler.h | \
+ *include/qemu/qemu-plugin.h | \
+ *include/glib-compat.h | \
+ *include/sysemu/os-posix.h | \
+ *include/sysemu/os-win32.h | \
*include/standard-headers/ )
# Removing include lines from osdep.h itself would be counterproductive.
echo "SKIPPING $f (special case header)"
continue
;;
esac
+ files="$files $f"
if [ "$MODE" = "c" ]; then
# First, use Coccinelle to add qemu/osdep.h before the first existing include
<setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
<stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
<limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
- <sys/stat.h> <sys/time.h> <assert.h> <signal.h>
+ <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h>
+ <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h>
"sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h"
"qemu/typedefs.h"
))' "$f"
done
+if [ "$DUPHEAD" = "yes" ] && [ -n "$files" ]; then
+ if egrep "^[[:space:]]*#[[:space:]]*include" $files | tr -d '[:blank:]' \
+ | sort | uniq -c | grep -v '^ *1 '; then
+ echo "Found duplicate header file includes. Please check the above files manually."
+ exit 1
+ fi
+fi
+
if [ "$GIT" = "yes" ]; then
- git add -- "$@"
+ git add -- $files
git commit --signoff -F - <<EOF
$GITSUBJ: Clean up includes
-Clean up includes so that osdep.h is included first and headers
-which it implies are not included manually.
-
This commit was created with scripts/clean-includes.
+All .c should include qemu/osdep.h first. The script performs three
+related cleanups:
+
+* Ensure .c files include qemu/osdep.h first.
+* Including it in a .h is redundant, since the .c already includes
+ it. Drop such inclusions.
+* Likewise, including headers qemu/osdep.h includes is redundant.
+ Drop these, too.
+
EOF
fi