blob: 58e1607a82eac570452ec5b91d45139c62ac543c [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:
Anand Jd66253e2016-10-21 12:27:06 +053017# clean-includes [--git subjectprefix] [--check-dup-head] file ...
Peter Maydelld57106a2016-02-23 11:58:02 +000018# or
Anand Jd66253e2016-10-21 12:27:06 +053019# clean-includes [--git subjectprefix] [--check-dup-head] --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#
Anand Jd66253e2016-10-21 12:27:06 +053026# If --check-dup-head is specified, additionally check for duplicate
27# header includes.
28#
Peter Maydelld57106a2016-02-23 11:58:02 +000029# 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 Maydell85071702015-12-07 16:23:43 +000032
33# This script requires Coccinelle to be installed.
34
Peter Maydellfd3e39a2016-02-23 11:58:02 +000035# .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 Maydell85071702015-12-07 16:23:43 +000040
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 Weilbbd90802016-05-16 15:23:33 +020045# for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \
Peter Maydell85071702015-12-07 16:23:43 +000046# grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
47# echo $i ; done
48
49
50GIT=no
Anand Jd66253e2016-10-21 12:27:06 +053051DUPHEAD=no
Peter Maydell85071702015-12-07 16:23:43 +000052
Peter Maydelld57106a2016-02-23 11:58:02 +000053# Extended regular expression defining files to ignore when using --all
Thomas Huth21166502022-06-03 18:42:49 +020054XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios)'
Peter Maydelld57106a2016-02-23 11:58:02 +000055
Anand Jd66253e2016-10-21 12:27:06 +053056while true
57do
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
81done
Peter Maydell85071702015-12-07 16:23:43 +000082
83if [ $# -eq 0 ]; then
Anand Jd66253e2016-10-21 12:27:06 +053084 echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]"
Peter Maydell85071702015-12-07 16:23:43 +000085 echo "(modifies the files in place)"
86 exit 1
87fi
88
Peter Maydelld57106a2016-02-23 11:58:02 +000089if [ "$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")
92fi
93
Peter Maydell85071702015-12-07 16:23:43 +000094# 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.
97COCCIFILE="$(mktemp --suffix=.cocci)"
98
99trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
100
101cat >"$COCCIFILE" <<EOT
102@@
103@@
104
105(
106+ #include "qemu/osdep.h"
107 #include "..."
108|
109+ #include "qemu/osdep.h"
110 #include <...>
111)
112EOT
113
Markus Armbrusteraa735872023-02-02 14:38:11 +0100114files=
Peter Maydell85071702015-12-07 16:23:43 +0000115for f in "$@"; do
Markus Armbruster6a7f0512023-02-02 14:38:13 +0100116 if [ -L "$f" ]; then
117 echo "SKIPPING $f (symbolic link)"
118 continue
119 fi
Peter Maydellfd3e39a2016-02-23 11:58:02 +0000120 case "$f" in
Paolo Bonzini139c1832020-02-04 12:41:01 +0100121 *.c.inc)
Peter Maydellf8e1f5d2016-02-23 14:49:42 +0000122 # These aren't standalone C source files
123 echo "SKIPPING $f (not a standalone source file)"
124 continue
125 ;;
Peter Maydellfd3e39a2016-02-23 11:58:02 +0000126 *.c)
127 MODE=c
128 ;;
129 *include/qemu/osdep.h | \
130 *include/qemu/compiler.h | \
Philippe Mathieu-Daudéa6703e62020-06-05 16:49:17 +0100131 *include/qemu/qemu-plugin.h | \
Peter Maydelldf891b92016-05-24 16:24:36 +0100132 *include/glib-compat.h | \
Paolo Bonzini02d0e092016-06-06 13:57:39 +0200133 *include/sysemu/os-posix.h | \
134 *include/sysemu/os-win32.h | \
Peter Maydellfd3e39a2016-02-23 11:58:02 +0000135 *include/standard-headers/ )
136 # Removing include lines from osdep.h itself would be counterproductive.
137 echo "SKIPPING $f (special case header)"
138 continue
139 ;;
140 *include/standard-headers/*)
141 echo "SKIPPING $f (autogenerated header)"
142 continue
143 ;;
144 *.h)
145 MODE=h
146 ;;
147 *)
148 echo "WARNING: ignoring $f (cannot handle non-C files)"
149 continue
150 ;;
151 esac
Markus Armbrusteraa735872023-02-02 14:38:11 +0100152 files="$files $f"
Peter Maydell85071702015-12-07 16:23:43 +0000153
Peter Maydellfd3e39a2016-02-23 11:58:02 +0000154 if [ "$MODE" = "c" ]; then
155 # First, use Coccinelle to add qemu/osdep.h before the first existing include
156 # (this will add two lines if the file uses both "..." and <...> #includes,
157 # but we will remove the extras in the next step)
158 spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
159
160 # Now remove any duplicate osdep.h includes
161 perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
162 else
163 # Remove includes of osdep.h itself
164 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
165 ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
166 fi
Peter Maydell85071702015-12-07 16:23:43 +0000167
168 # Remove includes that osdep.h already provides
169 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
170 ! (grep { $_ eq $1 } qw (
Markus Armbrusterda34e652016-03-14 09:01:28 +0100171 "config-host.h" "config-target.h" "qemu/compiler.h"
Stefan Weil8ff98f12016-03-14 15:47:05 +0100172 <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
Peter Maydell85071702015-12-07 16:23:43 +0000173 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
174 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
Peter Maydelldf891b92016-05-24 16:24:36 +0100175 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h>
Paolo Bonzini02d0e092016-06-06 13:57:39 +0200176 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h>
Markus Armbrusterda34e652016-03-14 09:01:28 +0100177 "sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h"
178 "qemu/typedefs.h"
Peter Maydell85071702015-12-07 16:23:43 +0000179 ))' "$f"
180
181done
182
Markus Armbrusteraa735872023-02-02 14:38:11 +0100183if [ "$DUPHEAD" = "yes" ] && [ -n "$files" ]; then
Markus Armbruster25551502023-02-02 14:38:12 +0100184 if egrep "^[[:space:]]*#[[:space:]]*include" $files | tr -d '[:blank:]' \
185 | sort | uniq -c | grep -v '^ *1 '; then
Anand Jd66253e2016-10-21 12:27:06 +0530186 echo "Found duplicate header file includes. Please check the above files manually."
187 exit 1
188 fi
189fi
190
Peter Maydell85071702015-12-07 16:23:43 +0000191if [ "$GIT" = "yes" ]; then
Markus Armbrusteraa735872023-02-02 14:38:11 +0100192 git add -- $files
Peter Maydell85071702015-12-07 16:23:43 +0000193 git commit --signoff -F - <<EOF
194$GITSUBJ: Clean up includes
195
Peter Maydell85071702015-12-07 16:23:43 +0000196This commit was created with scripts/clean-includes.
197
Markus Armbrusterd598d972023-02-02 14:38:14 +0100198All .c should include qemu/osdep.h first. The script performs three
199related cleanups:
200
201* Ensure .c files include qemu/osdep.h first.
202* Including it in a .h is redundant, since the .c already includes
203 it. Drop such inclusions.
204* Likewise, including headers qemu/osdep.h includes is redundant.
205 Drop these, too.
206
Peter Maydell85071702015-12-07 16:23:43 +0000207EOF
208
209fi