1 # Copyright (C) 1998, 1999, 2000 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
196 if [info exists LIBJAVA] {
197 set libjava $LIBJAVA;
199 set libjava [libjava_find_lib libjava gcj]
202 if [info exists LIBGC] {
205 set libgc [libjava_find_lib boehm-gc gcjgc]
208 if [info exists LIBQTHREADS] {
209 set libqthreads $LIBQTHREADS
211 set libqthreads [libjava_find_lib qthreads gcjcoop]
214 if [info exists LIBZ] {
217 set libz [libjava_find_lib zlib zgcj]
220 # FIXME: there's no way to determine whether -lpthread is
221 # required. We should get this info from configure, or it should
222 # just be in the compiler driver.
224 verbose "using LIBJAVA = $libjava" 2
225 verbose "using LIBGC = $libgc" 2
226 verbose "using LIBQTHREADS = $libqthreads" 2
227 verbose "using LIBZ = $libz" 2
230 # Basically we want to build up a colon separated path list from
231 # the value of $libjava.
233 # First strip away any -L arguments.
234 regsub -all -- "-L" $libjava "" ld_library_path
236 # Then remove any -lgcj argument.
237 regsub -all -- " -lgcj.*" $ld_library_path "" ld_library_path
239 # First strip away any -L arguments.
240 regsub -all -- "-L" $libgc $ld_library_path ld_library_path
242 # Then remove any -lgcjgc argument.
243 regsub -all -- " -lgcjgc.*" $ld_library_path "" ld_library_path
245 # That's enough to make things work for the normal case.
246 # If we wanted to handle an arbitrary value of libjava,
247 # then we'd have to do a lot more work.
249 # Set variables the dynamic linker looks at.
250 global original_ld_library_path
251 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
252 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
254 # Set the CLASSPATH environment variable
255 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
257 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
259 global wrapper_file wrap_compile_flags;
260 lappend args "additional_flags=$wrap_compile_flags";
261 lappend args "libs=$wrapper_file";
262 lappend args "libs=$libjava";
263 lappend args "libs=$libgc";
264 lappend args "libs=$libqthreads"
265 lappend args "libs=$libz"
268 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
269 lappend args "libs=${gluefile}"
270 lappend args "ldflags=$wrap_flags"
273 if [info exists TOOL_OPTIONS] {
274 lappend args "additional_flags=$TOOL_OPTIONS"
277 # Search for libtool. We need it to link.
279 set d [absolute $objdir]
280 foreach x {. .. ../.. ../../..} {
281 if {[file exists $d/$x/libtool]} then {
282 # We have to run silently to avoid DejaGNU lossage.
284 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
289 if {! $found_compiler} {
290 # Append -B$objdir/../ so that we find libgcj.spec before it
292 lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
300 # Run the test specified by srcfile and resultfile. compile_args and
301 # exec_args are options telling this proc how to work.
303 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
307 global srcdir subdir objdir
309 global GCJ_UNDER_TEST
313 # Make opts into an array.
316 foreach item $exec_args {
320 set errname [file rootname [file tail $srcfile]]
321 if {! [runtest_file_p $runtests $errname]} {
325 if {[info exists opts(no-link)]} {
330 set args [libjava_arguments $mode]
331 if {! [info exists opts(no-link)]} {
332 # Add the --main flag
333 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
334 if { $compile_args != "" } {
335 lappend args "additional_flags=$compile_args"
339 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
340 set executable "${objdir}/$out"
341 if {[info exists opts(no-link)]} {
342 append executable ".o"
345 set target executable
347 if { $compile_args != "" } {
348 set errname "$errname $compile_args"
351 set x [prune_warnings \
352 [libjava_tcompile $srcfile "$executable" $target $args]]
353 if {[info exists opts(xfail-gcj)]} {
357 verbose "target_compile failed: $x" 2
359 if {[info exists opts(shouldfail)]} {
360 pass "$errname compilation from source"
364 fail "$errname compilation from source"
365 if {[info exists opts(xfail-gcj)] || ! [info exists opts(no-exec)]} {
367 fail "$errname execution from source compiled test"
369 fail "$errname output from source compiled test"
373 if {[info exists opts(shouldfail)]} {
374 fail "$errname compilation from source"
377 pass "$errname compilation from source"
379 if {[info exists opts(no-exec)]
380 || [info exists opts(no-link)]} {
384 set result [libjava_load $executable "" "$inpfile"];
385 set status [lindex $result 0];
386 set output [lindex $result 1];
387 if {[info exists opts(xfail-exec)]} then {
390 $status "$errname execution from source compiled test"
391 if { $status != "pass" } {
393 fail "$errname execution from source compiled test"
397 verbose "resultfile is $resultfile"
398 set id [open $resultfile r];
400 append expected [read $id];
401 regsub -all "\r" "$output" "" output;
402 regsub "\n*$" $expected "" expected
403 regsub "\n*$" $output "" output
404 regsub "^\n*" $expected "" expected
405 regsub "^\n*" $output "" output
406 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
407 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
408 regsub -all "\[ \t\]\[ \t\]*" $output " " output
409 regsub -all "\[ \t\]*\n\n*" $output "\n" output
410 verbose "expected is $expected"
411 verbose "actual is $output"
413 if {$options == "regexp_match"} {
414 if [regexp $expected $output] {
418 if { $expected == $output } {
422 if {[info exists opts(xfail-output)]} {
425 if { $passed == 1 } {
426 pass "$errname output from source compiled test"
428 clone_output "expected was $expected"
429 clone_output "output was $output"
430 fail "$errname output from source compiled test"
436 # Run the test specified by srcfile and resultfile. compile_args and
437 # exec_args are options telling this proc how to work.
439 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
443 global srcdir subdir objdir
445 global GCJ_UNDER_TEST
449 # Make opts into an array.
452 foreach item $exec_args {
456 set errname [file rootname [file tail $srcfile]]
457 if {! [runtest_file_p $runtests $errname]} {
461 # bytecompile files with Sun's compiler for now.
462 set bc_ok [bytecompile_file $srcfile $objdir]
464 set javac [find_javac]
465 # This is an ugly heuristic but it will have to do.
466 if {[string match *gcj* $javac]} {
471 if {[info exists opts(xfail-$tag)]} {
475 if {[info exists opts(shouldfail)]} {
476 pass "$errname byte compilation"
479 fail "$errname byte compilation"
481 fail "$errname compilation from bytecode"
482 if {! [info exists opts(no-exec)]} {
484 fail "$errname execution from bytecode->native test"
486 fail "$errname output from bytecode->native test"
490 if {[info exists opts(shouldfail)]} {
491 fail "$errname byte compilation"
494 pass "$errname byte compilation"
496 # Find name to use for --main, and name of all class files.
497 set jvscan [find_jvscan]
498 verbose "jvscan is $jvscan"
499 set main_name [string trim \
501 [libjava_tcompile $srcfile "" none \
502 "compiler=$jvscan additional_flags=--print-main"]]]
503 verbose "main name is $main_name"
504 set class_out [string trim \
506 [libjava_tcompile $srcfile "" none \
507 "compiler=$jvscan additional_flags=--list-class"]]]
508 verbose "class list is $class_out"
510 if {[string match "*parse error*" $main_name]
511 || [string match "*parse error*" $class_out]} {
512 # Do the remaining fails.
514 fail "$errname compilation from bytecode"
515 if {! [info exists opts(no-exec)]} {
517 fail "$errname execution from bytecode->native test"
519 fail "$errname output from bytecode->native test"
524 # Turn "a b" into "a.class b.class".
525 # Also, turn "foo.bar" into "foo/bar.class".
527 foreach file [split [string trim $class_out]] {
528 set file [join [split $file .] /]
529 lappend class_files $objdir/$file.class
532 # Usually it is an error for a test program not to have a `main'
533 # method. However, for no-exec tests it is ok. Treat no-link
535 if {[info exists opts(no-link)]} {
540 if {[info exists opts(no-exec)]} {
543 } elseif {$main_name == ""} {
544 perror "No `main' given in program $errname"
548 lappend largs "additional_flags=--main=$main_name"
549 set executable "${objdir}/$main_name"
554 set args [libjava_arguments $mode]
555 eval lappend args $largs
557 if { $compile_args != "" } {
558 lappend args "additional_flags=$compile_args"
561 if { $compile_args != "" } {
562 set errname "$errname $compile_args"
565 verbose "compilation command = $args" 2
566 # When compiling and not linking, we have to build each .o
567 # separately. We do this because DejaGNU's target_compile won't
568 # accept an empty "destfile" argument when the mode is "compile".
569 if {$mode == "compile"} {
570 foreach c_file $class_files {
571 set executable [file rootname [file tail $c_file]].o
572 set x [prune_warnings \
573 [libjava_tcompile $c_file "$executable" $type $args]]
579 # This is no evil: we de-listify CLASS_FILES so that we can
580 # turn around and quote the `$' in it for the shell. I really
581 # hate DejaGNU. It is so !@#$!@# unpredictable.
583 foreach stupid $class_files {
584 set hack "$hack $stupid"
586 set x [prune_warnings \
587 [libjava_tcompile $hack "$executable" $type $args]]
589 if {[info exists opts(xfail-byte)]} {
593 verbose "target_compile failed: $x" 2
594 fail "$errname compilation from bytecode"
596 if {! [info exists opts(no-exec)]} {
597 fail "$errname execution from bytecode->native test"
599 fail "$errname output from bytecode->native test"
603 pass "$errname compilation from bytecode"
605 if {[info exists opts(no-exec)]} {
609 set result [libjava_load $executable "" "$inpfile"];
610 set status [lindex $result 0];
611 set output [lindex $result 1];
612 if {[info exists opts(xfail-exec)]} {
615 $status "$errname execution from bytecode->native test"
616 if { $status != "pass" } {
618 fail "$errname output from bytecode->native test"
622 verbose "resultfile is $resultfile"
623 set id [open $resultfile r];
625 append expected [read $id];
626 regsub -all "\r" "$output" "" output;
627 regsub "\n*$" $expected "" expected
628 regsub "\n*$" $output "" output
629 regsub "^\n*" $expected "" expected
630 regsub "^\n*" $output "" output
631 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
632 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
633 regsub -all "\[ \t\]\[ \t\]*" $output " " output
634 regsub -all "\[ \t\]*\n\n*" $output "\n" output
635 verbose "expected is $expected"
636 verbose "actual is $output"
638 if {[info exists opts(xfail-output)]} {
641 if {$options == "regexp_match"} {
642 if [regexp $expected $output] {
646 if { $expected == $output } {
650 if { $passed == 1 } {
651 pass "$errname output from bytecode->native test"
653 clone_output "expected was $expected"
654 clone_output "output was $output"
655 fail "$errname output from bytecode->native test"
661 # Run the test specified by srcfile and resultfile. compile_args and
662 # exec_args are options telling this proc how to work.
663 # `no-link' don't try to link the program
664 # `no-exec' don't try to run the test
665 # `xfail-gcj' compilation from source will fail
666 # `xfail-javac' compilation with javac will fail
667 # `xfail-gcjC' compilation with gcj -C will fail
668 # `shouldfail' compilation from source is supposed to fail
669 # This is different from xfail, which marks a known
670 # failure that we just havne't fixed.
671 # A compilation marked this way should fail with any
673 # `xfail-byte' compilation from bytecode will fail
674 # `xfail-exec' exec will fail
675 # `xfail-output' output will be wrong
677 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
678 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
679 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
683 # libjava_version -- extract and print the version number of libjavap
685 proc default_libjava_version {} {
688 proc default_libjava_start { } {