OSDN Git Service

* lib/libjava.exp (libjava_arguments): Set --tag on libtool
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / lib / libjava.exp
1 # Copyright (C) 1998, 1999, 2000 Free Software Foundation
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 a particular executable.  FIXME: this relies on DejaGnu
25 # internals.  These should probably be exposed in a better way.
26 proc libjava_find_program {prog} {
27     global tool_root_dir
28
29     set file [lookfor_file $tool_root_dir $prog]
30     if { $file == "" } {
31         set file [lookfor_file $tool_root_dir gcc/$prog];
32     }
33     if {$file == ""} {
34         set file $prog
35     }
36     return $file
37 }
38
39 # Find `jv-scan'.
40 proc find_jvscan {} {
41     return [libjava_find_program jv-scan]
42 }
43
44 # Find `gcjh'.
45 proc find_gcjh {} {
46     return [libjava_find_program gcjh]
47 }
48
49 proc bytecompile_file { file objdir {classpath {}} } {
50     global env
51     global SUN_JAVAC
52     set dirname [file dirname $file]
53
54     # If JDK doesn't run on your platform but some other
55     # JDK-compatible javac does, you may set SUN_JAVAC to point to it.
56     # One of the most important properties of a SUN_JAVAC is that it
57     # must create class-files even for classes that have not been
58     # specified in the command line, but that were needed to compile
59     # those that have.  For example, Pizza won't do it, but you can
60     # use `kaffe sun.tools.javac.Main', if you have Sun's classes.zip
61     # in the kaffe's default search path.
62     if ![info exists SUN_JAVAC] {
63         if [info exists env(SUN_JAVAC)] {
64             set SUN_JAVAC $env(SUN_JAVAC)
65         } else {
66             set SUN_JAVAC "javac"
67         }
68     }
69
70     catch {unset env(CLASSPATH)}
71     if {$classpath != ""} then {
72         set env(CLASSPATH) $classpath
73     }
74     if {[catch {
75         set here [pwd]
76         cd $dirname
77         set q [eval exec "$SUN_JAVAC [list $file] -d $objdir 2>@ stdout"]
78         cd $here
79         # "return" a result
80         set q $q
81     } msg]} then {
82         verbose "couldn't compile $file: $msg"
83         set r 0
84     } else {
85         set r 1
86     }
87     return $r
88 }
89
90 set libjava_initialized 0
91
92 #
93 # Build the status wrapper library as needed.
94 #
95 proc libjava_init { args } {
96     global wrapper_file;
97     global wrap_compile_flags;
98     global libjava_initialized
99     global GCJ_UNDER_TEST
100     global TOOL_EXECUTABLE
101     global original_ld_library_path
102     global env
103
104     if { $libjava_initialized == 1 } { return; }
105
106     if ![info exists GCJ_UNDER_TEST] {
107         if [info exists TOOL_EXECUTABLE] {
108             set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
109         } else {
110             if [info exists env(GCJ)] {
111                 set GCJ_UNDER_TEST $env(GCJ)
112             } else {
113                 set GCJ_UNDER_TEST "[find_gcj]"
114             }
115         }
116     }
117
118     if [info exists env(LD_LIBRARY_PATH)] {
119         set original_ld_library_path $env(LD_LIBRARY_PATH)
120     } else {
121         if [info exists env(SHLIB_PATH)] {
122             set original_ld_library_path $env(SHLIB_PATH)
123         } else {
124             set original_ld_library_path ""
125         }
126     }
127
128     set wrapper_file "";
129     set wrap_compile_flags "";
130     if [target_info exists needs_status_wrapper] {
131         set result [build_wrapper "testglue.o"];
132         if { $result != "" } {
133             set wrapper_file [lindex $result 0];
134             set wrap_compile_flags [lindex $result 1];
135         } else {
136             warning "Status wrapper failed to build."
137         }
138     }
139
140     set libjava_initialized 1
141 }
142
143 # Find a library.  We know where libtool puts the actual libraries,
144 # and we look there.  The implementation is fairly hacky.  We can't
145 # compile with -nodefaultlibs, because that will also eliminate the
146 # system libraries we need.  In order to have gcj still work, it must
147 # find the appropriate libraries so we must add -L options for their
148 # paths.  However we can't simply use those libraries; we still need
149 # libtool for linking.
150 proc libjava_find_lib {dir name} {
151     global base_dir
152     set gp [get_multilibs]
153     foreach sub {.libs _libs} {
154         if {$gp != ""} {
155             if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
156                 return "$gp/$dir/lib${name}.la -L$gp/$dir/$sub"
157             }
158         }
159         set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
160                    "$base_dir/../../$dir/lib${name}.la -L$base_dir/../../$dir/$sub" \
161                    ""]
162         if {$lib != ""} {
163             return $lib
164         }
165     }
166     return ""
167 }
168
169 # Compute arguments needed for compiler.  MODE is a libtool mode:
170 # either compile or link.
171 proc libjava_arguments {{mode compile}} {
172     global base_dir
173     global LIBJAVA
174     global LIBGC
175     global LIBQTHREADS
176     global LIBZ
177     global srcdir subdir objdir
178     global TOOL_OPTIONS
179     global GCJ_UNDER_TEST
180     global tmpdir
181     global runtests
182     global env
183
184     if [info exists LIBJAVA] {
185         set libjava $LIBJAVA;
186     } else {
187         set libjava [libjava_find_lib libjava gcj]
188     }
189
190     if [info exists LIBGC] {
191         set libgc $LIBGC;
192     } else {
193         set libgc [libjava_find_lib boehm-gc gcjgc]
194     }
195
196     if [info exists LIBQTHREADS] {
197         set libqthreads $LIBQTHREADS
198     } else {
199         set libqthreads [libjava_find_lib qthreads gcjcoop]
200     }
201
202     if [info exists LIBZ] {
203         set libz $LIBZ
204     } else {
205         set libz [libjava_find_lib zlib zgcj]
206     }
207
208     # FIXME: there's no way to determine whether -lpthread is
209     # required.  We should get this info from configure, or it should
210     # just be in the compiler driver.
211
212     verbose "using LIBJAVA = $libjava" 2
213     verbose "using LIBGC = $libgc" 2
214     verbose "using LIBQTHREADS = $libqthreads" 2
215     verbose "using LIBZ = $libz" 2
216     set args ""
217
218     # Basically we want to build up a colon separated path list from
219     # the value of $libjava.
220
221     # First strip away any -L arguments.
222     regsub -all -- "-L" $libjava "" ld_library_path
223
224     # Then remove any -lgcj argument.
225     regsub -all -- " -lgcj.*" $ld_library_path "" ld_library_path
226
227     # First strip away any -L arguments.
228     regsub -all -- "-L" $libgc $ld_library_path ld_library_path
229
230     # Then remove any -lgcjgc argument.
231     regsub -all -- " -lgcjgc.*" $ld_library_path "" ld_library_path
232
233     # That's enough to make things work for the normal case.
234     # If we wanted to handle an arbitrary value of libjava,
235     # then we'd have to do a lot more work.
236
237     # Set variables the dynamic linker looks at.
238     global original_ld_library_path
239     setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
240     setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
241
242     # Set the CLASSPATH environment variable
243     verbose "CLASSPATH is .:$srcdir/$subdir:$objdir/..:$objdir/../libgcj.zip"
244     global env
245     set env(CLASSPATH) ".:$srcdir/$subdir:$objdir/..:$objdir/../libgcj.zip"
246
247     global wrapper_file wrap_compile_flags;
248     lappend args "additional_flags=$wrap_compile_flags";
249     lappend args "libs=$wrapper_file";
250     lappend args "libs=$libjava";
251     lappend args "libs=$libgc";
252     lappend args "libs=$libqthreads"
253     lappend args "libs=$libz"
254     lappend args debug
255
256     if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
257         lappend args "libs=${gluefile}"
258         lappend args "ldflags=$wrap_flags"
259     }
260
261     if [info exists TOOL_OPTIONS] {
262         lappend args "additional_flags=$TOOL_OPTIONS"
263     }
264
265     # Search for libtool.  We need it to link.
266     set found_compiler 0
267     set d [absolute $objdir]
268     foreach x {. .. ../.. ../../..} {
269         if {[file exists $d/$x/libtool]} then {
270             # We have to run silently to avoid DejaGNU lossage.
271             lappend args \
272               "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
273             set found_compiler 1
274             break
275         }
276     }
277     if {! $found_compiler} {
278         # Append -B$objdir/../ so that we find libgcj.spec before it
279         # is installed.
280         lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
281     }
282
283     return $args
284 }
285
286
287 #
288 # Run the test specified by srcfile and resultfile. compile_args and
289 # exec_args are options telling this proc how to work.
290 #
291 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
292     global base_dir
293     global LIBJAVA
294     global LIBGC
295     global srcdir subdir objdir
296     global TOOL_OPTIONS
297     global GCJ_UNDER_TEST
298     global tmpdir
299     global runtests
300
301     # Make opts into an array.
302     set opts(_) x
303     unset opts(_)
304     foreach item $exec_args {
305         set opts($item) x
306     }
307
308     set errname [file rootname [file tail $srcfile]]
309     if {! [runtest_file_p $runtests $errname]} {
310         return
311     }
312
313     if {[info exists opts(no-link)]} {
314         set mode compile
315     } else {
316         set mode link
317     }
318     set args [libjava_arguments $mode]
319     if {! [info exists opts(no-link)]} {
320         # Add the --main flag
321         lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
322         if { $compile_args != "" } {
323             lappend args "additional_flags=$compile_args"
324         }
325     }
326
327     regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
328     set executable "${objdir}/$out"
329     if {[info exists opts(no-link)]} {
330         append executable ".o"
331         set target object
332     } else {
333         set target executable
334     }
335     if { $compile_args != "" } {
336         set errname "$errname $compile_args"
337     }
338
339     set x [prune_warnings \
340              [target_compile $srcfile "$executable" $target $args]]
341     if {[info exists opts(xfail-gcj)]} {
342         setup_xfail *-*-*
343     }
344     if { $x != "" } {
345         verbose "target_compile failed: $x" 2
346
347         if {[info exists opts(shouldfail)]} {
348             pass "$errname compilation from source"
349             return
350         }
351
352         fail "$errname compilation from source"
353         if {[info exists opts(xfail-gcj)] || ! [info exists opts(no-exec)]} {
354             setup_xfail "*-*-*"
355             fail "$errname execution from source compiled test"
356             setup_xfail "*-*-*"
357             fail "$errname output from source compiled test"
358         }
359         return
360     }
361     if {[info exists opts(shouldfail)]} {
362         fail "$errname compilation from source"
363         return
364     }
365     pass "$errname compilation from source"
366
367     if {[info exists opts(no-exec)]
368         || [info exists opts(no-link)]} {
369         return
370     }
371
372     set result [libjava_load $executable "" "$inpfile"];
373     set status [lindex $result 0];
374     set output [lindex $result 1];
375     if {[info exists opts(xfail-exec)]} then {
376         setup_xfail *-*-*
377     }
378     $status "$errname execution from source compiled test"
379     if { $status != "pass" } {
380         setup_xfail "*-*-*"
381         fail "$errname execution from source compiled test"
382         return;
383     }
384
385     verbose "resultfile is $resultfile"
386     set id [open $resultfile r];
387     set expected ""
388     append expected [read $id];
389     regsub -all "\r" "$output" "" output;
390     regsub "\n*$" $expected "" expected
391     regsub "\n*$" $output "" output
392     regsub "^\n*" $expected "" expected
393     regsub "^\n*" $output "" output
394     regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
395     regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
396     regsub -all "\[ \t\]\[ \t\]*" $output " " output
397     regsub -all "\[ \t\]*\n\n*" $output "\n" output
398     verbose "expected is $expected"
399     verbose "actual is $output"
400     set passed 0;
401     if {$options == "regexp_match"} {
402         if [regexp $expected $output] {
403             set passed 1;
404         }
405     } else {
406         if { $expected == $output } {
407             set passed 1;
408         }
409     }
410     if {[info exists opts(xfail-output)]} {
411         setup_xfail *-*-*
412     }
413     if { $passed == 1 } {
414         pass "$errname output from source compiled test"
415     } else {
416         clone_output "expected was $expected"
417         clone_output "output was $output"
418         fail "$errname output from source compiled test"
419     }
420     close $id;
421 }
422
423 #
424 # Run the test specified by srcfile and resultfile. compile_args and
425 # exec_args are options telling this proc how to work.
426 #
427 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
428     global base_dir
429     global LIBJAVA
430     global LIBGC
431     global srcdir subdir objdir
432     global TOOL_OPTIONS
433     global GCJ_UNDER_TEST
434     global tmpdir
435     global runtests
436
437     # Make opts into an array.
438     set opts(_) x
439     unset opts(_)
440     foreach item $exec_args {
441         set opts($item) x
442     }
443
444     set errname [file rootname [file tail $srcfile]]
445     if {! [runtest_file_p $runtests $errname]} {
446         return
447     }
448
449     # bytecompile files with Sun's compiler for now.
450     set bc_ok [bytecompile_file $srcfile $objdir]
451     # FIXME: assumes we are using javac to compile to bytecode.
452     # This is not always the case.
453     if {[info exists opts(xfail-javac)]} {
454         setup_xfail *-*-*
455     }
456     if {! $bc_ok} then {
457         if {[info exists opts(shouldfail)]} {
458             pass "$errname byte compilation"
459             return
460         }
461         fail "$errname byte compilation"
462         setup_xfail "*-*-*"
463         fail "$errname compilation from bytecode"
464         if {! [info exists opts(no-exec)]} {
465             setup_xfail "*-*-*"
466             fail "$errname execution from bytecode->native test"
467             setup_xfail "*-*-*"
468             fail "$errname output from bytecode->native test"
469         }
470         return
471     }
472     if {[info exists opts(shouldfail)]} {
473         fail "$errname byte compilation"
474         return
475     }
476     pass "$errname byte compilation"
477
478     # Find name to use for --main, and name of all class files.
479     set jvscan [find_jvscan]
480     verbose "jvscan is $jvscan"
481     set main_name [string trim \
482                      [prune_warnings \
483                         [target_compile $srcfile "" none \
484                            "compiler=$jvscan additional_flags=--print-main"]]]
485     verbose "main name is $main_name"
486     set class_out [string trim \
487                      [prune_warnings \
488                         [target_compile $srcfile "" none \
489                            "compiler=$jvscan additional_flags=--list-class"]]]
490     verbose "class list is $class_out"
491
492     if {[string match "*parse error*" $main_name]
493         || [string match "*parse error*" $class_out]} {
494         # Do the remaining fails.
495         setup_xfail "*-*-*"
496         fail "$errname compilation from bytecode"
497         if {! [info exists opts(no-exec)]} {
498             setup_xfail "*-*-*"
499             fail "$errname execution from bytecode->native test"
500             setup_xfail "*-*-*"
501             fail "$errname output from bytecode->native test"
502         }
503         return
504     }
505
506     # Turn "a b" into "a.class b.class".
507     # Also, turn "foo.bar" into "foo/bar.class".
508     set class_files {}
509     foreach file [split [string trim $class_out]] {
510         set file [join [split $file .] /]
511         lappend class_files $objdir/$file.class
512     }
513
514     # Usually it is an error for a test program not to have a `main'
515     # method.  However, for no-exec tests it is ok.  Treat no-link
516     # like no-exec here.
517     if {[info exists opts(no-link)]} {
518         set opts(no-exec) x
519     }
520     set largs {}
521     if {$main_name == ""} {
522         if {! [info exists opts(no-exec)]} {
523             perror "No `main' given in program $errname"
524             return
525         } else {
526             set type object
527             set mode compile
528         }
529     } else {
530         set type executable
531         lappend largs "additional_flags=--main=$main_name"
532         set executable "${objdir}/$main_name"
533         set mode link
534     }
535
536     # Initial arguments.
537     set args [libjava_arguments $mode]
538     eval lappend args $largs
539
540     if { $compile_args != "" } {
541         lappend args "additional_flags=$compile_args"
542     }
543
544     if { $compile_args != "" } {
545         set errname "$errname $compile_args"
546     }
547
548     verbose "compilation command = $args" 2
549     # When compiling and not linking, we have to build each .o
550     # separately.  We do this because DejaGNU's target_compile won't
551     # accept an empty "destfile" argument when the mode is "compile".
552     if {$mode == "compile"} {
553         foreach c_file $class_files {
554             set executable [file rootname [file tail $c_file]].o
555             set x [prune_warnings \
556                      [target_compile $c_file "$executable" $type $args]]
557             if {$x != ""} {
558                 break
559             }
560         }
561     } else {
562         set x [prune_warnings \
563                  [target_compile $class_files "$executable" $type $args]]
564     }
565     if {[info exists opts(xfail-byte)]} {
566         setup_xfail *-*-*
567     }
568     if { $x != "" } {
569         verbose "target_compile failed: $x" 2
570         fail "$errname compilation from bytecode"
571         setup_xfail "*-*-*"
572         if {! [info exists opts(no-exec)]} {
573             fail "$errname execution from bytecode->native test"
574             setup_xfail "*-*-*"
575             fail "$errname output from bytecode->native test"
576         }
577         return;
578     }
579     pass "$errname compilation from bytecode"
580
581     if {[info exists opts(no-exec)]} {
582         return
583     }
584
585     set result [libjava_load $executable "" "$inpfile"];
586     set status [lindex $result 0];
587     set output [lindex $result 1];
588     if {[info exists opts(xfail-exec)]} {
589         setup_xfail *-*-*
590     }
591     $status "$errname execution from bytecode->native test"
592     if { $status != "pass" } {
593         setup_xfail "*-*-*"
594         fail "$errname output from bytecode->native test"
595         return;
596     }
597
598     verbose "resultfile is $resultfile"
599     set id [open $resultfile r];
600     set expected ""
601     append expected [read $id];
602     regsub -all "\r" "$output" "" output;
603     regsub "\n*$" $expected "" expected
604     regsub "\n*$" $output "" output
605     regsub "^\n*" $expected "" expected
606     regsub "^\n*" $output "" output
607     regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
608     regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
609     regsub -all "\[ \t\]\[ \t\]*" $output " " output
610     regsub -all "\[ \t\]*\n\n*" $output "\n" output
611     verbose "expected is $expected"
612     verbose "actual is $output"
613     set passed 0;
614     if {[info exists opts(xfail-output)]} {
615         setup_xfail *-*-*
616     }
617     if {$options == "regexp_match"} {
618         if [regexp $expected $output] {
619             set passed 1;
620         }
621     } else {
622         if { $expected == $output } {
623             set passed 1;
624         }
625     }
626     if { $passed == 1 } {
627         pass "$errname output from bytecode->native test"
628     } else {
629         clone_output "expected was $expected"
630         clone_output "output was $output"
631         fail "$errname output from bytecode->native test"
632     }
633     close $id;
634 }
635
636 #
637 # Run the test specified by srcfile and resultfile. compile_args and
638 # exec_args are options telling this proc how to work.
639 #   `no-link'     don't try to link the program
640 #   `no-exec'     don't try to run the test
641 #   `xfail-gcj'   compilation from source will fail
642 #   `xfail-javac' compilation with javac will fail
643 #   `xfail-gcjC'  compilation with gcj -C will fail
644 #   `shouldfail'  compilation from source is supposed to fail
645 #                 This is different from xfail, which marks a known
646 #                 failure that we just havne't fixed.
647 #                 A compilation marked this way should fail with any
648 #                 front end.
649 #   `xfail-byte'  compilation from bytecode will fail
650 #   `xfail-exec'  exec will fail
651 #   `xfail-output' output will be wrong
652 #
653 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
654     test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
655     test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
656  }
657
658 #
659 # libjava_version -- extract and print the version number of libjavap
660 #
661 proc default_libjava_version {} {
662 }
663
664 proc default_libjava_start { } {
665 }
666
667 # Local Variables:
668 # tcl-indent-level:4
669 # End: