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