Commit | Line | Data |
---|---|---|
9e395550 | 1 | #!/bin/bash |
74425eee NP |
2 | |
3 | SPATCH="`which ${SPATCH:=spatch}`" | |
4 | ||
90d06a46 KC |
5 | trap kill_running SIGTERM SIGINT |
6 | declare -a SPATCH_PID | |
7 | ||
26e56720 BS |
8 | # The verbosity may be set by the environmental parameter V= |
9 | # as for example with 'make V=1 coccicheck' | |
10 | ||
11 | if [ -n "$V" -a "$V" != "0" ]; then | |
90d06a46 | 12 | VERBOSE="$V" |
26e56720 BS |
13 | else |
14 | VERBOSE=0 | |
15 | fi | |
16 | ||
90d06a46 KC |
17 | if [ -z "$J" ]; then |
18 | NPROC=$(getconf _NPROCESSORS_ONLN) | |
19 | else | |
20 | NPROC="$J" | |
21 | fi | |
22 | ||
93f14468 | 23 | FLAGS="$SPFLAGS --very-quiet" |
9e395550 NP |
24 | |
25 | # spatch only allows include directories with the syntax "-I include" | |
26 | # while gcc also allows "-Iinclude" and "-include include" | |
27 | COCCIINCLUDE=${LINUXINCLUDE//-I/-I } | |
28 | COCCIINCLUDE=${COCCIINCLUDE//-include/-I} | |
29 | ||
1e9dea2a NP |
30 | if [ "$C" = "1" -o "$C" = "2" ]; then |
31 | ONLINE=1 | |
32 | ||
9e395550 NP |
33 | # Take only the last argument, which is the C file to test |
34 | shift $(( $# - 1 )) | |
35 | OPTIONS="$COCCIINCLUDE $1" | |
1e9dea2a NP |
36 | else |
37 | ONLINE=0 | |
d0bc1fb4 | 38 | if [ "$KBUILD_EXTMOD" = "" ] ; then |
93f14468 | 39 | OPTIONS="--dir $srctree $COCCIINCLUDE" |
d0bc1fb4 | 40 | else |
93f14468 | 41 | OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" |
d0bc1fb4 | 42 | fi |
1e9dea2a NP |
43 | fi |
44 | ||
bad6a409 | 45 | if [ "$KBUILD_EXTMOD" != "" ] ; then |
93f14468 | 46 | OPTIONS="--patch $srctree $OPTIONS" |
bad6a409 NP |
47 | fi |
48 | ||
74425eee NP |
49 | if [ ! -x "$SPATCH" ]; then |
50 | echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' | |
51 | exit 1 | |
52 | fi | |
53 | ||
54 | if [ "$MODE" = "" ] ; then | |
1e9dea2a | 55 | if [ "$ONLINE" = "0" ] ; then |
1f0a6742 NP |
56 | echo 'You have not explicitly specified the mode to use. Using default "report" mode.' |
57 | echo 'Available modes are the following: patch, report, context, org' | |
1e9dea2a | 58 | echo 'You can specify the mode with "make coccicheck MODE=<mode>"' |
1f0a6742 NP |
59 | echo 'Note however that some modes are not implemented by some semantic patches.' |
60 | fi | |
61 | MODE="report" | |
62 | fi | |
63 | ||
64 | if [ "$MODE" = "chain" ] ; then | |
65 | if [ "$ONLINE" = "0" ] ; then | |
66 | echo 'You have selected the "chain" mode.' | |
67 | echo 'All available modes will be tried (in that order): patch, report, context, org' | |
1e9dea2a | 68 | fi |
03ee0c42 | 69 | elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then |
93f14468 | 70 | FLAGS="$FLAGS --no-show-diff" |
74425eee NP |
71 | fi |
72 | ||
1e9dea2a NP |
73 | if [ "$ONLINE" = "0" ] ; then |
74 | echo '' | |
75 | echo 'Please check for false positives in the output before submitting a patch.' | |
76 | echo 'When using "patch" mode, carefully review the patch before submitting it.' | |
77 | echo '' | |
78 | fi | |
74425eee | 79 | |
5303265a | 80 | run_cmd() { |
90d06a46 | 81 | local i |
5303265a | 82 | if [ $VERBOSE -ne 0 ] ; then |
90d06a46 | 83 | echo "Running ($NPROC in parallel): $@" |
5303265a | 84 | fi |
90d06a46 | 85 | for i in $(seq 0 $(( NPROC - 1)) ); do |
93f14468 | 86 | eval "$@ --max $NPROC --index $i &" |
90d06a46 KC |
87 | SPATCH_PID[$i]=$! |
88 | if [ $VERBOSE -eq 2 ] ; then | |
89 | echo "${SPATCH_PID[$i]} running" | |
90 | fi | |
91 | done | |
92 | wait | |
5303265a BS |
93 | } |
94 | ||
90d06a46 KC |
95 | kill_running() { |
96 | for i in $(seq $(( NPROC - 1 )) ); do | |
97 | if [ $VERBOSE -eq 2 ] ; then | |
98 | echo "Killing ${SPATCH_PID[$i]}" | |
99 | fi | |
100 | kill ${SPATCH_PID[$i]} 2>/dev/null | |
101 | done | |
102 | } | |
5303265a | 103 | |
1e9dea2a | 104 | coccinelle () { |
74425eee | 105 | COCCI="$1" |
74425eee NP |
106 | |
107 | OPT=`grep "Option" $COCCI | cut -d':' -f2` | |
74425eee | 108 | |
93f14468 | 109 | # The option '--parse-cocci' can be used to syntactically check the SmPL files. |
1e9dea2a NP |
110 | # |
111 | # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null | |
74425eee | 112 | |
35d88a38 | 113 | if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then |
74425eee | 114 | |
1e9dea2a | 115 | FILE=`echo $COCCI | sed "s|$srctree/||"` |
74425eee | 116 | |
3c908417 NP |
117 | echo "Processing `basename $COCCI`" |
118 | echo "with option(s) \"$OPT\"" | |
119 | echo '' | |
1e9dea2a NP |
120 | echo 'Message example to submit a patch:' |
121 | ||
3c908417 | 122 | sed -ne 's|^///||p' $COCCI |
1e9dea2a | 123 | |
062c1825 NP |
124 | if [ "$MODE" = "patch" ] ; then |
125 | echo ' The semantic patch that makes this change is available' | |
126 | elif [ "$MODE" = "report" ] ; then | |
127 | echo ' The semantic patch that makes this report is available' | |
128 | elif [ "$MODE" = "context" ] ; then | |
129 | echo ' The semantic patch that spots this code is available' | |
130 | elif [ "$MODE" = "org" ] ; then | |
131 | echo ' The semantic patch that makes this Org report is available' | |
132 | else | |
133 | echo ' The semantic patch that makes this output is available' | |
134 | fi | |
1e9dea2a NP |
135 | echo " in $FILE." |
136 | echo '' | |
137 | echo ' More information about semantic patching is available at' | |
138 | echo ' http://coccinelle.lip6.fr/' | |
139 | echo '' | |
140 | ||
3c908417 NP |
141 | if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then |
142 | echo 'Semantic patch information:' | |
143 | sed -ne 's|^//#||p' $COCCI | |
144 | echo '' | |
145 | fi | |
2c1160c8 | 146 | fi |
3c908417 | 147 | |
2c1160c8 | 148 | if [ "$MODE" = "chain" ] ; then |
5303265a | 149 | run_cmd $SPATCH -D patch \ |
93f14468 | 150 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a | 151 | run_cmd $SPATCH -D report \ |
93f14468 | 152 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ |
5303265a | 153 | run_cmd $SPATCH -D context \ |
93f14468 | 154 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a | 155 | run_cmd $SPATCH -D org \ |
93f14468 | 156 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 |
c05cd6dd | 157 | elif [ "$MODE" = "rep+ctxt" ] ; then |
5303265a | 158 | run_cmd $SPATCH -D report \ |
93f14468 | 159 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ |
5303265a | 160 | run_cmd $SPATCH -D context \ |
93f14468 | 161 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a | 162 | else |
93f14468 | 163 | run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a | 164 | fi |
74425eee | 165 | |
74425eee NP |
166 | } |
167 | ||
168 | if [ "$COCCI" = "" ] ; then | |
169 | for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do | |
1e9dea2a | 170 | coccinelle $f |
74425eee NP |
171 | done |
172 | else | |
1e9dea2a | 173 | coccinelle $COCCI |
74425eee | 174 | fi |