OSDN Git Service

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