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 # Read an `xfail' file if it exists. Returns a list of xfail tokens.
14 proc libjava_read_xfail {file} {
15 if {! [file exists $file]} {
19 set tokens [string trim [read $fd]]
24 # Find a particular executable. FIXME: this relies on DejaGnu
25 # internals. These should probably be exposed in a better way.
26 proc libjava_find_program {prog} {
29 set file [lookfor_file $tool_root_dir $prog]
31 set file [lookfor_file $tool_root_dir gcc/$prog];
41 return [libjava_find_program jv-scan]
46 return [libjava_find_program gcjh]
49 proc bytecompile_file { file objdir {classpath {}} } {
52 set dirname [file dirname $file]
54 # If JDK doesn't run on your platform but some other
55 # JDK-compatible javac does, you may set SUN_JAVAC to point to it.
56 # One of the most important properties of a SUN_JAVAC is that it
57 # must create class-files even for classes that have not been
58 # specified in the command line, but that were needed to compile
59 # those that have. For example, Pizza won't do it, but you can
60 # use `kaffe sun.tools.javac.Main', if you have Sun's classes.zip
61 # in the kaffe's default search path.
62 if ![info exists SUN_JAVAC] {
63 if [info exists env(SUN_JAVAC)] {
64 set SUN_JAVAC $env(SUN_JAVAC)
70 catch {unset env(CLASSPATH)}
71 if {$classpath != ""} then {
72 set env(CLASSPATH) $classpath
77 set q [eval exec "$SUN_JAVAC [list $file] -d $objdir 2>@ stdout"]
82 verbose "couldn't compile $file: $msg"
90 set libjava_initialized 0
93 # Build the status wrapper library as needed.
95 proc libjava_init { args } {
97 global wrap_compile_flags;
98 global libjava_initialized
100 global TOOL_EXECUTABLE
101 global original_ld_library_path
104 if { $libjava_initialized == 1 } { return; }
106 if ![info exists GCJ_UNDER_TEST] {
107 if [info exists TOOL_EXECUTABLE] {
108 set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
110 if [info exists env(GCJ)] {
111 set GCJ_UNDER_TEST $env(GCJ)
113 set GCJ_UNDER_TEST "[find_gcj]"
118 if [info exists env(LD_LIBRARY_PATH)] {
119 set original_ld_library_path $env(LD_LIBRARY_PATH)
121 if [info exists env(SHLIB_PATH)] {
122 set original_ld_library_path $env(SHLIB_PATH)
124 set original_ld_library_path ""
129 set wrap_compile_flags "";
130 if [target_info exists needs_status_wrapper] {
131 set result [build_wrapper "testglue.o"];
132 if { $result != "" } {
133 set wrapper_file [lindex $result 0];
134 set wrap_compile_flags [lindex $result 1];
136 warning "Status wrapper failed to build."
140 set libjava_initialized 1
143 # Find a library. We know where libtool puts the actual libraries,
144 # and we look there. The implementation is fairly hacky. We can't
145 # compile with -nodefaultlibs, because that will also eliminate the
146 # system libraries we need. In order to have gcj still work, it must
147 # find the appropriate libraries so we must add -L options for their
148 # paths. However we can't simply use those libraries; we still need
149 # libtool for linking.
150 proc libjava_find_lib {dir name} {
152 set gp [get_multilibs]
153 foreach sub {.libs _libs} {
155 if {[file exists $gp/$dir/$sub/lib${name}.a]} then {
156 return "$gp/$dir/lib${name}.la -L$gp/$dir/$sub"
159 set lib [findfile $base_dir/../../$dir/$sub/lib${name}.a \
160 "$base_dir/../../$dir/lib${name}.la -L$base_dir/../../$dir/$sub" \
169 # Compute arguments needed for compiler. MODE is a libtool mode:
170 # either compile or link.
171 proc libjava_arguments {{mode compile}} {
177 global srcdir subdir objdir
179 global GCJ_UNDER_TEST
184 if [info exists LIBJAVA] {
185 set libjava $LIBJAVA;
187 set libjava [libjava_find_lib libjava gcj]
190 if [info exists LIBGC] {
193 set libgc [libjava_find_lib boehm-gc gcjgc]
196 if [info exists LIBQTHREADS] {
197 set libqthreads $LIBQTHREADS
199 set libqthreads [libjava_find_lib qthreads gcjcoop]
202 if [info exists LIBZ] {
205 set libz [libjava_find_lib zlib zgcj]
208 # FIXME: there's no way to determine whether -lpthread is
209 # required. We should get this info from configure, or it should
210 # just be in the compiler driver.
212 verbose "using LIBJAVA = $libjava" 2
213 verbose "using LIBGC = $libgc" 2
214 verbose "using LIBQTHREADS = $libqthreads" 2
215 verbose "using LIBZ = $libz" 2
218 # Basically we want to build up a colon separated path list from
219 # the value of $libjava.
221 # First strip away any -L arguments.
222 regsub -all -- "-L" $libjava "" ld_library_path
224 # Then remove any -lgcj argument.
225 regsub -all -- " -lgcj.*" $ld_library_path "" ld_library_path
227 # First strip away any -L arguments.
228 regsub -all -- "-L" $libgc $ld_library_path ld_library_path
230 # Then remove any -lgcjgc argument.
231 regsub -all -- " -lgcjgc.*" $ld_library_path "" ld_library_path
233 # That's enough to make things work for the normal case.
234 # If we wanted to handle an arbitrary value of libjava,
235 # then we'd have to do a lot more work.
237 # Set variables the dynamic linker looks at.
238 global original_ld_library_path
239 setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
240 setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
242 # Set the CLASSPATH environment variable
243 verbose "CLASSPATH is .:$srcdir/$subdir:$objdir/..:$objdir/../libgcj.zip"
245 set env(CLASSPATH) ".:$srcdir/$subdir:$objdir/..:$objdir/../libgcj.zip"
247 global wrapper_file wrap_compile_flags;
248 lappend args "additional_flags=$wrap_compile_flags";
249 lappend args "libs=$wrapper_file";
250 lappend args "libs=$libjava";
251 lappend args "libs=$libgc";
252 lappend args "libs=$libqthreads"
253 lappend args "libs=$libz"
256 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
257 lappend args "libs=${gluefile}"
258 lappend args "ldflags=$wrap_flags"
261 if [info exists TOOL_OPTIONS] {
262 lappend args "additional_flags=$TOOL_OPTIONS"
265 # Search for libtool. We need it to link.
267 set d [absolute $objdir]
268 foreach x {. .. ../.. ../../..} {
269 if {[file exists $d/$x/libtool]} then {
270 # We have to run silently to avoid DejaGNU lossage.
272 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
277 if {! $found_compiler} {
278 # Append -B$objdir/../ so that we find libgcj.spec before it
280 lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
288 # Run the test specified by srcfile and resultfile. compile_args and
289 # exec_args are options telling this proc how to work.
291 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
295 global srcdir subdir objdir
297 global GCJ_UNDER_TEST
301 # Make opts into an array.
304 foreach item $exec_args {
308 set errname [file rootname [file tail $srcfile]]
309 if {! [runtest_file_p $runtests $errname]} {
313 if {[info exists opts(no-link)]} {
318 set args [libjava_arguments $mode]
319 if {! [info exists opts(no-link)]} {
320 # Add the --main flag
321 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
322 if { $compile_args != "" } {
323 lappend args "additional_flags=$compile_args"
327 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
328 set executable "${objdir}/$out"
329 if {[info exists opts(no-link)]} {
330 append executable ".o"
333 set target executable
335 if { $compile_args != "" } {
336 set errname "$errname $compile_args"
339 set x [prune_warnings \
340 [target_compile $srcfile "$executable" $target $args]]
341 if {[info exists opts(xfail-gcj)]} {
345 verbose "target_compile failed: $x" 2
347 if {[info exists opts(shouldfail)]} {
348 pass "$errname compilation from source"
352 fail "$errname compilation from source"
353 if {[info exists opts(xfail-gcj)] || ! [info exists opts(no-exec)]} {
355 fail "$errname execution from source compiled test"
357 fail "$errname output from source compiled test"
361 if {[info exists opts(shouldfail)]} {
362 fail "$errname compilation from source"
365 pass "$errname compilation from source"
367 if {[info exists opts(no-exec)]
368 || [info exists opts(no-link)]} {
372 set result [libjava_load $executable "" "$inpfile"];
373 set status [lindex $result 0];
374 set output [lindex $result 1];
375 if {[info exists opts(xfail-exec)]} then {
378 $status "$errname execution from source compiled test"
379 if { $status != "pass" } {
381 fail "$errname execution from source compiled test"
385 verbose "resultfile is $resultfile"
386 set id [open $resultfile r];
388 append expected [read $id];
389 regsub -all "\r" "$output" "" output;
390 regsub "\n*$" $expected "" expected
391 regsub "\n*$" $output "" output
392 regsub "^\n*" $expected "" expected
393 regsub "^\n*" $output "" output
394 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
395 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
396 regsub -all "\[ \t\]\[ \t\]*" $output " " output
397 regsub -all "\[ \t\]*\n\n*" $output "\n" output
398 verbose "expected is $expected"
399 verbose "actual is $output"
401 if {$options == "regexp_match"} {
402 if [regexp $expected $output] {
406 if { $expected == $output } {
410 if {[info exists opts(xfail-output)]} {
413 if { $passed == 1 } {
414 pass "$errname output from source compiled test"
416 clone_output "expected was $expected"
417 clone_output "output was $output"
418 fail "$errname output from source compiled test"
424 # Run the test specified by srcfile and resultfile. compile_args and
425 # exec_args are options telling this proc how to work.
427 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
431 global srcdir subdir objdir
433 global GCJ_UNDER_TEST
437 # Make opts into an array.
440 foreach item $exec_args {
444 set errname [file rootname [file tail $srcfile]]
445 if {! [runtest_file_p $runtests $errname]} {
449 # bytecompile files with Sun's compiler for now.
450 set bc_ok [bytecompile_file $srcfile $objdir]
451 # FIXME: assumes we are using javac to compile to bytecode.
452 # This is not always the case.
453 if {[info exists opts(xfail-javac)]} {
457 if {[info exists opts(shouldfail)]} {
458 pass "$errname byte compilation"
461 fail "$errname byte compilation"
463 fail "$errname compilation from bytecode"
464 if {! [info exists opts(no-exec)]} {
466 fail "$errname execution from bytecode->native test"
468 fail "$errname output from bytecode->native test"
472 if {[info exists opts(shouldfail)]} {
473 fail "$errname byte compilation"
476 pass "$errname byte compilation"
478 # Find name to use for --main, and name of all class files.
479 set jvscan [find_jvscan]
480 verbose "jvscan is $jvscan"
481 set main_name [string trim \
483 [target_compile $srcfile "" none \
484 "compiler=$jvscan additional_flags=--print-main"]]]
485 verbose "main name is $main_name"
486 set class_out [string trim \
488 [target_compile $srcfile "" none \
489 "compiler=$jvscan additional_flags=--list-class"]]]
490 verbose "class list is $class_out"
492 if {[string match "*parse error*" $main_name]
493 || [string match "*parse error*" $class_out]} {
494 # Do the remaining fails.
496 fail "$errname compilation from bytecode"
497 if {! [info exists opts(no-exec)]} {
499 fail "$errname execution from bytecode->native test"
501 fail "$errname output from bytecode->native test"
506 # Turn "a b" into "a.class b.class".
507 # Also, turn "foo.bar" into "foo/bar.class".
509 foreach file [split [string trim $class_out]] {
510 set file [join [split $file .] /]
511 lappend class_files $objdir/$file.class
514 # Usually it is an error for a test program not to have a `main'
515 # method. However, for no-exec tests it is ok. Treat no-link
517 if {[info exists opts(no-link)]} {
521 if {$main_name == ""} {
522 if {! [info exists opts(no-exec)]} {
523 perror "No `main' given in program $errname"
531 lappend largs "additional_flags=--main=$main_name"
532 set executable "${objdir}/$main_name"
537 set args [libjava_arguments $mode]
538 eval lappend args $largs
540 if { $compile_args != "" } {
541 lappend args "additional_flags=$compile_args"
544 if { $compile_args != "" } {
545 set errname "$errname $compile_args"
548 verbose "compilation command = $args" 2
549 # When compiling and not linking, we have to build each .o
550 # separately. We do this because DejaGNU's target_compile won't
551 # accept an empty "destfile" argument when the mode is "compile".
552 if {$mode == "compile"} {
553 foreach c_file $class_files {
554 set executable [file rootname [file tail $c_file]].o
555 set x [prune_warnings \
556 [target_compile $c_file "$executable" $type $args]]
562 set x [prune_warnings \
563 [target_compile $class_files "$executable" $type $args]]
565 if {[info exists opts(xfail-byte)]} {
569 verbose "target_compile failed: $x" 2
570 fail "$errname compilation from bytecode"
572 if {! [info exists opts(no-exec)]} {
573 fail "$errname execution from bytecode->native test"
575 fail "$errname output from bytecode->native test"
579 pass "$errname compilation from bytecode"
581 if {[info exists opts(no-exec)]} {
585 set result [libjava_load $executable "" "$inpfile"];
586 set status [lindex $result 0];
587 set output [lindex $result 1];
588 if {[info exists opts(xfail-exec)]} {
591 $status "$errname execution from bytecode->native test"
592 if { $status != "pass" } {
594 fail "$errname output from bytecode->native test"
598 verbose "resultfile is $resultfile"
599 set id [open $resultfile r];
601 append expected [read $id];
602 regsub -all "\r" "$output" "" output;
603 regsub "\n*$" $expected "" expected
604 regsub "\n*$" $output "" output
605 regsub "^\n*" $expected "" expected
606 regsub "^\n*" $output "" output
607 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
608 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
609 regsub -all "\[ \t\]\[ \t\]*" $output " " output
610 regsub -all "\[ \t\]*\n\n*" $output "\n" output
611 verbose "expected is $expected"
612 verbose "actual is $output"
614 if {[info exists opts(xfail-output)]} {
617 if {$options == "regexp_match"} {
618 if [regexp $expected $output] {
622 if { $expected == $output } {
626 if { $passed == 1 } {
627 pass "$errname output from bytecode->native test"
629 clone_output "expected was $expected"
630 clone_output "output was $output"
631 fail "$errname output from bytecode->native test"
637 # Run the test specified by srcfile and resultfile. compile_args and
638 # exec_args are options telling this proc how to work.
639 # `no-link' don't try to link the program
640 # `no-exec' don't try to run the test
641 # `xfail-gcj' compilation from source will fail
642 # `xfail-javac' compilation with javac will fail
643 # `xfail-gcjC' compilation with gcj -C will fail
644 # `shouldfail' compilation from source is supposed to fail
645 # This is different from xfail, which marks a known
646 # failure that we just havne't fixed.
647 # A compilation marked this way should fail with any
649 # `xfail-byte' compilation from bytecode will fail
650 # `xfail-exec' exec will fail
651 # `xfail-output' output will be wrong
653 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
654 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
655 test_libjava_from_javac $options $srcfile $compile_args $inpfile $resultfile $exec_args
659 # libjava_version -- extract and print the version number of libjavap
661 proc default_libjava_version {} {
664 proc default_libjava_start { } {