gotest: only use [TD] on big-endian PPC64 non-AIX systems
[official-gcc.git] / libgo / testsuite / gotest
blob434c26de933eb773035fe847d0c6c9ac8617b892
1 #!/bin/sh
2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # Using all the *_test.go files in the current directory, write out a file
7 # _testmain.go that runs all its tests. Compile everything and run the
8 # tests.
9 # If files are named on the command line, use them instead of *_test.go.
11 # Makes egrep,grep work better in general if we put them
12 # in ordinary C mode instead of what the current language is.
13 LANG=C
14 LC_ALL=C
15 LC_CTYPE=C
16 export LANG LC_ALL LC_CTYPE
18 GC=${GC:-gccgo}
19 GL=${GL:-${GC-gccgo}}
20 GOLIBS=${GOLIBS:-}
21 export GC GL GOLIBS
23 NM=${NM:-nm}
25 # srcdir is where the source files are found. basedir is where the
26 # source file paths are relative to.
27 # gofiles are the test files. pkgfiles are the source files.
28 srcdir=.
29 basedir=.
30 goarch=""
31 gofiles=""
32 goos=""
33 pkgfiles=""
34 loop=true
35 keep=false
36 pkgpath=
37 prefix=
38 dejagnu=no
39 timeout=240
40 testname=""
41 bench=""
42 trace=false
43 while $loop; do
44 case "x$1" in
45 x--srcdir)
46 srcdir=$2
47 shift
48 shift
50 x--srcdir=*)
51 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
52 shift
54 x--basedir)
55 basedir=$2
56 shift
57 shift
59 x--basedir=*)
60 basedir=`echo $1 | sed -e 's/^--basedir=//'`
61 shift
63 x--goarch)
64 goarch=$2
65 shift
66 shift
68 x--goarch=*)
69 goarch=`echo $1 | sed -e 's/^--goarch=//'`
70 shift
72 x--goos)
73 goos=$2
74 shift
75 shift
77 x--goos=*)
78 goos=`echo $1 | sed -e 's/^--goos=//'`
79 shift
81 x--pkgpath)
82 pkgpath=$2
83 shift
84 shift
86 x--pkgpath=*)
87 pkgpath=`echo $1 | sed -e 's/^--pkgpath=//'`
88 shift
90 x--prefix)
91 prefix=$2
92 shift
93 shift
95 x--prefix=*)
96 prefix=`echo $1 | sed -e 's/^--prefix=//'`
97 shift
99 x--keep)
100 keep=true
101 shift
103 x--pkgfiles)
104 pkgfiles=$2
105 shift
106 shift
108 x--pkgfiles=*)
109 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
110 shift
112 x--dejagnu)
113 dejagnu=$2
114 shift
115 shift
117 x--dejagnu=*)
118 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
119 shift
121 x--timeout)
122 timeout=$2
123 shift
124 shift
126 x--timeout=*)
127 timeout=`echo $1 | sed -e 's/^--timeout=//'`
128 shift
130 x--testname)
131 testname=$2
132 shift
133 shift
135 x--testname=*)
136 testname=`echo $1 | sed -e 's/^--testname=//'`
137 shift
139 x--bench)
140 bench=$2
141 shift
142 shift
144 x--bench=*)
145 bench=`echo $1 | sed -e 's/^--bench=//'`
146 shift
148 x--trace)
149 trace=true
150 shift
152 x-*)
153 loop=false
156 loop=false
159 gofiles="$gofiles $1"
160 shift
162 esac
163 done
165 DIR=gotest$$
166 rm -rf $DIR
167 mkdir $DIR
169 cd $DIR
170 mkdir test
171 cd test
173 if test $keep = false; then
174 trap "cd ../..; rm -rf $DIR" 0 1 2 3 14 15
175 else
176 trap "cd ../..; echo Keeping $DIR" 0 1 2 3 14 15
179 case "$srcdir" in
183 srcdir="../../$srcdir"
185 esac
187 SRCDIR=$srcdir
188 export SRCDIR
190 case "$basedir" in
194 basedir="../../$basedir"
196 esac
198 # Link all the files/directories in srcdir into our working directory,
199 # so that the tests do not have to refer to srcdir to find test data.
200 ln -s $srcdir/* .
202 # Some tests refer to a ../testdata directory.
203 if test -e $srcdir/../testdata; then
204 rm -f ../testdata
205 abssrcdir=`cd $srcdir && pwd`
206 ln -s $abssrcdir/../testdata ../testdata
209 # Copy the .go files because io/utils_test.go expects a regular file.
210 case "x$gofiles" in
212 case "x$pkgfiles" in
214 for f in `cd $srcdir; ls *.go`; do
215 rm -f $f;
216 cp $srcdir/$f .
217 done
220 for f in $pkgfiles; do
221 case $f in
223 b=`basename $f`
224 rm -f $b
225 cp $f $b
228 if test -f $basedir/$f; then
229 b=`basename $f`
230 rm -f $b
231 cp $basedir/$f $b
232 elif test -f ../../$f; then
233 b=`basename $f`
234 rm -f $b
235 cp ../../$f $b
236 else
237 echo "file $f not found" 1>&2
238 exit 1
241 esac
242 done
243 for f in `cd $srcdir; ls *_test.go`; do
244 rm -f $f
245 cp $srcdir/$f .
246 done
248 esac
251 for f in $gofiles; do
252 b=`basename $f`
253 rm -f $b
254 cp $basedir/$f $b
255 done
256 case "x$pkgfiles" in
258 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
259 rm -f $f
260 cp $srcdir/$f .
261 done
264 for f in $pkgfiles; do
265 case $f in
267 b=`basename $f`
268 rm -f $b
269 cp $f $b
272 if test -f $basedir/$f; then
273 b=`basename $f`
274 rm -f $b
275 cp $basedir/$f $b
276 elif test -f ../../$f; then
277 b=`basename $f`
278 rm -f $b
279 cp ../../$f $b
280 else
281 echo "file $f not found" 1>&2
282 exit 1
285 esac
286 done
288 esac
290 esac
292 # Some tests expect the _obj directory created by the gc Makefiles.
293 mkdir _obj
295 # Some tests expect the _test directory created by the gc Makefiles.
296 mkdir _test
298 case "x$gofiles" in
300 for f in `ls *_test.go`; do
301 tag1=`echo $f | sed -e 's/^.*_\([^_]*\)_test.go$/\1/'`
302 tag2=`echo $f | sed -e 's/^.*_\([^_]*\)_[^_]*_test.go$/\1/'`
303 if test x$tag1 = x$f; then
304 tag1=
306 if test x$tag2 = x$f; then
307 tag2=
310 case "$tag1" in
311 "") ;;
312 $goarch) ;;
313 $goos) ;;
314 aix | android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
315 tag1=nonmatchingtag
317 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | ppc | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64)
318 tag1=nonmatchingtag
320 esac
322 case "$tag2" in
323 "") ;;
324 $goarch) ;;
325 $goos) ;;
326 aix | android | darwin | dragonfly | freebsd | linux | nacl | netbsd | openbsd | plan9 | solaris | windows)
327 tag2=nonmatchingtag
329 386 | amd64 | amd64p32 | arm | armbe | arm64 | arm64be | alpha | ia64 | m68k | ppc64 | ppc64le | mips | mipsle | mips64 | mips64le | mips64p32 | mips64p32le | ppc | riscv64 | s390 | s390x | sh | shbe | sparc | sparc64)
330 tag2=nonmatchingtag
332 esac
334 if test x$tag1 != xnonmatchingtag -a x$tag2 != xnonmatchingtag; then
335 tags=`sed '/^package /q' < $f | grep '^// +build '`
336 omatch=true
337 first=true
338 match=false
339 for tag in $tags; do
340 case $tag in
341 "//")
343 "+build")
344 if test "$first" = "true"; then
345 first=false
346 elif test "$match" = "false"; then
347 omatch=false
349 match=false
351 $goos | $goarch | cgo | gccgo | go1.[0-9])
352 match=true
354 "!"$goos | "!"$goarch | "!cgo" | "!gccgo" | "!"go1.[0-9])
356 *,*)
357 cmatch=true
358 for ctag in `echo $tag | sed -e 's/,/ /g'`; do
359 case $ctag in
360 $goos | $goarch | cgo | gccgo | go1.[0-9])
362 "!"$goos | "!"$goarch | "!cgo" | "!gccgo" | "!"go1.[0-9])
363 cmatch=false
365 "!"*)
368 cmatch=false
370 esac
371 done
372 if test "$cmatch" = "true"; then
373 match=true
376 "!"*)
377 match=true
379 esac
380 done
382 if test "$match" = "false" -a "$first" = "false"; then
383 omatch=false
386 if test "$omatch" = "true"; then
387 gofiles="$gofiles $f"
390 done
393 xgofiles=$gofiles
394 gofiles=
395 for f in $xgofiles; do
396 gofiles="$gofiles `basename $f`"
397 done
398 esac
400 case "x$gofiles" in
402 echo 'no test files found' 1>&2
403 exit 1
405 esac
407 # Run any commands given in sources, like
408 # // gotest: $GC foo.go
409 # to build any test-only dependencies.
410 holdGC="$GC"
411 GC="$GC -g -c -I ."
412 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
413 GC="$holdGC"
415 case "x$pkgfiles" in
417 pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
420 for f in $pkgfiles; do
421 pkgbasefiles="$pkgbasefiles `basename $f`"
422 done
424 esac
426 case "x$pkgfiles" in
428 echo 'no source files found' 1>&2
429 exit 1
431 esac
433 # Split $gofiles into external gofiles (those in *_test packages)
434 # and internal ones (those in the main package).
435 xgofiles=
436 for f in $gofiles; do
437 package=`grep '^package[ ]' $f | sed 1q`
438 case "$package" in
439 *_test)
440 xgofiles="$xgofiles $f"
443 ngofiles="$ngofiles $f"
445 esac
446 done
447 gofiles=$ngofiles
449 # External $O file
450 xofile=""
451 havex=false
452 if [ "x$xgofiles" != "x" ]; then
453 xofile="_xtest_.o"
454 havex=true
457 testmain=
458 if $havex && fgrep 'func TestMain(' $xgofiles >/dev/null 2>&1; then
459 package=`grep '^package[ ]' $xgofiles | sed 1q | sed -e 's/.* //'`
460 testmain="${package}.TestMain"
461 elif test -n "$gofiles" && fgrep 'func TestMain(' $gofiles >/dev/null 2>&1; then
462 package=`grep '^package[ ]' $gofiles | sed 1q | sed -e 's/.* //'`
463 testmain="${package}.TestMain"
466 set -e
468 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
470 pkgpatharg=
471 xpkgpatharg=
472 prefixarg=
473 if test -n "$pkgpath"; then
474 pkgpatharg="-fgo-pkgpath=$pkgpath"
475 xpkgpatharg="-fgo-pkgpath=${pkgpath}_test"
476 elif test -n "$prefix"; then
477 prefixarg="-fgo-prefix=$prefix"
480 if test "$trace" = "true"; then
481 echo $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
483 $GC -g $pkgpatharg $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
485 if $havex; then
486 mkdir -p `dirname $package`
487 cp _gotest_.o `dirname $package`/lib`basename $package`.a
488 if test "$trace" = "true"; then
489 echo $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
491 $GC -g $xpkgpatharg -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
494 # They all compile; now generate the code to call them.
496 testname() {
497 # Remove the package from the name used with the -test option.
498 echo $1 | sed 's/^.*\.//'
501 localname() {
502 # The package main has been renamed to __main__ when imported.
503 # Adjust its uses.
504 echo $1 | sed 's/^main\./__main__./'
508 text="T"
510 # On systems using PPC64 ELF ABI v1 function symbols show up
511 # as descriptors in the data section. We assume that $goarch
512 # distinguishes v1 (ppc64) from v2 (ppc64le).
513 if test "$goos" != "aix" && test "$goarch" = "ppc64"; then
514 text="[TD]"
517 symtogo='sed -e s/_test\([^A-Za-z0-9]\)/XXXtest\1/ -e s/.*_\([^_]*\.\)/\1/ -e s/XXXtest/_test/'
519 # test functions are named TestFoo
520 # the grep -v eliminates methods and other special names
521 # that have multiple dots.
522 pattern='Test([^a-z].*)?'
523 # The -p option tells GNU nm not to sort.
524 # The -v option tells Solaris nm to sort by value.
525 tests=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '[^ ]\..*\.' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
526 if [ "x$tests" = x ]; then
527 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
528 exit 2
530 # benchmarks are named BenchmarkFoo.
531 pattern='Benchmark([^a-z].*)?'
532 benchmarks=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '[^ ]\..*\.' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
534 # examples are named ExampleFoo
535 pattern='Example([^a-z].*)?'
536 examples=$($NM -p -v _gotest_.o $xofile | egrep " $text .*\."$pattern'$' | grep -v '[^ ]\..*\.' | fgrep -v ' __go_' | sed 's/.* //' | $symtogo)
538 # package spec
539 echo 'package main'
540 echo
541 # imports
542 if echo "$tests" | egrep -v '_test\.' >/dev/null; then
543 echo 'import "./_gotest_"'
545 if $havex; then
546 echo 'import "./_xtest_"'
548 echo 'import "testing"'
549 echo 'import "testing/internal/testdeps"'
550 if ! test -n "$testmain"; then
551 echo 'import __os__ "os"'
553 # test array
554 echo
555 echo 'var tests = []testing.InternalTest {'
556 for i in $tests
558 n=$(testname $i)
559 if test "$n" != "TestMain"; then
560 j=$(localname $i)
561 echo ' {"'$n'", '$j'},'
563 done
564 echo '}'
566 # benchmark array
567 # The comment makes the multiline declaration
568 # gofmt-safe even when there are no benchmarks.
569 echo 'var benchmarks = []testing.InternalBenchmark{ //'
570 for i in $benchmarks
572 n=$(testname $i)
573 j=$(localname $i)
574 echo ' {"'$n'", '$j'},'
575 done
576 echo '}'
578 # examples array
579 echo 'var examples = []testing.InternalExample{ //'
580 # This doesn't work because we don't pick up the output.
581 #for i in $examples
583 # n=$(testname $i)
584 # j=$(localname $i)
585 # echo ' {"'$n'", '$j', ""},'
586 #done
587 echo '}'
589 # body
590 echo \
592 func main() {
593 m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
595 if test -n "$testmain"; then
596 echo " ${testmain}(m)"
597 else
598 echo ' __os__.Exit(m.Run())'
601 echo '}'
602 }>_testmain.go
604 case "x$dejagnu" in
605 xno)
606 if test "$trace" = "true"; then
607 echo ${GC} -g -c _testmain.go
609 ${GC} -g -c _testmain.go
611 if test "$trace" = "true"; then
612 echo ${GL} *.o ${GOLIBS}
614 ${GL} *.o ${GOLIBS}
616 set +e
617 if test "$bench" = ""; then
618 if test "$trace" = "true"; then
619 echo ./a.out -test.short -test.timeout=${timeout}s "$@"
621 ./a.out -test.short -test.timeout=${timeout}s "$@" &
622 pid=$!
623 (sleep `expr $timeout + 10`
624 echo > gotest-timeout
625 echo "timed out in gotest" 1>&2
626 kill -9 $pid) &
627 alarmpid=$!
628 wait $pid
629 status=$?
630 if ! test -f gotest-timeout; then
631 sleeppid=`ps -o pid,ppid,comm | grep " $alarmpid " | grep sleep | sed -e 's/ *\([0-9]*\) .*$/\1/'`
632 kill $alarmpid
633 wait $alarmpid
634 if test "$sleeppid" != ""; then
635 kill $sleeppid
638 else
639 if test "$trace" = "true"; then
640 echo ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
642 ./a.out -test.run=^\$ -test.bench="${bench}" "$@"
643 status=$?
645 exit $status
647 xyes)
648 rm -rf ../../testsuite/*.o
649 files=`echo *`
650 for f in $files; do
651 if test "$f" = "_obj" || test "$f" = "_test"; then
652 continue
654 rm -rf ../../testsuite/$f
655 if test -f $f; then
656 cp $f ../../testsuite/
657 else
658 ln -s ../$DIR/test/$f ../../testsuite/
660 done
661 cd ../../testsuite
662 rm -rf _obj _test
663 mkdir _obj _test
664 if test "$testname" != ""; then
665 GOTESTNAME="$testname"
666 export GOTESTNAME
668 $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR/test"
669 # Useful when using make check-target-libgo
670 cat libgo.log >> libgo-all.log
671 cat libgo.sum >> libgo-all.sum
672 rm -rf $files
674 esac