1 # Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007 Free Software Foundation
3 proc load_gcc_lib { filename } {
5 load_file $srcdir/../../gcc/testsuite/lib/$filename
9 load_gcc_lib target-libpath.exp
11 # GCJ_UNDER_TEST is the compiler under test.
15 if ![info exists tmpdir] {
19 # This is like `prune_warnings', but it also prunes away a warning
20 # from the bytecode front end that we don't care about.
21 proc libjava_prune_warnings {text} {
22 set text [prune_warnings $text]
23 set tlist [split $text \n]
24 set len [llength $tlist]
25 for {set i [expr {$len - 1}]} {$i >= 2} {incr i -1} {
26 if {[string match "*unreachable bytecode*" [lindex $tlist $i]]} {
27 # Delete this line, all other unreachable warnings and the previous
28 # two lines containing the method and class.
30 while {[string match "*unreachable bytecode*" [lindex $tlist $j]]} {
34 set tlist [lreplace $tlist $j $i]
38 return [join $tlist \n]
41 # This is like `target_compile' but it does some surgery to work
42 # around stupid DejaGNU bugs. In particular DejaGNU has very poor
43 # quoting, so for instance a `$' will be re-evaluated at spawn time.
45 proc libjava_tcompile {source destfile type options} {
46 # This strange-looking expression really does quote the `$'.
47 regsub -all -- {\$} $source {\$} source
48 regsub -all -- {\$} $destfile {\$} destfile
49 return [target_compile $source $destfile $type $options]
52 # Read an `xfail' file if it exists. Returns a list of xfail tokens.
53 proc libjava_read_xfail {file} {
54 if {! [file exists $file]} {
58 set tokens [string trim [read $fd]]
63 # Find a particular executable. FIXME: this relies on DejaGnu
64 # internals. These should probably be exposed in a better way.
65 proc libjava_find_program {prog} {
68 set file [lookfor_file $tool_root_dir $prog]
70 set file [lookfor_file $tool_root_dir gcc/$prog];
80 return [libjava_find_program gjavah]
84 global SUN_JAVAC GCJ_UNDER_TEST env libgcj_jar
85 # If JDK doesn't run on your platform but some other
86 # JDK-compatible javac does, you may set SUN_JAVAC to point to it.
87 # One of the most important properties of a SUN_JAVAC is that it
88 # must create class-files even for classes that have not been
89 # specified in the command line, but that were needed to compile
90 # those that have. For example, Pizza won't do it, but you can
91 # use `kaffe sun.tools.javac.Main', if you have Sun's classes.zip
92 # in the kaffe's default search path.
93 if {![info exists SUN_JAVAC]} {
94 if {[info exists env(SUN_JAVAC)]} {
95 set SUN_JAVAC $env(SUN_JAVAC)
97 set SUN_JAVAC "$GCJ_UNDER_TEST -C -I$libgcj_jar"
103 proc bytecompile_file { file objdir {classpath {}} } {
105 set dirname [file dirname $file]
107 set javac [find_javac]
108 if {$classpath != ""} then {
109 set env(CLASSPATH) $classpath
113 send_log "byte compile: $javac -g [list $file] -d $objdir 2>@ stdout\n"
115 set q [eval exec "$javac -g [list $file] -d $objdir 2>@ stdout"]
117 send_log "couldn't compile $file: $msg\n"
126 set libjava_initialized 0
129 # Build the status wrapper library as needed.
131 proc libjava_init { args } {
133 global wrap_compile_flags;
134 global libjava_initialized libjava_uses_threads
135 global GCJ_UNDER_TEST
136 global TOOL_EXECUTABLE
138 global env libgcj_jar
140 global libjava_libgcc_s_path
141 global target_triplet
142 global libjava_version
144 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
148 if { $libjava_initialized == 1 } { return; }
150 if ![info exists GCJ_UNDER_TEST] {
151 if [info exists TOOL_EXECUTABLE] {
152 set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
154 if [info exists env(GCJ)] {
155 set GCJ_UNDER_TEST $env(GCJ)
157 set GCJ_UNDER_TEST "[find_gcj]"
162 # Find the libgcj jar file.
164 # FIXME: This finds libgcj.spec for the default multilib.
165 # If thread models differ between multilibs, this has to be moved
166 # to libjava_arguments
167 set specdir [libjava_find_spec]
169 set text [eval exec "$GCJ_UNDER_TEST -B$specdir -v 2>@ stdout"]
170 regexp " version \[^\n\r\]*" $text version
171 set libjava_version [lindex $version 1]
173 verbose "version: $libjava_version"
175 set libgcj_jar [glob $objdir/../libgcj-$libjava_version.jar]
176 verbose "jar file is $libgcj_jar"
178 # The -B is so we find libgcj.spec.
179 regexp -- "Thread model: (\[^\n\]+)\n" $text ignore model
180 set libjava_uses_threads [expr {! ($model == "no"
182 || $model == "single")}]
184 # Always set encoding used by gcj.
185 append GCJ_UNDER_TEST " --encoding=UTF-8"
188 set wrap_compile_flags "";
189 if [target_info exists needs_status_wrapper] {
190 set result [build_wrapper "testglue.o"];
191 if { $result != "" } {
192 set wrapper_file [lindex $result 0];
193 set wrap_compile_flags [lindex $result 1];
195 warning "Status wrapper failed to build."
199 # Finally, add the gcc build directory so that we can find the
200 # shared libgcc. This, like much of dejagnu, is hideous.
201 set libjava_libgcc_s_path {}
203 if { [istarget "*-*-darwin*"] } {
204 set so_extension "dylib"
206 set so_extension "so"
208 set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.${so_extension}]
210 set gccdir [file dirname $gccdir]
211 lappend libjava_libgcc_s_path $gccdir
212 verbose "libjava_libgcc_s_path = $libjava_libgcc_s_path"
213 set compiler ${gccdir}/xgcc
214 if { [is_remote host] == 0 && [which $compiler] != 0 } {
215 foreach i "[exec $compiler --print-multi-lib]" {
217 regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
218 set mldir [string trimright $mldir "\;@"]
219 if { "$mldir" == "." } {
222 if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.${so_extension}.*]] >= 1 } {
223 lappend libjava_libgcc_s_path "${gccdir}/${mldir}"
229 set libjava_initialized 1
232 # Find a library. We know where libtool puts the actual libraries,
233 # and we look there. The implementation is fairly hacky. We can't
234 # compile with -nodefaultlibs, because that will also eliminate the
235 # system libraries we need. In order to have gcj still work, it must
236 # find the appropriate libraries so we must add -L options for their
237 # paths. However we can't simply use those libraries; we still need
238 # libtool for linking.
239 # Don't return the the lib${name}.la files here, since this causes the
240 # libraries to be linked twice: once as lib${name}.so/dylib and another time
241 # via gcj's implicit -l${name}. This is both unnecessary and causes the
242 # Solaris ld to warn: attempted multiple inclusion of file. This warning
243 # is not ignored by the dejagnu framework and cannot be disabled.
244 proc libjava_find_lib {dir name} {
246 set gp [get_multilibs]
247 foreach extension {so dll dylib sl a} {
248 foreach sub {.libs _libs} {
250 if {[file exists $gp/$dir/$sub/lib${name}.${extension}]} then {
251 # Just return the `-L' option. The library itself
252 # will be picked up via the spec file.
253 return "-L$gp/$dir/$sub"
256 # Just return the `-L' option. The library itself will be
257 # picked up via the spec file.
259 $base_dir/../../$dir/$sub/lib${name}.${extension} \
260 "-L$base_dir/../../$dir/$sub" \
270 # Find libgcj.spec. We need to use the file corresponding to the multilib
271 # under test since they might differ. Append a trailing slash since this
273 proc libjava_find_spec {} {
278 # Find `gij'. Return empty string if not found.
279 proc libjava_find_gij {} {
280 global base_dir objdir
282 set gijdir [lookfor_file [get_multilibs] libjava];
283 # Fall back if get_multilibs fails.
285 set gijdir "$objdir/.."
287 if {! [file exists $gijdir/gij]} {
293 # Remove a bunch of files.
294 proc gcj_cleanup {args} {
296 if {[string match *.o $file]} {
297 verbose "removing [file rootname $file].lo"
298 file delete -force [file rootname $file].lo
300 file delete -force -- $file
301 verbose "removing $file"
303 # It is simplest to do this instead of trying to figure out what
304 # bits in .libs ought to be removed.
305 catch {system "rm -rf .libs"}
308 # Compute arguments needed for compiler. MODE is a libtool mode:
309 # either compile or link.
310 proc libjava_arguments {{mode compile}} {
313 global srcdir subdir objdir
315 global GCJ_UNDER_TEST
321 global libjava_libgcc_s_path
322 global libjava_ld_library_path
323 global ld_library_path
324 global target_triplet
326 if [info exists LIBJAVA] {
327 set libjava $LIBJAVA;
329 set libjava [libjava_find_lib libjava gcj]
332 verbose "using LIBJAVA = $libjava" 2
335 # Basically we want to build up a colon separated path list from
336 # the value of $libjava.
339 foreach dir [list $libjava] {
340 foreach item [split $dir " "] {
341 switch -glob -- $item {
343 lappend lpath [string range $item 2 end]
349 set lpath [concat $lpath $libjava_libgcc_s_path]
350 verbose "lpath = $lpath ; libgcc_s_path = $libjava_libgcc_s_path"
351 set ld_library_path [join $lpath :]
352 set libjava_ld_library_path "$ld_library_path"
354 # That's enough to make things work for the normal case.
355 # If we wanted to handle an arbitrary value of libjava,
356 # then we'd have to do a lot more work.
358 set_ld_library_path_env_vars
359 if [info exists env(LD_LIBRARY_PATH)] {
360 verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
363 # Determine CLASSPATH separator
364 if { [string match "i?86-pc-mingw32*" $target_triplet] } {
370 # Set the CLASSPATH environment variable
373 [join [list . $srcdir/$subdir $objdir $libgcj_jar] $sep]
374 verbose "CLASSPATH is $env(CLASSPATH)"
376 # Disable all warnings, as ecj is rather chatty.
377 lappend args "additional_flags=-w"
379 if {$mode == "link"} {
380 global wrapper_file wrap_compile_flags
381 lappend args "additional_flags=$wrap_compile_flags"
383 if { [regexp "linux" $target_triplet] } {
384 lappend args "additional_flags=-specs=libgcj-test.spec"
387 lappend args "libs=$wrapper_file"
388 lappend args "libs=$libjava"
392 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
393 lappend args "libs=${gluefile}"
394 lappend args "ldflags=$wrap_flags"
397 if [info exists TOOL_OPTIONS] {
398 lappend args "additional_flags=$TOOL_OPTIONS"
401 # Determine libgcj.spec corresponding to multilib under test.
402 set specdir [libjava_find_spec]
404 # Search for libtool. We need it to link.
406 set d [absolute $objdir]
407 foreach x {. .. ../.. ../../..} {
408 if {[file exists $d/$x/libtool]} then {
409 # We have to run silently to avoid DejaGNU lossage.
411 "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$specdir"
416 if {! $found_compiler} {
417 # Append -B$specdir so that we find libgcj.spec before it
419 lappend args "compiler=$GCJ_UNDER_TEST -B$specdir"
422 # Avoid libtool wrapper scripts when possible.
423 # but not if libtool warnings results in FAILs
424 if {$mode == "link"} {
425 if {! [istarget "*-*-cygwin*"] && ! [istarget "*-*-mingw*"]
426 && ! [istarget "*-*-darwin*"] } {
427 lappend args "additional_flags=-no-install"
429 if { [istarget "*-*-darwin*"] } {
430 lappend args "additional_flags=-bind_at_load"
431 lappend args "additional_flags=-multiply_defined suppress"
433 if { [istarget "*-*-darwin9*"] || [istarget "*-*-darwin1*"] || [istarget "*-*-darwin2*"] } {
434 lappend args "additional_flags=-Wl,-allow_stack_execute"
442 # Link a bunch of objects into a program. MAIN is the name of the
443 # class holding `main'. Return 0 on failure.
444 proc gcj_link {program main files {options {}}} {
445 set arguments [libjava_arguments link]
446 if {[llength $options]} {
447 eval lappend arguments $options
449 lappend arguments "additional_flags=--main=$main"
450 set x [libjava_prune_warnings \
451 [libjava_tcompile $files $program executable $arguments]]
453 verbose "link failure: $x" 2
454 fail "linking $program"
456 fail "running $program"
460 pass "linking $program"
464 # Invoke the program and see what happens. Return 0 on failure.
465 proc gcj_invoke {program expectFile ld_library_additions} {
467 global libjava_ld_library_path
468 global ld_library_path
470 set ld_library_path "$libjava_ld_library_path"
471 if {[llength $ld_library_additions] > 0} {
472 append ld_library_path :[join $ld_library_additions :]
475 set_ld_library_path_env_vars
476 if [info exists env(LD_LIBRARY_PATH)] {
477 verbose "LD_LIBRARY_PATH=$env(LD_LIBRARY_PATH)"
480 set result [libjava_load ./$program]
481 set status [lindex $result 0]
482 set output [lindex $result 1]
485 restore_ld_library_path_env_vars
487 if {$status != "pass"} {
488 verbose "got $output"
490 untested "$program output"
494 set id [open $expectFile r]
495 set expected [read $id]
498 if {! [string compare $output $expected]} {
499 pass "$program output"
502 fail "$program output"
507 proc exec_gij {jarfile expectFile {ld_library_additions {}} {addl_flags {}}} {
509 global libjava_ld_library_path
510 global ld_library_path
512 set ld_library_path "$libjava_ld_library_path"
513 if {[llength $ld_library_additions] > 0} {
514 append ld_library_path :[join $ld_library_additions :]
517 set_ld_library_path_env_vars
518 if [info exists env(LD_LIBRARY_PATH)] {
519 verbose "LD_LIBRARY_PATH=$env(LD_LIBRARY_PATH)"
522 set gij [libjava_find_gij]
523 set classname [file rootname [file tail $jarfile]]
525 puts "LD_LIBRARY_PATH=. $gij -cp $jarfile $addl_flags $classname"
527 set result [libjava_load $gij "-cp $jarfile $addl_flags $classname"]
528 set status [lindex $result 0]
529 set output [lindex $result 1]
531 restore_ld_library_path_env_vars
533 if {$status != "pass"} {
534 verbose "got $output"
535 fail "$classname run"
536 untested "$classname output"
540 set id [open $expectFile r]
541 set expected [read $id]
544 if {! [string compare $output $expected]} {
545 pass "$classname output"
548 fail "$classname output"
553 # Invoke a program and check its output. EXECUTABLE is the program;
554 # ARGS are the arguments to the program. Returns 1 if tests passed
555 # (or things were left untested), 0 otherwise.
556 proc libjava_invoke {errname testName optName executable inpfile resultfile
557 ld_library_additions args} {
559 global libjava_ld_library_path
560 global ld_library_path
562 set ld_library_path "$libjava_ld_library_path"
563 if {[llength $ld_library_additions] > 0} {
564 append ld_library_path :[join $ld_library_additions :]
567 set_ld_library_path_env_vars
568 if [info exists env(LD_LIBRARY_PATH)] {
569 verbose "LD_LIBRARY_PATH=$env(LD_LIBRARY_PATH)"
574 if {[info exists opts(no-exec)]} {
575 if {[info exists opts(need-threads)]} {
576 # This means we wanted to try to run it but we couldn't
577 # because threads aren't supported. So we have to
578 # generate an `untested'.
579 untested "$errname execution - $testName"
580 untested "$errname output - $testName"
585 send_log "invoke: $executable $args $inpfile\n"
587 set result [libjava_load $executable $args "$inpfile"]
588 set status [lindex $result 0]
589 set output [lindex $result 1]
591 # Restore LD_LIBRARY_PATH setting.
592 restore_ld_library_path_env_vars
594 if {[info exists opts(xfail-exec)]} then {
597 $status "$errname execution - $testName"
598 if { $status != "pass" } {
599 untested "$errname output - $testName"
603 verbose "resultfile is $resultfile"
604 set id [open $resultfile r]
606 append expected [read $id]
607 regsub -all "\r" "$output" "" output
608 regsub "\n*$" $expected "" expected
609 regsub "\n*$" $output "" output
610 regsub "^\n*" $expected "" expected
611 regsub "^\n*" $output "" output
612 regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
613 regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
614 regsub -all "\[ \t\]\[ \t\]*" $output " " output
615 regsub -all "\[ \t\]*\n\n*" $output "\n" output
616 verbose "expected is $expected"
617 verbose "actual is $output"
619 if {[info exists opts(regexp_match)]} {
620 if [regexp $expected $output] {
624 if { $expected == $output } {
628 if {[info exists opts(xfail-output)]} {
631 if { $passed == 1 } {
632 pass "$errname output - $testName"
634 fail "$errname output - $testName"
642 # Run the test specified by srcfile and resultfile. compile_args and
643 # exec_args are options telling this proc how to work.
645 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
647 global srcdir subdir objdir
649 global GCJ_UNDER_TEST
653 # Make opts into an array.
656 foreach item $exec_args {
660 # If we need threads and we don't have them then set the `no-exec'
661 # flag. This is case is also handled specially later.
662 if {[info exists opts(need-threads)]} {
663 global libjava_uses_threads
664 if {! $libjava_uses_threads} {
669 set errname [file rootname [file tail $srcfile]]
670 if {! [runtest_file_p $runtests $errname]} {
674 if {[info exists opts(no-link)]} {
679 set args [libjava_arguments $mode]
680 if {! [info exists opts(no-link)]} {
681 # Add the --main flag
682 lappend args "additional_flags=--main=[file rootname [file tail $srcfile]]"
683 if { $compile_args != "" } {
684 lappend args "additional_flags=$compile_args"
688 regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
689 set executable "${objdir}/$out"
690 if {[info exists opts(no-link)]} {
691 append executable ".o"
694 # DOS/win32 targets default to .exe if no suffix is given
695 # We then try to delete a file that doesn't exist. It is
696 # simpler to add the suffix everywhere.
697 append executable ".exe"
698 set target executable
700 if { $compile_args != "" } {
701 set errname "$errname $compile_args"
704 set removeList [list $executable]
706 set x [libjava_prune_warnings \
707 [libjava_tcompile $srcfile "$executable" $target $args]]
708 if {[info exists opts(xfail-gcj)]} {
712 verbose "target_compile failed: $x" 2
714 if {[info exists opts(shouldfail)]} {
715 pass "$errname compilation from source"
716 eval gcj_cleanup $removeList
720 fail "$errname compilation from source"
721 if {[info exists opts(xfail-gcj)]
722 || ! [info exists opts(no-exec)]
723 || [info exists opts(need-threads)]} {
724 untested "$errname execution from source compiled test"
725 untested "$errname output from source compiled test"
729 if {[info exists opts(shouldfail)]} {
730 fail "$errname compilation from source"
733 pass "$errname compilation from source"
735 # Set up the options the way they are expected by libjava_invoke.
736 if {[info exists opts(xfail-source-output)]} {
737 set opts(xfail-output) x
739 if {[libjava_invoke $errname "source compiled test" opts $executable \
740 $inpfile $resultfile ""]} {
741 # Everything ok, so clean up.
742 eval gcj_cleanup $removeList
747 # Run the test specified by srcfile and resultfile. compile_args and
748 # exec_args are options telling this proc how to work.
749 # `no-link' don't try to link the program
750 # `no-exec' don't try to run the test
751 # `xfail-gcj' compilation from source will fail
752 # `xfail-javac' compilation with javac will fail
753 # `xfail-gcjC' compilation with gcj -C will fail
754 # `shouldfail' compilation from source is supposed to fail
755 # This is different from xfail, which marks a known
756 # failure that we just haven't fixed.
757 # A compilation marked this way should fail with any
759 # `xfail-byte' compilation from bytecode will fail
760 # `xfail-exec' exec will fail
762 # output will be wrong
763 # `xfail-byte-output'
764 # output will be wrong when compiled from bytecode
765 # `xfail-source-output'
766 # output will be wrong when compiled from source code
768 # test relies on thread support
770 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
771 test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
773 # Test BC-ABI compilation.
774 set compile_args_bcabi $compile_args
775 lappend compile_args_bcabi "-findirect-dispatch"
776 test_libjava_from_source $options $srcfile $compile_args_bcabi $inpfile $resultfile $exec_args
780 # libjava_version -- extract and print the version number of libjavap
782 proc default_libjava_version {} {
785 proc default_libjava_start { } {