OSDN Git Service

fd5ceb481ed47635dca6c7b0207ae65eed67fc95
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / lib / libjava.exp
1 # Copyright (C) 1998, 1999 Cygnus Solutions
2
3 load_lib "libgloss.exp"
4
5 # GCJ_UNDER_TEST is the compiler under test.
6
7 global tmpdir
8
9 if ![info exists tmpdir] {
10     set tmpdir "/tmp"
11 }
12
13 # Read an `xfail' file if it exists.  Returns a list of xfail tokens.
14 proc libjava_read_xfail {file} {
15     if {! [file exists $file]} {
16         return ""
17     }
18     set fd [open $file r]
19     set tokens [string trim [read $fd]]
20     close $fd
21     return $tokens
22 }
23
24 # Find `jv-scan'.  FIXME: this relies on DejaGnu internals.  These
25 # should probably be exposed in a better way.
26 proc find_jvscan {} {
27     global tool_root_dir
28
29     set file [lookfor_file $tool_root_dir jv-scan]
30     if { $file == "" } {
31         set file [lookfor_file $tool_root_dir gcc/jv-scan];
32     }
33     if {$file == ""} {
34         set file jv-scan
35     }
36     return $file
37 }
38
39 proc bytecompile_file { file objdir {classpath {}} } {
40     global env
41     set dirname  [file dirname $file];
42
43     catch {unset env(CLASSPATH)}
44     if {$classpath != ""} then {
45         set env(CLASSPATH) $classpath
46     }
47     if {[catch {system "cd $dirname; javac $file -d $objdir"} msg]} then {
48         verbose "couldn't compile $file: $msg"
49         set r 0
50     } else {
51         set r 1
52     }
53     return $r
54 }
55
56 set libjava_initialized 0
57
58 #
59 # Build the status wrapper library as needed.
60 #
61 proc libjava_init { args } {
62     global wrapper_file;
63     global wrap_compile_flags;
64     global libjava_initialized
65     global GCJ_UNDER_TEST
66     global TOOL_EXECUTABLE
67
68     if { $libjava_initialized == 1 } { return; }
69
70     if ![info exists GCJ_UNDER_TEST] {
71         if [info exists TOOL_EXECUTABLE] {
72             set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
73         } else {
74             set GCJ_UNDER_TEST "[find_gcj]"
75         }
76     }
77
78     set wrapper_file "";
79     set wrap_compile_flags "";
80     if [target_info exists needs_status_wrapper] {
81         set result [build_wrapper "testglue.o"];
82         if { $result != "" } {
83             set wrapper_file [lindex $result 0];
84             set wrap_compile_flags [lindex $result 1];
85         } else {
86             warning "Status wrapper failed to build."
87         }
88     }
89
90     set libjava_initialized 1
91 }
92
93 # Find a library.  We know where libtool puts the actual libraries,
94 # and we look there.  The implementation is fairly hacky.  We can't
95 # compile with -nodefaultlibs, because that will also eliminate the
96 # system libraries we need.  In order to have gcj still work, it must
97 # find the appropriate libraries so we must add -L options for their
98 # paths.  However we can't simply use those libraries; we still need
99 # libtool for linking.
100 proc libjava_find_lib {dir name} {
101     global base_dir
102     set gp [get_multilibs]
103     foreach sub {.libs _libs} {
104         if {$gp != ""} {
105             if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
106                 return "$gp/$dir/lib${name}.la -L$gp/$dir/$sub"
107             }
108         }
109         set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
110                    "$base_dir/../../$dir/lib${name}.la -L$base_dir/../../$dir/$sub" \
111                    ""]
112         if {$lib != ""} {
113             return $lib
114         }
115     }
116     return ""
117 }
118
119 # Compute arguments needed for compiler.  MODE is a libtool mode:
120 # either compile or link.
121 proc libjava_arguments {{mode compile}} {
122     global base_dir
123     global LIBJAVA
124     global LIBGC
125     global LIBQTHREADS
126     global LIBZ
127     global srcdir subdir objdir
128     global TOOL_OPTIONS
129     global GCJ_UNDER_TEST
130     global tmpdir
131     global runtests
132
133     if [info exists LIBJAVA] {
134         set libjava $LIBJAVA;
135     } else {
136         set libjava [libjava_find_lib libjava gcj]
137     }
138
139     if [info exists LIBGC] {
140         set libgc $LIBGC;
141     } else {
142         set libgc [libjava_find_lib boehm-gc gcjgc]
143     }
144
145     if [info exists LIBQTHREADS] {
146         set libqthreads $LIBQTHREADS
147     } else {
148         set libqthreads [libjava_find_lib qthreads gcjcoop]
149     }
150
151     if [info exists LIBZ] {
152         set libz $LIBZ
153     } else {
154         set libz [libjava_find_lib zlib zgcj]
155     }
156
157     # FIXME: there's no way to determine whether -lpthread is
158     # required.  We should get this info from configure, or it should
159     # just be in the compiler driver.
160
161     verbose "using LIBJAVA = $libjava" 2
162     verbose "using LIBGC = $libgc" 2
163     verbose "using LIBQTHREADS = $libqthreads" 2
164     verbose "using LIBZ = $libz" 2
165     set args ""
166
167     # Basically we want to build up a colon separated path list from
168     # the value of $libjava.
169
170     # First strip away any -L arguments.
171     regsub -all -- "-L" $libjava "" ld_library_path
172
173     # Then remove any -lgcj argument.
174     regsub -all -- " -lgcj.*" $ld_library_path "" ld_library_path
175
176     # First strip away any -L arguments.
177     regsub -all -- "-L" $libgc $ld_library_path ld_library_path
178
179     # Then remove any -lgcjgc argument.
180     regsub -all -- " -lgcjgc.*" $ld_library_path "" ld_library_path
181
182     # That's enough to make things work for the normal case.
183     # If we wanted to handle an arbitrary value of libjava,
184     # then we'd have to do a lot more work.
185
186     # Set variables the dynamic linker looks at.
187     setenv LD_LIBRARY_PATH $ld_library_path
188     setenv SHLIB_PATH $ld_library_path
189
190     # Set the CLASSPATH environment variable
191     verbose "CLASSPATH is $objdir/.."
192     global env
193     set env(CLASSPATH) "$objdir/.."
194
195     global wrapper_file wrap_compile_flags;
196     lappend args "additional_flags=$wrap_compile_flags";
197     lappend args "libs=$wrapper_file";
198     lappend args "libs=$libjava";
199     lappend args "libs=$libgc";
200     lappend args "libs=$libqthreads"
201     lappend args "libs=$libz"
202     lappend args debug
203
204     if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
205         lappend args "libs=${gluefile}"
206         lappend args "ldflags=$wrap_flags"
207     }
208
209     if [info exists TOOL_OPTIONS] {
210         lappend args "additional_flags=$TOOL_OPTIONS"
211     }
212
213     # Search for libtool.  We need it to link.
214     set found_compiler 0
215     set d [absolute $objdir]
216     foreach x {. .. ../.. ../../..} {
217         if {[file exists $d/$x/libtool]} then {
218             # We have to run silently to avoid DejaGNU lossage.
219             lappend args \
220               "compiler=$d/$x/libtool --silent --mode=$mode $GCJ_UNDER_TEST"
221             set found_compiler 1
222             break
223         }
224     }
225     if {! $found_compiler} {
226         lappend args "compiler=$GCJ_UNDER_TEST"
227     }
228
229     return $args
230 }
231
232
233 #
234 # Run the test specified by srcfile and resultfile. compile_args and
235 # exec_args are options telling this proc how to work.
236 #   `no-exec'     don't try to run the test
237 #   `xfail-gcj'   compilation from source will fail
238 #   `xfail-javac' compilation with javac will fail
239 #   `xfail-gcjC'  compilation with gcj -C will fail
240 #   `xfail-byte'  compilation from bytecode will fail
241 #   `xfail-exec'  exec will fail
242 #   `xfail-output' output will be wrong
243 #   
244 #
245 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
246     global base_dir
247     global LIBJAVA
248     global LIBGC
249     global srcdir subdir objdir
250     global TOOL_OPTIONS
251     global GCJ_UNDER_TEST
252     global tmpdir
253     global runtests
254
255     # Make opts into an array.
256     set opts(_) x
257     unset opts(_)
258     foreach item $exec_args {
259         set opts($item) x
260     }
261
262     set errname [file rootname [file tail $srcfile]]
263     if {! [runtest_file_p $runtests $errname]} {
264         return
265     }
266
267     set args [libjava_arguments link]
268     # Add the --main flag
269     lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
270     if { $compile_args != "" } {
271         lappend args "additional_flags=$compile_args"
272     }
273
274     regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
275     set executable "${objdir}/$out"
276     if { $compile_args != "" } {
277         set errname "$errname $compile_args"
278     }
279
280     set x [target_compile $srcfile "$executable" executable $args]
281     if {[info exists opts(xfail-gcj)]} {
282         setup_xfail *-*-*
283     }
284     if { $x != "" } {
285         verbose "target_compile failed: $x" 2
286         fail "$errname compilation from source"
287         if {[info exists opts(xfail-gcj)] || ! [info exists opts(no-exec)]} {
288             setup_xfail "*-*-*"
289             fail "$errname execution from source compiled test"
290             setup_xfail "*-*-*"
291             fail "$errname output from source compiled test"
292         }
293         return;
294     }
295     pass "$errname compilation from source"
296
297     if {[info exists opts(no-exec)]} {
298         return
299     }
300
301     set result [libjava_load $executable "" "$inpfile"];
302     set status [lindex $result 0];
303     set output [lindex $result 1];
304     if {[info exists opts(xfail-exec)]} then {
305         setup_xfail *-*-*
306     }
307     $status "$errname execution from source compiled test"
308     if { $status != "pass" } {
309         setup_xfail "*-*-*"
310         fail "$errname execution from source compiled test"
311         return;
312     }
313
314     verbose "resultfile is $resultfile"
315     set id [open $resultfile r];
316     set expected ""
317     append expected [read $id];
318     regsub -all "\r" "$output" "" output;
319     regsub "\n*$" $expected "" expected
320     regsub "\n*$" $output "" output
321     regsub "^\n*" $expected "" expected
322     regsub "^\n*" $output "" output
323     regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
324     regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
325     regsub -all "\[ \t\]\[ \t\]*" $output " " output
326     regsub -all "\[ \t\]*\n\n*" $output "\n" output
327     verbose "expected is $expected"
328     verbose "actual is $output"
329     set passed 0;
330     if {$options == "regexp_match"} {
331         if [regexp $expected $output] {
332             set passed 1;
333         }
334     } else {
335         if { $expected == $output } {
336             set passed 1;
337         }
338     }
339     if {[info exists opts(xfail-output)]} {
340         setup_xfail *-*-*
341     }
342     if { $passed == 1 } {
343         pass "$errname output from source compiled test"
344     } else {
345         clone_output "expected was $expected"
346         clone_output "output was $output"
347         fail "$errname output from source compiled test"
348     }
349     close $id;
350 }
351
352 #
353 # Run the test specified by srcfile and resultfile. compile_args and
354 # exec_args are options telling this proc how to work.
355 #   `no-exec' don't try to run the test
356 #   `xfail-gcj'   compilation from source will fail
357 #   `xfail-javac' compilation with javac will fail
358 #   `xfail-gcjC'  compilation with gcj -C will fail
359 #   `xfail-byte'  compilation from bytecode will fail
360 #   `xfail-exec'  exec will fail
361 #   `xfail-output' output will be wrong
362 #
363 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
364     global base_dir
365     global LIBJAVA
366     global LIBGC
367     global srcdir subdir objdir
368     global TOOL_OPTIONS
369     global GCJ_UNDER_TEST
370     global tmpdir
371     global runtests
372
373     # Make opts into an array.
374     set opts(_) x
375     unset opts(_)
376     foreach item $exec_args {
377         set opts($item) x
378     }
379
380     set errname [file rootname [file tail $srcfile]]
381     if {! [runtest_file_p $runtests $errname]} {
382         return
383     }
384
385     # bytecompile files with Sun's compiler for now.
386     set bc_ok [bytecompile_file $srcfile $objdir]
387     # FIXME: assumes we are using javac to compile to bytecode.
388     # This is not always the case.
389     if {[info exists opts(xfail-javac)]} {
390         setup_xfail *-*-*
391     }
392     if {! $bc_ok} then {
393         fail "$errname byte compilation"
394         setup_xfail "*-*-*"
395         fail "$errname compilation from bytecode"
396         if {! [info exists opts(no-exec)]} {
397             setup_xfail "*-*-*"
398             fail "$errname execution from bytecode->native test"
399             setup_xfail "*-*-*"
400             fail "$errname output from bytecode->native test"
401         }
402         return
403     }
404     pass "$errname byte compilation"
405
406     # Find name to use for --main, and name of all class files.
407     set jvscan [find_jvscan]
408     verbose "jvscan is $jvscan"
409     set main_name [string trim \
410                      [target_compile $srcfile "" none \
411                         "compiler=$jvscan additional_flags=--print-main"]]
412     verbose "main name is $main_name"
413     set class_out [string trim \
414                      [target_compile $srcfile "" none \
415                         "compiler=$jvscan additional_flags=--list-class"]]
416     verbose "class list is $class_out"
417     # FIXME: As of Wed Feb 24 1999, `jv-scan --list-class' prints
418     # nothing if the file contains an interface and not a class.  I
419     # believe this is a jv-scan bug.
420     if {$class_out == ""} then {
421         set class_files \
422           [list $objdir/[file rootname [file tail $srcfile]].class]
423     } else {
424         # Turn "a b" into "a.class b.class".
425         set class_files {}
426         foreach file [split [string trim $class_out]] {
427             lappend class_files $objdir/$file.class
428         }
429     }
430
431     # Usually it is an error for a test program not to have a `main'
432     # method.  However, for no-exec tests it is ok.
433     set largs {}
434     if {$main_name == ""} {
435         if {! [info exists opts(no-exec)]} {
436             perror "No `main' given in program $errname"
437             return
438         } else {
439             set type object
440             set executable [file rootname [file tail $srcfile]].o
441             set mode compile
442         }
443     } else {
444         set type executable
445         lappend largs "additional_flags=--main=$main_name"
446         set executable "${objdir}/$main_name"
447         set mode link
448     }
449
450     # Initial arguments.
451     set args [libjava_arguments $mode]
452     eval lappend args $largs
453
454     if { $compile_args != "" } {
455         lappend args "additional_flags=$compile_args"
456     }
457
458     if { $compile_args != "" } {
459         set errname "$errname $compile_args"
460     }
461
462     verbose "compilation command = $args" 2
463     set x [target_compile $class_files "$executable" $type $args]
464     if {[info exists opts(xfail-byte)]} {
465         setup_xfail *-*-*
466     }
467     if { $x != "" } {
468         verbose "target_compile failed: $x" 2
469         fail "$errname compilation from bytecode"
470         setup_xfail "*-*-*"
471         if {! [info exists opts(no-exec)]} {
472             fail "$errname execution from bytecode->native test"
473             setup_xfail "*-*-*"
474             fail "$errname output from bytecode->native test"
475         }
476         return;
477     }
478     pass "$errname compilation from bytecode"
479
480     if {[info exists opts(no-exec)]} {
481         return
482     }
483
484     set result [libjava_load $executable "" "$inpfile"];
485     set status [lindex $result 0];
486     set output [lindex $result 1];
487     if {[info exists opts(xfail-exec)]} {
488         setup_xfail *-*-*
489     }
490     $status "$errname execution from bytecode->native test"
491     if { $status != "pass" } {
492         setup_xfail "*-*-*"
493         fail "$errname output from bytecode->native test"
494         return;
495     }
496
497     verbose "resultfile is $resultfile"
498     set id [open $resultfile r];
499     set expected ""
500     append expected [read $id];
501     regsub -all "\r" "$output" "" output;
502     regsub "\n*$" $expected "" expected
503     regsub "\n*$" $output "" output
504     regsub "^\n*" $expected "" expected
505     regsub "^\n*" $output "" output
506     regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
507     regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
508     regsub -all "\[ \t\]\[ \t\]*" $output " " output
509     regsub -all "\[ \t\]*\n\n*" $output "\n" output
510     verbose "expected is $expected"
511     verbose "actual is $output"
512     set passed 0;
513     if {[info exists opts(xfail-output)]} {
514         setup_xfail *-*-*
515     }
516     if {$options == "regexp_match"} {
517         if [regexp $expected $output] {
518             set passed 1;
519         }
520     } else {
521         if { $expected == $output } {
522             set passed 1;
523         }
524     }
525     if { $passed == 1 } {
526         pass "$errname output from bytecode->native test"
527     } else {
528         clone_output "expected was $expected"
529         clone_output "output was $output"
530         fail "$errname output from bytecode->native test"
531     }
532     close $id;
533 }
534
535 #
536 # Run the test specified by srcfile and resultfile. compile_args and
537 # exec_args are options telling this proc how to work.
538 #   `no-exec' don't try to run the test
539 #   `xfail-gcj'   compilation from source will fail
540 #   `xfail-javac' compilation with javac will fail
541 #   `xfail-gcjC'  compilation with gcj -C will fail
542 #   `xfail-byte'  compilation from bytecode will fail
543 #   `xfail-exec'  exec will fail
544 #   `xfail-output' output will be wrong
545 #
546 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
547     test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
548     test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
549  }
550
551 #
552 # libjava_version -- extract and print the version number of libjavap
553 #
554 proc default_libjava_version {} {
555 }
556
557 proc default_libjava_start { } {
558 }
559
560 # Local Variables:
561 # tcl-indent-level:4
562 # End: