OSDN Git Service

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