1#!/bin/sh -e 2# 3# Clean up QEMU #include lines by ensuring that qemu/osdep.h 4# is the first include listed. 5# 6# Copyright (c) 2015 Linaro Limited 7# 8# Authors: 9# Peter Maydell <peter.maydell@linaro.org> 10# 11# This work is licensed under the terms of the GNU GPL, version 2 12# or (at your option) any later version. See the COPYING file in 13# the top-level directory. 14 15# Usage: 16# clean-includes [--git subjectprefix] file ... 17# 18# If the --git subjectprefix option is given, then after making 19# the changes to the files this script will create a git commit 20# with the subject line "subjectprefix: Clean up includes" 21# and a boilerplate commit message. 22 23# This script requires Coccinelle to be installed. 24 25 26# The following one-liner may be handy for finding files to run this on. 27# However some caution is required regarding files that might be part 28# of the guest agent or standalone tests. 29 30# for i in `git ls-tree --name-only HEAD` ; do test -f $i && \ 31# grep -E '^# *include' $i | head -1 | grep 'osdep.h' ; test $? != 0 && \ 32# echo $i ; done 33 34 35GIT=no 36 37if [ $# -ne 0 ] && [ "$1" = "--git" ]; then 38 if [ $# -eq 1 ]; then 39 echo "--git option requires an argument" 40 exit 1 41 fi 42 GITSUBJ="$2" 43 GIT=yes 44 shift 45 shift 46fi 47 48if [ $# -eq 0 ]; then 49 echo "Usage: clean-includes [--git subjectprefix] foo.c ..." 50 echo "(modifies the files in place)" 51 exit 1 52fi 53 54# Annoyingly coccinelle won't read a scriptfile unless its 55# name ends '.cocci', so write it out to a tempfile with the 56# right kind of name. 57COCCIFILE="$(mktemp --suffix=.cocci)" 58 59trap 'rm -f -- "$COCCIFILE"' INT TERM HUP EXIT 60 61cat >"$COCCIFILE" <<EOT 62@@ 63@@ 64 65( 66+ #include "qemu/osdep.h" 67 #include "..." 68| 69+ #include "qemu/osdep.h" 70 #include <...> 71) 72EOT 73 74 75for f in "$@"; do 76 # First, use coccinelle to add qemu/osdep.h before the first existing include 77 # (this will add two lines if the file uses both "..." and <...> #includes, 78 # but we will remove the extras in the next step) 79 spatch --in-place --no-show-diff --cocci-file "$COCCIFILE" "$f" 80 81 # Now remove any duplicate osdep.h includes 82 perl -n -i -e 'print if !/#include "qemu\/osdep.h"/ || !$n++;' "$f" 83 84 # Remove includes that osdep.h already provides 85 perl -n -i -e 'print if !/\s*#\s*include\s*(["<][^>"]*[">])/ || 86 ! (grep { $_ eq $1 } qw ( 87 "config-host.h" "qemu/compiler.h" "config.h" 88 <stdarg.h> <stddef.h> <stdbool.h> <stdint.h> <sys/types.h> 89 <stdlib.h> <stdio.h> <string.h> <strings.h> <inttypes.h> 90 <limits.h> <unistd.h> <time.h> <ctype.h> <errno.h> <fcntl.h> 91 <sys/stat.h> <sys/time.h> <assert.h> <signal.h> 92 "glib-compat.h" "qapi/error.h" 93 ))' "$f" 94 95done 96 97if [ "$GIT" = "yes" ]; then 98 git add -- "$@" 99 git commit --signoff -F - <<EOF 100$GITSUBJ: Clean up includes 101 102Clean up includes so that osdep.h is included first and headers 103which it implies are not included manually. 104 105This commit was created with scripts/clean-includes. 106 107EOF 108 109fi 110