3 # Clean up QEMU #include lines by ensuring that qemu/osdep.h
4 # is the first include listed in .c files, and no headers provided
5 # by osdep.h itself are redundantly included in either .c or .h files.
7 # Copyright (c) 2015 Linaro Limited
10 # Peter Maydell <peter.maydell@linaro.org>
12 # This work is licensed under the terms of the GNU GPL, version 2
13 # or (at your option) any later version. See the COPYING file in
14 # the top-level directory.
17 # clean-includes [--git subjectprefix] [--check-dup-head] file ...
19 # clean-includes [--git subjectprefix] [--check-dup-head] --all
21 # If the --git subjectprefix option is given, then after making
22 # the changes to the files this script will create a git commit
23 # with the subject line "subjectprefix: Clean up includes"
24 # and a boilerplate commit message.
26 # If --check-dup-head is specified, additionally check for duplicate
29 # Using --all will cause clean-includes to run on the whole source
30 # tree (excluding certain directories which are known not to need
33 # This script requires Coccinelle to be installed.
35 # .c files will have the osdep.h included added, and redundant
37 # .h files will have redundant includes (including includes of osdep.h)
39 # Other files (including C++ and ObjectiveC) can't be handled by this script.
41 # The following one-liner may be handy for finding files to run this on.
42 # However some caution is required regarding files that might be part
43 # of the guest agent or standalone tests.
45 # for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \
46 # grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
53 # Extended regular expression defining files to ignore when using --all
54 XDIRREGEX
='^(tests/tcg|tests/multiboot|pc-bios)'
61 echo "--git option requires an argument"
84 echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]"
85 echo "(modifies the files in place)"
89 if [ "$1" = "--all" ]; then
90 # We assume there are no files in the tree with spaces in their name
91 set -- $
(git ls-files
'*.[ch]' |
grep -E -v "$XDIRREGEX")
94 # Annoyingly coccinelle won't read a scriptfile unless its
95 # name ends '.cocci', so write it out to a tempfile with the
97 COCCIFILE
="$(mktemp --suffix=.cocci)"
99 trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
101 cat >"$COCCIFILE" <<EOT
106 + #include "qemu/osdep.h"
109 + #include "qemu/osdep.h"
117 echo "SKIPPING $f (symbolic link)"
122 # These aren't standalone C source files
123 echo "SKIPPING $f (not a standalone source file)"
129 *include
/qemu
/osdep.h | \
130 *include
/qemu
/compiler.h | \
131 *include
/qemu
/qemu-plugin.h | \
132 *include
/glib-compat.h | \
133 *include
/sysemu
/os-posix.h | \
134 *include
/sysemu
/os-win32.h | \
135 *include
/standard-headers
/ )
136 # Removing include lines from osdep.h itself would be counterproductive.
137 echo "SKIPPING $f (special case header)"
140 *include
/standard-headers
/*)
141 echo "SKIPPING $f (autogenerated header)"
148 echo "WARNING: ignoring $f (cannot handle non-C files)"
154 if [ "$MODE" = "c" ]; then
155 # First, use Coccinelle to add qemu/osdep.h before the first existing include
156 # (this will add two lines if the file uses both "..." and <...> #includes,
157 # but we will remove the extras in the next step)
158 spatch
--in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
160 # Now remove any duplicate osdep.h includes
161 perl
-n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
163 # Remove includes of osdep.h itself
164 perl
-n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
165 ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
168 # Remove includes that osdep.h already provides
169 perl
-n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
170 ! (grep { $_ eq $1 } qw (
171 "config-host.h" "config-target.h" "qemu/compiler.h"
172 <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
173 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
174 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
175 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h>
176 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h>
177 "sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h"
183 if [ "$DUPHEAD" = "yes" ] && [ -n "$files" ]; then
184 if egrep "^[[:space:]]*#[[:space:]]*include" $files |
tr -d '[:blank:]' \
185 |
sort |
uniq -c |
grep -v '^ *1 '; then
186 echo "Found duplicate header file includes. Please check the above files manually."
191 if [ "$GIT" = "yes" ]; then
193 git commit
--signoff -F - <<EOF
194 $GITSUBJ: Clean up includes
196 This commit was created with scripts/clean-includes.
198 All .c should include qemu/osdep.h first. The script performs three
201 * Ensure .c files include qemu/osdep.h first.
202 * Including it in a .h is redundant, since the .c already includes
203 it. Drop such inclusions.
204 * Likewise, including headers qemu/osdep.h includes is redundant.