OSDN Git Service

gcc/testsuite:
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / lib / libjava.exp
index 3abb648..9b45f3c 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 1998, 1999 Cygnus Solutions
+# Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
 
 load_lib "libgloss.exp"
 
@@ -10,6 +10,17 @@ if ![info exists tmpdir] {
     set tmpdir "/tmp"
 }
 
+# This is like `target_compile' but it does some surgery to work
+# around stupid DejaGNU bugs.  In particular DejaGNU has very poor
+# quoting, so for instance a `$' will be re-evaluated at spawn time.
+# We don't want that.
+proc libjava_tcompile {source destfile type options} {
+    # This strange-looking expression really does quote the `$'.
+    regsub -all -- {\$} $source {\$} source
+    regsub -all -- {\$} $destfile {\$} destfile
+    return [target_compile $source $destfile $type $options]
+}
+
 # Read an `xfail' file if it exists.  Returns a list of xfail tokens.
 proc libjava_read_xfail {file} {
     if {! [file exists $file]} {
@@ -21,35 +32,70 @@ proc libjava_read_xfail {file} {
     return $tokens
 }
 
-# Find `jv-scan'.  FIXME: this relies on DejaGnu internals.  These
-# should probably be exposed in a better way.
-proc find_jvscan {} {
+# Find a particular executable.  FIXME: this relies on DejaGnu
+# internals.  These should probably be exposed in a better way.
+proc libjava_find_program {prog} {
     global tool_root_dir
 
-    set file [lookfor_file $tool_root_dir jv-scan]
+    set file [lookfor_file $tool_root_dir $prog]
     if { $file == "" } {
-       set file [lookfor_file $tool_root_dir gcc/jv-scan];
+       set file [lookfor_file $tool_root_dir gcc/$prog];
     }
     if {$file == ""} {
-       set file jv-scan
+       set file $prog
     }
     return $file
 }
 
+# Find `jv-scan'.
+proc find_jvscan {} {
+    return [libjava_find_program jv-scan]
+}
+
+# Find `gcjh'.
+proc find_gcjh {} {
+    return [libjava_find_program gcjh]
+}
+
+proc find_javac {} {
+    global SUN_JAVAC GCJ_UNDER_TEST env
+    # If JDK doesn't run on your platform but some other
+    # JDK-compatible javac does, you may set SUN_JAVAC to point to it.
+    # One of the most important properties of a SUN_JAVAC is that it
+    # must create class-files even for classes that have not been
+    # specified in the command line, but that were needed to compile
+    # those that have.  For example, Pizza won't do it, but you can
+    # use `kaffe sun.tools.javac.Main', if you have Sun's classes.zip
+    # in the kaffe's default search path.
+    if {![info exists SUN_JAVAC]} {
+       if {[info exists env(SUN_JAVAC)]} {
+           set SUN_JAVAC $env(SUN_JAVAC)
+       } else {
+           set SUN_JAVAC "$GCJ_UNDER_TEST -C"
+       }
+    }
+    return $SUN_JAVAC
+}
+
 proc bytecompile_file { file objdir {classpath {}} } {
     global env
-    set dirname  [file dirname $file];
+    set dirname [file dirname $file]
 
-    catch {unset env(CLASSPATH)}
+    set javac [find_javac]
     if {$classpath != ""} then {
         set env(CLASSPATH) $classpath
     }
-    if {[catch {system "cd $dirname; javac $file -d $objdir"} msg]} then {
+    set here [pwd]
+    cd $dirname
+    if {[catch {
+       set q [eval exec "$javac [list $file] -d $objdir 2>@ stdout"]
+    } msg]} then {
        verbose "couldn't compile $file: $msg"
        set r 0
     } else {
        set r 1
     }
+    cd $here
     return $r
 }
 
@@ -61,9 +107,11 @@ set libjava_initialized 0
 proc libjava_init { args } {
     global wrapper_file;
     global wrap_compile_flags;
-    global libjava_initialized
+    global libjava_initialized libjava_uses_threads
     global GCJ_UNDER_TEST
     global TOOL_EXECUTABLE
+    global original_ld_library_path
+    global env objdir
 
     if { $libjava_initialized == 1 } { return; }
 
@@ -71,7 +119,31 @@ proc libjava_init { args } {
        if [info exists TOOL_EXECUTABLE] {
            set GCJ_UNDER_TEST $TOOL_EXECUTABLE;
        } else {
-           set GCJ_UNDER_TEST "[find_gcj]"
+           if [info exists env(GCJ)] {
+               set GCJ_UNDER_TEST $env(GCJ)
+           } else {
+               set GCJ_UNDER_TEST "[find_gcj]"
+           }
+       }
+    }
+
+    # The -B is so we find libgcj.spec.
+    set text [eval exec "$GCJ_UNDER_TEST -B$objdir/../ -v 2>@ stdout"]
+    regexp -- "Thread model: (\[^\n\]+)\n" $text ignore model
+    set libjava_uses_threads [expr {! ($model == "no"
+                                      || $model == "none"
+                                      || $model == "single")}]
+
+    # Always set encoding used by gcj.
+    append GCJ_UNDER_TEST " --encoding=UTF-8"
+
+    if [info exists env(LD_LIBRARY_PATH)] {
+       set original_ld_library_path $env(LD_LIBRARY_PATH)
+    } else {
+       if [info exists env(SHLIB_PATH)] {
+           set original_ld_library_path $env(SHLIB_PATH)
+       } else {
+           set original_ld_library_path ""
        }
     }
 
@@ -116,6 +188,21 @@ proc libjava_find_lib {dir name} {
     return ""
 }
 
+# Remove a bunch of files.
+proc gcj_cleanup {args} {
+    foreach file $args {
+       if {[string match *.o $file]} {
+           verbose "removing [file rootname $file].lo"
+           file delete -force [file rootname $file].lo
+       }
+       file delete -force -- $file
+       verbose "removing $file"
+    }
+    # It is simplest to do this instead of trying to figure out what
+    # bits in .libs ought to be removed.
+    catch {system "rm -rf .libs"}
+}
+
 # Compute arguments needed for compiler.  MODE is a libtool mode:
 # either compile or link.
 proc libjava_arguments {{mode compile}} {
@@ -129,6 +216,8 @@ proc libjava_arguments {{mode compile}} {
     global GCJ_UNDER_TEST
     global tmpdir
     global runtests
+    global env
+    global tool_root_dir
 
     if [info exists LIBJAVA] {
        set libjava $LIBJAVA;
@@ -167,30 +256,41 @@ proc libjava_arguments {{mode compile}} {
     # Basically we want to build up a colon separated path list from
     # the value of $libjava.
 
-    # First strip away any -L arguments.
-    regsub -all -- "-L" $libjava "" ld_library_path
-
-    # Then remove any -lgcj argument.
-    regsub -all -- " -lgcj.*" $ld_library_path "" ld_library_path
+    set lpath {}
+    foreach dir [list $libjava $libgc $libz] {
+       foreach item [split $dir " "] {
+           switch -glob -- $item {
+               "-L*" {
+                   lappend lpath [string range $item 2 end]
+               }
+           }
+       }
+    }
 
-    # First strip away any -L arguments.
-    regsub -all -- "-L" $libgc $ld_library_path ld_library_path
+    # Finally, add the gcc build directory so that we can find the
+    # shared libgcc.  This, like much of dejagnu, is hideous.
+    set gccdir [lookfor_file $tool_root_dir gcc/libgcc_s.so]
+    if {$gccdir != ""} {
+       lappend lpath [file dirname $gccdir]
+    }
 
-    # Then remove any -lgcjgc argument.
-    regsub -all -- " -lgcjgc.*" $ld_library_path "" ld_library_path
+    set ld_library_path [join $lpath :]
 
     # That's enough to make things work for the normal case.
     # If we wanted to handle an arbitrary value of libjava,
     # then we'd have to do a lot more work.
 
     # Set variables the dynamic linker looks at.
-    setenv LD_LIBRARY_PATH $ld_library_path
-    setenv SHLIB_PATH $ld_library_path
+    global original_ld_library_path
+    setenv LD_LIBRARY_PATH "$ld_library_path:$original_ld_library_path"
+    setenv SHLIB_PATH "$ld_library_path:$original_ld_library_path"
+
+    verbose "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
 
     # Set the CLASSPATH environment variable
-    verbose "CLASSPATH is $objdir/.."
+    verbose "CLASSPATH is .:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
     global env
-    set env(CLASSPATH) "$objdir/.."
+    set env(CLASSPATH) ".:$srcdir/$subdir:$objdir:$objdir/../libgcj.jar"
 
     global wrapper_file wrap_compile_flags;
     lappend args "additional_flags=$wrap_compile_flags";
@@ -217,31 +317,50 @@ proc libjava_arguments {{mode compile}} {
        if {[file exists $d/$x/libtool]} then {
            # We have to run silently to avoid DejaGNU lossage.
            lappend args \
-             "compiler=$d/$x/libtool --silent --mode=$mode $GCJ_UNDER_TEST"
+             "compiler=$d/$x/libtool --silent --tag=GCJ --mode=$mode $GCJ_UNDER_TEST -B$objdir/../"
            set found_compiler 1
            break
        }
     }
     if {! $found_compiler} {
-       lappend args "compiler=$GCJ_UNDER_TEST"
+       # Append -B$objdir/../ so that we find libgcj.spec before it
+       # is installed.
+       lappend args "compiler=$GCJ_UNDER_TEST -B$objdir/../"
+    }
+
+    # Avoid libtool wrapper scripts when possible.
+    if {$mode == "link"} {
+        lappend args "additional_flags=-no-install"
     }
 
     return $args
 }
 
+# Link a bunch of objects into a program.  MAIN is the name of the
+# class holding `main'.  Return 0 on failure.
+proc gcj_link {program main files {options {}}} {
+    set arguments [libjava_arguments link]
+    if {[llength $options]} {
+       eval lappend arguments $options
+    }
+    lappend arguments "additional_flags=--main=$main"
+    set x [prune_warnings [libjava_tcompile $files $program executable \
+                            $arguments]]
+    if {$x != ""} {
+       verbose "link failure: $x" 2
+       fail "linking $program"
+       setup_xfail "*-*-*"
+       fail "running $program"
+       return 0
+    }
+
+    pass "linking $program"
+    return 1
+}
 
 #
 # Run the test specified by srcfile and resultfile. compile_args and
 # exec_args are options telling this proc how to work.
-#   `no-link'     don't try to link the program
-#   `no-exec'     don't try to run the test
-#   `xfail-gcj'   compilation from source will fail
-#   `xfail-javac' compilation with javac will fail
-#   `xfail-gcjC'  compilation with gcj -C will fail
-#   `xfail-byte'  compilation from bytecode will fail
-#   `xfail-exec'  exec will fail
-#   `xfail-output' output will be wrong
-#   
 #
 proc test_libjava_from_source { options srcfile compile_args inpfile resultfile exec_args } {
     global base_dir
@@ -260,6 +379,15 @@ proc test_libjava_from_source { options srcfile compile_args inpfile resultfile
        set opts($item) x
     }
 
+    # If we need threads and we don't have them then set the `no-exec'
+    # flag.  This is case is also handled specially later.
+    if {[info exists opts(need-threads)]} {
+       global libjava_uses_threads
+       if {! $libjava_uses_threads} {
+           set opts(no-exec) x
+       }
+    }
+
     set errname [file rootname [file tail $srcfile]]
     if {! [runtest_file_p $runtests $errname]} {
         return
@@ -291,25 +419,50 @@ proc test_libjava_from_source { options srcfile compile_args inpfile resultfile
        set errname "$errname $compile_args"
     }
 
-    set x [target_compile $srcfile "$executable" $target $args]
+    set removeList [list $executable]
+
+    set x [prune_warnings \
+            [libjava_tcompile $srcfile "$executable" $target $args]]
     if {[info exists opts(xfail-gcj)]} {
        setup_xfail *-*-*
     }
     if { $x != "" } {
        verbose "target_compile failed: $x" 2
+
+       if {[info exists opts(shouldfail)]} {
+           pass "$errname compilation from source"
+           eval gcj_cleanup $removeList
+           return
+       }
+
        fail "$errname compilation from source"
-       if {[info exists opts(xfail-gcj)] || ! [info exists opts(no-exec)]} {
-           setup_xfail "*-*-*"
-           fail "$errname execution from source compiled test"
-           setup_xfail "*-*-*"
-           fail "$errname output from source compiled test"
+       if {[info exists opts(xfail-gcj)]
+           || ! [info exists opts(no-exec)]
+           || [info exists opts(need-threads)]} {
+           untested "$errname execution from source compiled test"
+           untested "$errname output from source compiled test"
        }
-       return;
+       return
+    }
+    if {[info exists opts(shouldfail)]} {
+       fail "$errname compilation from source"
+       return
     }
     pass "$errname compilation from source"
 
-    if {[info exists opts(no-exec)]
-       || [info exists opts(no-link)]} {
+    if {[info exists opts(no-exec)]} {
+       if {[info exists opts(need-threads)]} {
+           # This means we wanted to try to run it but we couldn't
+           # because threads aren't supported.  So we have to
+           # generate an `untested'.
+           untested "$errname execution from source compiled test"
+           untested "$errname output from source compiled test"
+       }
+       eval gcj_cleanup $removeList
+       return
+    }
+    if {[info exists opts(no-link)]} {
+       eval gcj_cleanup $removeList
        return
     }
 
@@ -321,8 +474,7 @@ proc test_libjava_from_source { options srcfile compile_args inpfile resultfile
     }
     $status "$errname execution from source compiled test"
     if { $status != "pass" } {
-       setup_xfail "*-*-*"
-       fail "$errname execution from source compiled test"
+       untested "$errname output from source compiled test"
        return;
     }
 
@@ -356,6 +508,7 @@ proc test_libjava_from_source { options srcfile compile_args inpfile resultfile
     }
     if { $passed == 1 } {
        pass "$errname output from source compiled test"
+       eval gcj_cleanup $removeList
     } else {
        clone_output "expected was $expected"
        clone_output "output was $output"
@@ -367,14 +520,6 @@ proc test_libjava_from_source { options srcfile compile_args inpfile resultfile
 #
 # Run the test specified by srcfile and resultfile. compile_args and
 # exec_args are options telling this proc how to work.
-#   `no-link'     don't try to link the program
-#   `no-exec' don't try to run the test
-#   `xfail-gcj'   compilation from source will fail
-#   `xfail-javac' compilation with javac will fail
-#   `xfail-gcjC'  compilation with gcj -C will fail
-#   `xfail-byte'  compilation from bytecode will fail
-#   `xfail-exec'  exec will fail
-#   `xfail-output' output will be wrong
 #
 proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile exec_args } {
     global base_dir
@@ -393,6 +538,14 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
        set opts($item) x
     }
 
+    # If we need threads and we don't have them then set the `no-exec'
+    # flag.  This is case is also handled specially later.
+    if {[info exists opts(need-threads)]} {
+       global libjava_uses_threads
+       if {! $libjava_uses_threads} {
+           set opts(no-exec) x
+       }
+    }
     set errname [file rootname [file tail $srcfile]]
     if {! [runtest_file_p $runtests $errname]} {
         return
@@ -400,52 +553,72 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
 
     # bytecompile files with Sun's compiler for now.
     set bc_ok [bytecompile_file $srcfile $objdir]
-    # FIXME: assumes we are using javac to compile to bytecode.
-    # This is not always the case.
-    if {[info exists opts(xfail-javac)]} {
+
+    set javac [find_javac]
+    # This is an ugly heuristic but it will have to do.
+    if {[string match *gcj* $javac]} {
+       set tag gcjC
+    } else {
+       set tag javac
+    }
+    if {[info exists opts(xfail-$tag)]} {
        setup_xfail *-*-*
     }
     if {! $bc_ok} then {
+       if {[info exists opts(shouldfail)]} {
+           pass "$errname byte compilation"
+           return
+       }
        fail "$errname byte compilation"
-       setup_xfail "*-*-*"
-       fail "$errname compilation from bytecode"
-       if {! [info exists opts(no-exec)]} {
-           setup_xfail "*-*-*"
-           fail "$errname execution from bytecode->native test"
-           setup_xfail "*-*-*"
-           fail "$errname output from bytecode->native test"
+       untested "$errname compilation from bytecode"
+       if {! [info exists opts(no-exec)]
+           || [info exists opts(need-threads)]} {
+           untested "$errname execution from bytecode->native test"
+           untested "$errname output from bytecode->native test"
        }
        return
     }
+    if {[info exists opts(shouldfail)]} {
+       fail "$errname byte compilation"
+       return
+    }
     pass "$errname byte compilation"
 
+    set removeList {}
+
     # Find name to use for --main, and name of all class files.
     set jvscan [find_jvscan]
     verbose "jvscan is $jvscan"
     set main_name [string trim \
-                    [target_compile $srcfile "" none \
-                       "compiler=$jvscan additional_flags=--print-main"]]
+                    [prune_warnings \
+                       [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --print-main" "" "" 300] 1]]]
     verbose "main name is $main_name"
     set class_out [string trim \
-                    [target_compile $srcfile "" none \
-                       "compiler=$jvscan additional_flags=--list-class"]]
+                    [prune_warnings \
+                       [lindex [local_exec "$jvscan --encoding=UTF-8 $srcfile --list-class" "" "" 300] 1]]]
     verbose "class list is $class_out"
-    # FIXME: As of Wed Feb 24 1999, `jv-scan --list-class' prints
-    # nothing if the file contains an interface and not a class.  I
-    # believe this is a jv-scan bug.
-    if {$class_out == ""} then {
-       set class_files \
-         [list $objdir/[file rootname [file tail $srcfile]].class]
-    } else {
-       # Turn "a b" into "a.class b.class".
-       # Also, turn "foo.bar" into "foo/bar.class".
-       set class_files {}
-        foreach file [split [string trim $class_out]] {
-           set file [join [split $file .] /]
-           lappend class_files $objdir/$file.class
+
+    if {[string match "*parse error*" $main_name]
+       || [string match "*parse error*" $class_out]} {
+       untested "$errname compilation from bytecode"
+       if {! [info exists opts(no-exec)]
+           || [info exists opts(need-threads)]} {
+           untested "$errname execution from bytecode->native test"
+           untested "$errname output from bytecode->native test"
        }
+       return
+    }
+
+    # Turn "a b" into "a.class b.class".
+    # Also, turn "foo.bar" into "foo/bar.class".
+    set class_files {}
+    foreach file [split [string trim $class_out]] {
+       set file [join [split $file .] /]
+       lappend class_files $objdir/$file.class
     }
 
+    eval lappend removeList $class_files
+
     # Usually it is an error for a test program not to have a `main'
     # method.  However, for no-exec tests it is ok.  Treat no-link
     # like no-exec here.
@@ -453,14 +626,13 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
        set opts(no-exec) x
     }
     set largs {}
-    if {$main_name == ""} {
-       if {! [info exists opts(no-exec)]} {
-           perror "No `main' given in program $errname"
-           return
-       } else {
-           set type object
-           set mode compile
-       }
+
+    if {[info exists opts(no-exec)]} {
+       set type object
+       set mode compile
+    } elseif {$main_name == ""} {
+       perror "No `main' given in program $errname"
+       return
     } else {
        set type executable
        lappend largs "additional_flags=--main=$main_name"
@@ -487,13 +659,24 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
     if {$mode == "compile"} {
        foreach c_file $class_files {
            set executable [file rootname [file tail $c_file]].o
-           set x [target_compile $c_file "$executable" $type $args]
+           set x [prune_warnings \
+                    [libjava_tcompile $c_file "$executable" $type $args]]
+           lappend removeList $executable
            if {$x != ""} {
                break
            }
        }
     } else {
-       set x [target_compile $class_files "$executable" $type $args]
+       # This is so evil: we de-listify CLASS_FILES so that we can
+       # turn around and quote the `$' in it for the shell.  I really
+       # hate DejaGNU.  It is so !@#$!@# unpredictable.
+       set hack ""
+       foreach stupid $class_files {
+           set hack "$hack $stupid"
+       }
+       lappend removeList $executable
+       set x [prune_warnings \
+                [libjava_tcompile $hack "$executable" $type $args]]
     }
     if {[info exists opts(xfail-byte)]} {
        setup_xfail *-*-*
@@ -501,17 +684,21 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
     if { $x != "" } {
        verbose "target_compile failed: $x" 2
        fail "$errname compilation from bytecode"
-       setup_xfail "*-*-*"
-       if {! [info exists opts(no-exec)]} {
-           fail "$errname execution from bytecode->native test"
-           setup_xfail "*-*-*"
-           fail "$errname output from bytecode->native test"
+       if {! [info exists opts(no-exec)]
+           || [info exists opts(need-threads)]} {
+           untested "$errname execution from bytecode->native test"
+           untested "$errname output from bytecode->native test"
        }
        return;
     }
     pass "$errname compilation from bytecode"
 
     if {[info exists opts(no-exec)]} {
+       if {[info exists opts(need-threads)]} {
+           untested "$errname execution from bytecode->native test"
+           untested "$errname output from bytecode->native test"
+       }
+       eval gcj_cleanup $removeList
        return
     }
 
@@ -523,9 +710,8 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
     }
     $status "$errname execution from bytecode->native test"
     if { $status != "pass" } {
-       setup_xfail "*-*-*"
-       fail "$errname output from bytecode->native test"
-       return;
+       untested "$errname output from bytecode->native test"
+       return
     }
 
     verbose "resultfile is $resultfile"
@@ -558,6 +744,7 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
     }
     if { $passed == 1 } {
        pass "$errname output from bytecode->native test"
+       eval gcj_cleanup $removeList
     } else {
        clone_output "expected was $expected"
        clone_output "output was $output"
@@ -570,13 +757,21 @@ proc test_libjava_from_javac { options srcfile compile_args inpfile resultfile e
 # Run the test specified by srcfile and resultfile. compile_args and
 # exec_args are options telling this proc how to work.
 #   `no-link'     don't try to link the program
-#   `no-exec' don't try to run the test
+#   `no-exec'     don't try to run the test
 #   `xfail-gcj'   compilation from source will fail
 #   `xfail-javac' compilation with javac will fail
 #   `xfail-gcjC'  compilation with gcj -C will fail
+#   `shouldfail'  compilation from source is supposed to fail
+#                 This is different from xfail, which marks a known
+#                 failure that we just havne't fixed.
+#                 A compilation marked this way should fail with any
+#                 front end.
 #   `xfail-byte'  compilation from bytecode will fail
 #   `xfail-exec'  exec will fail
-#   `xfail-output' output will be wrong
+#   `xfail-output'
+#                 output will be wrong
+#   `need-threads'
+#                 test relies on thread support
 #
 proc test_libjava { options srcfile compile_args inpfile resultfile exec_args } {
     test_libjava_from_source $options $srcfile $compile_args $inpfile $resultfile $exec_args
@@ -592,6 +787,20 @@ proc default_libjava_version {} {
 proc default_libjava_start { } {
 }
 
+# On IRIX 6, we have to set variables akin to LD_LIBRARY_PATH, but
+# called LD_LIBRARYN32_PATH (for the N32 ABI) and LD_LIBRARY64_PATH
+# (for the 64-bit ABI).  The right way to do this would be to modify
+# unix.exp -- but that's not an option since it's part of DejaGNU
+# proper, so we do it here, by trickery.  We really only need to do 
+# this on IRIX, but it shouldn't hurt to do it anywhere else.
+
+proc ${tool}_set_ld_library_path { name element op } {
+  setenv LD_LIBRARYN32_PATH [getenv LD_LIBRARY_PATH]
+  setenv LD_LIBRARY64_PATH [getenv LD_LIBRARY_PATH]
+}
+
+trace variable env(LD_LIBRARY_PATH) w ${tool}_set_ld_library_path
+
 # Local Variables:
 # tcl-indent-level:4
 # End: