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 `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.
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]
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]} {
30 set tokens [string trim [read $fd]]
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} {
40 set file [lookfor_file $tool_root_dir $prog]
42 set file [lookfor_file $tool_root_dir gcc/$prog];
52 return [libjava_find_program jv-scan]
57 return [libjava_find_program gcjh]
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)
74 set SUN_JAVAC "$GCJ_UNDER_TEST -C"
80 proc bytecompile_file { file objdir {classpath {}} } {
82 set dirname [file dirname $file]
84 set javac [find_javac]
85 if {$classpath != ""} then {
86 set env(CLASSPATH) $classpath
91 set q [eval exec "$javac [list $file] -d $objdir 2>@ stdout"]
93 verbose "couldn't compile $file: $msg"
102 set libjava_initialized 0
105 # Build the status wrapper library as needed.
107 proc libjava_init { args } {
109 global wrap_compile_flags;
110 global libjava_initialized libjava_uses_threads
111 global GCJ_UNDER_TEST
112 global TOOL_EXECUTABLE
113 global original_ld_library_path
115 global env gcc_version
117 if { $libjava_initialized == 1 } { return; }
119 if ![info exists GCJ_UNDER_TEST] {
120 if [info exists TOOL_EXECUTABLE] {
121 set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
123 if [info exists env(GCJ)] {
124 set GCJ_UNDER_TEST $env(GCJ)
126 set GCJ_UNDER_TEST "[find_gcj]"
131 # Determine the version so we can find the libgcj jar file.
132 set text [eval exec "$GCJ_UNDER_TEST --version 2>@ stdout"]
133 regexp -- "gcj \[^ \]+ (\[^ \]+) .*" $text ignore gcc_version
134 verbose "jar file is libgcj-$gcc_version.jar"
136 # FIXME: This finds libgcj.spec for the default multilib.
137 # If thread models differ between multilibs, this has to be moved
138 # to libjava_arguments
139 set specdir [libjava_find_spec]
141 # The -B is so we find libgcj.spec.
142 set text [eval exec "$GCJ_UNDER_TEST -B$specdir -v 2>@ stdout"]
143 regexp -- "Thread model: (\[^\n\]+)\n" $text ignore model
144 set libjava_uses_threads [expr {! ($model == "no"
146 || $model == "single")}]
148 # Always set encoding used by gcj.
149 append GCJ_UNDER_TEST " --encoding=UTF-8"
151 if [info exists env(LD_LIBRARY_PATH)] {
152 set original_ld_library_path $env(LD_LIBRARY_PATH)
154 if [info exists env(SHLIB_PATH)] {
155 set original_ld_library_path $env(SHLIB_PATH)
157 set original_ld_library_path ""
162 set wrap_compile_flags "";
163 if [target_info exists needs_status_wrapper] {
164 set result [build_wrapper "testglue.o"];
165 if { $result != "" } {
166 set wrapper_file [lindex $result 0];
167 set wrap_compile_flags [lindex $result 1];
169 warning "Status wrapper failed to build."
173 set libjava_initialized 1
176 # Find a library. We know where libtool puts the actual libraries,
177 # and we look there. The implementation is fairly hacky. We can't
178 # compile with -nodefaultlibs, because that will also eliminate the
179 # system libraries we need. In order to have gcj still work, it must
180 # find the appropriate libraries so we must add -L options for their
181 # paths. However we can't simply use those libraries; we still need
182 # libtool for linking.
183 # Don't return the the lib${name}.la files here, since this causes the
184 # libraries to be linked twice: once as lib${name}.so and another time
185 # via gcj's implicit -l${name}. This is both unnecessary and causes the
186 # Solaris ld to warn: attempted multiple inclusion of file. This warning
187 # is not ignored by the dejagnu framework and cannot be disabled.
188 proc libjava_find_lib {dir name} {
190 set gp [get_multilibs]
191 foreach sub {.libs _libs} {
193 if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
194 # Just return the `-L' option. The library itself
195 # will be picked up via the spec file.
196 return "-L$gp/$dir/$sub"
199 # Just return the `-L' option. The library itself will be
200 # picked up via the spec file.
201 set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
202 "-L$base_dir/../../$dir/$sub" \
211 # Find libgcj.spec. We need to use the file corresponding to the multilib
212 # under test since they might differ. Append a trailing slash since this
214 proc libjava_find_spec {} {
216 set gp [get_multilibs]
218 if {[file exists $gp/libjava/libgcj.spec]} then {
219 return "$gp/libjava/"
224 # Remove a bunch of files.
225 proc gcj_cleanup {args} {
227 if {[string match *.o $file]} {
228 verbose "removing [file rootname $file].lo"
229 file delete -force [file rootname $file].lo
231 file delete -force -- $file
232 verbose "removing $file"
234 # It is simplest to do this instead of trying to figure out what
235 # bits in .libs ought to be removed.
236 catch {system "rm -rf .libs"}
239 # Compute arguments needed for compiler. MODE is a libtool mode:
240 # either compile or link.
241 proc libjava_arguments {{mode compile}} {
247 global srcdir subdir objdir
249 global GCJ_UNDER_TEST
256 if [info exists LIBJAVA] {
257 set libjava $LIBJAVA;
259 set libjava [libjava_find_lib libjava gcj]
262 if [info exists LIBGC] {
265 set libgc [libjava_find_lib boehm-gc gcjgc]
268 if [info exists LIBQTHREADS] {
269 set libqthreads $LIBQTHREADS
271 set libqthreads [libjava_find_lib qthreads gcjcoop]
274 if [info exists LIBZ] {
277 set libz [libjava_find_lib zlib zgcj]
280 # FIXME: there's no way to determine whether -lpthread is
281 # required. We should get this info from configure, or it should
282 # just be in the compiler driver.
284 verbose "using LIBJAVA = $libjava" 2
285 verbose "using LIBGC = $libgc" 2
286 verbose "using LIBQTHREADS = $libqthreads" 2
287 verbose "using LIBZ = $libz" 2
290 # Basically we want to build up a colon separated path list from
291 # the value of $libjava.
294 foreach dir [list $libjava $libgc $libz] {
295 foreach item [split $dir " "] {
296 switch -glob -- $item {
298 lappend lpath [string range $item 2 end]
304 # Finally, add the gcc build directory so that we can find the
305 # shared libgcc. This, like much of dejagnu, is hideous.
306 set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.so]
308 lappend lpath [file dirname $gccdir]
311 set ld_library_path [join $lpath :]
313 # That's enough to make things work for the normal case.
314 # If we wanted to handle an arbitrary value of libjava,
315 # then we'd have to do a lot more work.
317 # Set variables the dynamic linker looks at.
318 global original_ld_library_path
319 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
320 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
322 verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
324 # Set the CLASSPATH environment variable
325 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$objdir/../libgcj-$gcc_version.jar"
327 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$objdir/../libgcj-$gcc_version.jar"
329 if {$mode == "link"} {
330 global wrapper_file wrap_compile_flags;
331 lappend args "additional_flags=$wrap_compile_flags";
332 lappend args "libs=$wrapper_file";
333 lappend args "libs=$libjava";
334 lappend args "libs=$libgc";
335 lappend args "libs=$libqthreads"
336 lappend args "libs=$libz"
340 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
341 lappend args "libs=${gluefile}"
342 lappend args "ldflags=$wrap_flags"
345 if [info exists TOOL_OPTIONS] {
346 lappend args "additional_flags=$TOOL_OPTIONS"
349 # Determine libgcj.spec corresponding to multilib under test.
350 set specdir [libjava_find_spec]
352 # Search for libtool. We need it to link.
354 set d [absolute $objdir]
355 foreach x {. .. ../.. ../../..} {
356 if {[file exists $d/$x/libtool]} then {
357 # We have to run silently to avoid DejaGNU lossage.
359 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$specdir"
364 if {! $found_compiler} {
365 # Append -B$specdir so that we find libgcj.spec before it
367 lappend args "compiler=$GCJ_UNDER_TEST -B$specdir"
370 # Avoid libtool wrapper scripts when possible.
371 if {$mode == "link"} {
372 lappend args "additional_flags=-no-install"
378 # Link a bunch of objects into a program. MAIN is the name of the
379 # class holding `main'. Return 0 on failure.
380 proc gcj_link {program main files {options {}}} {
381 set arguments [libjava_arguments link]
382 if {[llength $options]} {
383 eval lappend arguments $options
385 lappend arguments "additional_flags=--main=$main"
386 set x [prune_warnings [libjava_tcompile $files $program executable \
389 verbose "link failure: $x" 2
390 fail "linking $program"
392 fail "running $program"
396 pass "linking $program"
401 # Run the test specified by srcfile and resultfile. compile_args and
402 # exec_args are options telling this proc how to work.
404 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
408 global srcdir subdir objdir
410 global GCJ_UNDER_TEST
414 # Make opts into an array.
417 foreach item $exec_args {
421 # If we need threads and we don't have them then set the `no-exec'
422 # flag. This is case is also handled specially later.
423 if {[info exists opts(need-threads)]} {
424 global libjava_uses_threads
425 if {! $libjava_uses_threads} {
430 set errname [file rootname [file tail $srcfile]]
431 if {! [runtest_file_p $runtests $errname]} {
435 if {[info exists opts(no-link)]} {
440 set args [libjava_arguments $mode]
441 if {! [info exists opts(no-link)]} {
442 # Add the --main flag
443 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
444 if { $compile_args != "" } {
445 lappend args "additional_flags=$compile_args"
449 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
450 set executable "${objdir}/$out"
451 if {[info exists opts(no-link)]} {
452 append executable ".o"
455 # DOS/win32 targets default to .exe if no suffix is given
456 # We then try to delete a file that doesn't exist. It is
457 # simpler to add the suffix everywhere.
458 append executable ".exe"
459 set target executable
461 if { $compile_args != "" } {
462 set errname "$errname $compile_args"
465 set removeList [list $executable]
467 set x [prune_warnings \
468 [libjava_tcompile $srcfile "$executable" $target $args]]
469 if {[info exists opts(xfail-gcj)]} {
473 verbose "target_compile failed: $x" 2
475 if {[info exists opts(shouldfail)]} {
476 pass "$errname compilation from source"
477 eval gcj_cleanup $removeList
481 fail "$errname compilation from source"
482 if {[info exists opts(xfail-gcj)]
483 || ! [info exists opts(no-exec)]
484 || [info exists opts(need-threads)]} {
485 untested "$errname execution from source compiled test"
486 untested "$errname output from source compiled test"
490 if {[info exists opts(shouldfail)]} {
491 fail "$errname compilation from source"
494 pass "$errname compilation from source"
496 if {[info exists opts(no-exec)]} {
497 if {[info exists opts(need-threads)]} {
498 # This means we wanted to try to run it but we couldn't
499 # because threads aren't supported. So we have to
500 # generate an `untested'.
501 untested "$errname execution from source compiled test"
502 untested "$errname output from source compiled test"
504 eval gcj_cleanup $removeList
507 if {[info exists opts(no-link)]} {
508 eval gcj_cleanup $removeList
512 set result [libjava_load $executable "" "$inpfile"];
513 set status [lindex $result 0];
514 set output [lindex $result 1];
515 if {[info exists opts(xfail-exec)]} then {
518 $status "$errname execution from source compiled test"
519 if { $status != "pass" } {
520 untested "$errname output from source compiled test"
524 verbose "resultfile is $resultfile"
525 set id [open $resultfile r];
527 append expected [read $id];
528 regsub -all "\r" "$output" "" output;
529 regsub "\n*$" $expected "" expected
530 regsub "\n*$" $output "" output
531 regsub "^\n*" $expected "" expected
532 regsub "^\n*" $output "" output
533 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
534 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
535 regsub -all "\[ \t\]\[ \t\]*" $output " " output
536 regsub -all "\[ \t\]*\n\n*" $output "\n" output
537 verbose "expected is $expected"
538 verbose "actual is $output"
540 if {$options == "regexp_match"} {
541 if [regexp $expected $output] {
545 if { $expected == $output } {
549 if {[info exists opts(xfail-output)]} {
552 if {[info exists opts(xfail-source-output)]} {
555 if { $passed == 1 } {
556 pass "$errname output from source compiled test"
557 eval gcj_cleanup $removeList
559 fail "$errname output from source compiled test"
565 # Run the test specified by srcfile and resultfile. compile_args and
566 # exec_args are options telling this proc how to work.
568 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
572 global srcdir subdir objdir
574 global GCJ_UNDER_TEST
578 # Make opts into an array.
581 foreach item $exec_args {
585 # If we need threads and we don't have them then set the `no-exec'
586 # flag. This is case is also handled specially later.
587 if {[info exists opts(need-threads)]} {
588 global libjava_uses_threads
589 if {! $libjava_uses_threads} {
593 set errname [file rootname [file tail $srcfile]]
594 if {! [runtest_file_p $runtests $errname]} {
598 # bytecompile files with Sun's compiler for now.
599 set bc_ok [bytecompile_file $srcfile $objdir]
601 set javac [find_javac]
602 # This is an ugly heuristic but it will have to do.
603 if {[string match *gcj* $javac]} {
608 if {[info exists opts(xfail-$tag)]} {
612 if {[info exists opts(shouldfail)]} {
613 pass "$errname byte compilation"
616 fail "$errname byte compilation"
617 untested "$errname compilation from bytecode"
618 if {! [info exists opts(no-exec)]
619 || [info exists opts(need-threads)]} {
620 untested "$errname execution from bytecode->native test"
621 untested "$errname output from bytecode->native test"
625 if {[info exists opts(shouldfail)]} {
626 fail "$errname byte compilation"
629 pass "$errname byte compilation"
633 # Find name to use for --main, and name of all class files.
634 set jvscan [find_jvscan]
635 verbose "jvscan is $jvscan"
636 set main_name [string trim \
638 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --print-main" "" "" 300] 1]]]
639 verbose "main name is $main_name"
640 set class_out [string trim \
642 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --list-class" "" "" 300] 1]]]
643 verbose "class list is $class_out"
645 if {[string match "*parse error*" $main_name]
646 || [string match "*parse error*" $class_out]} {
647 untested "$errname compilation from bytecode"
648 if {! [info exists opts(no-exec)]
649 || [info exists opts(need-threads)]} {
650 untested "$errname execution from bytecode->native test"
651 untested "$errname output from bytecode->native test"
656 # Turn "a b" into "a.class b.class".
657 # Also, turn "foo.bar" into "foo/bar.class".
659 foreach file [split [string trim $class_out]] {
660 set file [join [split $file .] /]
661 lappend class_files $objdir/$file.class
664 eval lappend removeList $class_files
666 # Usually it is an error for a test program not to have a `main'
667 # method. However, for no-exec tests it is ok. Treat no-link
669 if {[info exists opts(no-link)]} {
674 if {[info exists opts(no-exec)]} {
677 } elseif {$main_name == ""} {
678 perror "No `main' given in program $errname"
682 lappend largs "additional_flags=--main=$main_name"
683 set executable "${objdir}/$main_name"
688 set args [libjava_arguments $mode]
689 eval lappend args $largs
691 if { $compile_args != "" } {
692 lappend args "additional_flags=$compile_args"
695 if { $compile_args != "" } {
696 set errname "$errname $compile_args"
699 verbose "compilation command = $args" 2
700 # When compiling and not linking, we have to build each .o
701 # separately. We do this because DejaGNU's target_compile won't
702 # accept an empty "destfile" argument when the mode is "compile".
703 if {$mode == "compile"} {
704 foreach c_file $class_files {
705 set executable [file rootname [file tail $c_file]].o
706 set x [prune_warnings \
707 [libjava_tcompile $c_file "$executable" $type $args]]
708 lappend removeList $executable
714 # This is so evil: we de-listify CLASS_FILES so that we can
715 # turn around and quote the `$' in it for the shell. I really
716 # hate DejaGNU. It is so !@#$!@# unpredictable.
718 foreach stupid $class_files {
719 set hack "$hack $stupid"
721 lappend removeList $executable
722 set x [prune_warnings \
723 [libjava_tcompile $hack "$executable" $type $args]]
725 if {[info exists opts(xfail-byte)]} {
729 verbose "target_compile failed: $x" 2
730 fail "$errname compilation from bytecode"
731 if {! [info exists opts(no-exec)]
732 || [info exists opts(need-threads)]} {
733 untested "$errname execution from bytecode->native test"
734 untested "$errname output from bytecode->native test"
738 pass "$errname compilation from bytecode"
740 if {[info exists opts(no-exec)]} {
741 if {[info exists opts(need-threads)]} {
742 untested "$errname execution from bytecode->native test"
743 untested "$errname output from bytecode->native test"
745 eval gcj_cleanup $removeList
749 set result [libjava_load $executable "" "$inpfile"];
750 set status [lindex $result 0];
751 set output [lindex $result 1];
752 if {[info exists opts(xfail-exec)]} {
755 $status "$errname execution from bytecode->native test"
756 if { $status != "pass" } {
757 untested "$errname output from bytecode->native test"
761 verbose "resultfile is $resultfile"
762 set id [open $resultfile r];
764 append expected [read $id];
765 regsub -all "\r" "$output" "" output;
766 regsub "\n*$" $expected "" expected
767 regsub "\n*$" $output "" output
768 regsub "^\n*" $expected "" expected
769 regsub "^\n*" $output "" output
770 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
771 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
772 regsub -all "\[ \t\]\[ \t\]*" $output " " output
773 regsub -all "\[ \t\]*\n\n*" $output "\n" output
774 verbose "expected is $expected"
775 verbose "actual is $output"
777 if {[info exists opts(xfail-output)]} {
780 if {[info exists opts(xfail-byte-output)]} {
783 if {$options == "regexp_match"} {
784 if [regexp $expected $output] {
788 if { $expected == $output } {
792 if { $passed == 1 } {
793 pass "$errname output from bytecode->native test"
794 eval gcj_cleanup $removeList
796 fail "$errname output from bytecode->native test"
802 # Run the test specified by srcfile and resultfile. compile_args and
803 # exec_args are options telling this proc how to work.
804 # `no-link' don't try to link the program
805 # `no-exec' don't try to run the test
806 # `xfail-gcj' compilation from source will fail
807 # `xfail-javac' compilation with javac will fail
808 # `xfail-gcjC' compilation with gcj -C will fail
809 # `shouldfail' compilation from source is supposed to fail
810 # This is different from xfail, which marks a known
811 # failure that we just havne't fixed.
812 # A compilation marked this way should fail with any
814 # `xfail-byte' compilation from bytecode will fail
815 # `xfail-exec' exec will fail
817 # output will be wrong
818 # `xfail-byte-output'
819 # output will be wrong when compiled from bytecode
820 # `xfail-source-output'
821 # output will be wrong when compiled from source code
823 # test relies on thread support
825 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
826 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
827 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
831 # libjava_version -- extract and print the version number of libjavap
833 proc default_libjava_version {} {
836 proc default_libjava_start { } {
839 # On IRIX 6, we have to set variables akin to LD_LIBRARY_PATH, but
840 # called LD_LIBRARYN32_PATH (for the N32 ABI) and LD_LIBRARY64_PATH
841 # (for the 64-bit ABI). The right way to do this would be to modify
842 # unix.exp -- but that's not an option since it's part of DejaGNU
843 # proper, so we do it here, by trickery. We really only need to do
844 # this on IRIX, but it shouldn't hurt to do it anywhere else.
846 proc ${tool}_set_ld_library_path { name element op } {
847 setenv LD_LIBRARYN32_PATH [getenv LD_LIBRARY_PATH]
848 setenv LD_LIBRARY64_PATH [getenv LD_LIBRARY_PATH]
851 trace variable env(LD_LIBRARY_PATH) w ${tool}_set_ld_library_path