2015-12-07 17:23:43 +01:00
|
|
|
#!/bin/sh -e
|
|
|
|
#
|
|
|
|
# Clean up QEMU #include lines by ensuring that qemu/osdep.h
|
2016-02-23 12:58:02 +01:00
|
|
|
# is the first include listed in .c files, and no headers provided
|
|
|
|
# by osdep.h itself are redundantly included in either .c or .h files.
|
2015-12-07 17:23:43 +01:00
|
|
|
#
|
|
|
|
# Copyright (c) 2015 Linaro Limited
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Peter Maydell <peter.maydell@linaro.org>
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2
|
|
|
|
# or (at your option) any later version. See the COPYING file in
|
|
|
|
# the top-level directory.
|
|
|
|
|
|
|
|
# Usage:
|
2016-10-21 08:57:06 +02:00
|
|
|
# clean-includes [--git subjectprefix] [--check-dup-head] file ...
|
2016-02-23 12:58:02 +01:00
|
|
|
# or
|
2016-10-21 08:57:06 +02:00
|
|
|
# clean-includes [--git subjectprefix] [--check-dup-head] --all
|
2015-12-07 17:23:43 +01:00
|
|
|
#
|
|
|
|
# If the --git subjectprefix option is given, then after making
|
|
|
|
# the changes to the files this script will create a git commit
|
|
|
|
# with the subject line "subjectprefix: Clean up includes"
|
|
|
|
# and a boilerplate commit message.
|
2016-02-23 12:58:02 +01:00
|
|
|
#
|
2016-10-21 08:57:06 +02:00
|
|
|
# If --check-dup-head is specified, additionally check for duplicate
|
|
|
|
# header includes.
|
|
|
|
#
|
2016-02-23 12:58:02 +01:00
|
|
|
# Using --all will cause clean-includes to run on the whole source
|
|
|
|
# tree (excluding certain directories which are known not to need
|
|
|
|
# handling).
|
2015-12-07 17:23:43 +01:00
|
|
|
|
|
|
|
# This script requires Coccinelle to be installed.
|
|
|
|
|
2016-02-23 12:58:02 +01:00
|
|
|
# .c files will have the osdep.h included added, and redundant
|
|
|
|
# includes removed.
|
|
|
|
# .h files will have redundant includes (including includes of osdep.h)
|
|
|
|
# removed.
|
|
|
|
# Other files (including C++ and ObjectiveC) can't be handled by this script.
|
2015-12-07 17:23:43 +01:00
|
|
|
|
|
|
|
# The following one-liner may be handy for finding files to run this on.
|
|
|
|
# However some caution is required regarding files that might be part
|
|
|
|
# of the guest agent or standalone tests.
|
|
|
|
|
2016-05-16 15:23:33 +02:00
|
|
|
# for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \
|
2015-12-07 17:23:43 +01:00
|
|
|
# grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
|
|
|
|
# echo $i ; done
|
|
|
|
|
|
|
|
|
|
|
|
GIT=no
|
2016-10-21 08:57:06 +02:00
|
|
|
DUPHEAD=no
|
2015-12-07 17:23:43 +01:00
|
|
|
|
2016-02-23 12:58:02 +01:00
|
|
|
# Extended regular expression defining files to ignore when using --all
|
2022-06-03 18:42:49 +02:00
|
|
|
XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios)'
|
2016-02-23 12:58:02 +01:00
|
|
|
|
2016-10-21 08:57:06 +02:00
|
|
|
while true
|
|
|
|
do
|
|
|
|
case $1 in
|
|
|
|
"--git")
|
|
|
|
if [ $# -eq 1 ]; then
|
|
|
|
echo "--git option requires an argument"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
GITSUBJ="$2"
|
|
|
|
GIT=yes
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
"--check-dup-head")
|
|
|
|
DUPHEAD=yes
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
"--")
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2015-12-07 17:23:43 +01:00
|
|
|
|
|
|
|
if [ $# -eq 0 ]; then
|
2016-10-21 08:57:06 +02:00
|
|
|
echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]"
|
2015-12-07 17:23:43 +01:00
|
|
|
echo "(modifies the files in place)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-02-23 12:58:02 +01:00
|
|
|
if [ "$1" = "--all" ]; then
|
|
|
|
# We assume there are no files in the tree with spaces in their name
|
|
|
|
set -- $(git ls-files '*.[ch]' | grep -E -v "$XDIRREGEX")
|
|
|
|
fi
|
|
|
|
|
2015-12-07 17:23:43 +01:00
|
|
|
# Annoyingly coccinelle won't read a scriptfile unless its
|
|
|
|
# name ends '.cocci', so write it out to a tempfile with the
|
|
|
|
# right kind of name.
|
|
|
|
COCCIFILE="$(mktemp --suffix=.cocci)"
|
|
|
|
|
|
|
|
trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT
|
|
|
|
|
|
|
|
cat >"$COCCIFILE" <<EOT
|
|
|
|
@@
|
|
|
|
@@
|
|
|
|
|
|
|
|
(
|
|
|
|
+ #include "qemu/osdep.h"
|
|
|
|
#include "..."
|
|
|
|
|
|
|
|
|
+ #include "qemu/osdep.h"
|
|
|
|
#include <...>
|
|
|
|
)
|
|
|
|
EOT
|
|
|
|
|
2023-02-02 14:38:11 +01:00
|
|
|
files=
|
2015-12-07 17:23:43 +01:00
|
|
|
for f in "$@"; do
|
2016-02-23 12:58:02 +01:00
|
|
|
case "$f" in
|
2020-02-04 12:41:01 +01:00
|
|
|
*.c.inc)
|
2016-02-23 15:49:42 +01:00
|
|
|
# These aren't standalone C source files
|
|
|
|
echo "SKIPPING $f (not a standalone source file)"
|
|
|
|
continue
|
|
|
|
;;
|
2016-02-23 12:58:02 +01:00
|
|
|
*.c)
|
|
|
|
MODE=c
|
|
|
|
;;
|
|
|
|
*include/qemu/osdep.h | \
|
|
|
|
*include/qemu/compiler.h | \
|
2020-06-05 17:49:17 +02:00
|
|
|
*include/qemu/qemu-plugin.h | \
|
2016-05-24 17:24:36 +02:00
|
|
|
*include/glib-compat.h | \
|
2016-06-06 13:57:39 +02:00
|
|
|
*include/sysemu/os-posix.h | \
|
|
|
|
*include/sysemu/os-win32.h | \
|
2016-02-23 12:58:02 +01:00
|
|
|
*include/standard-headers/ )
|
|
|
|
# Removing include lines from osdep.h itself would be counterproductive.
|
|
|
|
echo "SKIPPING $f (special case header)"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
*include/standard-headers/*)
|
|
|
|
echo "SKIPPING $f (autogenerated header)"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
*.h)
|
|
|
|
MODE=h
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "WARNING: ignoring $f (cannot handle non-C files)"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
esac
|
2023-02-02 14:38:11 +01:00
|
|
|
files="$files $f"
|
2016-02-23 12:58:02 +01:00
|
|
|
|
|
|
|
if [ "$MODE" = "c" ]; then
|
|
|
|
# First, use Coccinelle to add qemu/osdep.h before the first existing include
|
|
|
|
# (this will add two lines if the file uses both "..." and <...> #includes,
|
|
|
|
# but we will remove the extras in the next step)
|
|
|
|
spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f"
|
|
|
|
|
|
|
|
# Now remove any duplicate osdep.h includes
|
|
|
|
perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f"
|
|
|
|
else
|
|
|
|
# Remove includes of osdep.h itself
|
|
|
|
perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
|
|
|
|
! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f"
|
|
|
|
fi
|
2015-12-07 17:23:43 +01:00
|
|
|
|
|
|
|
# Remove includes that osdep.h already provides
|
|
|
|
perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
|
|
|
|
! (grep { $_ eq $1 } qw (
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
"config-host.h" "config-target.h" "qemu/compiler.h"
|
2016-03-14 15:47:05 +01:00
|
|
|
<setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
|
2015-12-07 17:23:43 +01:00
|
|
|
<stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
|
|
|
|
<limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
|
2016-05-24 17:24:36 +02:00
|
|
|
<sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h>
|
2016-06-06 13:57:39 +02:00
|
|
|
<sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h>
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
"sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h"
|
|
|
|
"qemu/typedefs.h"
|
2015-12-07 17:23:43 +01:00
|
|
|
))' "$f"
|
|
|
|
|
|
|
|
done
|
|
|
|
|
2023-02-02 14:38:11 +01:00
|
|
|
if [ "$DUPHEAD" = "yes" ] && [ -n "$files" ]; then
|
|
|
|
egrep "^[[:space:]]*#[[:space:]]*include" $files | tr -d '[:blank:]' \
|
2016-10-21 08:57:06 +02:00
|
|
|
| sort | uniq -c | awk '{if ($1 > 1) print $0}'
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "Found duplicate header file includes. Please check the above files manually."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2015-12-07 17:23:43 +01:00
|
|
|
if [ "$GIT" = "yes" ]; then
|
2023-02-02 14:38:11 +01:00
|
|
|
git add -- $files
|
2015-12-07 17:23:43 +01:00
|
|
|
git commit --signoff -F - <<EOF
|
|
|
|
$GITSUBJ: Clean up includes
|
|
|
|
|
|
|
|
Clean up includes so that osdep.h is included first and headers
|
|
|
|
which it implies are not included manually.
|
|
|
|
|
|
|
|
This commit was created with scripts/clean-includes.
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
fi
|