| 1 | #!/bin/sh -e |
| 2 | # |
| 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. |
| 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: |
| 17 | # clean-includes [--git subjectprefix] [--check-dup-head] file-or-dir ... |
| 18 | # or |
| 19 | # clean-includes [--git subjectprefix] [--check-dup-head] --all |
| 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. |
| 25 | # |
| 26 | # If --check-dup-head is specified, additionally check for duplicate |
| 27 | # header includes. |
| 28 | # |
| 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). This is equivalent to passing '.' as the directory to |
| 32 | # scan. |
| 33 | |
| 34 | # This script requires Coccinelle to be installed. |
| 35 | |
| 36 | # .c files will have the osdep.h included added, and redundant |
| 37 | # includes removed. |
| 38 | # .h files will have redundant includes (including includes of osdep.h) |
| 39 | # removed. |
| 40 | # Other files (including C++ and ObjectiveC) can't be handled by this script. |
| 41 | |
| 42 | GIT=no |
| 43 | DUPHEAD=no |
| 44 | |
| 45 | # Save the original arguments in case we want to put them in |
| 46 | # a git commit message, quoted for the shell so that we handle |
| 47 | # args with spaces/metacharacters correctly. |
| 48 | # The quote_sh() function is the same one we use in configure. |
| 49 | |
| 50 | quote_sh() { |
| 51 | printf "%s" "$1" | sed "s,','\\\\'',g; s,.*,'&'," |
| 52 | } |
| 53 | |
| 54 | quote_args() { |
| 55 | while [ $# -gt 0 ]; do |
| 56 | printf "%s" "$(quote_sh "$1")" |
| 57 | shift |
| 58 | if [ $# -gt 0 ]; then |
| 59 | printf " " |
| 60 | fi |
| 61 | done |
| 62 | } |
| 63 | |
| 64 | QUOTEDARGS="$(quote_args "$@")" |
| 65 | |
| 66 | while true |
| 67 | do |
| 68 | case $1 in |
| 69 | "--git") |
| 70 | if [ $# -eq 1 ]; then |
| 71 | echo "--git option requires an argument" |
| 72 | exit 1 |
| 73 | fi |
| 74 | GITSUBJ="$2" |
| 75 | GIT=yes |
| 76 | shift |
| 77 | shift |
| 78 | ;; |
| 79 | "--check-dup-head") |
| 80 | DUPHEAD=yes |
| 81 | shift |
| 82 | ;; |
| 83 | "--") |
| 84 | shift |
| 85 | break |
| 86 | ;; |
| 87 | *) |
| 88 | break |
| 89 | ;; |
| 90 | esac |
| 91 | done |
| 92 | |
| 93 | if [ $# -eq 0 ]; then |
| 94 | echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]" |
| 95 | echo "(modifies the files in place)" |
| 96 | exit 1 |
| 97 | fi |
| 98 | |
| 99 | # --all means "scan everything starting from the current directory" |
| 100 | if [ "$1" = "--all" ]; then |
| 101 | set -- '.' |
| 102 | fi |
| 103 | |
| 104 | # Annoyingly coccinelle won't read a scriptfile unless its |
| 105 | # name ends '.cocci', so write it out to a tempfile with the |
| 106 | # right kind of name. |
| 107 | COCCIFILE="$(mktemp --suffix=.cocci)" |
| 108 | REGEXFILE="$(mktemp --suffix=.regex)" |
| 109 | |
| 110 | trap 'rm -f -- "$COCCIFILE" "$REGEXFILE"' INT TERM HUP EXIT |
| 111 | |
| 112 | # List of extended regular expressions defining files to ignore |
| 113 | # Comments starting with '#' are permitted |
| 114 | grep -v '^#' >"$REGEXFILE" <<EOT |
| 115 | # These tests are generally standalone binaries |
| 116 | ^tests/(tcg|multiboot|fp|uefi-test-tools|qtest/migration/s390x) |
| 117 | # BIOS sources and third-party subprojects don't follow our rules |
| 118 | ^pc-bios |
| 119 | ^subprojects |
| 120 | # headers under rust are only used for input to bindgen |
| 121 | ^rust |
| 122 | # plugin binaries are standalone |
| 123 | ^contrib/plugins |
| 124 | # the ebpf tool is standalone, and the skeleton header is autogenerated |
| 125 | ^tools/ebpf |
| 126 | ^ebpf/rss.bpf.skeleton.h |
| 127 | # These files just include some other .c file and have no content themselves |
| 128 | ^linux-user/(mips64|x86_64)/(cpu_loop|signal).c |
| 129 | ^linux-user/mips64/elfload.c |
| 130 | # These are autogenerated headers |
| 131 | ^include/standard-headers/ |
| 132 | # osdep.h itself and its friends are expected to include system headers |
| 133 | ^include/qemu/osdep.h |
| 134 | ^include/qemu/compiler.h |
| 135 | ^include/glib-compat.h |
| 136 | ^include/system/os-(posix|win32|wasm).h |
| 137 | # This is for use by plugins, which are standalone binaries |
| 138 | ^include/qemu/qemu-plugin.h |
| 139 | # standalone tools used in building the hexagon target code |
| 140 | ^target/hexagon/(idef-parser|gen_semantics.c|gen_dectree_import.c) |
| 141 | # standalone tool |
| 142 | ^target/s390x/gen-features.c |
| 143 | # gen-vdso is a standalone tool |
| 144 | ^linux-user/gen-vdso.c |
| 145 | # feature-detection code used by meson.bulid |
| 146 | ^scripts/xen-detect.c |
| 147 | # autogenerated by tracetool |
| 148 | ^tests/tracetool/simple.c |
| 149 | # these just include another C file |
| 150 | ^tests/unit/test-rcu-(simpleq|slist|tailq).c |
| 151 | EOT |
| 152 | |
| 153 | # We assume there are no files in the tree with spaces in their name |
| 154 | set -- $(git ls-files "$@" | grep '\.[ch]$' | grep -E -v -f "$REGEXFILE") |
| 155 | |
| 156 | cat >"$COCCIFILE" <<EOT |
| 157 | @@ |
| 158 | @@ |
| 159 | |
| 160 | ( |
| 161 | + #include "qemu/osdep.h" |
| 162 | #include "..." |
| 163 | | |
| 164 | + #include "qemu/osdep.h" |
| 165 | #include <...> |
| 166 | ) |
| 167 | EOT |
| 168 | |
| 169 | files= |
| 170 | for f in "$@"; do |
| 171 | if [ -L "$f" ]; then |
| 172 | echo "SKIPPING $f (symbolic link)" |
| 173 | continue |
| 174 | fi |
| 175 | case "$f" in |
| 176 | *.c.inc) |
| 177 | # These aren't standalone C source files |
| 178 | echo "SKIPPING $f (not a standalone source file)" |
| 179 | continue |
| 180 | ;; |
| 181 | *.c) |
| 182 | MODE=c |
| 183 | ;; |
| 184 | *.h) |
| 185 | MODE=h |
| 186 | ;; |
| 187 | *) |
| 188 | echo "WARNING: ignoring $f (cannot handle non-C files)" |
| 189 | continue |
| 190 | ;; |
| 191 | esac |
| 192 | files="$files $f" |
| 193 | |
| 194 | if [ "$MODE" = "c" ]; then |
| 195 | # First, use Coccinelle to add qemu/osdep.h before the first existing include |
| 196 | # (this will add two lines if the file uses both "..." and <...> #includes, |
| 197 | # but we will remove the extras in the next step) |
| 198 | spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f" |
| 199 | |
| 200 | # Now remove any duplicate osdep.h includes |
| 201 | perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f" |
| 202 | else |
| 203 | # Remove includes of osdep.h itself |
| 204 | perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ || |
| 205 | ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f" |
| 206 | fi |
| 207 | |
| 208 | # Remove includes that osdep.h already provides |
| 209 | perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ || |
| 210 | ! (grep { $_ eq $1 } qw ( |
| 211 | "config-host.h" "config-target.h" "qemu/compiler.h" |
| 212 | <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h> |
| 213 | <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h> |
| 214 | <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h> |
| 215 | <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> |
| 216 | <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h> |
| 217 | "system/os-posix.h, system/os-win32.h "glib-compat.h" |
| 218 | "qemu/typedefs.h" |
| 219 | ))' "$f" |
| 220 | |
| 221 | done |
| 222 | |
| 223 | if [ "$DUPHEAD" = "yes" ] && [ -n "$files" ]; then |
| 224 | if egrep "^[[:space:]]*#[[:space:]]*include" $files | tr -d '[:blank:]' \ |
| 225 | | sort | uniq -c | grep -v '^ *1 '; then |
| 226 | echo "Found duplicate header file includes. Please check the above files manually." |
| 227 | exit 1 |
| 228 | fi |
| 229 | fi |
| 230 | |
| 231 | if [ "$GIT" = "yes" ]; then |
| 232 | git add -- $files |
| 233 | git commit --signoff -F - <<EOF |
| 234 | $GITSUBJ: Clean up includes |
| 235 | |
| 236 | This commit was created with scripts/clean-includes: |
| 237 | ./scripts/clean-includes $QUOTEDARGS |
| 238 | |
| 239 | All .c should include qemu/osdep.h first. The script performs three |
| 240 | related cleanups: |
| 241 | |
| 242 | * Ensure .c files include qemu/osdep.h first. |
| 243 | * Including it in a .h is redundant, since the .c already includes |
| 244 | it. Drop such inclusions. |
| 245 | * Likewise, including headers qemu/osdep.h includes is redundant. |
| 246 | Drop these, too. |
| 247 | |
| 248 | EOF |
| 249 | |
| 250 | fi |