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 catch {unset env(CLASSPATH)}
86 if {$classpath != ""} then {
87 set env(CLASSPATH) $classpath
92 set q [eval exec "$javac [list $file] -d $objdir 2>@ stdout"]
94 verbose "couldn't compile $file: $msg"
103 set libjava_initialized 0
106 # Build the status wrapper library as needed.
108 proc libjava_init { args } {
110 global wrap_compile_flags;
111 global libjava_initialized
112 global GCJ_UNDER_TEST
113 global TOOL_EXECUTABLE
114 global original_ld_library_path
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 if [info exists env(LD_LIBRARY_PATH)] {
132 set original_ld_library_path $env(LD_LIBRARY_PATH)
134 if [info exists env(SHLIB_PATH)] {
135 set original_ld_library_path $env(SHLIB_PATH)
137 set original_ld_library_path ""
142 set wrap_compile_flags "";
143 if [target_info exists needs_status_wrapper] {
144 set result [build_wrapper "testglue.o"];
145 if { $result != "" } {
146 set wrapper_file [lindex $result 0];
147 set wrap_compile_flags [lindex $result 1];
149 warning "Status wrapper failed to build."
153 set libjava_initialized 1
156 # Find a library. We know where libtool puts the actual libraries,
157 # and we look there. The implementation is fairly hacky. We can't
158 # compile with -nodefaultlibs, because that will also eliminate the
159 # system libraries we need. In order to have gcj still work, it must
160 # find the appropriate libraries so we must add -L options for their
161 # paths. However we can't simply use those libraries; we still need
162 # libtool for linking.
163 proc libjava_find_lib {dir name} {
165 set gp [get_multilibs]
166 foreach sub {.libs _libs} {
168 if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
169 return "$gp/$dir/lib${name}.la -L$gp/$dir/$sub"
172 set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
173 "$base_dir/../../$dir/lib${name}.la -L$base_dir/../../$dir/$sub" \
182 # Compute arguments needed for compiler. MODE is a libtool mode:
183 # either compile or link.
184 proc libjava_arguments {{mode compile}} {
190 global srcdir subdir objdir
192 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.
234 # First strip away any -L arguments.
235 regsub -all -- "-L" $libjava "" ld_library_path
237 # Then remove any -lgcj argument.
238 regsub -all -- " -lgcj.*" $ld_library_path "" ld_library_path
240 # First strip away any -L arguments.
241 regsub -all -- "-L" $libgc $ld_library_path ld_library_path
243 # Then remove any -lgcjgc argument.
244 regsub -all -- " -lgcjgc.*" $ld_library_path "" ld_library_path
246 # That's enough to make things work for the normal case.
247 # If we wanted to handle an arbitrary value of libjava,
248 # then we'd have to do a lot more work.
250 # Set variables the dynamic linker looks at.
251 global original_ld_library_path
252 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
253 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
255 # Set the CLASSPATH environment variable
256 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$objdir/../libgcj.zip"
258 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$objdir/../libgcj.zip"
260 global wrapper_file wrap_compile_flags;
261 lappend args "additional_flags=$wrap_compile_flags";
262 lappend args "libs=$wrapper_file";
263 lappend args "libs=$libjava";
264 lappend args "libs=$libgc";
265 lappend args "libs=$libqthreads"
266 lappend args "libs=$libz"
269 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
270 lappend args "libs=${gluefile}"
271 lappend args "ldflags=$wrap_flags"
274 if [info exists TOOL_OPTIONS] {
275 lappend args "additional_flags=$TOOL_OPTIONS"
278 # Search for libtool. We need it to link.
280 set d [absolute $objdir]
281 foreach x {. .. ../.. ../../..} {
282 if {[file exists $d/$x/libtool]} then {
283 # We have to run silently to avoid DejaGNU lossage.
285 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
290 if {! $found_compiler} {
291 # Append -B$objdir/../ so that we find libgcj.spec before it
293 lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
301 # Run the test specified by srcfile and resultfile. compile_args and
302 # exec_args are options telling this proc how to work.
304 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
308 global srcdir subdir objdir
310 global GCJ_UNDER_TEST
314 # Make opts into an array.
317 foreach item $exec_args {
321 set errname [file rootname [file tail $srcfile]]
322 if {! [runtest_file_p $runtests $errname]} {
326 if {[info exists opts(no-link)]} {
331 set args [libjava_arguments $mode]
332 if {! [info exists opts(no-link)]} {
333 # Add the --main flag
334 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
335 if { $compile_args != "" } {
336 lappend args "additional_flags=$compile_args"
340 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
341 set executable "${objdir}/$out"
342 if {[info exists opts(no-link)]} {
343 append executable ".o"
346 set target executable
348 if { $compile_args != "" } {
349 set errname "$errname $compile_args"
352 set x [prune_warnings \
353 [libjava_tcompile $srcfile "$executable" $target $args]]
354 if {[info exists opts(xfail-gcj)]} {
358 verbose "target_compile failed: $x" 2
360 if {[info exists opts(shouldfail)]} {
361 pass "$errname compilation from source"
365 fail "$errname compilation from source"
366 if {[info exists opts(xfail-gcj)] || ! [info exists opts(no-exec)]} {
368 fail "$errname execution from source compiled test"
370 fail "$errname output from source compiled test"
374 if {[info exists opts(shouldfail)]} {
375 fail "$errname compilation from source"
378 pass "$errname compilation from source"
380 if {[info exists opts(no-exec)]
381 || [info exists opts(no-link)]} {
385 set result [libjava_load $executable "" "$inpfile"];
386 set status [lindex $result 0];
387 set output [lindex $result 1];
388 if {[info exists opts(xfail-exec)]} then {
391 $status "$errname execution from source compiled test"
392 if { $status != "pass" } {
394 fail "$errname execution from source compiled test"
398 verbose "resultfile is $resultfile"
399 set id [open $resultfile r];
401 append expected [read $id];
402 regsub -all "\r" "$output" "" output;
403 regsub "\n*$" $expected "" expected
404 regsub "\n*$" $output "" output
405 regsub "^\n*" $expected "" expected
406 regsub "^\n*" $output "" output
407 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
408 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
409 regsub -all "\[ \t\]\[ \t\]*" $output " " output
410 regsub -all "\[ \t\]*\n\n*" $output "\n" output
411 verbose "expected is $expected"
412 verbose "actual is $output"
414 if {$options == "regexp_match"} {
415 if [regexp $expected $output] {
419 if { $expected == $output } {
423 if {[info exists opts(xfail-output)]} {
426 if { $passed == 1 } {
427 pass "$errname output from source compiled test"
429 clone_output "expected was $expected"
430 clone_output "output was $output"
431 fail "$errname output from source compiled test"
437 # Run the test specified by srcfile and resultfile. compile_args and
438 # exec_args are options telling this proc how to work.
440 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
444 global srcdir subdir objdir
446 global GCJ_UNDER_TEST
450 # Make opts into an array.
453 foreach item $exec_args {
457 set errname [file rootname [file tail $srcfile]]
458 if {! [runtest_file_p $runtests $errname]} {
462 # bytecompile files with Sun's compiler for now.
463 set bc_ok [bytecompile_file $srcfile $objdir]
465 set javac [find_javac]
466 # This is an ugly heuristic but it will have to do.
467 if {[string match *gcj* $javac]} {
472 if {[info exists opts(xfail-$tag)]} {
476 if {[info exists opts(shouldfail)]} {
477 pass "$errname byte compilation"
480 fail "$errname byte compilation"
482 fail "$errname compilation from bytecode"
483 if {! [info exists opts(no-exec)]} {
485 fail "$errname execution from bytecode->native test"
487 fail "$errname output from bytecode->native test"
491 if {[info exists opts(shouldfail)]} {
492 fail "$errname byte compilation"
495 pass "$errname byte compilation"
497 # Find name to use for --main, and name of all class files.
498 set jvscan [find_jvscan]
499 verbose "jvscan is $jvscan"
500 set main_name [string trim \
502 [libjava_tcompile $srcfile "" none \
503 "compiler=$jvscan additional_flags=--print-main"]]]
504 verbose "main name is $main_name"
505 set class_out [string trim \
507 [libjava_tcompile $srcfile "" none \
508 "compiler=$jvscan additional_flags=--list-class"]]]
509 verbose "class list is $class_out"
511 if {[string match "*parse error*" $main_name]
512 || [string match "*parse error*" $class_out]} {
513 # Do the remaining fails.
515 fail "$errname compilation from bytecode"
516 if {! [info exists opts(no-exec)]} {
518 fail "$errname execution from bytecode->native test"
520 fail "$errname output from bytecode->native test"
525 # Turn "a b" into "a.class b.class".
526 # Also, turn "foo.bar" into "foo/bar.class".
528 foreach file [split [string trim $class_out]] {
529 set file [join [split $file .] /]
530 lappend class_files $objdir/$file.class
533 # Usually it is an error for a test program not to have a `main'
534 # method. However, for no-exec tests it is ok. Treat no-link
536 if {[info exists opts(no-link)]} {
541 if {[info exists opts(no-exec)]} {
544 } elseif {$main_name == ""} {
545 perror "No `main' given in program $errname"
549 lappend largs "additional_flags=--main=$main_name"
550 set executable "${objdir}/$main_name"
555 set args [libjava_arguments $mode]
556 eval lappend args $largs
558 if { $compile_args != "" } {
559 lappend args "additional_flags=$compile_args"
562 if { $compile_args != "" } {
563 set errname "$errname $compile_args"
566 verbose "compilation command = $args" 2
567 # When compiling and not linking, we have to build each .o
568 # separately. We do this because DejaGNU's target_compile won't
569 # accept an empty "destfile" argument when the mode is "compile".
570 if {$mode == "compile"} {
571 foreach c_file $class_files {
572 set executable [file rootname [file tail $c_file]].o
573 set x [prune_warnings \
574 [libjava_tcompile $c_file "$executable" $type $args]]
580 # This is no evil: we de-listify CLASS_FILES so that we can
581 # turn around and quote the `$' in it for the shell. I really
582 # hate DejaGNU. It is so !@#$!@# unpredictable.
584 foreach stupid $class_files {
585 set hack "$hack $stupid"
587 set x [prune_warnings \
588 [libjava_tcompile $hack "$executable" $type $args]]
590 if {[info exists opts(xfail-byte)]} {
594 verbose "target_compile failed: $x" 2
595 fail "$errname compilation from bytecode"
597 if {! [info exists opts(no-exec)]} {
598 fail "$errname execution from bytecode->native test"
600 fail "$errname output from bytecode->native test"
604 pass "$errname compilation from bytecode"
606 if {[info exists opts(no-exec)]} {
610 set result [libjava_load $executable "" "$inpfile"];
611 set status [lindex $result 0];
612 set output [lindex $result 1];
613 if {[info exists opts(xfail-exec)]} {
616 $status "$errname execution from bytecode->native test"
617 if { $status != "pass" } {
619 fail "$errname output from bytecode->native test"
623 verbose "resultfile is $resultfile"
624 set id [open $resultfile r];
626 append expected [read $id];
627 regsub -all "\r" "$output" "" output;
628 regsub "\n*$" $expected "" expected
629 regsub "\n*$" $output "" output
630 regsub "^\n*" $expected "" expected
631 regsub "^\n*" $output "" output
632 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
633 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
634 regsub -all "\[ \t\]\[ \t\]*" $output " " output
635 regsub -all "\[ \t\]*\n\n*" $output "\n" output
636 verbose "expected is $expected"
637 verbose "actual is $output"
639 if {[info exists opts(xfail-output)]} {
642 if {$options == "regexp_match"} {
643 if [regexp $expected $output] {
647 if { $expected == $output } {
651 if { $passed == 1 } {
652 pass "$errname output from bytecode->native test"
654 clone_output "expected was $expected"
655 clone_output "output was $output"
656 fail "$errname output from bytecode->native test"
662 # Run the test specified by srcfile and resultfile. compile_args and
663 # exec_args are options telling this proc how to work.
664 # `no-link' don't try to link the program
665 # `no-exec' don't try to run the test
666 # `xfail-gcj' compilation from source will fail
667 # `xfail-javac' compilation with javac will fail
668 # `xfail-gcjC' compilation with gcj -C will fail
669 # `shouldfail' compilation from source is supposed to fail
670 # This is different from xfail, which marks a known
671 # failure that we just havne't fixed.
672 # A compilation marked this way should fail with any
674 # `xfail-byte' compilation from bytecode will fail
675 # `xfail-exec' exec will fail
676 # `xfail-output' output will be wrong
678 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
679 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
680 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
684 # libjava_version -- extract and print the version number of libjavap
686 proc default_libjava_version {} {
689 proc default_libjava_start { } {