From d19da8d1b42f1a75bef55bcfde5574b9b98474b6 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 14 Jun 2001 17:46:53 +0000 Subject: [PATCH] jni.exp: New file. * libjava.jni/jni.exp: New file. * lib/libjava.exp (gcj_link): New proc. From-SVN: r43375 --- libjava/testsuite/ChangeLog | 5 + libjava/testsuite/lib/libjava.exp | 21 ++++ libjava/testsuite/libjava.jni/jni.exp | 150 ++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 libjava/testsuite/libjava.jni/jni.exp diff --git a/libjava/testsuite/ChangeLog b/libjava/testsuite/ChangeLog index 71374659e63..f33c890d124 100644 --- a/libjava/testsuite/ChangeLog +++ b/libjava/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2001-06-14 Tom Tromey + + * libjava.jni/jni.exp: New file. + * lib/libjava.exp (gcj_link): New proc. + 2001-06-08 Tom Tromey Fix for PR libgcj/2874: diff --git a/libjava/testsuite/lib/libjava.exp b/libjava/testsuite/lib/libjava.exp index f6947ccdf3d..ace5f6c2b2d 100644 --- a/libjava/testsuite/lib/libjava.exp +++ b/libjava/testsuite/lib/libjava.exp @@ -336,6 +336,27 @@ proc libjava_arguments {{mode compile}} { 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 diff --git a/libjava/testsuite/libjava.jni/jni.exp b/libjava/testsuite/libjava.jni/jni.exp new file mode 100644 index 00000000000..4eb7b447fe7 --- /dev/null +++ b/libjava/testsuite/libjava.jni/jni.exp @@ -0,0 +1,150 @@ +# Tests for JNI code. + +# Compile a single C file and produce a .so file. OPTIONS is a list +# of options to pass to the compiler. Returns 0 on failure, 1 on +# success. +proc gcj_jni_compile_c_to_so {file {options {}}} { + global srcdir + + set name [file rootname [file tail $file]] + set soname lib${name}.so + + lappend options "additional_flags=-shared -fPIC" + # Find the generated header. + lappend options "additional_flags=-I." + # Find jni.h. + lappend options "additional_flags=-I$srcdir/../../include" + + set x [prune_warnings [target_compile $file $soname executable $options]] + if {$x != ""} { + verbose "target_compile failed: $x" 2 + fail "$name.c compilation" + return 0 + } + + pass "$name.c compilation" + return 1 +} + +# Build a header file from a .class file. Return 0 on failure. +proc gcj_jni_build_header {file} { + set gcjh [find_gcjh] + set file [file rootname $file] + set options [list "compiler=$gcjh" \ + "additional_flags=-jni"] + set x [prune_warnings [target_compile $file "" none $options]] + if {$x != ""} { + verbose "target_compile failed: $x" 2 + fail "$file header generation" + return 0 + } + + pass "$file header generation" + return 1 +} + +# Invoke the program and see what happens. Return 0 on failure. +proc gcj_invoke {program expectFile} { + global env + set lib_path $env(LD_LIBRARY_PATH) + setenv LD_LIBRARY_PATH .:$lib_path + setenv SHLIB_PATH .:$lib_path + + verbose "LD_LIBRARY_PATH=$env(LD_LIBRARY_PATH)" + + set result [libjava_load ./$program] + set status [lindex $result 0] + set output [lindex $result 1] + + # Restore setting + setenv LD_LIBRARY_PATH $lib_path + setenv SHLIB_PATH $lib_path + + if {$status != "pass"} { + verbose "got $output" + fail "$program run" + untested "$program output" + return 0 + } + + set id [open $expectFile r] + set expected [read $id] + close $id + + if {! [string compare $output $expected]} { + pass "$program output" + return 1 + } else { + fail "$program output" + return 0 + } +} + +# Do all the work for a single JNI test. Return 0 on failure. +proc gcj_jni_test_one {file} { + global runtests + + # The base name. We use it for several purposes. + set main [file rootname [file tail $file]] + if {! [runtest_file_p $runtests $main]} { + # Simply skip it. + return 1 + } + + if {! [bytecompile_file $file [pwd]]} { + fail "bytecompile $file" + # FIXME - should use `untested' on all remaining tests. + # But that is hard. + return 0 + } + pass "bytecompile $file" + + set bytefile [file rootname [file tail $file]].class + if {! [gcj_jni_build_header $bytefile]} { + # FIXME + return 0 + } + + set cfile [file rootname $file].c + if {! [gcj_jni_compile_c_to_so $cfile]} { + # FIXME + return 0 + } + + # We use -l$main because the .so is named the same as the main + # program. + set args [list "additional_flags=-fjni -L. -l$main"] + if {! [gcj_link $main $main $file $args]} { + # FIXME + return 0 + } + + if {! [gcj_invoke $main [file rootname $file].out]} { + # FIXME + return 0 + } + + # When we succeed we remove all our clutter. + eval gcj_cleanup [glob -nocomplain -- ${main}.*] [list $main lib${main}.so] + + return 1 +} + +# Run the JNI tests. +proc gcj_jni_run {} { + global srcdir subdir + global target_triplet host_triplet + + # For now we only test JNI on native builds. + if {$target_triplet == $host_triplet} { + catch "glob -nocomplain ${srcdir}/${subdir}/*.java" srcfiles + + foreach x $srcfiles { + gcj_jni_test_one $x + } + } else { + verbose "JNI tests not run in cross-compilation environment" + } +} + +gcj_jni_run -- 2.30.2