3 # Clean up QEMU #include lines by ensuring that qemu/osdep.h
4 # is the first include listed.
6 # Copyright (c) 2015 Linaro Limited
9 # Peter Maydell <peter.maydell@linaro.org>
11 # This work is licensed under the terms of the GNU GPL, version 2
12 # or (at your option) any later version. See the COPYING file in
13 # the top-level directory.
16 # clean-includes [--git subjectprefix] file ...
18 # If the --git subjectprefix option is given, then after making
19 # the changes to the files this script will create a git commit
20 # with the subject line "subjectprefix: Clean up includes"
21 # and a boilerplate commit message.
23 # This script requires Coccinelle to be installed.
26 # The following one-liner may be handy for finding files to run this on.
27 # However some caution is required regarding files that might be part
28 # of the guest agent or standalone tests.
30 # for i in `git ls-tree --name-only HEAD` ; do test -f $i && \
31 # grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
37 if [ $# -ne 0 ] && [ "$1" = "--git" ]; then
39 echo "--git option requires an argument"
49 echo "Usage: clean-includes [--git subjectprefix] foo.c ..."
50 echo "(modifies the files in place)"
54 # Annoyingly coccinelle won't read a scriptfile unless its
55 # name ends '.cocci', so write it out to a tempfile with the
57 COCCIFILE
="$(mktemp --suffix=.cocci)"
59 trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
61 cat >"$COCCIFILE" <<EOT
66 + #include "qemu/osdep.h"
69 + #include "qemu/osdep.h"
76 # First, use coccinelle to add qemu/osdep.h before the first existing include
77 # (this will add two lines if the file uses both "..." and <...> #includes,
78 # but we will remove the extras in the next step)
79 spatch
--in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
81 # Now remove any duplicate osdep.h includes
82 perl
-n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
84 # Remove includes that osdep.h already provides
85 perl
-n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
86 ! (grep { $_ eq $1 } qw (
87 "config-host.h" "qemu/compiler.h" "config.h"
88 <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
89 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
90 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
91 <sys/stat.h> <sys/time.h> <assert.h> <signal.h>
92 "glib-compat.h" "qapi/error.h"
97 if [ "$GIT" = "yes" ]; then
99 git commit
--signoff -F - <<EOF
100 $GITSUBJ: Clean up includes
102 Clean up includes so that osdep.h is included first and headers
103 which it implies are not included manually.
105 This commit was created with scripts/clean-includes.