Merge from the pain train
[official-gcc.git] / libjava / testsuite / libjava.jni / jni.exp
blobd736b8690d670939bae7c74763cad20ff30b8a26
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, else we assume it's .so
17 if { [ regexp {powerpc-apple-darwin} $host_triplet] } {
18 set so_extension "dylib"
19 set so_flag "-dynamiclib"
20 } else {
21 set so_extension "so"
22 set so_flag "-shared"
24 set filename [file tail $file]
25 set name [file rootname $filename]
26 set soname lib${name}.${so_extension}
28 lappend options "additional_flags=${so_flag} -fPIC"
29 # Find the generated header.
30 lappend options "additional_flags=-I. -I.."
32 # Ensure that the generated header has correct prototypes.
33 set cfile [file rootname $file].c
34 if { [file exists $cfile]} {
35 # This option is only valid for C sources.
36 lappend options "additional_flags=-Wmissing-prototypes"
39 # Find jni.h.
40 lappend options "additional_flags=-I$srcdir/../include"
42 # Append C++ options
43 lappend options "additional_flags=$options_cxx"
45 set x [libjava_prune_warnings \
46 [target_compile $file $soname executable $options]]
47 if {$x != ""} {
48 verbose "target_compile failed: $x" 2
49 fail "$filename compilation"
50 return 0
53 pass "$filename compilation"
54 return 1
57 # Build a header file from a .class file. Return 0 on failure.
58 proc gcj_jni_build_header {file} {
59 set gcjh [find_gcjh]
60 set file [file rootname $file]
61 set x [string trim [libjava_prune_warnings \
62 [lindex [local_exec "$gcjh -jni $file" "" "" 300] 1]]]
63 if {$x != ""} {
64 verbose "local_exec failed: $x" 2
65 fail "$file header generation"
66 return 0
69 pass "$file header generation"
70 return 1
73 # Do all the work for a single JNI test. Return 0 on failure.
74 proc gcj_jni_test_one {file} {
75 global runtests
76 global host_triplet
77 global INTERPRETER
79 # apple uses a different extension for shared/dynamic libraries
80 # so we check against powerpc-apple-darwin and set them to
81 # dylib, else we assume it's .so
83 if { [ regexp {powerpc-apple-darwin} $host_triplet] } {
84 set so_extension "dylib"
85 } else {
86 set so_extension "so"
89 # The base name. We use it for several purposes.
90 set main [file rootname [file tail $file]]
91 if {! [runtest_file_p $runtests $main]} {
92 # Simply skip it.
93 return 1
96 if {! [bytecompile_file $file [pwd]]} {
97 fail "bytecompile $file"
98 # FIXME - should use `untested' on all remaining tests.
99 # But that is hard.
100 return 0
102 pass "bytecompile $file"
104 set bytefile [file rootname [file tail $file]].class
105 if {! [gcj_jni_build_header $bytefile]} {
106 # FIXME
107 return 0
110 set cfile [file rootname $file].c
111 set cxxflags ""
112 set cxxldlibflags {}
113 # If there is no `.c' file, assume there is a `.cc' file.
114 if {! [file exists $cfile]} {
115 set cfile [file rootname $file].cc
117 set cxxflaglist {}
118 foreach arg [split [libjava_find_lib libstdc++-v3/src stdc++] " "] {
119 switch -glob -- $arg {
120 "-L*" {
121 set arg [string range $arg 2 end]
122 lappend cxxldlibflags $arg
123 # Strip the `.libs' directory; we link with libtool which
124 # doesn't need it.
125 set arg "-L[file dirname $arg]"
128 lappend cxxflaglist $arg
129 # In case the libstdc++ is not installed yet, we pass the build
130 # directory of it to the cxxflaglist.
131 lappend cxxflaglist "-L$cxxldlibflags"
134 # Darwin does not yet have a libgcc_s.dylib library.
135 # So we add it here. If the libgcc_s is installed, the libstdc++
136 # would pick it up.
137 if { [ regexp {powerpc-apple-darwin} $host_triplet] } {
138 lappend cxxflaglist "-lgcc_s -lstdc++"
139 } else {
140 lappend cxxflaglist "-lstdc++"
143 set cxxflags [join $cxxflaglist]
146 if {! [gcj_jni_compile_c_to_so $cfile $cxxflags]} {
147 # FIXME
148 return 0
151 set args [list "additional_flags=-fjni"]
152 if {! [gcj_link $main $main $file $args]} {
153 # FIXME
154 return 0
157 set resultfile [file rootname $file].out
159 if {! [gcj_invoke $main $resultfile $cxxldlibflags]} {
160 # FIXME
161 return 0
164 # We purposely ignore errors here; we still want to run the other
165 # appropriate tests.
166 set errname [file rootname [file tail $file]]
167 set gij [libjava_find_gij]
168 # libjava_find_gij will return "" if it couldn't find the
169 # program; in this case we want to skip the test.
170 # If the libraries are not installed yet, we have to pass them via
171 # cxxldlibflags to libjava_invoke.
172 if {$INTERPRETER == "yes" && $gij != ""} {
173 libjava_invoke $errname "gij test" opts $gij \
174 "" $resultfile $cxxldlibflags $main
177 # When we succeed we remove all our clutter.
178 eval gcj_cleanup [glob -nocomplain -- ${main}.*] \
179 [list $main lib${main}.${so_extension}]
181 return 1
184 # Compile a single C file and produce a binary. OPTIONS is a list of
185 # options to pass to the compiler. Returns 0 on failure, 1 on
186 # success.
187 proc gcj_jni_invocation_compile_c_to_binary {file {options {}}} {
188 global srcdir
189 global host_triplet
190 verbose "options: $options"
191 set options_cxx $options
192 set options ""
194 set filename [file tail $file]
195 set name [file rootname $filename]
197 # Find jni.h.
198 lappend options "additional_flags=-I$srcdir/../include"
200 # Append C++ options
201 lappend options "additional_flags=$options_cxx"
203 set x [libjava_prune_warnings \
204 [target_compile $file $name executable $options]]
205 if {$x != ""} {
206 verbose "target_compile failed: $x" 2
207 fail "$filename compilation"
208 return 0
211 pass "$filename compilation"
212 return 1
215 # Do all the work for a single invocation API test. Return 0 on
216 # failure.
217 proc gcj_jni_invocation_test_one {file} {
218 global runtests
219 global host_triplet
220 global INTERPRETER
222 # The base name. We use it for several purposes.
223 set main [file rootname [file tail $file]]
224 if {! [runtest_file_p $runtests $main]} {
225 # Simply skip it.
226 return 1
229 if {! [bytecompile_file $file [pwd]]} {
230 fail "bytecompile $file"
231 # FIXME - should use `untested' on all remaining tests.
232 # But that is hard.
233 return 0
235 pass "bytecompile $file"
237 set cfile [file rootname $file].c
238 set cxxflags "-lgcj"
240 if {! [gcj_jni_invocation_compile_c_to_binary $cfile $cxxflags]} {
241 # FIXME
242 return 0
245 set resultfile [file rootname $file].out
247 if {! [gcj_invoke $main $resultfile ""]} {
248 # FIXME
249 return 0
252 # We purposely ignore errors here; we still want to run the other
253 # appropriate tests.
254 set errname [file rootname [file tail $file]]
256 # When we succeed we remove all our clutter.
257 eval gcj_cleanup [glob -nocomplain -- ${main}.*] \
258 [list $main]
260 return 1
263 # Run the JNI tests.
264 proc gcj_jni_run {} {
265 global srcdir subdir
266 global build_triplet host_triplet
268 # For now we only test JNI on native builds.
269 if {$build_triplet == $host_triplet} {
270 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/*.java] } srcfiles
272 foreach x $srcfiles {
273 gcj_jni_test_one $x
276 # Run JNI invocation API tests
277 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/invocation/*.java] } srcfiles
279 foreach x $srcfiles {
280 gcj_jni_invocation_test_one $x
282 } else {
283 verbose "JNI tests not run in cross-compilation environment"
287 gcj_jni_run