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 # The -B is so we find libgcj.spec.
137 set text [eval exec "$GCJ_UNDER_TEST -B$objdir/../ -v 2>@ stdout"]
138 regexp -- "Thread model: (\[^\n\]+)\n" $text ignore model
139 set libjava_uses_threads [expr {! ($model == "no"
141 || $model == "single")}]
143 # Always set encoding used by gcj.
144 append GCJ_UNDER_TEST " --encoding=UTF-8"
146 if [info exists env(LD_LIBRARY_PATH)] {
147 set original_ld_library_path $env(LD_LIBRARY_PATH)
149 if [info exists env(SHLIB_PATH)] {
150 set original_ld_library_path $env(SHLIB_PATH)
152 set original_ld_library_path ""
157 set wrap_compile_flags "";
158 if [target_info exists needs_status_wrapper] {
159 set result [build_wrapper "testglue.o"];
160 if { $result != "" } {
161 set wrapper_file [lindex $result 0];
162 set wrap_compile_flags [lindex $result 1];
164 warning "Status wrapper failed to build."
168 set libjava_initialized 1
171 # Find a library. We know where libtool puts the actual libraries,
172 # and we look there. The implementation is fairly hacky. We can't
173 # compile with -nodefaultlibs, because that will also eliminate the
174 # system libraries we need. In order to have gcj still work, it must
175 # find the appropriate libraries so we must add -L options for their
176 # paths. However we can't simply use those libraries; we still need
177 # libtool for linking.
178 proc libjava_find_lib {dir name} {
180 set gp [get_multilibs]
181 foreach sub {.libs _libs} {
183 if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
184 # Just return the `-L' option. The library itself
185 # will be picked up via the spec file.
186 return "-L$gp/$dir/$sub"
189 # Just return the `-L' option. The library itself will be
190 # picked up via the spec file.
191 set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
192 "-L$base_dir/../../$dir/$sub" \
201 # Remove a bunch of files.
202 proc gcj_cleanup {args} {
204 if {[string match *.o $file]} {
205 verbose "removing [file rootname $file].lo"
206 file delete -force [file rootname $file].lo
208 file delete -force -- $file
209 verbose "removing $file"
211 # It is simplest to do this instead of trying to figure out what
212 # bits in .libs ought to be removed.
213 catch {system "rm -rf .libs"}
216 # Compute arguments needed for compiler. MODE is a libtool mode:
217 # either compile or link.
218 proc libjava_arguments {{mode compile}} {
224 global srcdir subdir objdir
226 global GCJ_UNDER_TEST
233 if [info exists LIBJAVA] {
234 set libjava $LIBJAVA;
236 set libjava [libjava_find_lib libjava gcj]
239 if [info exists LIBGC] {
242 set libgc [libjava_find_lib boehm-gc gcjgc]
245 if [info exists LIBQTHREADS] {
246 set libqthreads $LIBQTHREADS
248 set libqthreads [libjava_find_lib qthreads gcjcoop]
251 if [info exists LIBZ] {
254 set libz [libjava_find_lib zlib zgcj]
257 # FIXME: there's no way to determine whether -lpthread is
258 # required. We should get this info from configure, or it should
259 # just be in the compiler driver.
261 verbose "using LIBJAVA = $libjava" 2
262 verbose "using LIBGC = $libgc" 2
263 verbose "using LIBQTHREADS = $libqthreads" 2
264 verbose "using LIBZ = $libz" 2
267 # Basically we want to build up a colon separated path list from
268 # the value of $libjava.
271 foreach dir [list $libjava $libgc $libz] {
272 foreach item [split $dir " "] {
273 switch -glob -- $item {
275 lappend lpath [string range $item 2 end]
281 # Finally, add the gcc build directory so that we can find the
282 # shared libgcc. This, like much of dejagnu, is hideous.
283 set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.so]
285 lappend lpath [file dirname $gccdir]
288 set ld_library_path [join $lpath :]
290 # That's enough to make things work for the normal case.
291 # If we wanted to handle an arbitrary value of libjava,
292 # then we'd have to do a lot more work.
294 # Set variables the dynamic linker looks at.
295 global original_ld_library_path
296 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
297 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
299 verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
301 # Set the CLASSPATH environment variable
302 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$objdir/../libgcj-$gcc_version.jar"
304 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$objdir/../libgcj-$gcc_version.jar"
306 if {$mode == "link"} {
307 global wrapper_file wrap_compile_flags;
308 lappend args "additional_flags=$wrap_compile_flags";
309 lappend args "libs=$wrapper_file";
310 lappend args "libs=$libjava";
311 lappend args "libs=$libgc";
312 lappend args "libs=$libqthreads"
313 lappend args "libs=$libz"
317 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
318 lappend args "libs=${gluefile}"
319 lappend args "ldflags=$wrap_flags"
322 if [info exists TOOL_OPTIONS] {
323 lappend args "additional_flags=$TOOL_OPTIONS"
326 # Search for libtool. We need it to link.
328 set d [absolute $objdir]
329 foreach x {. .. ../.. ../../..} {
330 if {[file exists $d/$x/libtool]} then {
331 # We have to run silently to avoid DejaGNU lossage.
333 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
338 if {! $found_compiler} {
339 # Append -B$objdir/../ so that we find libgcj.spec before it
341 lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
344 # Avoid libtool wrapper scripts when possible.
345 if {$mode == "link"} {
346 lappend args "additional_flags=-no-install"
352 # Link a bunch of objects into a program. MAIN is the name of the
353 # class holding `main'. Return 0 on failure.
354 proc gcj_link {program main files {options {}}} {
355 set arguments [libjava_arguments link]
356 if {[llength $options]} {
357 eval lappend arguments $options
359 lappend arguments "additional_flags=--main=$main"
360 set x [prune_warnings [libjava_tcompile $files $program executable \
363 verbose "link failure: $x" 2
364 fail "linking $program"
366 fail "running $program"
370 pass "linking $program"
375 # Run the test specified by srcfile and resultfile. compile_args and
376 # exec_args are options telling this proc how to work.
378 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
382 global srcdir subdir objdir
384 global GCJ_UNDER_TEST
388 # Make opts into an array.
391 foreach item $exec_args {
395 # If we need threads and we don't have them then set the `no-exec'
396 # flag. This is case is also handled specially later.
397 if {[info exists opts(need-threads)]} {
398 global libjava_uses_threads
399 if {! $libjava_uses_threads} {
404 set errname [file rootname [file tail $srcfile]]
405 if {! [runtest_file_p $runtests $errname]} {
409 if {[info exists opts(no-link)]} {
414 set args [libjava_arguments $mode]
415 if {! [info exists opts(no-link)]} {
416 # Add the --main flag
417 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
418 if { $compile_args != "" } {
419 lappend args "additional_flags=$compile_args"
423 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
424 set executable "${objdir}/$out"
425 if {[info exists opts(no-link)]} {
426 append executable ".o"
429 set target executable
431 if { $compile_args != "" } {
432 set errname "$errname $compile_args"
435 set removeList [list $executable]
437 set x [prune_warnings \
438 [libjava_tcompile $srcfile "$executable" $target $args]]
439 if {[info exists opts(xfail-gcj)]} {
443 verbose "target_compile failed: $x" 2
445 if {[info exists opts(shouldfail)]} {
446 pass "$errname compilation from source"
447 eval gcj_cleanup $removeList
451 fail "$errname compilation from source"
452 if {[info exists opts(xfail-gcj)]
453 || ! [info exists opts(no-exec)]
454 || [info exists opts(need-threads)]} {
455 untested "$errname execution from source compiled test"
456 untested "$errname output from source compiled test"
460 if {[info exists opts(shouldfail)]} {
461 fail "$errname compilation from source"
464 pass "$errname compilation from source"
466 if {[info exists opts(no-exec)]} {
467 if {[info exists opts(need-threads)]} {
468 # This means we wanted to try to run it but we couldn't
469 # because threads aren't supported. So we have to
470 # generate an `untested'.
471 untested "$errname execution from source compiled test"
472 untested "$errname output from source compiled test"
474 eval gcj_cleanup $removeList
477 if {[info exists opts(no-link)]} {
478 eval gcj_cleanup $removeList
482 set result [libjava_load $executable "" "$inpfile"];
483 set status [lindex $result 0];
484 set output [lindex $result 1];
485 if {[info exists opts(xfail-exec)]} then {
488 $status "$errname execution from source compiled test"
489 if { $status != "pass" } {
490 untested "$errname output from source compiled test"
494 verbose "resultfile is $resultfile"
495 set id [open $resultfile r];
497 append expected [read $id];
498 regsub -all "\r" "$output" "" output;
499 regsub "\n*$" $expected "" expected
500 regsub "\n*$" $output "" output
501 regsub "^\n*" $expected "" expected
502 regsub "^\n*" $output "" output
503 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
504 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
505 regsub -all "\[ \t\]\[ \t\]*" $output " " output
506 regsub -all "\[ \t\]*\n\n*" $output "\n" output
507 verbose "expected is $expected"
508 verbose "actual is $output"
510 if {$options == "regexp_match"} {
511 if [regexp $expected $output] {
515 if { $expected == $output } {
519 if {[info exists opts(xfail-output)]} {
522 if {[info exists opts(xfail-source-output)]} {
525 if { $passed == 1 } {
526 pass "$errname output from source compiled test"
527 eval gcj_cleanup $removeList
529 fail "$errname output from source compiled test"
535 # Run the test specified by srcfile and resultfile. compile_args and
536 # exec_args are options telling this proc how to work.
538 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
542 global srcdir subdir objdir
544 global GCJ_UNDER_TEST
548 # Make opts into an array.
551 foreach item $exec_args {
555 # If we need threads and we don't have them then set the `no-exec'
556 # flag. This is case is also handled specially later.
557 if {[info exists opts(need-threads)]} {
558 global libjava_uses_threads
559 if {! $libjava_uses_threads} {
563 set errname [file rootname [file tail $srcfile]]
564 if {! [runtest_file_p $runtests $errname]} {
568 # bytecompile files with Sun's compiler for now.
569 set bc_ok [bytecompile_file $srcfile $objdir]
571 set javac [find_javac]
572 # This is an ugly heuristic but it will have to do.
573 if {[string match *gcj* $javac]} {
578 if {[info exists opts(xfail-$tag)]} {
582 if {[info exists opts(shouldfail)]} {
583 pass "$errname byte compilation"
586 fail "$errname byte compilation"
587 untested "$errname compilation from bytecode"
588 if {! [info exists opts(no-exec)]
589 || [info exists opts(need-threads)]} {
590 untested "$errname execution from bytecode->native test"
591 untested "$errname output from bytecode->native test"
595 if {[info exists opts(shouldfail)]} {
596 fail "$errname byte compilation"
599 pass "$errname byte compilation"
603 # Find name to use for --main, and name of all class files.
604 set jvscan [find_jvscan]
605 verbose "jvscan is $jvscan"
606 set main_name [string trim \
608 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --print-main" "" "" 300] 1]]]
609 verbose "main name is $main_name"
610 set class_out [string trim \
612 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --list-class" "" "" 300] 1]]]
613 verbose "class list is $class_out"
615 if {[string match "*parse error*" $main_name]
616 || [string match "*parse error*" $class_out]} {
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"
626 # Turn "a b" into "a.class b.class".
627 # Also, turn "foo.bar" into "foo/bar.class".
629 foreach file [split [string trim $class_out]] {
630 set file [join [split $file .] /]
631 lappend class_files $objdir/$file.class
634 eval lappend removeList $class_files
636 # Usually it is an error for a test program not to have a `main'
637 # method. However, for no-exec tests it is ok. Treat no-link
639 if {[info exists opts(no-link)]} {
644 if {[info exists opts(no-exec)]} {
647 } elseif {$main_name == ""} {
648 perror "No `main' given in program $errname"
652 lappend largs "additional_flags=--main=$main_name"
653 set executable "${objdir}/$main_name"
658 set args [libjava_arguments $mode]
659 eval lappend args $largs
661 if { $compile_args != "" } {
662 lappend args "additional_flags=$compile_args"
665 if { $compile_args != "" } {
666 set errname "$errname $compile_args"
669 verbose "compilation command = $args" 2
670 # When compiling and not linking, we have to build each .o
671 # separately. We do this because DejaGNU's target_compile won't
672 # accept an empty "destfile" argument when the mode is "compile".
673 if {$mode == "compile"} {
674 foreach c_file $class_files {
675 set executable [file rootname [file tail $c_file]].o
676 set x [prune_warnings \
677 [libjava_tcompile $c_file "$executable" $type $args]]
678 lappend removeList $executable
684 # This is so evil: we de-listify CLASS_FILES so that we can
685 # turn around and quote the `$' in it for the shell. I really
686 # hate DejaGNU. It is so !@#$!@# unpredictable.
688 foreach stupid $class_files {
689 set hack "$hack $stupid"
691 lappend removeList $executable
692 set x [prune_warnings \
693 [libjava_tcompile $hack "$executable" $type $args]]
695 if {[info exists opts(xfail-byte)]} {
699 verbose "target_compile failed: $x" 2
700 fail "$errname compilation from bytecode"
701 if {! [info exists opts(no-exec)]
702 || [info exists opts(need-threads)]} {
703 untested "$errname execution from bytecode->native test"
704 untested "$errname output from bytecode->native test"
708 pass "$errname compilation from bytecode"
710 if {[info exists opts(no-exec)]} {
711 if {[info exists opts(need-threads)]} {
712 untested "$errname execution from bytecode->native test"
713 untested "$errname output from bytecode->native test"
715 eval gcj_cleanup $removeList
719 set result [libjava_load $executable "" "$inpfile"];
720 set status [lindex $result 0];
721 set output [lindex $result 1];
722 if {[info exists opts(xfail-exec)]} {
725 $status "$errname execution from bytecode->native test"
726 if { $status != "pass" } {
727 untested "$errname output from bytecode->native test"
731 verbose "resultfile is $resultfile"
732 set id [open $resultfile r];
734 append expected [read $id];
735 regsub -all "\r" "$output" "" output;
736 regsub "\n*$" $expected "" expected
737 regsub "\n*$" $output "" output
738 regsub "^\n*" $expected "" expected
739 regsub "^\n*" $output "" output
740 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
741 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
742 regsub -all "\[ \t\]\[ \t\]*" $output " " output
743 regsub -all "\[ \t\]*\n\n*" $output "\n" output
744 verbose "expected is $expected"
745 verbose "actual is $output"
747 if {[info exists opts(xfail-output)]} {
750 if {[info exists opts(xfail-byte-output)]} {
753 if {$options == "regexp_match"} {
754 if [regexp $expected $output] {
758 if { $expected == $output } {
762 if { $passed == 1 } {
763 pass "$errname output from bytecode->native test"
764 eval gcj_cleanup $removeList
766 fail "$errname output from bytecode->native test"
772 # Run the test specified by srcfile and resultfile. compile_args and
773 # exec_args are options telling this proc how to work.
774 # `no-link' don't try to link the program
775 # `no-exec' don't try to run the test
776 # `xfail-gcj' compilation from source will fail
777 # `xfail-javac' compilation with javac will fail
778 # `xfail-gcjC' compilation with gcj -C will fail
779 # `shouldfail' compilation from source is supposed to fail
780 # This is different from xfail, which marks a known
781 # failure that we just havne't fixed.
782 # A compilation marked this way should fail with any
784 # `xfail-byte' compilation from bytecode will fail
785 # `xfail-exec' exec will fail
787 # output will be wrong
788 # `xfail-byte-output'
789 # output will be wrong when compiled from bytecode
790 # `xfail-source-output'
791 # output will be wrong when compiled from source code
793 # test relies on thread support
795 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
796 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
797 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
801 # libjava_version -- extract and print the version number of libjavap
803 proc default_libjava_version {} {
806 proc default_libjava_start { } {
809 # On IRIX 6, we have to set variables akin to LD_LIBRARY_PATH, but
810 # called LD_LIBRARYN32_PATH (for the N32 ABI) and LD_LIBRARY64_PATH
811 # (for the 64-bit ABI). The right way to do this would be to modify
812 # unix.exp -- but that's not an option since it's part of DejaGNU
813 # proper, so we do it here, by trickery. We really only need to do
814 # this on IRIX, but it shouldn't hurt to do it anywhere else.
816 proc ${tool}_set_ld_library_path { name element op } {
817 setenv LD_LIBRARYN32_PATH [getenv LD_LIBRARY_PATH]
818 setenv LD_LIBRARY64_PATH [getenv LD_LIBRARY_PATH]
821 trace variable env(LD_LIBRARY_PATH) w ${tool}_set_ld_library_path