OSDN Git Service

2002-01-05 H.J. Lu <hjl@gnu.org>
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / libjava.jni / jni.exp
1 # Tests for JNI code.
2
3 # Compile a single C file and produce a .so file.  OPTIONS is a list
4 # of options to pass to the compiler.  Returns 0 on failure, 1 on
5 # success.
6 proc gcj_jni_compile_c_to_so {file {options {}}} {
7   global srcdir
8
9   set name [file rootname [file tail $file]]
10   set soname lib${name}.so
11
12   lappend options "additional_flags=-shared -fPIC"
13   # Find the generated header.
14   lappend options "additional_flags=-I. -I.."
15   # Find jni.h.
16   lappend options "additional_flags=-I$srcdir/../include"
17
18   set x [prune_warnings [target_compile $file $soname executable $options]]
19   if {$x != ""} {
20     verbose "target_compile failed: $x" 2
21     fail "$name.c compilation"
22     return 0
23   }
24
25   pass "$name.c compilation"
26   return 1
27 }
28
29 # Build a header file from a .class file.  Return 0 on failure.
30 proc gcj_jni_build_header {file} {
31   set gcjh [find_gcjh]
32   set file [file rootname $file]
33   set options [list "compiler=$gcjh" \
34                  "additional_flags=-jni"]
35   set x [prune_warnings [target_compile $file "" none $options]]
36   if {$x != ""} {
37     verbose "target_compile failed: $x" 2
38     fail "$file header generation"
39     return 0
40   }
41
42   pass "$file header generation"
43   return 1
44 }
45
46 # Invoke the program and see what happens.  Return 0 on failure.
47 proc gcj_invoke {program expectFile} {
48   global env
49   set lib_path $env(LD_LIBRARY_PATH)
50   setenv LD_LIBRARY_PATH .:$lib_path
51   setenv SHLIB_PATH .:$lib_path
52
53   verbose "LD_LIBRARY_PATH=$env(LD_LIBRARY_PATH)"
54
55   set result [libjava_load ./$program]
56   set status [lindex $result 0]
57   set output [lindex $result 1]
58
59   # Restore setting
60   setenv LD_LIBRARY_PATH $lib_path
61   setenv SHLIB_PATH $lib_path
62
63   if {$status != "pass"} {
64     verbose "got $output"
65     fail "$program run"
66     untested "$program output"
67     return 0
68   }
69
70   set id [open $expectFile r]
71   set expected [read $id]
72   close $id
73
74   if {! [string compare $output $expected]} {
75     pass "$program output"
76     return 1
77   } else {
78     fail "$program output"
79     return 0
80   }
81 }
82
83 # Do all the work for a single JNI test.  Return 0 on failure.
84 proc gcj_jni_test_one {file} {
85   global runtests
86
87   # The base name.  We use it for several purposes.
88   set main [file rootname [file tail $file]]
89   if {! [runtest_file_p $runtests $main]} {
90     # Simply skip it.
91     return 1
92   }
93
94   if {! [bytecompile_file $file [pwd]]} {
95     fail "bytecompile $file"
96     # FIXME - should use `untested' on all remaining tests.
97     # But that is hard.
98     return 0
99   }
100   pass "bytecompile $file"
101
102   set bytefile [file rootname [file tail $file]].class
103   if {! [gcj_jni_build_header $bytefile]} {
104     # FIXME
105     return 0
106   }
107
108   set cfile [file rootname $file].c
109   set cxxflags ""
110   # If there is no `.c' file, assume there is a `.cc' file.
111   if {! [file exists $cfile]} {
112     set cfile [file rootname $file].cc
113     set cxxflags "-lstdc++"
114   }
115
116   if {! [gcj_jni_compile_c_to_so $cfile]} {
117     # FIXME
118     return 0
119   }
120
121   # We use -l$main because the .so is named the same as the main
122   # program.
123   set args [list "additional_flags=-fjni -L. -l$main $cxxflags"]
124   if {! [gcj_link $main $main $file $args]} {
125     # FIXME
126     return 0
127   }
128
129   if {! [gcj_invoke $main [file rootname $file].out]} {
130     # FIXME
131     return 0
132   }
133
134   # When we succeed we remove all our clutter.
135   eval gcj_cleanup [glob -nocomplain -- ${main}.*] [list $main lib${main}.so]
136
137   return 1
138 }
139
140 # Run the JNI tests.
141 proc gcj_jni_run {} {
142   global srcdir subdir
143   global target_triplet host_triplet
144
145   # For now we only test JNI on native builds.
146   if {$target_triplet == $host_triplet} {
147     catch "glob -nocomplain ${srcdir}/${subdir}/*.java" srcfiles
148
149     foreach x $srcfiles {
150       gcj_jni_test_one $x
151     }
152   } else {
153     verbose "JNI tests not run in cross-compilation environment"
154   }
155 }
156
157 gcj_jni_run