aboutsummaryrefslogtreecommitdiff
path: root/scripts/clean-includes
blob: 1af1f824b8e140a0faa41642b4eb178e2d96964b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh -e
#
# Clean up QEMU #include lines by ensuring that qemu/osdep.h
# is the first include listed.
#
# 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:
#   clean-includes [--git subjectprefix] file ...
#
# 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.

# This script requires Coccinelle to be installed.


# 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.

# for i in `git ls-tree --name-only HEAD`  ; do test -f $i && \
#   grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \
#   echo $i ; done


GIT=no

if [ $# -ne 0 ] && [ "$1" = "--git" ]; then
    if [ $# -eq 1 ]; then
        echo "--git option requires an argument"
        exit 1
    fi
    GITSUBJ="$2"
    GIT=yes
    shift
    shift
fi

if [ $# -eq 0 ]; then
    echo "Usage: clean-includes [--git subjectprefix] foo.c ..."
    echo "(modifies the files in place)"
    exit 1
fi

# 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


for f in "$@"; do
  # 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"

  # Remove includes that osdep.h already provides
  perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ ||
                          ! (grep { $_ eq $1 } qw (
           "config-host.h" "qemu/compiler.h" "config.h"
           <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h>
           <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h>
           <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h>
           <sys/stat.h> <sys/time.h> <assert.h> <signal.h>
           "glib-compat.h" "qapi/error.h"
            ))' "$f"

done

if [ "$GIT" = "yes" ]; then
    git add -- "$@"
    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