OSDN Git Service

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