1 # Copyright (C) 1998, 1999, 2000, 2001 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
116 if { $libjava_initialized == 1 } { return; }
118 if ![info exists GCJ_UNDER_TEST] {
119 if [info exists TOOL_EXECUTABLE] {
120 set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
122 if [info exists env(GCJ)] {
123 set GCJ_UNDER_TEST $env(GCJ)
125 set GCJ_UNDER_TEST "[find_gcj]"
130 # The -B is so we find libgcj.spec.
131 set text [eval exec "$GCJ_UNDER_TEST -B$objdir/../ -v 2>@ stdout"]
132 regexp -- "Thread model: (\[^\n\]+)\n" $text ignore model
133 set libjava_uses_threads [expr {! ($model == "no"
135 || $model == "single")}]
137 # Always set encoding used by gcj.
138 append GCJ_UNDER_TEST " --encoding=UTF-8"
140 if [info exists env(LD_LIBRARY_PATH)] {
141 set original_ld_library_path $env(LD_LIBRARY_PATH)
143 if [info exists env(SHLIB_PATH)] {
144 set original_ld_library_path $env(SHLIB_PATH)
146 set original_ld_library_path ""
151 set wrap_compile_flags "";
152 if [target_info exists needs_status_wrapper] {
153 set result [build_wrapper "testglue.o"];
154 if { $result != "" } {
155 set wrapper_file [lindex $result 0];
156 set wrap_compile_flags [lindex $result 1];
158 warning "Status wrapper failed to build."
162 set libjava_initialized 1
165 # Find a library. We know where libtool puts the actual libraries,
166 # and we look there. The implementation is fairly hacky. We can't
167 # compile with -nodefaultlibs, because that will also eliminate the
168 # system libraries we need. In order to have gcj still work, it must
169 # find the appropriate libraries so we must add -L options for their
170 # paths. However we can't simply use those libraries; we still need
171 # libtool for linking.
172 proc libjava_find_lib {dir name} {
174 set gp [get_multilibs]
175 foreach sub {.libs _libs} {
177 if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
178 return "$gp/$dir/lib${name}.la -L$gp/$dir/$sub"
181 set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
182 "$base_dir/../../$dir/lib${name}.la -L$base_dir/../../$dir/$sub" \
191 # Remove a bunch of files.
192 proc gcj_cleanup {args} {
194 if {[string match *.o $file]} {
195 verbose "removing [file rootname $file].lo"
196 file delete -force [file rootname $file].lo
198 file delete -force -- $file
199 verbose "removing $file"
201 # It is simplest to do this instead of trying to figure out what
202 # bits in .libs ought to be removed.
203 catch {system "rm -rf .libs"}
206 # Compute arguments needed for compiler. MODE is a libtool mode:
207 # either compile or link.
208 proc libjava_arguments {{mode compile}} {
214 global srcdir subdir objdir
216 global GCJ_UNDER_TEST
222 if [info exists LIBJAVA] {
223 set libjava $LIBJAVA;
225 set libjava [libjava_find_lib libjava gcj]
228 if [info exists LIBGC] {
231 set libgc [libjava_find_lib boehm-gc gcjgc]
234 if [info exists LIBQTHREADS] {
235 set libqthreads $LIBQTHREADS
237 set libqthreads [libjava_find_lib qthreads gcjcoop]
240 if [info exists LIBZ] {
243 set libz [libjava_find_lib zlib zgcj]
246 # FIXME: there's no way to determine whether -lpthread is
247 # required. We should get this info from configure, or it should
248 # just be in the compiler driver.
250 verbose "using LIBJAVA = $libjava" 2
251 verbose "using LIBGC = $libgc" 2
252 verbose "using LIBQTHREADS = $libqthreads" 2
253 verbose "using LIBZ = $libz" 2
256 # Basically we want to build up a colon separated path list from
257 # the value of $libjava.
260 foreach dir [list $libjava $libgc $libz] {
261 foreach item [split $dir " "] {
262 switch -glob -- $item {
264 lappend lpath [string range $item 2 end]
270 # Finally, add the gcc build directory so that we can find the
271 # shared libgcc. This, like much of dejagnu, is hideous.
272 set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.so]
274 lappend lpath [file dirname $gccdir]
277 set ld_library_path [join $lpath :]
279 # That's enough to make things work for the normal case.
280 # If we wanted to handle an arbitrary value of libjava,
281 # then we'd have to do a lot more work.
283 # Set variables the dynamic linker looks at.
284 global original_ld_library_path
285 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
286 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
288 verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
290 # Set the CLASSPATH environment variable
291 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
293 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
295 global wrapper_file wrap_compile_flags;
296 lappend args "additional_flags=$wrap_compile_flags";
297 lappend args "libs=$wrapper_file";
298 lappend args "libs=$libjava";
299 lappend args "libs=$libgc";
300 lappend args "libs=$libqthreads"
301 lappend args "libs=$libz"
304 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
305 lappend args "libs=${gluefile}"
306 lappend args "ldflags=$wrap_flags"
309 if [info exists TOOL_OPTIONS] {
310 lappend args "additional_flags=$TOOL_OPTIONS"
313 # Search for libtool. We need it to link.
315 set d [absolute $objdir]
316 foreach x {. .. ../.. ../../..} {
317 if {[file exists $d/$x/libtool]} then {
318 # We have to run silently to avoid DejaGNU lossage.
320 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
325 if {! $found_compiler} {
326 # Append -B$objdir/../ so that we find libgcj.spec before it
328 lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
331 # Avoid libtool wrapper scripts when possible.
332 if {$mode == "link"} {
333 lappend args "additional_flags=-no-install"
341 # Run the test specified by srcfile and resultfile. compile_args and
342 # exec_args are options telling this proc how to work.
344 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
348 global srcdir subdir objdir
350 global GCJ_UNDER_TEST
354 # Make opts into an array.
357 foreach item $exec_args {
361 # If we need threads and we don't have them then set the `no-exec'
362 # flag. This is case is also handled specially later.
363 if {[info exists opts(need-threads)]} {
364 global libjava_uses_threads
365 if {! $libjava_uses_threads} {
370 set errname [file rootname [file tail $srcfile]]
371 if {! [runtest_file_p $runtests $errname]} {
375 if {[info exists opts(no-link)]} {
380 set args [libjava_arguments $mode]
381 if {! [info exists opts(no-link)]} {
382 # Add the --main flag
383 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
384 if { $compile_args != "" } {
385 lappend args "additional_flags=$compile_args"
389 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
390 set executable "${objdir}/$out"
391 if {[info exists opts(no-link)]} {
392 append executable ".o"
395 set target executable
397 if { $compile_args != "" } {
398 set errname "$errname $compile_args"
401 set removeList [list $executable]
403 set x [prune_warnings \
404 [libjava_tcompile $srcfile "$executable" $target $args]]
405 if {[info exists opts(xfail-gcj)]} {
409 verbose "target_compile failed: $x" 2
411 if {[info exists opts(shouldfail)]} {
412 pass "$errname compilation from source"
413 eval gcj_cleanup $removeList
417 fail "$errname compilation from source"
418 if {[info exists opts(xfail-gcj)]
419 || ! [info exists opts(no-exec)]
420 || [info exists opts(need-threads)]} {
421 untested "$errname execution from source compiled test"
422 untested "$errname output from source compiled test"
426 if {[info exists opts(shouldfail)]} {
427 fail "$errname compilation from source"
430 pass "$errname compilation from source"
432 if {[info exists opts(no-exec)]} {
433 if {[info exists opts(need-threads)]} {
434 # This means we wanted to try to run it but we couldn't
435 # because threads aren't supported. So we have to
436 # generate an `untested'.
437 untested "$errname execution from source compiled test"
438 untested "$errname output from source compiled test"
440 eval gcj_cleanup $removeList
443 if {[info exists opts(no-link)]} {
444 eval gcj_cleanup $removeList
448 set result [libjava_load $executable "" "$inpfile"];
449 set status [lindex $result 0];
450 set output [lindex $result 1];
451 if {[info exists opts(xfail-exec)]} then {
454 $status "$errname execution from source compiled test"
455 if { $status != "pass" } {
456 untested "$errname output from source compiled test"
460 verbose "resultfile is $resultfile"
461 set id [open $resultfile r];
463 append expected [read $id];
464 regsub -all "\r" "$output" "" output;
465 regsub "\n*$" $expected "" expected
466 regsub "\n*$" $output "" output
467 regsub "^\n*" $expected "" expected
468 regsub "^\n*" $output "" output
469 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
470 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
471 regsub -all "\[ \t\]\[ \t\]*" $output " " output
472 regsub -all "\[ \t\]*\n\n*" $output "\n" output
473 verbose "expected is $expected"
474 verbose "actual is $output"
476 if {$options == "regexp_match"} {
477 if [regexp $expected $output] {
481 if { $expected == $output } {
485 if {[info exists opts(xfail-output)]} {
488 if { $passed == 1 } {
489 pass "$errname output from source compiled test"
490 eval gcj_cleanup $removeList
492 clone_output "expected was $expected"
493 clone_output "output was $output"
494 fail "$errname output from source compiled test"
500 # Run the test specified by srcfile and resultfile. compile_args and
501 # exec_args are options telling this proc how to work.
503 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
507 global srcdir subdir objdir
509 global GCJ_UNDER_TEST
513 # Make opts into an array.
516 foreach item $exec_args {
520 # If we need threads and we don't have them then set the `no-exec'
521 # flag. This is case is also handled specially later.
522 if {[info exists opts(need-threads)]} {
523 global libjava_uses_threads
524 if {! $libjava_uses_threads} {
528 set errname [file rootname [file tail $srcfile]]
529 if {! [runtest_file_p $runtests $errname]} {
533 # bytecompile files with Sun's compiler for now.
534 set bc_ok [bytecompile_file $srcfile $objdir]
536 set javac [find_javac]
537 # This is an ugly heuristic but it will have to do.
538 if {[string match *gcj* $javac]} {
543 if {[info exists opts(xfail-$tag)]} {
547 if {[info exists opts(shouldfail)]} {
548 pass "$errname byte compilation"
551 fail "$errname byte compilation"
552 untested "$errname compilation from bytecode"
553 if {! [info exists opts(no-exec)]
554 || [info exists opts(need-threads)]} {
555 untested "$errname execution from bytecode->native test"
556 untested "$errname output from bytecode->native test"
560 if {[info exists opts(shouldfail)]} {
561 fail "$errname byte compilation"
564 pass "$errname byte compilation"
568 # Find name to use for --main, and name of all class files.
569 set jvscan [find_jvscan]
570 verbose "jvscan is $jvscan"
571 set main_name [string trim \
573 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --print-main" "" "" 300] 1]]]
574 verbose "main name is $main_name"
575 set class_out [string trim \
577 [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --list-class" "" "" 300] 1]]]
578 verbose "class list is $class_out"
580 if {[string match "*parse error*" $main_name]
581 || [string match "*parse error*" $class_out]} {
582 untested "$errname compilation from bytecode"
583 if {! [info exists opts(no-exec)]
584 || [info exists opts(need-threads)]} {
585 untested "$errname execution from bytecode->native test"
586 untested "$errname output from bytecode->native test"
591 # Turn "a b" into "a.class b.class".
592 # Also, turn "foo.bar" into "foo/bar.class".
594 foreach file [split [string trim $class_out]] {
595 set file [join [split $file .] /]
596 lappend class_files $objdir/$file.class
599 eval lappend removeList $class_files
601 # Usually it is an error for a test program not to have a `main'
602 # method. However, for no-exec tests it is ok. Treat no-link
604 if {[info exists opts(no-link)]} {
609 if {[info exists opts(no-exec)]} {
612 } elseif {$main_name == ""} {
613 perror "No `main' given in program $errname"
617 lappend largs "additional_flags=--main=$main_name"
618 set executable "${objdir}/$main_name"
623 set args [libjava_arguments $mode]
624 eval lappend args $largs
626 if { $compile_args != "" } {
627 lappend args "additional_flags=$compile_args"
630 if { $compile_args != "" } {
631 set errname "$errname $compile_args"
634 verbose "compilation command = $args" 2
635 # When compiling and not linking, we have to build each .o
636 # separately. We do this because DejaGNU's target_compile won't
637 # accept an empty "destfile" argument when the mode is "compile".
638 if {$mode == "compile"} {
639 foreach c_file $class_files {
640 set executable [file rootname [file tail $c_file]].o
641 set x [prune_warnings \
642 [libjava_tcompile $c_file "$executable" $type $args]]
643 lappend removeList $executable
649 # This is so evil: we de-listify CLASS_FILES so that we can
650 # turn around and quote the `$' in it for the shell. I really
651 # hate DejaGNU. It is so !@#$!@# unpredictable.
653 foreach stupid $class_files {
654 set hack "$hack $stupid"
656 lappend removeList $executable
657 set x [prune_warnings \
658 [libjava_tcompile $hack "$executable" $type $args]]
660 if {[info exists opts(xfail-byte)]} {
664 verbose "target_compile failed: $x" 2
665 fail "$errname compilation from bytecode"
666 if {! [info exists opts(no-exec)]
667 || [info exists opts(need-threads)]} {
668 untested "$errname execution from bytecode->native test"
669 untested "$errname output from bytecode->native test"
673 pass "$errname compilation from bytecode"
675 if {[info exists opts(no-exec)]} {
676 if {[info exists opts(need-threads)]} {
677 untested "$errname execution from bytecode->native test"
678 untested "$errname output from bytecode->native test"
680 eval gcj_cleanup $removeList
684 set result [libjava_load $executable "" "$inpfile"];
685 set status [lindex $result 0];
686 set output [lindex $result 1];
687 if {[info exists opts(xfail-exec)]} {
690 $status "$errname execution from bytecode->native test"
691 if { $status != "pass" } {
692 untested "$errname output from bytecode->native test"
696 verbose "resultfile is $resultfile"
697 set id [open $resultfile r];
699 append expected [read $id];
700 regsub -all "\r" "$output" "" output;
701 regsub "\n*$" $expected "" expected
702 regsub "\n*$" $output "" output
703 regsub "^\n*" $expected "" expected
704 regsub "^\n*" $output "" output
705 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
706 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
707 regsub -all "\[ \t\]\[ \t\]*" $output " " output
708 regsub -all "\[ \t\]*\n\n*" $output "\n" output
709 verbose "expected is $expected"
710 verbose "actual is $output"
712 if {[info exists opts(xfail-output)]} {
715 if {$options == "regexp_match"} {
716 if [regexp $expected $output] {
720 if { $expected == $output } {
724 if { $passed == 1 } {
725 pass "$errname output from bytecode->native test"
726 eval gcj_cleanup $removeList
728 clone_output "expected was $expected"
729 clone_output "output was $output"
730 fail "$errname output from bytecode->native test"
736 # Run the test specified by srcfile and resultfile. compile_args and
737 # exec_args are options telling this proc how to work.
738 # `no-link' don't try to link the program
739 # `no-exec' don't try to run the test
740 # `xfail-gcj' compilation from source will fail
741 # `xfail-javac' compilation with javac will fail
742 # `xfail-gcjC' compilation with gcj -C will fail
743 # `shouldfail' compilation from source is supposed to fail
744 # This is different from xfail, which marks a known
745 # failure that we just havne't fixed.
746 # A compilation marked this way should fail with any
748 # `xfail-byte' compilation from bytecode will fail
749 # `xfail-exec' exec will fail
751 # output will be wrong
753 # test relies on thread support
755 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
756 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
757 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
761 # libjava_version -- extract and print the version number of libjavap
763 proc default_libjava_version {} {
766 proc default_libjava_start { } {