OSDN Git Service

Pass $(MATH_LIBS) $(NET_LIBS) when linking tests.
[pf3gnuchains/gcc-fork.git] / libgo / testsuite / gotest
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.
5
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.
10
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 unset LANG
14 export LC_ALL=C
15 export LC_CTYPE=C
16
17 GC=${GC:-gccgo}
18 GL=${GL:-${GC-gccgo}}
19 GOLIBS=${GOLIBS:-}
20 export GC GL GOLIBS
21
22 NM=${NM:-nm}
23
24 # srcdir is where the source files are found.  basedir is where the
25 # source file paths are relative to.
26 # gofiles are the test files.  pkgfiles are the source files.
27 srcdir=.
28 basedir=.
29 gofiles=""
30 pkgfiles=""
31 loop=true
32 keep=false
33 prefix=
34 dejagnu=no
35 timeout=240
36 testname=""
37 trace=false
38 while $loop; do
39         case "x$1" in
40         x--srcdir)
41                 srcdir=$2
42                 shift
43                 shift
44                 ;;
45         x--srcdir=*)
46                 srcdir=`echo $1 | sed -e 's/^--srcdir=//'`
47                 shift
48                 ;;
49         x--basedir)
50                 basedir=$2
51                 shift
52                 shift
53                 ;;
54         x--basedir=*)
55                 basedir=`echo $1 | sed -e 's/^--basedir=//'`
56                 shift
57                 ;;
58         x--prefix)
59                 prefix=$2
60                 shift
61                 shift
62                 ;;
63         x--prefix=*)
64                 prefix=`echo $1 | sed -e 's/^--prefix=//'`
65                 shift
66                 ;;
67         x--keep)
68                 keep=true
69                 shift
70                 ;;
71         x--pkgfiles)
72                 pkgfiles=$2
73                 shift
74                 shift
75                 ;;
76         x--pkgfiles=*)
77                 pkgfiles=`echo $1 | sed -e 's/^--pkgfiles=//'`
78                 shift
79                 ;;
80         x--dejagnu)
81                 dejagnu=$2
82                 shift
83                 shift
84                 ;;
85         x--dejagnu=*)
86                 dejagnu=`echo $1 | sed -e 's/^--dejagnu=//'`
87                 shift
88                 ;;
89         x--timeout)
90                 timeout=$2
91                 shift
92                 shift
93                 ;;
94         x--timeout=*)
95                 timeout=`echo $1 | sed -e 's/^--timeout=//'`
96                 shift
97                 ;;
98         x--testname)
99                 testname=$2
100                 shift
101                 shift
102                 ;;
103         x--testname=*)
104                 testname=`echo $1 | sed -e 's/^--testname=//'`
105                 shift
106                 ;;
107         x--trace)
108                 trace=true
109                 shift
110                 ;;
111         x-*)
112                 loop=false
113                 ;;
114         x)
115                 loop=false
116                 ;;
117         *)
118                 gofiles="$gofiles $1"
119                 shift
120                 ;;
121         esac
122 done
123
124 DIR=gotest$$
125 rm -rf $DIR
126 mkdir $DIR
127
128 cd $DIR
129
130 if test $keep = false; then
131   trap "cd ..; rm -rf $DIR" 0 1 2 3 14 15
132 else
133   trap "cd ..; echo Keeping $DIR" 0 1 2 3 14 15
134 fi
135
136 case "$srcdir" in
137         /*)
138                 ;;
139         *)
140                 srcdir="../$srcdir"
141                 ;;
142 esac
143
144 SRCDIR=$srcdir
145 export SRCDIR
146
147 case "$basedir" in
148         /*)
149                 ;;
150         *)
151                 basedir="../$basedir"
152                 ;;
153 esac
154
155 # Link all the files/directories in srcdir into our working directory,
156 # so that the tests do not have to refer to srcdir to find test data.
157 ln -s $srcdir/* .
158
159 # Some tests refer to a ../testdata directory.
160 if test -e $srcdir/../testdata; then
161   rm -f ../testdata
162   abssrcdir=`cd $srcdir && pwd`
163   ln -s $abssrcdir/../testdata ../testdata
164 fi
165
166 # Copy the .go files because io/utils_test.go expects a regular file.
167 case "x$gofiles" in
168 x)
169         case "x$pkgfiles" in
170         x)
171                 for f in `cd $srcdir; ls *.go`; do
172                     rm -f $f;
173                     cp $srcdir/$f .
174                 done
175                 ;;
176         *)
177                 for f in $pkgfiles; do
178                     if test -f $basedir/$f; then
179                         b=`basename $f`
180                         rm -f $b
181                         cp $basedir/$f $b
182                     elif test -f ../$f; then
183                         b=`basename $f`
184                         rm -f $b
185                         cp ../$f $b
186                     else
187                         echo "file $f not found" 1>&2
188                         exit 1
189                     fi
190                 done
191                 for f in `cd $srcdir; ls *_test.go`; do
192                     rm -f $f
193                     cp $srcdir/$f .
194                 done
195                 ;;
196         esac
197         ;;
198 *)
199         for f in $gofiles; do
200             b=`basename $f`
201             rm -f $b
202             cp $basedir/$f $b
203         done
204         case "x$pkgfiles" in
205         x)
206                 for f in `cd $srcdir; ls *.go | grep -v *_test.go`; do
207                     rm -f $f
208                     cp $srcdir/$f .
209                 done
210                 ;;
211         *)
212                 for f in $pkgfiles; do
213                     if test -f $basedir/$f; then
214                         b=`basename $f`
215                         rm -f $b
216                         cp $basedir/$f $b
217                     elif test -f ../$f; then
218                         b=`basename $f`
219                         rm -f $b
220                         cp ../$f $b
221                     else
222                         echo "file $f not found" 1>&2
223                         exit 1
224                     fi
225                 done
226                 ;;
227         esac
228         ;;
229 esac
230
231 # Some tests expect the _obj directory created by the gc Makefiles.
232 mkdir _obj
233
234 # Some tests expect the _test directory created by the gc Makefiles.
235 mkdir _test
236
237 case "x$gofiles" in
238 x)
239         gofiles=`ls *_test.go 2>/dev/null`
240 esac
241
242 case "x$gofiles" in
243 x)
244         echo 'no test files found' 1>&2
245         exit 1
246 esac
247
248 # Run any commands given in sources, like
249 #   // gotest: $GC foo.go
250 # to build any test-only dependencies.
251 holdGC="$GC"
252 GC="$GC -g -c -I ."
253 sed -n 's/^\/\/ gotest: //p' $gofiles | sh
254 GC="$holdGC"
255
256 case "x$pkgfiles" in
257 x)
258         pkgbasefiles=`ls *.go | grep -v _test.go 2>/dev/null`
259         ;;
260 *)
261         for f in $pkgfiles; do
262             pkgbasefiles="$pkgbasefiles `basename $f`"
263         done
264         ;;
265 esac
266
267 case "x$pkgfiles" in
268 x)
269         echo 'no source files found' 1>&2
270         exit 1
271         ;;
272 esac
273
274 # Split $gofiles into external gofiles (those in *_test packages)
275 # and internal ones (those in the main package).
276 xgofiles=$(echo $(grep '^package[       ]' $gofiles /dev/null | grep ':.*_test' | sed 's/:.*//'))
277 gofiles=$(echo $(grep '^package[        ]' $gofiles /dev/null | grep -v ':.*_test' | sed 's/:.*//'))
278
279 # External $O file
280 xofile=""
281 havex=false
282 if [ "x$xgofiles" != "x" ]; then
283         xofile="_xtest_.o"
284         havex=true
285 fi
286
287 set -e
288
289 package=`echo ${srcdir} | sed -e 's|^.*libgo/go/||'`
290
291 prefixarg=
292 if test -n "$prefix"; then
293         prefixarg="-fgo-prefix=$prefix"
294 fi
295
296 if test "$trace" = "true"; then
297   echo $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
298 fi
299 $GC -g $prefixarg -c -I . -fno-toplevel-reorder -o _gotest_.o $gofiles $pkgbasefiles
300
301 if $havex; then
302         mkdir -p `dirname $package`
303         cp _gotest_.o `dirname $package`/lib`basename $package`.a
304         if test "$trace" = "true"; then
305             echo $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
306         fi
307         $GC -g -c -I . -fno-toplevel-reorder -o $xofile $xgofiles
308 fi
309
310 # They all compile; now generate the code to call them.
311
312 localname() {
313         # The package main has been renamed to __main__ when imported.
314         # Adjust its uses.
315         echo $1 | sed 's/^main\./__main__./'
316 }
317
318 {
319         # test functions are named TestFoo
320         # the grep -v eliminates methods and other special names
321         # that have multiple dots.
322         pattern='Test([^a-z].*)?'
323         # The -p option tells GNU nm not to sort.
324         # The -v option tells Solaris nm to sort by value.
325         tests=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
326         if [ "x$tests" = x ]; then
327                 echo 'gotest: warning: no tests matching '$pattern in _gotest_.o $xofile 1>&2
328                 exit 2
329         fi
330         # benchmarks are named BenchmarkFoo.
331         pattern='Benchmark([^a-z].*)?'
332         benchmarks=$($NM -p -v _gotest_.o $xofile | egrep ' T .*\.'$pattern'$' | grep -v '\..*\..*\.' | sed 's/.* //' | sed 's/.*\.\(.*\.\)/\1/')
333
334         # package spec
335         echo 'package main'
336         echo
337         # imports
338         if echo "$tests" | egrep -v '_test\.' >/dev/null; then
339                 echo 'import "./_gotest_"'
340         fi
341         if $havex; then
342                 echo 'import "./_xtest_"'
343         fi
344         echo 'import "testing"'
345         echo 'import __os__     "os"' # rename in case tested package is called os
346         echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp
347         # test array
348         echo
349         echo 'var tests = []testing.InternalTest {'
350         for i in $tests
351         do
352                 j=$(localname $i)
353                 echo '  {"'$i'", '$j'},'
354         done
355         echo '}'
356         # benchmark array
357         # The comment makes the multiline declaration
358         # gofmt-safe even when there are no benchmarks.
359         echo 'var benchmarks = []testing.InternalBenchmark{ //'
360         for i in $benchmarks
361         do
362                 j=$(localname $i)
363                 echo '  {"'$i'", '$j'},'
364         done
365         echo '}'
366         # body
367         echo \
368 '
369 var matchPat string
370 var matchRe *__regexp__.Regexp
371
372 func matchString(pat, str string) (result bool, err __os__.Error) {
373         if matchRe == nil || matchPat != pat {
374                 matchPat = pat
375                 matchRe, err = __regexp__.Compile(matchPat)
376                 if err != nil {
377                         return
378                 }
379         }
380         return matchRe.MatchString(str), nil
381 }
382
383 func main() {
384         testing.Main(matchString, tests, benchmarks)
385 }'
386 }>_testmain.go
387
388 case "x$dejagnu" in
389 xno)
390         if test "$trace" = "true"; then
391             echo ${GC} -g -c _testmain.go
392         fi
393         ${GC} -g -c _testmain.go
394
395         if test "$trace" = "true"; then
396             echo ${GL} *.o ${GOLIBS}
397         fi
398         ${GL} *.o ${GOLIBS}
399
400         if test "$trace" = "true"; then
401             echo ./a.out -test.short -test.timeout=$timeout "$@"
402         fi
403         ./a.out -test.short -test.timeout=$timeout "$@" &
404         pid=$!
405         (sleep `expr $timeout + 10`
406             echo > gotest-timeout
407             echo "timed out in gotest" 1>&2
408             kill -9 $pid) &
409         alarmpid=$!
410         wait $pid
411         status=$?
412         if ! test -f gotest-timeout; then
413             kill $alarmpid
414         fi
415         exit $status
416         ;;
417 xyes)
418         rm -rf ../testsuite/*.o
419         files=`echo *`
420         for f in $files; do
421                 if test "$f" = "_obj" || test "$f" = "_test"; then
422                         continue
423                 fi
424                 rm -rf ../testsuite/$f
425                 if test -f $f; then
426                         cp $f ../testsuite/
427                 else
428                         ln -s ../$DIR/$f ../testsuite/
429                 fi
430         done
431         cd ../testsuite
432         rm -rf _obj _test
433         mkdir _obj _test
434         if test "$testname" != ""; then
435             GOTESTNAME="$testname"
436             export GOTESTNAME
437         fi
438         $MAKE check RUNTESTFLAGS="$RUNTESTFLAGS GOTEST_TMPDIR=$DIR"
439         # Useful when using make check-target-libgo
440         cat libgo.log >> libgo-all.log
441         cat libgo.sum >> libgo-all.sum
442         rm -rf $files
443         ;;
444 esac