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 ... 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). 32 33# This script requires Coccinelle to be installed. 34 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. 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 45# for i in $(git ls-tree --name-only HEAD) ; do test -f $i && \ 46# grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \ 47# echo $i ; done 48 49 50GIT=no 51DUPHEAD=no 52 53# Extended regular expression defining files to ignore when using --all 54XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios|disas/libvixl)' 55 56while 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 82 83if [ $# -eq 0 ]; then 84 echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]" 85 echo "(modifies the files in place)" 86 exit 1 87fi 88 89if [ "$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 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. 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 114for f in "$@"; do 115 case "$f" in 116 *.inc.c) 117 # These aren't standalone C source files 118 echo "SKIPPING $f (not a standalone source file)" 119 continue 120 ;; 121 *.c) 122 MODE=c 123 ;; 124 *include/qemu/osdep.h | \ 125 *include/qemu/compiler.h | \ 126 *include/glib-compat.h | \ 127 *include/sysemu/os-posix.h | \ 128 *include/sysemu/os-win32.h | \ 129 *include/standard-headers/ ) 130 # Removing include lines from osdep.h itself would be counterproductive. 131 echo "SKIPPING $f (special case header)" 132 continue 133 ;; 134 *include/standard-headers/*) 135 echo "SKIPPING $f (autogenerated header)" 136 continue 137 ;; 138 *.h) 139 MODE=h 140 ;; 141 *) 142 echo "WARNING: ignoring $f (cannot handle non-C files)" 143 continue 144 ;; 145 esac 146 147 if [ "$MODE" = "c" ]; then 148 # First, use Coccinelle to add qemu/osdep.h before the first existing include 149 # (this will add two lines if the file uses both "..." and <...> #includes, 150 # but we will remove the extras in the next step) 151 spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f" 152 153 # Now remove any duplicate osdep.h includes 154 perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f" 155 else 156 # Remove includes of osdep.h itself 157 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ || 158 ! (grep { $_ eq $1 } qw ("qemu/osdep.h"))' "$f" 159 fi 160 161 # Remove includes that osdep.h already provides 162 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ || 163 ! (grep { $_ eq $1 } qw ( 164 "config-host.h" "config-target.h" "qemu/compiler.h" 165 <setjmp.h> <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h> 166 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h> 167 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h> 168 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> 169 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> <glib.h> <sys/mman.h> 170 "sysemu/os-posix.h, sysemu/os-win32.h "glib-compat.h" 171 "qemu/typedefs.h" 172 ))' "$f" 173 174done 175 176if [ "$DUPHEAD" = "yes" ]; then 177 egrep "^[[:space:]]*#[[:space:]]*include" "$@" | tr -d '[:blank:]' \ 178 | sort | uniq -c | awk '{if ($1 > 1) print $0}' 179 if [ $? -eq 0 ]; then 180 echo "Found duplicate header file includes. Please check the above files manually." 181 exit 1 182 fi 183fi 184 185if [ "$GIT" = "yes" ]; then 186 git add -- "$@" 187 git commit --signoff -F - <<EOF 188$GITSUBJ: Clean up includes 189 190Clean up includes so that osdep.h is included first and headers 191which it implies are not included manually. 192 193This commit was created with scripts/clean-includes. 194 195EOF 196 197fi 198