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] file ...
19 # clean-includes [--git subjectprefix] --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 # Using --all will cause clean-includes to run on the whole source
27 # tree (excluding certain directories which are known not to need
30 # This script requires Coccinelle to be installed.
32 # .c files will have the osdep.h included added, and redundant
34 # .h files will have redundant includes (including includes of osdep.h)
36 # Other files (including C++ and ObjectiveC) can't be handled by this script.
38 # The following one-liner may be handy for finding files to run this on.
39 # However some caution is required regarding files that might be part
40 # of the guest agent or standalone tests.
42 # for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \
43 # grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
49 # Extended regular expression defining files to ignore when using --all
50 XDIRREGEX
='^(tests/tcg|tests/multiboot|pc-bios|disas/libvixl)'
52 if [ $# -ne 0 ] && [ "$1" = "--git" ]; then
54 echo "--git option requires an argument"
64 echo "Usage: clean-includes [--git subjectprefix] [--all | foo.c ...]"
65 echo "(modifies the files in place)"
69 if [ "$1" = "--all" ]; then
70 # We assume there are no files in the tree with spaces in their name
71 set -- $
(git ls-files
'*.[ch]' |
grep -E -v "$XDIRREGEX")
74 # Annoyingly coccinelle won't read a scriptfile unless its
75 # name ends '.cocci', so write it out to a tempfile with the
77 COCCIFILE
="$(mktemp --suffix=.cocci)"
79 trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
81 cat >"$COCCIFILE" <<EOT
86 + #include "qemu/osdep.h"
89 + #include "qemu/osdep.h"
98 # These aren't standalone C source files
99 echo "SKIPPING $f (not a standalone source file)"
105 *include
/qemu
/osdep.h | \
106 *include
/qemu
/compiler.h | \
107 *include
/glib-compat.h | \
108 *include
/sysemu
/os-posix.h | \
109 *include
/sysemu
/os-win32.h | \
110 *include
/standard-headers
/ )
111 # Removing include lines from osdep.h itself would be counterproductive.
112 echo "SKIPPING $f (special case header)"
115 *include
/standard-headers
/*)
116 echo "SKIPPING $f (autogenerated header)"
123 echo "WARNING: ignoring $f (cannot handle non-C files)"
128 if [ "$MODE" = "c" ]; then
129 # First, use Coccinelle to add qemu/osdep.h before the first existing include
130 # (this will add two lines if the file uses both "..." and <...> #includes,
131 # but we will remove the extras in the next step)
132 spatch
--in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
134 # Now remove any duplicate osdep.h includes
135 perl
-n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
137 # Remove includes of osdep.h itself
138 perl
-n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
139 ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
142 # Remove includes that osdep.h already provides
143 perl
-n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
144 ! (grep { $_ eq $1 } qw (
145 "config-host.h" "config-target.h" "qemu/compiler.h"
146 <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
147 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
148 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
149 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h>
150 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h>
151 "sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h"
157 if [ "$GIT" = "yes" ]; then
159 git commit
--signoff -F - <<EOF
160 $GITSUBJ: Clean up includes
162 Clean up includes so that osdep.h is included first and headers
163 which it implies are not included manually.
165 This commit was created with scripts/clean-includes.