Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 1 | #!/bin/sh -e |
| 2 | # |
| 3 | # Clean up QEMU #include lines by ensuring that qemu/osdep.h |
Peter Maydell | fd3e39a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 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. |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 6 | # |
| 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: |
Anand J | d66253e | 2016-10-21 12:27:06 +0530 | [diff] [blame] | 17 | # clean-includes [--git subjectprefix] [--check-dup-head] file ... |
Peter Maydell | d57106a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 18 | # or |
Anand J | d66253e | 2016-10-21 12:27:06 +0530 | [diff] [blame] | 19 | # clean-includes [--git subjectprefix] [--check-dup-head] --all |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 20 | # |
| 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 Maydell | d57106a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 25 | # |
Anand J | d66253e | 2016-10-21 12:27:06 +0530 | [diff] [blame] | 26 | # If --check-dup-head is specified, additionally check for duplicate |
| 27 | # header includes. |
| 28 | # |
Peter Maydell | d57106a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 29 | # Using --all will cause clean-includes to run on the whole source |
| 30 | # tree (excluding certain directories which are known not to need |
| 31 | # handling). |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 32 | |
| 33 | # This script requires Coccinelle to be installed. |
| 34 | |
Peter Maydell | fd3e39a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 35 | # .c files will have the osdep.h included added, and redundant |
| 36 | # includes removed. |
| 37 | # .h files will have redundant includes (including includes of osdep.h) |
| 38 | # removed. |
| 39 | # Other files (including C++ and ObjectiveC) can't be handled by this script. |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 40 | |
| 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. |
| 44 | |
Stefan Weil | bbd9080 | 2016-05-16 15:23:33 +0200 | [diff] [blame] | 45 | # for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \ |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 46 | # grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \ |
| 47 | # echo $i ; done |
| 48 | |
| 49 | |
| 50 | GIT=no |
Anand J | d66253e | 2016-10-21 12:27:06 +0530 | [diff] [blame] | 51 | DUPHEAD=no |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 52 | |
Peter Maydell | d57106a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 53 | # Extended regular expression defining files to ignore when using --all |
Thomas Huth | 2116650 | 2022-06-03 18:42:49 +0200 | [diff] [blame] | 54 | XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios)' |
Peter Maydell | d57106a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 55 | |
Anand J | d66253e | 2016-10-21 12:27:06 +0530 | [diff] [blame] | 56 | while true |
| 57 | do |
| 58 | case $1 in |
| 59 | "--git") |
| 60 | if [ $# -eq 1 ]; then |
| 61 | echo "--git option requires an argument" |
| 62 | exit 1 |
| 63 | fi |
| 64 | GITSUBJ="$2" |
| 65 | GIT=yes |
| 66 | shift |
| 67 | shift |
| 68 | ;; |
| 69 | "--check-dup-head") |
| 70 | DUPHEAD=yes |
| 71 | shift |
| 72 | ;; |
| 73 | "--") |
| 74 | shift |
| 75 | break |
| 76 | ;; |
| 77 | *) |
| 78 | break |
| 79 | ;; |
| 80 | esac |
| 81 | done |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 82 | |
| 83 | if [ $# -eq 0 ]; then |
Anand J | d66253e | 2016-10-21 12:27:06 +0530 | [diff] [blame] | 84 | echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]" |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 85 | echo "(modifies the files in place)" |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
Peter Maydell | d57106a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 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") |
| 92 | fi |
| 93 | |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 94 | # Annoyingly coccinelle won't read a scriptfile unless its |
| 95 | # name ends '.cocci', so write it out to a tempfile with the |
| 96 | # right kind of name. |
| 97 | COCCIFILE="$(mktemp --suffix=.cocci)" |
| 98 | |
| 99 | trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT |
| 100 | |
| 101 | cat >"$COCCIFILE" <<EOT |
| 102 | @@ |
| 103 | @@ |
| 104 | |
| 105 | ( |
| 106 | + #include "qemu/osdep.h" |
| 107 | #include "..." |
| 108 | | |
| 109 | + #include "qemu/osdep.h" |
| 110 | #include <...> |
| 111 | ) |
| 112 | EOT |
| 113 | |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 114 | for f in "$@"; do |
Peter Maydell | fd3e39a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 115 | case "$f" in |
Paolo Bonzini | 139c183 | 2020-02-04 12:41:01 +0100 | [diff] [blame] | 116 | *.c.inc) |
Peter Maydell | f8e1f5d | 2016-02-23 14:49:42 +0000 | [diff] [blame] | 117 | # These aren't standalone C source files |
| 118 | echo "SKIPPING $f (not a standalone source file)" |
| 119 | continue |
| 120 | ;; |
Peter Maydell | fd3e39a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 121 | *.c) |
| 122 | MODE=c |
| 123 | ;; |
| 124 | *include/qemu/osdep.h | \ |
| 125 | *include/qemu/compiler.h | \ |
Philippe Mathieu-Daudé | a6703e6 | 2020-06-05 16:49:17 +0100 | [diff] [blame] | 126 | *include/qemu/qemu-plugin.h | \ |
Peter Maydell | df891b9 | 2016-05-24 16:24:36 +0100 | [diff] [blame] | 127 | *include/glib-compat.h | \ |
Paolo Bonzini | 02d0e09 | 2016-06-06 13:57:39 +0200 | [diff] [blame] | 128 | *include/sysemu/os-posix.h | \ |
| 129 | *include/sysemu/os-win32.h | \ |
Peter Maydell | fd3e39a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 130 | *include/standard-headers/ ) |
| 131 | # Removing include lines from osdep.h itself would be counterproductive. |
| 132 | echo "SKIPPING $f (special case header)" |
| 133 | continue |
| 134 | ;; |
| 135 | *include/standard-headers/*) |
| 136 | echo "SKIPPING $f (autogenerated header)" |
| 137 | continue |
| 138 | ;; |
| 139 | *.h) |
| 140 | MODE=h |
| 141 | ;; |
| 142 | *) |
| 143 | echo "WARNING: ignoring $f (cannot handle non-C files)" |
| 144 | continue |
| 145 | ;; |
| 146 | esac |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 147 | |
Peter Maydell | fd3e39a | 2016-02-23 11:58:02 +0000 | [diff] [blame] | 148 | if [ "$MODE" = "c" ]; then |
| 149 | # First, use Coccinelle to add qemu/osdep.h before the first existing include |
| 150 | # (this will add two lines if the file uses both "..." and <...> #includes, |
| 151 | # but we will remove the extras in the next step) |
| 152 | spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f" |
| 153 | |
| 154 | # Now remove any duplicate osdep.h includes |
| 155 | perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f" |
| 156 | else |
| 157 | # Remove includes of osdep.h itself |
| 158 | perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ || |
| 159 | ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f" |
| 160 | fi |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 161 | |
| 162 | # Remove includes that osdep.h already provides |
| 163 | perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ || |
| 164 | ! (grep { $_ eq $1 } qw ( |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 165 | "config-host.h" "config-target.h" "qemu/compiler.h" |
Stefan Weil | 8ff98f1 | 2016-03-14 15:47:05 +0100 | [diff] [blame] | 166 | <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h> |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 167 | <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h> |
| 168 | <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h> |
Peter Maydell | df891b9 | 2016-05-24 16:24:36 +0100 | [diff] [blame] | 169 | <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> |
Paolo Bonzini | 02d0e09 | 2016-06-06 13:57:39 +0200 | [diff] [blame] | 170 | <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h> |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 171 | "sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h" |
| 172 | "qemu/typedefs.h" |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 173 | ))' "$f" |
| 174 | |
| 175 | done |
| 176 | |
Anand J | d66253e | 2016-10-21 12:27:06 +0530 | [diff] [blame] | 177 | if [ "$DUPHEAD" = "yes" ]; then |
| 178 | egrep "^[[:space:]]*#[[:space:]]*include" "$@" | tr -d '[:blank:]' \ |
| 179 | | sort | uniq -c | awk '{if ($1 > 1) print $0}' |
| 180 | if [ $? -eq 0 ]; then |
| 181 | echo "Found duplicate header file includes. Please check the above files manually." |
| 182 | exit 1 |
| 183 | fi |
| 184 | fi |
| 185 | |
Peter Maydell | 8507170 | 2015-12-07 16:23:43 +0000 | [diff] [blame] | 186 | if [ "$GIT" = "yes" ]; then |
| 187 | git add -- "$@" |
| 188 | git commit --signoff -F - <<EOF |
| 189 | $GITSUBJ: Clean up includes |
| 190 | |
| 191 | Clean up includes so that osdep.h is included first and headers |
| 192 | which it implies are not included manually. |
| 193 | |
| 194 | This commit was created with scripts/clean-includes. |
| 195 | |
| 196 | EOF |
| 197 | |
| 198 | fi |