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
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 if [info exists env(LD_LIBRARY_PATH)] {
131 set original_ld_library_path $env(LD_LIBRARY_PATH)
133 if [info exists env(SHLIB_PATH)] {
134 set original_ld_library_path $env(SHLIB_PATH)
136 set original_ld_library_path ""
141 set wrap_compile_flags "";
142 if [target_info exists needs_status_wrapper] {
143 set result [build_wrapper "testglue.o"];
144 if { $result != "" } {
145 set wrapper_file [lindex $result 0];
146 set wrap_compile_flags [lindex $result 1];
148 warning "Status wrapper failed to build."
152 set libjava_initialized 1
155 # Find a library. We know where libtool puts the actual libraries,
156 # and we look there. The implementation is fairly hacky. We can't
157 # compile with -nodefaultlibs, because that will also eliminate the
158 # system libraries we need. In order to have gcj still work, it must
159 # find the appropriate libraries so we must add -L options for their
160 # paths. However we can't simply use those libraries; we still need
161 # libtool for linking.
162 proc libjava_find_lib {dir name} {
164 set gp [get_multilibs]
165 foreach sub {.libs _libs} {
167 if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
168 return "$gp/$dir/lib${name}.la -L$gp/$dir/$sub"
171 set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
172 "$base_dir/../../$dir/lib${name}.la -L$base_dir/../../$dir/$sub" \
181 # Compute arguments needed for compiler. MODE is a libtool mode:
182 # either compile or link.
183 proc libjava_arguments {{mode compile}} {
189 global srcdir subdir objdir
191 global GCJ_UNDER_TEST
197 if [info exists LIBJAVA] {
198 set libjava $LIBJAVA;
200 set libjava [libjava_find_lib libjava gcj]
203 if [info exists LIBGC] {
206 set libgc [libjava_find_lib boehm-gc gcjgc]
209 if [info exists LIBQTHREADS] {
210 set libqthreads $LIBQTHREADS
212 set libqthreads [libjava_find_lib qthreads gcjcoop]
215 if [info exists LIBZ] {
218 set libz [libjava_find_lib zlib zgcj]
221 # FIXME: there's no way to determine whether -lpthread is
222 # required. We should get this info from configure, or it should
223 # just be in the compiler driver.
225 verbose "using LIBJAVA = $libjava" 2
226 verbose "using LIBGC = $libgc" 2
227 verbose "using LIBQTHREADS = $libqthreads" 2
228 verbose "using LIBZ = $libz" 2
231 # Basically we want to build up a colon separated path list from
232 # the value of $libjava.
235 foreach dir [list $libjava $libgc $libz] {
236 foreach item [split $dir " "] {
237 switch -glob -- $item {
239 lappend lpath [string range $item 2 end]
245 # Finally, add the gcc build directory so that we can find the
246 # shared libgcc. This, like much of dejagnu, is hideous.
247 set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.so]
249 lappend lpath [file dirname $gccdir]
252 set ld_library_path [join $lpath :]
254 # That's enough to make things work for the normal case.
255 # If we wanted to handle an arbitrary value of libjava,
256 # then we'd have to do a lot more work.
258 # Set variables the dynamic linker looks at.
259 global original_ld_library_path
260 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
261 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
263 verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
265 # Set the CLASSPATH environment variable
266 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
268 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
270 global wrapper_file wrap_compile_flags;
271 lappend args "additional_flags=$wrap_compile_flags";
272 lappend args "libs=$wrapper_file";
273 lappend args "libs=$libjava";
274 lappend args "libs=$libgc";
275 lappend args "libs=$libqthreads"
276 lappend args "libs=$libz"
279 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
280 lappend args "libs=${gluefile}"
281 lappend args "ldflags=$wrap_flags"
284 if [info exists TOOL_OPTIONS] {
285 lappend args "additional_flags=$TOOL_OPTIONS"
288 # Search for libtool. We need it to link.
290 set d [absolute $objdir]
291 foreach x {. .. ../.. ../../..} {
292 if {[file exists $d/$x/libtool]} then {
293 # We have to run silently to avoid DejaGNU lossage.
295 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
300 if {! $found_compiler} {
301 # Append -B$objdir/../ so that we find libgcj.spec before it
303 lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
311 # Run the test specified by srcfile and resultfile. compile_args and
312 # exec_args are options telling this proc how to work.
314 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
318 global srcdir subdir objdir
320 global GCJ_UNDER_TEST
324 # Make opts into an array.
327 foreach item $exec_args {
331 set errname [file rootname [file tail $srcfile]]
332 if {! [runtest_file_p $runtests $errname]} {
336 if {[info exists opts(no-link)]} {
341 set args [libjava_arguments $mode]
342 if {! [info exists opts(no-link)]} {
343 # Add the --main flag
344 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
345 if { $compile_args != "" } {
346 lappend args "additional_flags=$compile_args"
350 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
351 set executable "${objdir}/$out"
352 if {[info exists opts(no-link)]} {
353 append executable ".o"
356 set target executable
358 if { $compile_args != "" } {
359 set errname "$errname $compile_args"
362 set x [prune_warnings \
363 [libjava_tcompile $srcfile "$executable" $target $args]]
364 if {[info exists opts(xfail-gcj)]} {
368 verbose "target_compile failed: $x" 2
370 if {[info exists opts(shouldfail)]} {
371 pass "$errname compilation from source"
375 fail "$errname compilation from source"
376 if {[info exists opts(xfail-gcj)] || ! [info exists opts(no-exec)]} {
378 fail "$errname execution from source compiled test"
380 fail "$errname output from source compiled test"
384 if {[info exists opts(shouldfail)]} {
385 fail "$errname compilation from source"
388 pass "$errname compilation from source"
390 if {[info exists opts(no-exec)]
391 || [info exists opts(no-link)]} {
395 set result [libjava_load $executable "" "$inpfile"];
396 set status [lindex $result 0];
397 set output [lindex $result 1];
398 if {[info exists opts(xfail-exec)]} then {
401 $status "$errname execution from source compiled test"
402 if { $status != "pass" } {
404 fail "$errname execution from source compiled test"
408 verbose "resultfile is $resultfile"
409 set id [open $resultfile r];
411 append expected [read $id];
412 regsub -all "\r" "$output" "" output;
413 regsub "\n*$" $expected "" expected
414 regsub "\n*$" $output "" output
415 regsub "^\n*" $expected "" expected
416 regsub "^\n*" $output "" output
417 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
418 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
419 regsub -all "\[ \t\]\[ \t\]*" $output " " output
420 regsub -all "\[ \t\]*\n\n*" $output "\n" output
421 verbose "expected is $expected"
422 verbose "actual is $output"
424 if {$options == "regexp_match"} {
425 if [regexp $expected $output] {
429 if { $expected == $output } {
433 if {[info exists opts(xfail-output)]} {
436 if { $passed == 1 } {
437 pass "$errname output from source compiled test"
439 clone_output "expected was $expected"
440 clone_output "output was $output"
441 fail "$errname output from source compiled test"
447 # Run the test specified by srcfile and resultfile. compile_args and
448 # exec_args are options telling this proc how to work.
450 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
454 global srcdir subdir objdir
456 global GCJ_UNDER_TEST
460 # Make opts into an array.
463 foreach item $exec_args {
467 set errname [file rootname [file tail $srcfile]]
468 if {! [runtest_file_p $runtests $errname]} {
472 # bytecompile files with Sun's compiler for now.
473 set bc_ok [bytecompile_file $srcfile $objdir]
475 set javac [find_javac]
476 # This is an ugly heuristic but it will have to do.
477 if {[string match *gcj* $javac]} {
482 if {[info exists opts(xfail-$tag)]} {
486 if {[info exists opts(shouldfail)]} {
487 pass "$errname byte compilation"
490 fail "$errname byte compilation"
492 fail "$errname compilation from bytecode"
493 if {! [info exists opts(no-exec)]} {
495 fail "$errname execution from bytecode->native test"
497 fail "$errname output from bytecode->native test"
501 if {[info exists opts(shouldfail)]} {
502 fail "$errname byte compilation"
505 pass "$errname byte compilation"
507 # Find name to use for --main, and name of all class files.
508 set jvscan [find_jvscan]
509 verbose "jvscan is $jvscan"
510 set main_name [string trim \
512 [libjava_tcompile $srcfile "" none \
513 "compiler=$jvscan additional_flags=--print-main"]]]
514 verbose "main name is $main_name"
515 set class_out [string trim \
517 [libjava_tcompile $srcfile "" none \
518 "compiler=$jvscan additional_flags=--list-class"]]]
519 verbose "class list is $class_out"
521 if {[string match "*parse error*" $main_name]
522 || [string match "*parse error*" $class_out]} {
523 # Do the remaining fails.
525 fail "$errname compilation from bytecode"
526 if {! [info exists opts(no-exec)]} {
528 fail "$errname execution from bytecode->native test"
530 fail "$errname output from bytecode->native test"
535 # Turn "a b" into "a.class b.class".
536 # Also, turn "foo.bar" into "foo/bar.class".
538 foreach file [split [string trim $class_out]] {
539 set file [join [split $file .] /]
540 lappend class_files $objdir/$file.class
543 # Usually it is an error for a test program not to have a `main'
544 # method. However, for no-exec tests it is ok. Treat no-link
546 if {[info exists opts(no-link)]} {
551 if {[info exists opts(no-exec)]} {
554 } elseif {$main_name == ""} {
555 perror "No `main' given in program $errname"
559 lappend largs "additional_flags=--main=$main_name"
560 set executable "${objdir}/$main_name"
565 set args [libjava_arguments $mode]
566 eval lappend args $largs
568 if { $compile_args != "" } {
569 lappend args "additional_flags=$compile_args"
572 if { $compile_args != "" } {
573 set errname "$errname $compile_args"
576 verbose "compilation command = $args" 2
577 # When compiling and not linking, we have to build each .o
578 # separately. We do this because DejaGNU's target_compile won't
579 # accept an empty "destfile" argument when the mode is "compile".
580 if {$mode == "compile"} {
581 foreach c_file $class_files {
582 set executable [file rootname [file tail $c_file]].o
583 set x [prune_warnings \
584 [libjava_tcompile $c_file "$executable" $type $args]]
590 # This is no evil: we de-listify CLASS_FILES so that we can
591 # turn around and quote the `$' in it for the shell. I really
592 # hate DejaGNU. It is so !@#$!@# unpredictable.
594 foreach stupid $class_files {
595 set hack "$hack $stupid"
597 set x [prune_warnings \
598 [libjava_tcompile $hack "$executable" $type $args]]
600 if {[info exists opts(xfail-byte)]} {
604 verbose "target_compile failed: $x" 2
605 fail "$errname compilation from bytecode"
607 if {! [info exists opts(no-exec)]} {
608 fail "$errname execution from bytecode->native test"
610 fail "$errname output from bytecode->native test"
614 pass "$errname compilation from bytecode"
616 if {[info exists opts(no-exec)]} {
620 set result [libjava_load $executable "" "$inpfile"];
621 set status [lindex $result 0];
622 set output [lindex $result 1];
623 if {[info exists opts(xfail-exec)]} {
626 $status "$errname execution from bytecode->native test"
627 if { $status != "pass" } {
629 fail "$errname output from bytecode->native test"
633 verbose "resultfile is $resultfile"
634 set id [open $resultfile r];
636 append expected [read $id];
637 regsub -all "\r" "$output" "" output;
638 regsub "\n*$" $expected "" expected
639 regsub "\n*$" $output "" output
640 regsub "^\n*" $expected "" expected
641 regsub "^\n*" $output "" output
642 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
643 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
644 regsub -all "\[ \t\]\[ \t\]*" $output " " output
645 regsub -all "\[ \t\]*\n\n*" $output "\n" output
646 verbose "expected is $expected"
647 verbose "actual is $output"
649 if {[info exists opts(xfail-output)]} {
652 if {$options == "regexp_match"} {
653 if [regexp $expected $output] {
657 if { $expected == $output } {
661 if { $passed == 1 } {
662 pass "$errname output from bytecode->native test"
664 clone_output "expected was $expected"
665 clone_output "output was $output"
666 fail "$errname output from bytecode->native test"
672 # Run the test specified by srcfile and resultfile. compile_args and
673 # exec_args are options telling this proc how to work.
674 # `no-link' don't try to link the program
675 # `no-exec' don't try to run the test
676 # `xfail-gcj' compilation from source will fail
677 # `xfail-javac' compilation with javac will fail
678 # `xfail-gcjC' compilation with gcj -C will fail
679 # `shouldfail' compilation from source is supposed to fail
680 # This is different from xfail, which marks a known
681 # failure that we just havne't fixed.
682 # A compilation marked this way should fail with any
684 # `xfail-byte' compilation from bytecode will fail
685 # `xfail-exec' exec will fail
686 # `xfail-output' output will be wrong
688 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
689 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
690 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
694 # libjava_version -- extract and print the version number of libjavap
696 proc default_libjava_version {} {
699 proc default_libjava_start { } {