2006-05-06 Andreas Tobler <a.tobler@schweiz.ch>
[official-gcc.git] / libjava / testsuite / libjava.jni / jni.exp
blob6cd677d77aa17d67d5843eae28b9544c6ced8d7b
1 # Tests for JNI code.
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 global host_triplet
9 verbose "options: $options"
10 set options_cxx $options
11 set options ""
13 # Apple uses a different extension for shared/dynamic libraries
14 # so we check against powerpc-apple-darwin and set them to
15 # dylib.
16 # HP-UX uses sl, so we check this too, otherwise we take so.
18 if { [istarget "*-*-darwin*"] } {
19 set so_extension "dylib"
20 set so_flag "-dynamiclib"
21 } elseif { [istarget "hppa*-hp-hpux*"] } {
22 set so_extension "sl"
23 set so_flag "-shared"
24 } else {
25 set so_extension "so"
26 set so_flag "-shared"
28 set filename [file tail $file]
29 set name [file rootname $filename]
30 set soname lib${name}.${so_extension}
32 lappend options "additional_flags=${so_flag} -fPIC"
33 # Find the generated header.
34 lappend options "additional_flags=-I. -I.."
36 # Ensure that the generated header has correct prototypes.
37 set cfile [file rootname $file].c
38 if { [file exists $cfile]} {
39 # This option is only valid for C sources.
40 lappend options "additional_flags=-Wmissing-prototypes"
43 # Find jni.h and jni_md.h.
44 lappend options "additional_flags=-I$srcdir/../include -I$srcdir/../classpath/include"
46 # Append C++ options
47 lappend options "additional_flags=$options_cxx"
49 set x [libjava_prune_warnings \
50 [target_compile $file $soname executable $options]]
51 if {$x != ""} {
52 verbose "target_compile failed: $x" 2
53 fail "$filename compilation"
54 return 0
57 pass "$filename compilation"
58 return 1
61 # Build a header file from a .class file. Return 0 on failure.
62 proc gcj_jni_build_header {file} {
63 set gcjh [find_gcjh]
64 set file [file rootname $file]
65 set x [string trim [libjava_prune_warnings \
66 [lindex [local_exec "$gcjh -jni $file" "" "" 300] 1]]]
67 if {$x != ""} {
68 verbose "local_exec failed: $x" 2
69 fail "$file header generation"
70 return 0
73 pass "$file header generation"
74 return 1
77 # Do all the work for a single JNI test. Return 0 on failure.
78 proc gcj_jni_test_one {file} {
79 global runtests
80 global host_triplet
81 global INTERPRETER
83 # Apple uses a different extension for shared/dynamic libraries
84 # so we check against powerpc-apple-darwin and set them to
85 # dylib.
86 # HP-UX uses sl, so we check this too, otherwise we take so.
88 if { [istarget "*-*-darwin*"] } {
89 set so_extension "dylib"
90 } elseif { [istarget "hppa*-hp-hpux*"] } {
91 set so_extension "sl"
92 } else {
93 set so_extension "so"
96 # The base name. We use it for several purposes.
97 set main [file rootname [file tail $file]]
98 if {! [runtest_file_p $runtests $main]} {
99 # Simply skip it.
100 return 1
103 if {! [bytecompile_file $file [pwd]]} {
104 fail "bytecompile $file"
105 # FIXME - should use `untested' on all remaining tests.
106 # But that is hard.
107 return 0
109 pass "bytecompile $file"
111 set bytefile [file rootname [file tail $file]].class
112 if {! [gcj_jni_build_header $bytefile]} {
113 # FIXME
114 return 0
117 set cfile [file rootname $file].c
118 set cxxflags ""
119 set cxxldlibflags {}
120 # If there is no `.c' file, assume there is a `.cc' file.
121 if {! [file exists $cfile]} {
122 set cfile [file rootname $file].cc
124 set cxxflaglist {}
125 foreach arg [split [libjava_find_lib libstdc++-v3/src stdc++] " "] {
126 switch -glob -- $arg {
127 "-L*" {
128 set arg [string range $arg 2 end]
129 lappend cxxldlibflags $arg
130 # Strip the `.libs' directory; we link with libtool which
131 # doesn't need it.
132 set arg "-L[file dirname $arg]"
135 lappend cxxflaglist $arg
136 # In case the libstdc++ is not installed yet, we pass the build
137 # directory of it to the cxxflaglist.
138 lappend cxxflaglist "-L$cxxldlibflags"
141 # If you're building the compiler with --prefix set to a place
142 # where it's not yet installed, then the linker won't be able to
143 # find the libgcc used by libffi.dylib. We could pass the
144 # -dylib_file option, but that's complicated, and it's much easier
145 # to just make the linker find libgcc using -L options.
146 if { [istarget "*-*-darwin*"] } {
147 lappend cxxflaglist "-shared-libgcc -lstdc++"
148 } else {
149 lappend cxxflaglist "-lstdc++"
152 set cxxflags [join $cxxflaglist]
155 if {! [gcj_jni_compile_c_to_so $cfile $cxxflags]} {
156 # FIXME
157 return 0
160 set args [list "additional_flags=-fjni"]
161 if {! [gcj_link $main $main $file $args]} {
162 # FIXME
163 return 0
166 set resultfile [file rootname $file].out
168 if {! [gcj_invoke $main $resultfile $cxxldlibflags]} {
169 # FIXME
170 return 0
173 # We purposely ignore errors here; we still want to run the other
174 # appropriate tests.
175 set errname [file rootname [file tail $file]]
176 set gij [libjava_find_gij]
177 # libjava_find_gij will return "" if it couldn't find the
178 # program; in this case we want to skip the test.
179 # If the libraries are not installed yet, we have to pass them via
180 # cxxldlibflags to libjava_invoke.
181 if {$INTERPRETER == "yes" && $gij != ""} {
182 libjava_invoke $errname "gij test" opts $gij \
183 "" $resultfile $cxxldlibflags $main
186 # When we succeed we remove all our clutter.
187 eval gcj_cleanup [glob -nocomplain -- ${main}.*] \
188 [list $main lib${main}.${so_extension}]
190 return 1
193 # Compile a single C file and produce a binary. OPTIONS is a list of
194 # options to pass to the compiler. Returns 0 on failure, 1 on
195 # success.
196 proc gcj_jni_invocation_compile_c_to_binary {file {options {}}} {
197 global srcdir
198 global host_triplet
199 verbose "options: $options"
200 set options_cxx $options
201 set options ""
203 set filename [file tail $file]
204 set name [file rootname $filename]
206 # Set some darwin specific options
207 if { [istarget "*-*-darwin*"] } {
208 lappend options "additional_flags= -bind_at_load -multiply_defined suppress"
210 # Find the generated header.
211 lappend options "additional_flags=-I. -I.."
213 # Find jni.h and jni_md.h.
214 lappend options "additional_flags=-I$srcdir/../include -I$srcdir/../classpath/include"
216 # Append C++ options
217 lappend options "additional_flags=$options_cxx"
219 set x [libjava_prune_warnings \
220 [target_compile $file $name executable $options]]
221 if {$x != ""} {
222 verbose "target_compile failed: $x" 2
223 fail "$filename compilation"
224 return 0
227 pass "$filename compilation"
228 return 1
231 # Do all the work for a single invocation API test. Return 0 on
232 # failure.
233 proc gcj_jni_invocation_test_one {file} {
234 global runtests
235 global host_triplet
236 global INTERPRETER
238 # The base name. We use it for several purposes.
239 set main [file rootname [file tail $file]]
240 if {! [runtest_file_p $runtests $main]} {
241 # Simply skip it.
242 return 1
245 if {! [bytecompile_file $file [pwd]]} {
246 fail "bytecompile $file"
247 # FIXME - should use `untested' on all remaining tests.
248 # But that is hard.
249 return 0
251 pass "bytecompile $file"
253 set cfile [file rootname $file].c
254 # Darwin needs -liconv linked, otherwise we get some unresolved.
255 # If you're building the compiler with --prefix set to a place
256 # where it's not yet installed, then the linker won't be able to
257 # find the libgcc used by libffi.dylib. We could pass the
258 # -dylib_file option, but that's complicated, and it's much easier
259 # to just make the linker find libgcc using -L options.
260 if { [istarget "*-*-darwin*"] } {
261 set cxxflags "-L../.libs -shared-libgcc -lgcj -liconv"
262 } else {
263 global LIBJAVA
264 if [info exists LIBJAVA] {
265 set libjava $LIBJAVA;
266 } else {
267 set libjava [libjava_find_lib libjava gcj]
269 set cxxflags "$libjava -lgcj"
272 if { [istarget "*-*-solaris*"] } {
273 lappend cxxflags "-lsocket"
276 if {! [gcj_jni_invocation_compile_c_to_binary $cfile $cxxflags]} {
277 # FIXME
278 return 0
281 set resultfile [file rootname $file].out
283 if {! [gcj_invoke $main $resultfile ""]} {
284 # FIXME
285 return 0
288 # We purposely ignore errors here; we still want to run the other
289 # appropriate tests.
290 set errname [file rootname [file tail $file]]
292 # When we succeed we remove all our clutter.
293 eval gcj_cleanup [glob -nocomplain -- ${main}.*] \
294 [list $main]
296 return 1
299 # Run the JNI tests.
300 proc gcj_jni_run {} {
301 global srcdir subdir
302 global build_triplet host_triplet
304 # For now we only test JNI on native builds.
305 if {$build_triplet == $host_triplet} {
306 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/*.java] } srcfiles
308 foreach x $srcfiles {
309 gcj_jni_test_one $x
312 # Run JNI invocation API tests
313 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/invocation/*.java] } srcfiles
315 foreach x $srcfiles {
316 gcj_jni_invocation_test_one $x
318 } else {
319 verbose "JNI tests not run in cross-compilation environment"
323 gcj_jni_run