0090c20d929f5011502730b4f6bd13515c16eb9f
[external/binutils.git] / gdb / testsuite / dg-extract-results.sh
1 #! /bin/sh
2
3 # For a specified tool and optional list of test variants, extract
4 # test results from one or more test summary (.sum) files and combine
5 # the results into a new test summary file, sent to the standard output.
6 # The resulting file can be used with test result comparison scripts for
7 # results from tests that were run in parallel.  See usage() below.
8
9 # Copyright (C) 2008-2017 Free Software Foundation, Inc.
10 # Contributed by Janis Johnson <janis187@us.ibm.com>
11 #
12 # This file is part of GCC.
13 #
14 # GCC is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 3, or (at your option)
17 # any later version.
18 #
19 # GCC is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27 PROGNAME=dg-extract-results.sh
28
29 # Try to use the python version if possible, since it tends to be faster.
30 PYTHON_VER=`echo "$0" | sed 's/sh$/py/'`
31 if test "$PYTHON_VER" != "$0" &&
32    test -f "$PYTHON_VER" &&
33    python -c 'import sys; sys.exit (0 if sys.version_info >= (2, 6) else 1)' \
34      > /dev/null 2> /dev/null; then
35   exec python $PYTHON_VER "$@"
36 fi
37
38 usage() {
39   cat <<EOF >&2
40 Usage: $PROGNAME [-t tool] [-l variant-list] [-L] sum-file ...
41
42     tool           The tool (e.g. g++, libffi) for which to create a
43                    new test summary file.  If not specified then all
44                    specified sum files must be for the same tool.
45     variant-list   One or more test variant names.  If the list is
46                    not specified then one is constructed from all
47                    variants in the files for <tool>.
48     sum-file       A test summary file with the format of those
49                    created by runtest from DejaGnu.
50     If -L is used, merge *.log files instead of *.sum.  In this
51     mode the exact order of lines may not be preserved, just different
52     Running *.exp chunks should be in correct order.
53 EOF
54 }
55
56 # Write a message to the standard error.
57
58 msg() {
59   echo "$@" >&2
60 }
61
62 # Parse the command-line options.
63
64 VARIANTS=""
65 TOOL=""
66 MODE="sum"
67
68 while getopts "l:t:L" ARG; do
69   case $ARG in
70   l)  VARIANTS="${VARIANTS} ${OPTARG}";;
71   t)  test -z "$TOOL" || (msg "${PROGNAME}: only one tool can be specified"; exit 1);
72       TOOL="${OPTARG}";;
73   L)  MODE="log";;
74   \?) usage; exit 0;;
75   esac
76 done
77 shift `expr ${OPTIND} - 1`
78
79 if test $# -lt 1 ; then
80   usage
81   exit 1
82 fi
83
84 TMPDIR=${TMPDIR-/tmp}
85 SUM_FILES="$@"
86 FIRST_SUM=$1
87 TMP=
88 trap 'EXIT_STATUS=$?; rm -rf $TMP && exit $EXIT_STATUS' 0
89 # Create a (secure) tmp directory for tmp files.
90 {
91   TMP=`(umask 077 && mktemp -d -q "${TMPDIR}/dg-combine-results-$$-XXXXXX") 2>/dev/null` &&
92   test -n "$TMP" && test -d "$TMP"
93 } ||
94 {
95   TMP=${TMPDIR}/dg-combine-results-$$-$RANDOM
96   (umask 077 && mkdir $TMP)
97 } ||
98 {
99   msg "${PROGNAME}: cannot create a temporary directory"
100   { (exit 1); exit 1; }
101 }
102
103 # Find a good awk.
104
105 if test -z "$AWK" ; then
106   for AWK in gawk nawk awk
107   do
108     if type $AWK 2>&1 | grep 'not found' > /dev/null 2>&1 ; then
109       :
110     else
111       break
112     fi
113   done
114 fi
115
116 # Verify that the specified summary files exist.
117
118 ERROR=0
119 for FILE in $SUM_FILES
120 do
121   if ! test -f $FILE ; then
122     msg "${PROGNAME}: file $FILE does not exist."
123     ERROR=1
124   fi
125 done
126 test $ERROR -eq 0 || exit 1
127
128 # Test if grep supports the '--text' option
129
130 GREP=grep
131
132 if echo -e '\x00foo\x00' | $GREP --text foo > /dev/null 2>&1 ; then
133   GREP="grep --text"
134 else
135   # Our grep does not recognize the '--text' option.  We have to
136   # treat our files in order to remove any non-printable character.
137   for file in $SUM_FILES ; do
138     mv $file ${file}.orig
139     cat -v ${file}.orig > $file
140   done
141 fi
142
143 if [ -z "$TOOL" ]; then
144   # If no tool was specified, all specified summary files must be for
145   # the same tool.
146
147   CNT=`$GREP '=== .* tests ===' $SUM_FILES | $AWK '{ print $3 }' | sort -u | wc -l`
148   if [ $CNT -eq 1 ]; then
149     TOOL=`$GREP '=== .* tests ===' $FIRST_SUM | $AWK '{ print $2 }'`
150   else
151     msg "${PROGNAME}: sum files are for multiple tools, specify a tool"
152     msg ""
153     usage
154     exit 1
155   fi
156 else
157   # Ignore the specified summary files that are not for this tool.  This
158   # should keep the relevant files in the same order.
159
160   SUM_FILES=`$GREP -l "=== $TOOL" $SUM_FILES`
161   if test -z "$SUM_FILES" ; then
162     msg "${PROGNAME}: none of the specified files are results for $TOOL"
163     exit 1
164   fi
165 fi
166
167 if [ "$TOOL" = acats ]; then
168   # Acats *.sum or *.log files aren't dejagnu generated, and they have
169   # somewhat different format.
170   ACATS_AWK=${TMP}/acats.awk
171   cat <<EOF > $ACATS_AWK
172 BEGIN {
173   print_prologue=1; curfile=""; insummary=0
174   passcnt=0; failcnt=0; unsupcnt=0; failures=""
175 }
176 /^[ \t]*=== acats configuration ===/ {
177   insummary=0
178   if (print_prologue) print
179   next
180 }
181 /^[ \t]*=== acats tests ===/ {
182   if (print_prologue) print
183   print_prologue=0
184   next
185 }
186 /^Running chapter / {
187   if (curfile) close (curfile)
188   curfile="${TMP}/chapter-"\$3
189   print >> curfile
190   next
191 }
192 /^[ \t]*=== acats Summary ===/ {
193   if (curfile) close (curfile)
194   curfile=""
195   insummary=1
196   next
197 }
198 /^# of expected passes/         { if (insummary == 1) passcnt += \$5; next; }
199 /^# of unexpected failures/     { if (insummary == 1) failcnt += \$5; next; }
200 /^# of unsupported tests/       { if (insummary == 1) unsupcnt += \$5; next; }
201 /^\*\*\* FAILURES: / {
202   if (insummary == 1) {
203     if (failures) sub(/^\*\*\* FAILURES:/,"")
204     failures=failures""\$0
205   }
206 }
207 {
208   if (print_prologue) { print; next }
209   if (curfile) print >> curfile
210 }
211 END {
212   system ("cat ${TMP}/chapter-*")
213   print "               === acats Summary ==="
214   print "# of expected passes           " passcnt
215   print "# of unexpected failures       " failcnt
216   if (unsupcnt) print "# of unsupported tests           " unsupcnt
217   if (failures) print failures
218 }
219 EOF
220
221   rm -f ${TMP}/chapter-*
222   $AWK -f $ACATS_AWK $SUM_FILES
223   exit 0
224 fi
225
226 # If no variants were specified, find all variants in the remaining
227 # summary files.  Otherwise, ignore specified variants that aren't in
228 # any of those summary files.
229
230 if test -z "$VARIANTS" ; then
231   VAR_AWK=${TMP}/variants.awk
232   cat <<EOF > $VAR_AWK
233 /^Schedule of variations:/      { in_vars=1; next }
234 /^$/                            { in_vars=0 }
235 /^Running target/               { exit }
236 { if (in_vars==1) print \$1; else next }
237 EOF
238
239   touch ${TMP}/varlist
240   for FILE in $SUM_FILES; do
241     $AWK -f $VAR_AWK $FILE >> ${TMP}/varlist
242   done
243   VARIANTS="`sort -u ${TMP}/varlist`"
244 else
245   VARS="$VARIANTS"
246   VARIANTS=""
247   for VAR in $VARS
248   do
249     $GREP "Running target $VAR" $SUM_FILES > /dev/null && VARIANTS="$VARIANTS $VAR"
250   done
251 fi
252
253 # Find out if we have more than one variant, or any at all.
254
255 VARIANT_COUNT=0
256 for VAR in $VARIANTS
257 do
258   VARIANT_COUNT=`expr $VARIANT_COUNT + 1`
259 done
260
261 if test $VARIANT_COUNT -eq 0 ; then
262   msg "${PROGNAME}: no file for $TOOL has results for the specified variants"
263   exit 1
264 fi
265
266 cat $SUM_FILES \
267   | $AWK '/^Running/ { if ($2 != "target" && $3 == "...") print "EXPFILE: "$2 } ' \
268   | sort -u > ${TMP}/expfiles
269
270 # Write the begining of the combined summary file.
271
272 head -n 2 $FIRST_SUM
273 echo
274 echo "          === $TOOL tests ==="
275 echo
276 echo "Schedule of variations:"
277 for VAR in $VARIANTS
278 do
279   echo "    $VAR"
280 done
281 echo
282
283 # For each test variant for the tool, copy test reports from each of the
284 # summary files.  Set up two awk scripts from within the loop to
285 # initialize VAR and TOOL with the script, rather than assuming that the
286 # available version of awk can pass variables from the command line.
287
288 for VAR in $VARIANTS
289 do
290   GUTS_AWK=${TMP}/guts.awk
291   cat << EOF > $GUTS_AWK
292 BEGIN {
293   variant="$VAR"
294   firstvar=1
295   expfileno=1
296   cnt=0
297   print_using=0
298   need_close=0
299 }
300 /^EXPFILE: / {
301   expfiles[expfileno] = \$2
302   expfilesr[\$2] = expfileno
303   expfileno = expfileno + 1
304 }
305 /^Running target / {
306   curvar = \$3
307   if (variant == curvar && firstvar == 1) { print; print_using=1; firstvar = 0 }
308   next
309 }
310 /^Using / {
311   if (variant == curvar && print_using) { print; next }
312 }
313 /^Running .*\\.exp \\.\\.\\./ {
314   print_using=0
315   if (variant == curvar) {
316     if (need_close) close(curfile)
317     curfile="${TMP}/list"expfilesr[\$2]
318     expfileseen[\$2]=expfileseen[\$2] + 1
319     need_close=0
320     testname="00"
321     next
322   }
323 }
324 /^\t\t=== .* ===$/ { curvar = ""; next }
325 /^(PASS|XPASS|FAIL|XFAIL|UNRESOLVED|WARNING|ERROR|UNSUPPORTED|UNTESTED|KFAIL):/ {
326   testname=\$2
327   # Ugly hack for gfortran.dg/dg.exp
328   if ("$TOOL" == "gfortran" && testname ~ /^gfortran.dg\/g77\//)
329     testname="h"testname
330 }
331 /^$/ { if ("$MODE" == "sum") next }
332 { if (variant == curvar && curfile) {
333     if ("$MODE" == "sum") {
334       printf "%s %08d|", testname, cnt >> curfile
335       cnt = cnt + 1
336     }
337     filewritten[curfile]=1
338     need_close=1
339     print >> curfile
340   } else
341     next
342 }
343 END {
344   n=1
345   while (n < expfileno) {
346     if (expfileseen[expfiles[n]]) {
347       print "Running "expfiles[n]" ..."
348       if (filewritten["${TMP}/list"n]) {
349         if (expfileseen[expfiles[n]] == 1)
350           cmd="cat"
351         else
352           cmd="LC_ALL=C sort"
353         if ("$MODE" == "sum")
354           system (cmd" ${TMP}/list"n" | sed -n 's/^[^ ]* [^ |]*|//p'")
355         else
356           system ("cat ${TMP}/list"n)
357       }
358     }
359     n = n + 1
360   }
361 }
362 EOF
363
364   SUMS_AWK=${TMP}/sums.awk
365   rm -f $SUMS_AWK
366   cat << EOF > $SUMS_AWK
367 BEGIN {
368   variant="$VAR"
369   tool="$TOOL"
370   passcnt=0; failcnt=0; untstcnt=0; xpasscnt=0; xfailcnt=0; kpasscnt=0; kfailcnt=0; unsupcnt=0; unrescnt=0;
371   curvar=""; insummary=0
372 }
373 /^Running target /              { curvar = \$3; next }
374 /^# of /                        { if (variant == curvar) insummary = 1 }
375 /^# of expected passes/         { if (insummary == 1) passcnt += \$5; next; }
376 /^# of unexpected successes/    { if (insummary == 1) xpasscnt += \$5; next; }
377 /^# of unexpected failures/     { if (insummary == 1) failcnt += \$5; next; }
378 /^# of expected failures/       { if (insummary == 1) xfailcnt += \$5; next; }
379 /^# of unknown successes/       { if (insummary == 1) kpasscnt += \$5; next; }
380 /^# of known failures/          { if (insummary == 1) kfailcnt += \$5; next; }
381 /^# of untested testcases/      { if (insummary == 1) untstcnt += \$5; next; }
382 /^# of unresolved testcases/    { if (insummary == 1) unrescnt += \$5; next; }
383 /^# of unsupported tests/       { if (insummary == 1) unsupcnt += \$5; next; }
384 /^$/                            { if (insummary == 1)
385                                     { insummary = 0; curvar = "" }
386                                   next
387                                 }
388 { next }
389 END {
390   printf ("\t\t=== %s Summary for %s ===\n\n", tool, variant)
391   if (passcnt != 0) printf ("# of expected passes\t\t%d\n", passcnt)
392   if (failcnt != 0) printf ("# of unexpected failures\t%d\n", failcnt)
393   if (xpasscnt != 0) printf ("# of unexpected successes\t%d\n", xpasscnt)
394   if (xfailcnt != 0) printf ("# of expected failures\t\t%d\n", xfailcnt)
395   if (kpasscnt != 0) printf ("# of unknown successes\t\t%d\n", kpasscnt)
396   if (kfailcnt != 0) printf ("# of known failures\t\t%d\n", kfailcnt)
397   if (untstcnt != 0) printf ("# of untested testcases\t\t%d\n", untstcnt)
398   if (unrescnt != 0) printf ("# of unresolved testcases\t%d\n", unrescnt)
399   if (unsupcnt != 0) printf ("# of unsupported tests\t\t%d\n", unsupcnt)
400 }
401 EOF
402
403   PVAR=`echo $VAR | sed 's,/,.,g'`
404   TMPFILE=${TMP}/var-$PVAR
405   rm -f $TMPFILE
406   rm -f ${TMP}/list*
407   cat ${TMP}/expfiles $SUM_FILES | $AWK -f $GUTS_AWK
408   cat $SUM_FILES | $AWK -f $SUMS_AWK > $TMPFILE
409   # If there are multiple variants, output the counts for this one;
410   # otherwise there will just be the final counts at the end.
411   test $VARIANT_COUNT -eq 1 || cat $TMPFILE
412 done
413
414 # Set up an awk script to get the combined summary counts for the tool.
415
416 TOTAL_AWK=${TMP}/total.awk
417 cat << EOF > $TOTAL_AWK
418 BEGIN {
419   tool="$TOOL"
420   passcnt=0; failcnt=0; untstcnt=0; xpasscnt=0; xfailcnt=0; kfailcnt=0; unsupcnt=0; unrescnt=0
421 }
422 /^# of expected passes/         { passcnt += \$5 }
423 /^# of unexpected failures/     { failcnt += \$5 }
424 /^# of unexpected successes/    { xpasscnt += \$5 }
425 /^# of expected failures/       { xfailcnt += \$5 }
426 /^# of unknown successes/       { kpasscnt += \$5 }
427 /^# of known failures/          { kfailcnt += \$5 }
428 /^# of untested testcases/      { untstcnt += \$5 }
429 /^# of unresolved testcases/    { unrescnt += \$5 }
430 /^# of unsupported tests/       { unsupcnt += \$5 }
431 END {
432   printf ("\n\t\t=== %s Summary ===\n\n", tool)
433   if (passcnt != 0) printf ("# of expected passes\t\t%d\n", passcnt)
434   if (failcnt != 0) printf ("# of unexpected failures\t%d\n", failcnt)
435   if (xpasscnt != 0) printf ("# of unexpected successes\t%d\n", xpasscnt)
436   if (xfailcnt != 0) printf ("# of expected failures\t\t%d\n", xfailcnt)
437   if (kpasscnt != 0) printf ("# of unknown successes\t\t%d\n", kpasscnt)
438   if (kfailcnt != 0) printf ("# of known failures\t\t%d\n", kfailcnt)
439   if (untstcnt != 0) printf ("# of untested testcases\t\t%d\n", untstcnt)
440   if (unrescnt != 0) printf ("# of unresolved testcases\t%d\n", unrescnt)
441   if (unsupcnt != 0) printf ("# of unsupported tests\t\t%d\n", unsupcnt)
442 }
443 EOF
444
445 # Find the total summaries for the tool and add to the end of the output.
446 cat ${TMP}/var-* | $AWK -f $TOTAL_AWK
447
448 # This is ugly, but if there's version output from the compiler under test
449 # at the end of the file, we want it.  The other thing that might be there
450 # is the final summary counts.
451 tail -2 $FIRST_SUM | $GREP '^#' > /dev/null || tail -2 $FIRST_SUM
452
453 exit 0