blob: a1faa60710e07e4d2616e8a1948b4cff878c6d59 [file] [log] [blame]
Peter Maydell85071702015-12-07 16:23:43 +00001#!/bin/sh -e
2#
3# Clean up QEMU #include lines by ensuring that qemu/osdep.h
Peter Maydellfd3e39a2016-02-23 11:58:02 +00004# 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.
Peter Maydell85071702015-12-07 16:23:43 +00006#
7# Copyright (c) 2015 Linaro Limited
8#
9# Authors:
10# Peter Maydell <peter.maydell@linaro.org>
11#
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.
15
16# Usage:
17# clean-includes [--git subjectprefix] file ...
Peter Maydelld57106a2016-02-23 11:58:02 +000018# or
19# clean-includes [--git subjectprefix] --all
Peter Maydell85071702015-12-07 16:23:43 +000020#
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.
Peter Maydelld57106a2016-02-23 11:58:02 +000025#
26# Using --all will cause clean-includes to run on the whole source
27# tree (excluding certain directories which are known not to need
28# handling).
Peter Maydell85071702015-12-07 16:23:43 +000029
30# This script requires Coccinelle to be installed.
31
Peter Maydellfd3e39a2016-02-23 11:58:02 +000032# .c files will have the osdep.h included added, and redundant
33# includes removed.
34# .h files will have redundant includes (including includes of osdep.h)
35# removed.
36# Other files (including C++ and ObjectiveC) can't be handled by this script.
Peter Maydell85071702015-12-07 16:23:43 +000037
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.
41
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 && \
44# echo $i ; done
45
46
47GIT=no
48
Peter Maydelld57106a2016-02-23 11:58:02 +000049# Extended regular expression defining files to ignore when using --all
50XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios|disas/libvixl)'
51
Peter Maydell85071702015-12-07 16:23:43 +000052if [ $# -ne 0 ] && [ "$1" = "--git" ]; then
53 if [ $# -eq 1 ]; then
54 echo "--git option requires an argument"
55 exit 1
56 fi
57 GITSUBJ="$2"
58 GIT=yes
59 shift
60 shift
61fi
62
63if [ $# -eq 0 ]; then
Peter Maydelld57106a2016-02-23 11:58:02 +000064 echo "Usage: clean-includes [--git subjectprefix] [--all | foo.c ...]"
Peter Maydell85071702015-12-07 16:23:43 +000065 echo "(modifies the files in place)"
66 exit 1
67fi
68
Peter Maydelld57106a2016-02-23 11:58:02 +000069if [ "$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")
72fi
73
Peter Maydell85071702015-12-07 16:23:43 +000074# Annoyingly coccinelle won't read a scriptfile unless its
75# name ends '.cocci', so write it out to a tempfile with the
76# right kind of name.
77COCCIFILE="$(mktemp --suffix=.cocci)"
78
79trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
80
81cat >"$COCCIFILE" <<EOT
82@@
83@@
84
85(
86+ #include "qemu/osdep.h"
87 #include "..."
88|
89+ #include "qemu/osdep.h"
90 #include <...>
91)
92EOT
93
94
95for f in "$@"; do
Peter Maydellfd3e39a2016-02-23 11:58:02 +000096 case "$f" in
Peter Maydellf8e1f5d2016-02-23 14:49:42 +000097 *.inc.c)
98 # These aren't standalone C source files
99 echo "SKIPPING $f (not a standalone source file)"
100 continue
101 ;;
Peter Maydellfd3e39a2016-02-23 11:58:02 +0000102 *.c)
103 MODE=c
104 ;;
105 *include/qemu/osdep.h | \
106 *include/qemu/compiler.h | \
107 *include/config.h | \
108 *include/standard-headers/ )
109 # Removing include lines from osdep.h itself would be counterproductive.
110 echo "SKIPPING $f (special case header)"
111 continue
112 ;;
113 *include/standard-headers/*)
114 echo "SKIPPING $f (autogenerated header)"
115 continue
116 ;;
117 *.h)
118 MODE=h
119 ;;
120 *)
121 echo "WARNING: ignoring $f (cannot handle non-C files)"
122 continue
123 ;;
124 esac
Peter Maydell85071702015-12-07 16:23:43 +0000125
Peter Maydellfd3e39a2016-02-23 11:58:02 +0000126 if [ "$MODE" = "c" ]; then
127 # First, use Coccinelle to add qemu/osdep.h before the first existing include
128 # (this will add two lines if the file uses both "..." and <...> #includes,
129 # but we will remove the extras in the next step)
130 spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
131
132 # Now remove any duplicate osdep.h includes
133 perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
134 else
135 # Remove includes of osdep.h itself
136 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
137 ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
138 fi
Peter Maydell85071702015-12-07 16:23:43 +0000139
140 # Remove includes that osdep.h already provides
141 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
142 ! (grep { $_ eq $1 } qw (
143 "config-host.h" "qemu/compiler.h" "config.h"
144 <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
145 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
146 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
147 <sys/stat.h> <sys/time.h> <assert.h> <signal.h>
148 "glib-compat.h" "qapi/error.h"
149 ))' "$f"
150
151done
152
153if [ "$GIT" = "yes" ]; then
154 git add -- "$@"
155 git commit --signoff -F - <<EOF
156$GITSUBJ: Clean up includes
157
158Clean up includes so that osdep.h is included first and headers
159which it implies are not included manually.
160
161This commit was created with scripts/clean-includes.
162
163EOF
164
165fi