OSDN Git Service

2006-07-04 Andreas Tobler <a.tobler@schweiz.ch>
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / lib / libjava.exp
1 # Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006 Free Software Foundation
2
3 proc load_gcc_lib { filename } {
4     global srcdir
5     load_file $srcdir/../../gcc/testsuite/lib/$filename
6 }
7
8 load_lib libgloss.exp
9 load_gcc_lib target-libpath.exp
10
11 # GCJ_UNDER_TEST is the compiler under test.
12
13 global tmpdir
14
15 if ![info exists tmpdir] {
16     set tmpdir "/tmp"
17 }
18
19 # This is like `prune_warnings', but it also prunes away a warning
20 # from the bytecode front end that we don't care about.
21 proc libjava_prune_warnings {text} {
22     set text [prune_warnings $text]
23     set tlist [split $text \n]
24     set len [llength $tlist]
25     for {set i [expr {$len - 1}]} {$i >= 2} {incr i -1} {
26         if {[string match "*unreachable bytecode*" [lindex $tlist $i]]} {
27             # Delete this line, all other unreachable warnings and the previous
28             # two lines containing the method and class.
29             set j [expr {$i - 1}]
30             while {[string match "*unreachable bytecode*" [lindex $tlist $j]]} {
31                 incr j -1
32             }
33             incr j -1
34             set tlist [lreplace $tlist $j $i]
35             set i $j
36         }
37     }
38     return [join $tlist \n]
39 }
40
41 # This is like `target_compile' but it does some surgery to work
42 # around stupid DejaGNU bugs.  In particular DejaGNU has very poor
43 # quoting, so for instance a `$' will be re-evaluated at spawn time.
44 # We don't want that.
45 proc libjava_tcompile {source destfile type options} {
46     # This strange-looking expression really does quote the `$'.
47     regsub -all -- {\$} $source {\$} source
48     regsub -all -- {\$} $destfile {\$} destfile
49     return [target_compile $source $destfile $type $options]
50 }
51
52 # Read an `xfail' file if it exists.  Returns a list of xfail tokens.
53 proc libjava_read_xfail {file} {
54     if {! [file exists $file]} {
55         return ""
56     }
57     set fd [open $file r]
58     set tokens [string trim [read $fd]]
59     close $fd
60     return $tokens
61 }
62
63 # Find a particular executable.  FIXME: this relies on DejaGnu
64 # internals.  These should probably be exposed in a better way.
65 proc libjava_find_program {prog} {
66     global tool_root_dir
67
68     set file [lookfor_file $tool_root_dir $prog]
69     if { $file == "" } {
70         set file [lookfor_file $tool_root_dir gcc/$prog];
71     }
72     if {$file == ""} {
73         set file $prog
74     }
75     return $file
76 }
77
78 # Find `jv-scan'.
79 proc find_jvscan {} {
80     return [libjava_find_program jv-scan]
81 }
82
83 # Find `gcjh'.
84 proc find_gcjh {} {
85     return [libjava_find_program gcjh]
86 }
87
88 proc find_javac {} {
89     global SUN_JAVAC GCJ_UNDER_TEST env libgcj_jar
90     # If JDK doesn't run on your platform but some other
91     # JDK-compatible javac does, you may set SUN_JAVAC to point to it.
92     # One of the most important properties of a SUN_JAVAC is that it
93     # must create class-files even for classes that have not been
94     # specified in the command line, but that were needed to compile
95     # those that have.  For example, Pizza won't do it, but you can
96     # use `kaffe sun.tools.javac.Main', if you have Sun's classes.zip
97     # in the kaffe's default search path.
98     if {![info exists SUN_JAVAC]} {
99         if {[info exists env(SUN_JAVAC)]} {
100             set SUN_JAVAC $env(SUN_JAVAC)
101         } else {
102             set SUN_JAVAC "$GCJ_UNDER_TEST -C -I$libgcj_jar"
103         }
104     }
105     return $SUN_JAVAC
106 }
107
108 proc bytecompile_file { file objdir {classpath {}} } {
109     global env
110     set dirname [file dirname $file]
111
112     set javac [find_javac]
113     if {$classpath != ""} then {
114         set env(CLASSPATH) $classpath
115     }
116     set here [pwd]
117     cd $dirname
118     send_log "byte compile: $javac -g [list $file] -d $objdir 2>@ stdout\n"
119     if {[catch {
120         set q [eval exec "$javac -g [list $file] -d $objdir 2>@ stdout"]
121     } msg]} then {
122         send_log "couldn't compile $file: $msg\n"
123         set r 0
124     } else {
125         set r 1
126     }
127     cd $here
128     return $r
129 }
130
131 set libjava_initialized 0
132
133 #
134 # Build the status wrapper library as needed.
135 #
136 proc libjava_init { args } {
137     global wrapper_file;
138     global wrap_compile_flags;
139     global libjava_initialized libjava_uses_threads
140     global GCJ_UNDER_TEST
141     global TOOL_EXECUTABLE
142     global env objdir
143     global env libgcj_jar
144     global tool_root_dir
145     global libjava_libgcc_s_path
146     global target_triplet
147     global libjava_version
148
149     # We set LC_ALL and LANG to C so that we get the same error messages as expected.
150     setenv LC_ALL C
151     setenv LANG C
152
153     if { $libjava_initialized == 1 } { return; }
154
155     if ![info exists GCJ_UNDER_TEST] {
156         if [info exists TOOL_EXECUTABLE] {
157             set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
158         } else {
159             if [info exists env(GCJ)] {
160                 set GCJ_UNDER_TEST $env(GCJ)
161             } else {
162                 set GCJ_UNDER_TEST "[find_gcj]"
163             }
164         }
165     }
166
167     # Find the libgcj jar file.
168
169     # FIXME: This finds libgcj.spec for the default multilib.
170     # If thread models differ between multilibs, this has to be moved
171     # to libjava_arguments
172     set specdir [libjava_find_spec]
173
174     set text [eval exec "$GCJ_UNDER_TEST -B$specdir -v 2>@ stdout"]
175     regexp " version \[^\n\r\]*" $text version
176     set libjava_version [lindex $version 1]
177
178     verbose "version: $libjava_version"
179
180     set libgcj_jar [glob $objdir/../libgcj-$libjava_version.jar]
181     verbose "jar file is $libgcj_jar"
182
183     # The -B is so we find libgcj.spec.
184     regexp -- "Thread model: (\[^\n\]+)\n" $text ignore model
185     set libjava_uses_threads [expr {! ($model == "no"
186                                        || $model == "none"
187                                        || $model == "single")}]
188
189     # Always set encoding used by gcj.
190     append GCJ_UNDER_TEST " --encoding=UTF-8"
191
192     set wrapper_file "";
193     set wrap_compile_flags "";
194     if [target_info exists needs_status_wrapper] {
195         set result [build_wrapper "testglue.o"];
196         if { $result != "" } {
197             set wrapper_file [lindex $result 0];
198             set wrap_compile_flags [lindex $result 1];
199         } else {
200             warning "Status wrapper failed to build."
201         }
202     }
203
204     # Finally, add the gcc build directory so that we can find the
205     # shared libgcc.  This, like much of dejagnu, is hideous.
206     set libjava_libgcc_s_path {}
207
208     if { [istarget "*-*-darwin*"] } {
209         set so_extension "dylib"
210     } else {
211         set so_extension "so"
212     }
213     set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.${so_extension}]
214     if {$gccdir != ""} {
215         set gccdir [file dirname $gccdir]
216         lappend libjava_libgcc_s_path $gccdir
217         verbose "libjava_libgcc_s_path = $libjava_libgcc_s_path"
218         set compiler ${gccdir}/xgcc
219         if { [is_remote host] == 0 && [which $compiler] != 0 } {
220             foreach i "[exec $compiler --print-multi-lib]" {
221                 set mldir ""
222                 regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
223                 set mldir [string trimright $mldir "\;@"]
224                 if { "$mldir" == "." } {
225                     continue
226                 }
227                 if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.${so_extension}.*]] >= 1 } {
228                     lappend libjava_libgcc_s_path "${gccdir}/${mldir}"
229                 }
230             }
231         }
232     }
233
234     set libjava_initialized 1
235 }
236
237 # Find a library.  We know where libtool puts the actual libraries,
238 # and we look there.  The implementation is fairly hacky.  We can't
239 # compile with -nodefaultlibs, because that will also eliminate the
240 # system libraries we need.  In order to have gcj still work, it must
241 # find the appropriate libraries so we must add -L options for their
242 # paths.  However we can't simply use those libraries; we still need
243 # libtool for linking.
244 # Don't return the the lib${name}.la files here, since this causes the
245 # libraries to be linked twice: once as lib${name}.so/dylib and another time
246 # via gcj's implicit -l${name}.  This is both unnecessary and causes the
247 # Solaris ld to warn: attempted multiple inclusion of file.  This warning
248 # is not ignored by the dejagnu framework and cannot be disabled.
249 proc libjava_find_lib {dir name} {
250     global base_dir
251     set gp [get_multilibs]
252     foreach extension {so dll dylib sl a} {
253         foreach sub {.libs _libs} {
254             if {$gp != ""} {
255                 if {[file exists $gp/$dir/$sub/lib${name}.${extension}]} then {
256                     # Just return the `-L' option.  The library itself
257                     # will be picked up via the spec file.
258                     return "-L$gp/$dir/$sub"
259                 }
260             }
261             # Just return the `-L' option.  The library itself will be
262             # picked up via the spec file.
263             set lib [findfile \
264                        $base_dir/../../$dir/$sub/lib${name}.${extension} \
265                        "-L$base_dir/../../$dir/$sub" \
266                        ""]
267             if {$lib != ""} {
268                 return $lib
269             }
270         }
271     }
272     return ""
273 }
274
275 # Find libgcj.spec.  We need to use the file corresponding to the multilib
276 # under test since they might differ.  Append a trailing slash since this
277 # is used with -B.
278 proc libjava_find_spec {} {
279     global objdir
280     return "$objdir/../"
281 }
282
283 # Find `gij'.  Return empty string if not found.
284 proc libjava_find_gij {} {
285     global base_dir objdir
286
287     set gijdir [lookfor_file [get_multilibs] libjava];
288     # Fall back if get_multilibs fails.
289     if {$gijdir == ""} {
290       set gijdir "$objdir/.."
291     }
292     if {! [file exists $gijdir/gij]} {
293         return ""
294     }
295     return $gijdir/gij
296 }
297
298 # Remove a bunch of files.
299 proc gcj_cleanup {args} {
300     foreach file $args {
301         if {[string match *.o $file]} {
302             verbose "removing [file rootname $file].lo"
303             file delete -force [file rootname $file].lo
304         }
305         file delete -force -- $file
306         verbose "removing $file"
307     }
308     # It is simplest to do this instead of trying to figure out what
309     # bits in .libs ought to be removed.
310     catch {system "rm -rf .libs"}
311 }
312
313 # Compute arguments needed for compiler.  MODE is a libtool mode:
314 # either compile or link.
315 proc libjava_arguments {{mode compile}} {
316     global base_dir
317     global LIBJAVA
318     global srcdir subdir objdir
319     global TOOL_OPTIONS
320     global GCJ_UNDER_TEST
321     global tmpdir
322     global runtests
323     global env
324     global tool_root_dir
325     global libgcj_jar
326     global libjava_libgcc_s_path
327     global libjava_ld_library_path
328     global ld_library_path
329     global target_triplet
330
331     if [info exists LIBJAVA] {
332         set libjava $LIBJAVA;
333     } else {
334         set libjava [libjava_find_lib libjava gcj]
335     }
336
337     verbose "using LIBJAVA = $libjava" 2
338     set args ""
339
340     # Basically we want to build up a colon separated path list from
341     # the value of $libjava.
342
343     set lpath "."
344     foreach dir [list $libjava] {
345         foreach item [split $dir " "] {
346             switch -glob -- $item {
347                 "-L*" {
348                     lappend lpath [string range $item 2 end]
349                 }
350             }
351         }
352     }
353
354     set lpath [concat $lpath $libjava_libgcc_s_path]
355     verbose "lpath = $lpath ; libgcc_s_path = $libjava_libgcc_s_path"
356     set ld_library_path [join $lpath :]
357     set libjava_ld_library_path "$ld_library_path"
358
359     # That's enough to make things work for the normal case.
360     # If we wanted to handle an arbitrary value of libjava,
361     # then we'd have to do a lot more work.
362
363     set_ld_library_path_env_vars
364     if [info exists env(LD_LIBRARY_PATH)] {
365         verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
366     }
367
368     # Determine CLASSPATH separator
369     if { [string match "i?86-pc-mingw32*" $target_triplet] } {
370         set sep ";"
371     } else {
372         set sep ":"
373     }
374
375     # Set the CLASSPATH environment variable
376     global env
377     set env(CLASSPATH) \
378         [join [list . $srcdir/$subdir $objdir $libgcj_jar] $sep]
379     verbose "CLASSPATH is $env(CLASSPATH)"
380
381     if {$mode == "link"} {
382         global wrapper_file wrap_compile_flags
383         lappend args "additional_flags=$wrap_compile_flags"
384         lappend args "libs=$wrapper_file"
385         lappend args "libs=$libjava"
386         lappend args debug
387     }
388
389     if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
390         lappend args "libs=${gluefile}"
391         lappend args "ldflags=$wrap_flags"
392     }
393
394     if [info exists TOOL_OPTIONS] {
395         lappend args "additional_flags=$TOOL_OPTIONS"
396     }
397
398     # Determine libgcj.spec corresponding to multilib under test.
399     set specdir [libjava_find_spec]
400
401     # Search for libtool.  We need it to link.
402     set found_compiler 0
403     set d [absolute $objdir]
404     foreach x {. .. ../.. ../../..} {
405         if {[file exists $d/$x/libtool]} then {
406             # We have to run silently to avoid DejaGNU lossage.
407             lappend args \
408               "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$specdir"
409             set found_compiler 1
410             break
411         }
412     }
413     if {! $found_compiler} {
414         # Append -B$specdir so that we find libgcj.spec before it
415         # is installed.
416         lappend args "compiler=$GCJ_UNDER_TEST -B$specdir"
417     }
418
419     # Avoid libtool wrapper scripts when possible.
420     # but not if libtool warnings results in FAILs
421     if {$mode == "link"} {
422         if {! [istarget "*-*-cygwin*"] && ! [istarget "*-*-mingw*"] } {
423             lappend args "additional_flags=-no-install"
424         }
425         if { [istarget "*-*-darwin*"] } {
426             lappend args "additional_flags=-bind_at_load"
427             lappend args "additional_flags=-multiply_defined suppress"
428         }
429     }
430
431     return $args
432 }
433
434 # Link a bunch of objects into a program.  MAIN is the name of the
435 # class holding `main'.  Return 0 on failure.
436 proc gcj_link {program main files {options {}}} {
437     set arguments [libjava_arguments link]
438     if {[llength $options]} {
439         eval lappend arguments $options
440     }
441     lappend arguments "additional_flags=--main=$main"
442     set x [libjava_prune_warnings \
443              [libjava_tcompile $files $program executable $arguments]]
444     if {$x != ""} {
445         verbose "link failure: $x" 2
446         fail "linking $program"
447         setup_xfail "*-*-*"
448         fail "running $program"
449         return 0
450     }
451
452     pass "linking $program"
453     return 1
454 }
455
456 # Invoke the program and see what happens.  Return 0 on failure.
457 proc gcj_invoke {program expectFile ld_library_additions} {
458   global env
459   global libjava_ld_library_path
460   global ld_library_path
461
462   set ld_library_path "$libjava_ld_library_path"
463   if {[llength $ld_library_additions] > 0} {
464     append ld_library_path :[join $ld_library_additions :]
465   }
466
467   set_ld_library_path_env_vars
468   if [info exists env(LD_LIBRARY_PATH)] {
469     verbose "LD_LIBRARY_PATH=$env(LD_LIBRARY_PATH)"
470   }
471
472   set result [libjava_load ./$program]
473   set status [lindex $result 0]
474   set output [lindex $result 1]
475
476   # Restore setting
477   restore_ld_library_path_env_vars
478
479   if {$status != "pass"} {
480     verbose "got $output"
481     fail "$program run"
482     untested "$program output"
483     return 0
484   }
485
486   set id [open $expectFile r]
487   set expected [read $id]
488   close $id
489
490   if {! [string compare $output $expected]} {
491     pass "$program output"
492     return 1
493   } else {
494     fail "$program output"
495     return 0
496   }
497 }
498
499 # Invoke a program and check its output.  EXECUTABLE is the program;
500 # ARGS are the arguments to the program.  Returns 1 if tests passed
501 # (or things were left untested), 0 otherwise.
502 proc libjava_invoke {errname testName optName executable inpfile resultfile
503                       ld_library_additions args} {
504     global env
505     global libjava_ld_library_path
506     global ld_library_path
507
508     set ld_library_path "$libjava_ld_library_path"
509     if {[llength $ld_library_additions] > 0} {
510         append ld_library_path :[join $ld_library_additions :]
511     }
512
513     set_ld_library_path_env_vars
514     if [info exists env(LD_LIBRARY_PATH)] {
515         verbose "LD_LIBRARY_PATH=$env(LD_LIBRARY_PATH)"
516     }
517
518     upvar $optName opts
519
520     if {[info exists opts(no-exec)]} {
521         if {[info exists opts(need-threads)]} {
522             # This means we wanted to try to run it but we couldn't
523             # because threads aren't supported.  So we have to
524             # generate an `untested'.
525             untested "$errname execution - $testName"
526             untested "$errname output - $testName"
527         }
528         return 1
529     }
530
531     set result [libjava_load $executable $args "$inpfile"]
532     set status [lindex $result 0]
533     set output [lindex $result 1]
534
535     # Restore LD_LIBRARY_PATH setting.
536     restore_ld_library_path_env_vars
537
538     if {[info exists opts(xfail-exec)]} then {
539         setup_xfail *-*-*
540     }
541     $status "$errname execution - $testName"
542     if { $status != "pass" } {
543         untested "$errname output - $testName"
544         return 0
545     }
546
547     verbose "resultfile is $resultfile"
548     set id [open $resultfile r]
549     set expected ""
550     append expected [read $id]
551     regsub -all "\r" "$output" "" output
552     regsub "\n*$" $expected "" expected
553     regsub "\n*$" $output "" output
554     regsub "^\n*" $expected "" expected
555     regsub "^\n*" $output "" output
556     regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
557     regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
558     regsub -all "\[ \t\]\[ \t\]*" $output " " output
559     regsub -all "\[ \t\]*\n\n*" $output "\n" output
560     verbose "expected is $expected"
561     verbose "actual is $output"
562     set passed 0
563     if {[info exists opts(regexp_match)]} {
564         if [regexp $expected $output] {
565             set passed 1
566         }
567     } else {
568         if { $expected == $output } {
569             set passed 1
570         }
571     }
572     if {[info exists opts(xfail-output)]} {
573         setup_xfail *-*-*
574     }
575     if { $passed == 1 } {
576         pass "$errname output - $testName"
577     } else {
578         fail "$errname output - $testName"
579     }
580     close $id
581
582     return $passed
583 }
584
585 #
586 # Run the test specified by srcfile and resultfile. compile_args and
587 # exec_args are options telling this proc how to work.
588 #
589 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
590     global base_dir
591     global srcdir subdir objdir
592     global TOOL_OPTIONS
593     global GCJ_UNDER_TEST
594     global tmpdir
595     global runtests
596
597     # Make opts into an array.
598     set opts(_) x
599     unset opts(_)
600     foreach item $exec_args {
601         set opts($item) x
602     }
603
604     # If we need threads and we don't have them then set the `no-exec'
605     # flag.  This is case is also handled specially later.
606     if {[info exists opts(need-threads)]} {
607         global libjava_uses_threads
608         if {! $libjava_uses_threads} {
609             set opts(no-exec) x
610         }
611     }
612
613     set errname [file rootname [file tail $srcfile]]
614     if {! [runtest_file_p $runtests $errname]} {
615         return
616     }
617
618     if {[info exists opts(no-link)]} {
619         set mode compile
620     } else {
621         set mode link
622     }
623     set args [libjava_arguments $mode]
624     if {! [info exists opts(no-link)]} {
625         # Add the --main flag
626         lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
627         if { $compile_args != "" } {
628             lappend args "additional_flags=$compile_args"
629         }
630     }
631
632     regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
633     set executable "${objdir}/$out"
634     if {[info exists opts(no-link)]} {
635         append executable ".o"
636         set target object
637     } else {
638         # DOS/win32 targets default to .exe if no suffix is given
639         # We then try to delete a file that doesn't exist.  It is
640         # simpler to add the suffix everywhere.
641         append executable ".exe"
642         set target executable
643     }
644     if { $compile_args != "" } {
645         set errname "$errname $compile_args"
646     }
647
648     set removeList [list $executable]
649
650     set x [libjava_prune_warnings \
651              [libjava_tcompile $srcfile "$executable" $target $args]]
652     if {[info exists opts(xfail-gcj)]} {
653         setup_xfail *-*-*
654     }
655     if { $x != "" } {
656         verbose "target_compile failed: $x" 2
657
658         if {[info exists opts(shouldfail)]} {
659             pass "$errname compilation from source"
660             eval gcj_cleanup $removeList
661             return
662         }
663
664         fail "$errname compilation from source"
665         if {[info exists opts(xfail-gcj)]
666             || ! [info exists opts(no-exec)]
667             || [info exists opts(need-threads)]} {
668             untested "$errname execution from source compiled test"
669             untested "$errname output from source compiled test"
670         }
671         return
672     }
673     if {[info exists opts(shouldfail)]} {
674         fail "$errname compilation from source"
675         return
676     }
677     pass "$errname compilation from source"
678
679     # Set up the options the way they are expected by libjava_invoke.
680     if {[info exists opts(xfail-source-output)]} {
681         set opts(xfail-output) x
682     }
683     if {[libjava_invoke $errname "source compiled test" opts $executable \
684            $inpfile $resultfile ""]} {
685         # Everything ok, so clean up.
686         eval gcj_cleanup $removeList
687     }
688 }
689
690 #
691 # Run the test specified by srcfile and resultfile. compile_args and
692 # exec_args are options telling this proc how to work.
693 #
694 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
695     global base_dir
696     global srcdir subdir objdir
697     global TOOL_OPTIONS
698     global GCJ_UNDER_TEST
699     global tmpdir
700     global runtests
701     global INTERPRETER
702
703     # Make opts into an array.
704     set opts(_) x
705     unset opts(_)
706     foreach item $exec_args {
707         set opts($item) x
708     }
709
710     # If we need threads and we don't have them then set the `no-exec'
711     # flag.  This is case is also handled specially later.
712     if {[info exists opts(need-threads)]} {
713         global libjava_uses_threads
714         if {! $libjava_uses_threads} {
715             set opts(no-exec) x
716         }
717     }
718     set errname [file rootname [file tail $srcfile]]
719     if {! [runtest_file_p $runtests $errname]} {
720         return
721     }
722
723     # bytecompile files with Sun's compiler for now.
724     set bc_ok [bytecompile_file $srcfile $objdir]
725
726     set javac [find_javac]
727     # This is an ugly heuristic but it will have to do.
728     if {[string match *gcj* $javac]} {
729         set tag gcjC
730     } else {
731         set tag javac
732     }
733     if {[info exists opts(xfail-$tag)]} {
734         setup_xfail *-*-*
735     }
736     if {! $bc_ok} then {
737         if {[info exists opts(shouldfail)]} {
738             pass "$errname byte compilation"
739             return
740         }
741         fail "$errname byte compilation"
742         untested "$errname compilation from bytecode"
743         if {! [info exists opts(no-exec)]
744             || [info exists opts(need-threads)]} {
745             untested "$errname execution from bytecode->native test"
746             untested "$errname output from bytecode->native test"
747         }
748         return
749     }
750     if {[info exists opts(shouldfail)]} {
751         fail "$errname byte compilation"
752         return
753     }
754     pass "$errname byte compilation"
755
756     set removeList {}
757
758     # Find name to use for --main, and name of all class files.
759     set jvscan [find_jvscan]
760     verbose "jvscan is $jvscan"
761     set main_name [string trim \
762                      [libjava_prune_warnings \
763                         [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --print-main" "" "" 300] 1]]]
764     verbose "main name is $main_name"
765     set class_out [string trim \
766                      [libjava_prune_warnings \
767                         [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --list-class" "" "" 300] 1]]]
768     verbose "class list is $class_out"
769
770     if {[string match "*parse error*" $main_name]
771         || [string match "*parse error*" $class_out]} {
772         untested "$errname compilation from bytecode"
773         if {! [info exists opts(no-exec)]
774             || [info exists opts(need-threads)]} {
775             untested "$errname execution from bytecode->native test"
776             untested "$errname output from bytecode->native test"
777         }
778         return
779     }
780
781     # Turn "a b" into "a.class b.class".
782     # Also, turn "foo.bar" into "foo/bar.class".
783     set class_files {}
784     foreach file [split [string trim $class_out]] {
785         set file [join [split $file .] /]
786         lappend class_files $objdir/$file.class
787     }
788
789     eval lappend removeList $class_files
790
791     # Usually it is an error for a test program not to have a `main'
792     # method.  However, for no-exec tests it is ok.  Treat no-link
793     # like no-exec here.
794     if {[info exists opts(no-link)]} {
795         set opts(no-exec) x
796     }
797     set largs {}
798
799     if {[info exists opts(no-exec)]} {
800         set type object
801         set mode compile
802     } elseif {$main_name == ""} {
803         perror "No `main' given in program $errname"
804         return
805     } else {
806         set type executable
807         lappend largs "additional_flags=--main=$main_name"
808         # DOS/win32 targets default to .exe if no suffix is given
809         # We then try to delete a file that doesn't exist.  It is
810         # simpler to add the suffix everywhere.
811         set executable "${objdir}/${main_name}.exe"
812         set mode link
813     }
814
815     # We purposely ignore errors here; we still want to run the other
816     # appropriate tests.
817     set gij [libjava_find_gij]
818     # libjava_find_gij will return "" if it couldn't find the
819     # program; in this case we want to skip the test.
820     if {$INTERPRETER == "yes" && $gij != ""} {
821         libjava_invoke $errname "gij test" opts $gij \
822           $inpfile $resultfile "" $main_name
823     }
824
825     # Initial arguments.
826     set args [libjava_arguments $mode]
827     eval lappend args $largs
828
829     if { $compile_args != "" } {
830         lappend args "additional_flags=$compile_args"
831     }
832
833     if { $compile_args != "" } {
834         set errname "$errname $compile_args"
835     }
836
837     verbose "compilation command = $args" 2
838     # When compiling and not linking, we have to build each .o
839     # separately.  We do this because DejaGNU's target_compile won't
840     # accept an empty "destfile" argument when the mode is "compile".
841     if {$mode == "compile"} {
842         foreach c_file $class_files {
843             set executable [file rootname [file tail $c_file]].o
844             # Don't write files which contain $ chars.
845             regsub -all "\\$" $executable "\^" executable
846             set x [libjava_prune_warnings \
847                      [libjava_tcompile '$c_file' "$executable" $type $args]]
848             lappend removeList $executable
849             if {$x != ""} {
850                 break
851             }
852         }
853     } else {
854         # This is so evil: we de-listify CLASS_FILES so that we can
855         # turn around and quote the `$' in it for the shell.  I really
856         # hate DejaGNU.  It is so !@#$!@# unpredictable.
857         set hack ""
858         foreach stupid $class_files {
859             set hack "$hack $stupid"
860         }
861         lappend removeList $executable
862         set x [libjava_prune_warnings \
863                  [libjava_tcompile $hack "$executable" $type $args]]
864     }
865     if {[info exists opts(xfail-byte)]} {
866         setup_xfail *-*-*
867     }
868     if { $x != "" } {
869         verbose "target_compile failed: $x" 2
870         fail "$errname compilation from bytecode"
871         if {! [info exists opts(no-exec)]
872             || [info exists opts(need-threads)]} {
873             untested "$errname execution from bytecode->native test"
874             untested "$errname output from bytecode->native test"
875         }
876         return;
877     }
878     pass "$errname compilation from bytecode"
879
880     # Set up the options the way they are expected by libjava_invoke.
881     if {[info exists opts(xfail-byte-output)]} {
882         set opts(xfail-output) x
883     }
884     if {[libjava_invoke $errname "bytecode->native test" opts $executable \
885            $inpfile $resultfile ""]} {
886         # Everything ok, so clean up.
887         eval gcj_cleanup $removeList
888     }
889 }
890
891 #
892 # Run the test specified by srcfile and resultfile. compile_args and
893 # exec_args are options telling this proc how to work.
894 #   `no-link'     don't try to link the program
895 #   `no-exec'     don't try to run the test
896 #   `xfail-gcj'   compilation from source will fail
897 #   `xfail-javac' compilation with javac will fail
898 #   `xfail-gcjC'  compilation with gcj -C will fail
899 #   `shouldfail'  compilation from source is supposed to fail
900 #                 This is different from xfail, which marks a known
901 #                 failure that we just haven't fixed.
902 #                 A compilation marked this way should fail with any
903 #                 front end.
904 #   `xfail-byte'  compilation from bytecode will fail
905 #   `xfail-exec'  exec will fail
906 #   `xfail-output'
907 #                 output will be wrong
908 #   `xfail-byte-output'
909 #                 output will be wrong when compiled from bytecode
910 #   `xfail-source-output'
911 #                 output will be wrong when compiled from source code
912 #   `need-threads'
913 #                 test relies on thread support
914 #
915 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
916     test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
917     test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
918     
919     # Test BC-ABI compilation, currently for bytecode->native only
920     set compile_args_bcabi $compile_args
921     lappend compile_args_bcabi "-findirect-dispatch"
922
923     test_libjava_from_javac $options $srcfile $compile_args_bcabi $inpfile $resultfile $exec_args
924  }
925
926 #
927 # libjava_version -- extract and print the version number of libjavap
928 #
929 proc default_libjava_version {} {
930 }
931
932 proc default_libjava_start { } {
933 }
934
935 # Local Variables:
936 # tcl-indent-level:4
937 # End: