1 # Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation
3 load_lib "libgloss.exp"
5 # GCJ_UNDER_TEST is the compiler under test.
9 if ![info exists tmpdir] {
13 # This is like `prune_warnings', but it also prunes away a warning
14 # from the bytecode front end that we don't care about.
15 proc libjava_prune_warnings {text} {
16 set text [prune_warnings $text]
17 set tlist [split $text \n]
18 set len [llength $tlist]
19 for {set i [expr {$len - 1}]} {$i >= 2} {incr i -1} {
20 if {[string match "*unreachable bytecode*" [lindex $tlist $i]]} {
21 # Delete this line and the previous two lines.
22 set tlist [lreplace $tlist [expr {$i - 2}] $i]
26 return [join $tlist \n]
29 # This is like `target_compile' but it does some surgery to work
30 # around stupid DejaGNU bugs. In particular DejaGNU has very poor
31 # quoting, so for instance a `$' will be re-evaluated at spawn time.
33 proc libjava_tcompile {source destfile type options} {
34 # This strange-looking expression really does quote the `$'.
35 regsub -all -- {\$} $source {\$} source
36 regsub -all -- {\$} $destfile {\$} destfile
37 return [target_compile $source $destfile $type $options]
40 # Read an `xfail' file if it exists. Returns a list of xfail tokens.
41 proc libjava_read_xfail {file} {
42 if {! [file exists $file]} {
46 set tokens [string trim [read $fd]]
51 # Find a particular executable. FIXME: this relies on DejaGnu
52 # internals. These should probably be exposed in a better way.
53 proc libjava_find_program {prog} {
56 set file [lookfor_file $tool_root_dir $prog]
58 set file [lookfor_file $tool_root_dir gcc/$prog];
68 return [libjava_find_program jv-scan]
73 return [libjava_find_program gcjh]
77 global SUN_JAVAC GCJ_UNDER_TEST env
78 # If JDK doesn't run on your platform but some other
79 # JDK-compatible javac does, you may set SUN_JAVAC to point to it.
80 # One of the most important properties of a SUN_JAVAC is that it
81 # must create class-files even for classes that have not been
82 # specified in the command line, but that were needed to compile
83 # those that have. For example, Pizza won't do it, but you can
84 # use `kaffe sun.tools.javac.Main', if you have Sun's classes.zip
85 # in the kaffe's default search path.
86 if {![info exists SUN_JAVAC]} {
87 if {[info exists env(SUN_JAVAC)]} {
88 set SUN_JAVAC $env(SUN_JAVAC)
90 set SUN_JAVAC "$GCJ_UNDER_TEST -C"
96 proc bytecompile_file { file objdir {classpath {}} } {
98 set dirname [file dirname $file]
100 set javac [find_javac]
101 if {$classpath != ""} then {
102 set env(CLASSPATH) $classpath
107 set q [eval exec "$javac [list $file] -d $objdir 2>@ stdout"]
109 verbose "couldn't compile $file: $msg"
118 set libjava_initialized 0
121 # Build the status wrapper library as needed.
123 proc libjava_init { args } {
125 global wrap_compile_flags;
126 global libjava_initialized libjava_uses_threads
127 global GCJ_UNDER_TEST
128 global TOOL_EXECUTABLE
129 global original_ld_library_path
131 global env libgcj_jar
133 global libjava_libgcc_s_path
135 if { $libjava_initialized == 1 } { return; }
137 if ![info exists GCJ_UNDER_TEST] {
138 if [info exists TOOL_EXECUTABLE] {
139 set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
141 if [info exists env(GCJ)] {
142 set GCJ_UNDER_TEST $env(GCJ)
144 set GCJ_UNDER_TEST "[find_gcj]"
149 # Find the libgcj jar file.
150 set libgcj_jar [glob $objdir/../libgcj-*.jar]
151 verbose "jar file is $libgcj_jar"
153 # FIXME: This finds libgcj.spec for the default multilib.
154 # If thread models differ between multilibs, this has to be moved
155 # to libjava_arguments
156 set specdir [libjava_find_spec]
158 # The -B is so we find libgcj.spec.
159 set text [eval exec "$GCJ_UNDER_TEST -B$specdir -v 2>@ stdout"]
160 regexp -- "Thread model: (\[^\n\]+)\n" $text ignore model
161 set libjava_uses_threads [expr {! ($model == "no"
163 || $model == "single")}]
165 # Always set encoding used by gcj.
166 append GCJ_UNDER_TEST " --encoding=UTF-8"
168 if [info exists env(LD_LIBRARY_PATH)] {
169 set original_ld_library_path $env(LD_LIBRARY_PATH)
171 if [info exists env(SHLIB_PATH)] {
172 set original_ld_library_path $env(SHLIB_PATH)
174 set original_ld_library_path ""
179 set wrap_compile_flags "";
180 if [target_info exists needs_status_wrapper] {
181 set result [build_wrapper "testglue.o"];
182 if { $result != "" } {
183 set wrapper_file [lindex $result 0];
184 set wrap_compile_flags [lindex $result 1];
186 warning "Status wrapper failed to build."
190 # Finally, add the gcc build directory so that we can find the
191 # shared libgcc. This, like much of dejagnu, is hideous.
192 set libjava_libgcc_s_path {}
193 set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.so]
195 set gccdir [file dirname $gccdir]
196 lappend libjava_libgcc_s_path $gccdir
197 set compiler ${gccdir}/xgcc
198 if { [is_remote host] == 0 && [which $compiler] != 0 } {
199 foreach i "[exec $compiler --print-multi-lib]" {
201 regexp -- "\[a-z0-9=/\.-\]*;" $i mldir
202 set mldir [string trimright $mldir "\;@"]
203 if { "$mldir" == "." } {
206 if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] == 1 } {
207 lappend libjava_libgcc_s_path "${gccdir}/${mldir}"
213 set libjava_initialized 1
216 # Find a library. We know where libtool puts the actual libraries,
217 # and we look there. The implementation is fairly hacky. We can't
218 # compile with -nodefaultlibs, because that will also eliminate the
219 # system libraries we need. In order to have gcj still work, it must
220 # find the appropriate libraries so we must add -L options for their
221 # paths. However we can't simply use those libraries; we still need
222 # libtool for linking.
223 # Don't return the the lib${name}.la files here, since this causes the
224 # libraries to be linked twice: once as lib${name}.so and another time
225 # via gcj's implicit -l${name}. This is both unnecessary and causes the
226 # Solaris ld to warn: attempted multiple inclusion of file. This warning
227 # is not ignored by the dejagnu framework and cannot be disabled.
228 proc libjava_find_lib {dir name} {
230 set gp [get_multilibs]
231 foreach sub {.libs _libs} {
233 if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
234 # Just return the `-L' option. The library itself
235 # will be picked up via the spec file.
236 return "-L$gp/$dir/$sub"
239 # Just return the `-L' option. The library itself will be
240 # picked up via the spec file.
241 set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
242 "-L$base_dir/../../$dir/$sub" \
251 # Find libgcj.spec. We need to use the file corresponding to the multilib
252 # under test since they might differ. Append a trailing slash since this
254 proc libjava_find_spec {} {
256 set gp [get_multilibs]
258 if {[file exists $gp/libjava/libgcj.spec]} then {
259 return "$gp/libjava/"
265 proc libjava_find_gij {} {
267 set gp [get_multilibs]
269 set file $gp/libjava/gij
271 set file $base_dir/../gij
274 if {[file exists $file]} {
280 # Remove a bunch of files.
281 proc gcj_cleanup {args} {
283 if {[string match *.o $file]} {
284 verbose "removing [file rootname $file].lo"
285 file delete -force [file rootname $file].lo
287 file delete -force -- $file
288 verbose "removing $file"
290 # It is simplest to do this instead of trying to figure out what
291 # bits in .libs ought to be removed.
292 catch {system "rm -rf .libs"}
295 # Compute arguments needed for compiler. MODE is a libtool mode:
296 # either compile or link.
297 proc libjava_arguments {{mode compile}} {
303 global srcdir subdir objdir
305 global GCJ_UNDER_TEST
311 global libjava_libgcc_s_path
313 if [info exists LIBJAVA] {
314 set libjava $LIBJAVA;
316 set libjava [libjava_find_lib libjava gcj]
319 if [info exists LIBGC] {
322 set libgc [libjava_find_lib boehm-gc gcjgc]
325 if [info exists LIBQTHREADS] {
326 set libqthreads $LIBQTHREADS
328 set libqthreads [libjava_find_lib qthreads gcjcoop]
331 if [info exists LIBZ] {
334 set libz [libjava_find_lib zlib zgcj]
337 # FIXME: there's no way to determine whether -lpthread is
338 # required. We should get this info from configure, or it should
339 # just be in the compiler driver.
341 verbose "using LIBJAVA = $libjava" 2
342 verbose "using LIBGC = $libgc" 2
343 verbose "using LIBQTHREADS = $libqthreads" 2
344 verbose "using LIBZ = $libz" 2
347 # Basically we want to build up a colon separated path list from
348 # the value of $libjava.
351 foreach dir [list $libjava $libgc $libz] {
352 foreach item [split $dir " "] {
353 switch -glob -- $item {
355 lappend lpath [string range $item 2 end]
361 set lpath [concat $lpath $libjava_libgcc_s_path]
362 set ld_library_path [join $lpath :]
364 # That's enough to make things work for the normal case.
365 # If we wanted to handle an arbitrary value of libjava,
366 # then we'd have to do a lot more work.
368 # Set variables the dynamic linker looks at.
369 global original_ld_library_path
370 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
371 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
373 verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
375 # Set the CLASSPATH environment variable
376 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$libgcj_jar"
378 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$libgcj_jar"
380 if {$mode == "link"} {
381 global wrapper_file wrap_compile_flags;
382 lappend args "additional_flags=$wrap_compile_flags";
383 lappend args "libs=$wrapper_file";
384 lappend args "libs=$libjava";
385 lappend args "libs=$libgc";
386 lappend args "libs=$libqthreads"
387 lappend args "libs=$libz"
391 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
392 lappend args "libs=${gluefile}"
393 lappend args "ldflags=$wrap_flags"
396 if [info exists TOOL_OPTIONS] {
397 lappend args "additional_flags=$TOOL_OPTIONS"
400 # Determine libgcj.spec corresponding to multilib under test.
401 set specdir [libjava_find_spec]
403 # Search for libtool. We need it to link.
405 set d [absolute $objdir]
406 foreach x {. .. ../.. ../../..} {
407 if {[file exists $d/$x/libtool]} then {
408 # We have to run silently to avoid DejaGNU lossage.
410 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$specdir"
415 if {! $found_compiler} {
416 # Append -B$specdir so that we find libgcj.spec before it
418 lappend args "compiler=$GCJ_UNDER_TEST -B$specdir"
421 # Avoid libtool wrapper scripts when possible.
422 # but not if libtool warnings results in FAILs
423 if {$mode == "link"} {
424 if {! [istarget "*-*-cygwin*"] && ! [istarget "*-*-mingw*"] } {
425 lappend args "additional_flags=-no-install"
432 # Link a bunch of objects into a program. MAIN is the name of the
433 # class holding `main'. Return 0 on failure.
434 proc gcj_link {program main files {options {}}} {
435 set arguments [libjava_arguments link]
436 if {[llength $options]} {
437 eval lappend arguments $options
439 lappend arguments "additional_flags=--main=$main"
440 set x [libjava_prune_warnings \
441 [libjava_tcompile $files $program executable $arguments]]
443 verbose "link failure: $x" 2
444 fail "linking $program"
446 fail "running $program"
450 pass "linking $program"
454 # Invoke a program and check its output. EXECUTABLE is the program;
455 # ARGS are the arguments to the program. Returns 1 if tests passed
456 # (or things were left untested), 0 otherwise.
457 proc libjava_invoke {errname testName optName executable inpfile resultfile args} {
460 if {[info exists opts(no-exec)]} {
461 if {[info exists opts(need-threads)]} {
462 # This means we wanted to try to run it but we couldn't
463 # because threads aren't supported. So we have to
464 # generate an `untested'.
465 untested "$errname execution - $testName"
466 untested "$errname output - $testName"
471 set result [libjava_load $executable $args "$inpfile"]
472 set status [lindex $result 0]
473 set output [lindex $result 1]
474 if {[info exists opts(xfail-exec)]} then {
477 $status "$errname execution - $testName"
478 if { $status != "pass" } {
479 untested "$errname output - $testName"
483 verbose "resultfile is $resultfile"
484 set id [open $resultfile r]
486 append expected [read $id]
487 regsub -all "\r" "$output" "" output
488 regsub "\n*$" $expected "" expected
489 regsub "\n*$" $output "" output
490 regsub "^\n*" $expected "" expected
491 regsub "^\n*" $output "" output
492 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
493 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
494 regsub -all "\[ \t\]\[ \t\]*" $output " " output
495 regsub -all "\[ \t\]*\n\n*" $output "\n" output
496 verbose "expected is $expected"
497 verbose "actual is $output"
499 if {[info exists opts(regexp_match)]} {
500 if [regexp $expected $output] {
504 if { $expected == $output } {
508 if {[info exists opts(xfail-output)]} {
511 if { $passed == 1 } {
512 pass "$errname output - $testName"
514 fail "$errname output - $testName"
522 # Run the test specified by srcfile and resultfile. compile_args and
523 # exec_args are options telling this proc how to work.
525 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
529 global srcdir subdir objdir
531 global GCJ_UNDER_TEST
535 # Make opts into an array.
538 foreach item $exec_args {
542 # If we need threads and we don't have them then set the `no-exec'
543 # flag. This is case is also handled specially later.
544 if {[info exists opts(need-threads)]} {
545 global libjava_uses_threads
546 if {! $libjava_uses_threads} {
551 set errname [file rootname [file tail $srcfile]]
552 if {! [runtest_file_p $runtests $errname]} {
556 if {[info exists opts(no-link)]} {
561 set args [libjava_arguments $mode]
562 if {! [info exists opts(no-link)]} {
563 # Add the --main flag
564 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
565 if { $compile_args != "" } {
566 lappend args "additional_flags=$compile_args"
570 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
571 set executable "${objdir}/$out"
572 if {[info exists opts(no-link)]} {
573 append executable ".o"
576 # DOS/win32 targets default to .exe if no suffix is given
577 # We then try to delete a file that doesn't exist. It is
578 # simpler to add the suffix everywhere.
579 append executable ".exe"
580 set target executable
582 if { $compile_args != "" } {
583 set errname "$errname $compile_args"
586 set removeList [list $executable]
588 set x [libjava_prune_warnings \
589 [libjava_tcompile $srcfile "$executable" $target $args]]
590 if {[info exists opts(xfail-gcj)]} {
594 verbose "target_compile failed: $x" 2
596 if {[info exists opts(shouldfail)]} {
597 pass "$errname compilation from source"
598 eval gcj_cleanup $removeList
602 fail "$errname compilation from source"
603 if {[info exists opts(xfail-gcj)]
604 || ! [info exists opts(no-exec)]
605 || [info exists opts(need-threads)]} {
606 untested "$errname execution from source compiled test"
607 untested "$errname output from source compiled test"
611 if {[info exists opts(shouldfail)]} {
612 fail "$errname compilation from source"
615 pass "$errname compilation from source"
617 # Set up the options the way they are expected by libjava_invoke.
618 if {[info exists opts(xfail-source-output)]} {
619 set opts(xfail-output) x
621 if {[libjava_invoke $errname "source compiled test" opts $executable \
622 $inpfile $resultfile]} {
623 # Everything ok, so clean up.
624 eval gcj_cleanup $removeList
629 # Run the test specified by srcfile and resultfile. compile_args and
630 # exec_args are options telling this proc how to work.
632 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
636 global srcdir subdir objdir
638 global GCJ_UNDER_TEST
643 # Make opts into an array.
646 foreach item $exec_args {
650 # If we need threads and we don't have them then set the `no-exec'
651 # flag. This is case is also handled specially later.
652 if {[info exists opts(need-threads)]} {
653 global libjava_uses_threads
654 if {! $libjava_uses_threads} {
658 set errname [file rootname [file tail $srcfile]]
659 if {! [runtest_file_p $runtests $errname]} {
663 # bytecompile files with Sun's compiler for now.
664 set bc_ok [bytecompile_file $srcfile $objdir]
666 set javac [find_javac]
667 # This is an ugly heuristic but it will have to do.
668 if {[string match *gcj* $javac]} {
673 if {[info exists opts(xfail-$tag)]} {
677 if {[info exists opts(shouldfail)]} {
678 pass "$errname byte compilation"
681 fail "$errname byte compilation"
682 untested "$errname compilation from bytecode"
683 if {! [info exists opts(no-exec)]
684 || [info exists opts(need-threads)]} {
685 untested "$errname execution from bytecode->native test"
686 untested "$errname output from bytecode->native test"
690 if {[info exists opts(shouldfail)]} {
691 fail "$errname byte compilation"
694 pass "$errname byte compilation"
698 # Find name to use for --main, and name of all class files.
699 set jvscan [find_jvscan]
700 verbose "jvscan is $jvscan"
701 set main_name [string trim \
702 [libjava_prune_warnings \
703 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --print-main" "" "" 300] 1]]]
704 verbose "main name is $main_name"
705 set class_out [string trim \
706 [libjava_prune_warnings \
707 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --list-class" "" "" 300] 1]]]
708 verbose "class list is $class_out"
710 if {[string match "*parse error*" $main_name]
711 || [string match "*parse error*" $class_out]} {
712 untested "$errname compilation from bytecode"
713 if {! [info exists opts(no-exec)]
714 || [info exists opts(need-threads)]} {
715 untested "$errname execution from bytecode->native test"
716 untested "$errname output from bytecode->native test"
721 # Turn "a b" into "a.class b.class".
722 # Also, turn "foo.bar" into "foo/bar.class".
724 foreach file [split [string trim $class_out]] {
725 set file [join [split $file .] /]
726 lappend class_files $objdir/$file.class
729 eval lappend removeList $class_files
731 # Usually it is an error for a test program not to have a `main'
732 # method. However, for no-exec tests it is ok. Treat no-link
734 if {[info exists opts(no-link)]} {
739 if {[info exists opts(no-exec)]} {
742 } elseif {$main_name == ""} {
743 perror "No `main' given in program $errname"
747 lappend largs "additional_flags=--main=$main_name"
748 # DOS/win32 targets default to .exe if no suffix is given
749 # We then try to delete a file that doesn't exist. It is
750 # simpler to add the suffix everywhere.
751 set executable "${objdir}/${main_name}.exe"
755 # We purposely ignore errors here; we still want to run the other
757 set gij [libjava_find_gij]
758 # libjava_find_gij will return `gij' if it couldn't find the
759 # program; in this case we want to skip the test.
760 if {$INTERPRETER == "yes" && $gij != "gij"} {
761 libjava_invoke $errname "gij test" opts $gij \
762 $inpfile $resultfile $main_name
766 set args [libjava_arguments $mode]
767 eval lappend args $largs
769 if { $compile_args != "" } {
770 lappend args "additional_flags=$compile_args"
773 if { $compile_args != "" } {
774 set errname "$errname $compile_args"
777 verbose "compilation command = $args" 2
778 # When compiling and not linking, we have to build each .o
779 # separately. We do this because DejaGNU's target_compile won't
780 # accept an empty "destfile" argument when the mode is "compile".
781 if {$mode == "compile"} {
782 foreach c_file $class_files {
783 set executable [file rootname [file tail $c_file]].o
784 set x [libjava_prune_warnings \
785 [libjava_tcompile $c_file "$executable" $type $args]]
786 lappend removeList $executable
792 # This is so evil: we de-listify CLASS_FILES so that we can
793 # turn around and quote the `$' in it for the shell. I really
794 # hate DejaGNU. It is so !@#$!@# unpredictable.
796 foreach stupid $class_files {
797 set hack "$hack $stupid"
799 lappend removeList $executable
800 set x [libjava_prune_warnings \
801 [libjava_tcompile $hack "$executable" $type $args]]
803 if {[info exists opts(xfail-byte)]} {
807 verbose "target_compile failed: $x" 2
808 fail "$errname compilation from bytecode"
809 if {! [info exists opts(no-exec)]
810 || [info exists opts(need-threads)]} {
811 untested "$errname execution from bytecode->native test"
812 untested "$errname output from bytecode->native test"
816 pass "$errname compilation from bytecode"
818 # Set up the options the way they are expected by libjava_invoke.
819 if {[info exists opts(xfail-byte-output)]} {
820 set opts(xfail-output) x
822 if {[libjava_invoke $errname "bytecode->native test" opts $executable \
823 $inpfile $resultfile]} {
824 # Everything ok, so clean up.
825 eval gcj_cleanup $removeList
830 # Run the test specified by srcfile and resultfile. compile_args and
831 # exec_args are options telling this proc how to work.
832 # `no-link' don't try to link the program
833 # `no-exec' don't try to run the test
834 # `xfail-gcj' compilation from source will fail
835 # `xfail-javac' compilation with javac will fail
836 # `xfail-gcjC' compilation with gcj -C will fail
837 # `shouldfail' compilation from source is supposed to fail
838 # This is different from xfail, which marks a known
839 # failure that we just haven't fixed.
840 # A compilation marked this way should fail with any
842 # `xfail-byte' compilation from bytecode will fail
843 # `xfail-exec' exec will fail
845 # output will be wrong
846 # `xfail-byte-output'
847 # output will be wrong when compiled from bytecode
848 # `xfail-source-output'
849 # output will be wrong when compiled from source code
851 # test relies on thread support
853 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
854 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
855 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
859 # libjava_version -- extract and print the version number of libjavap
861 proc default_libjava_version {} {
864 proc default_libjava_start { } {
867 # On IRIX 6, we have to set variables akin to LD_LIBRARY_PATH, but
868 # called LD_LIBRARYN32_PATH (for the N32 ABI) and LD_LIBRARY64_PATH
869 # (for the 64-bit ABI). The right way to do this would be to modify
870 # unix.exp -- but that's not an option since it's part of DejaGNU
871 # proper, so we do it here, by trickery. We really only need to do
872 # this on IRIX, but it shouldn't hurt to do it anywhere else.
874 proc ${tool}_set_ld_library_path { name element op } {
875 setenv LD_LIBRARYN32_PATH [getenv LD_LIBRARY_PATH]
876 setenv LD_LIBRARY64_PATH [getenv LD_LIBRARY_PATH]
879 trace variable env(LD_LIBRARY_PATH) w ${tool}_set_ld_library_path