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
6 proc gcj_jni_compile_c_to_so {file {options {}}} {
9 verbose "options: $options"
10 set options_cxx $options
13 # Apple uses a different extension for shared/dynamic libraries
14 # so we check against powerpc-apple-darwin and set them to
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*"] } {
24 } elseif { [istarget "*-*-cygwin*"] || [istarget "*-*-mingw*"] } {
25 set so_extension "dll"
31 set filename [file tail $file]
32 set name [file rootname $filename]
33 set soname lib${name}.${so_extension}
35 if { [istarget "*arm-*eabi*"] } {
36 lappend options "additional_flags=-fexceptions"
39 lappend options "additional_flags=${so_flag} -fPIC"
40 # Find the generated header.
41 lappend options "additional_flags=-I. -I.. -I$srcdir/$subdir -fdollars-in-identifiers"
43 # Ensure that the generated header has correct prototypes.
44 set cfile [file rootname $file].c
45 if { [file exists $cfile]} {
46 # This option is only valid for C sources.
47 lappend options "additional_flags=-Wmissing-prototypes"
50 # Find jni.h and jni_md.h.
51 lappend options "additional_flags=-I$srcdir/../include -I$srcdir/../classpath/include"
54 lappend options "additional_flags=$options_cxx"
56 set x [libjava_prune_warnings \
57 [target_compile $file $soname executable $options]]
59 verbose "target_compile failed: $x" 2
60 fail "$filename compilation"
64 pass "$filename compilation"
68 # Build a header file from a .class file. Return 0 on failure.
69 proc gcj_jni_build_header {file} {
73 set file [file rootname $file]
75 set cmd "$gcjh -jni -force -classpath .:$libgcj_jar $file"
78 set x [string trim [libjava_prune_warnings \
79 [lindex [local_exec $cmd "" "" 300] 1]]]
81 verbose "local_exec failed: $x" 2
82 fail "$file header generation"
86 pass "$file header generation"
90 # Do all the work for a single JNI test. Return 0 on failure.
91 proc gcj_jni_test_one {file} {
96 # Apple uses a different extension for shared/dynamic libraries
97 # so we check against powerpc-apple-darwin and set them to
99 # HP-UX uses sl, so we check this too, otherwise we take so.
101 if { [istarget "*-*-darwin*"] } {
102 set so_extension "dylib"
103 } elseif { [istarget "hppa*-hp-hpux*"] } {
104 set so_extension "sl"
105 } elseif { [istarget "*-*-cygwin*"] || [istarget "*-*-mingw*"] } {
106 set so_extension "dll"
108 set so_extension "so"
111 # The base name. We use it for several purposes.
112 set main [file rootname [file tail $file]]
113 if {! [runtest_file_p $runtests $main]
114 || $main == "pr29812" || $main == "pr29812_injar"} {
119 # if {! [bytecompile_file $file [pwd]]} {
120 # fail "bytecompile $file"
121 # # FIXME - should use `untested' on all remaining tests.
122 # # But that is hard.
125 # pass "bytecompile $file"
127 # set bytefile [file rootname [file tail $file]].class
128 # if {! [gcj_jni_build_header $bytefile]} {
133 set cfile [file rootname $file].c
136 # If there is no `.c' file, assume there is a `.cc' file.
137 if {! [file exists $cfile]} {
138 set cfile [file rootname $file].cc
141 foreach arg [split [libjava_find_lib libstdc++-v3/src stdc++] " "] {
142 switch -glob -- $arg {
144 set arg [string range $arg 2 end]
145 lappend cxxldlibflags $arg
146 # Strip the `.libs' directory; we link with libtool which
148 set arg "-L[file dirname $arg]"
151 lappend cxxflaglist $arg
152 # In case the libstdc++ is not installed yet, we pass the build
153 # directory of it to the cxxflaglist.
154 lappend cxxflaglist "-L$cxxldlibflags"
157 # If you're building the compiler with --prefix set to a place
158 # where it's not yet installed, then the linker won't be able to
159 # find the libgcc used by libffi.dylib. We could pass the
160 # -dylib_file option, but that's complicated, and it's much easier
161 # to just make the linker find libgcc using -L options.
162 if { [istarget "*-*-darwin*"] } {
163 lappend cxxflaglist "-shared-libgcc -lstdc++"
165 lappend cxxflaglist "-lstdc++"
168 # ARM C++ emits an ABI warning for varargs.
169 if { [istarget "arm*"] } {
170 lappend cxxflaglist "-Wno-abi"
173 set cxxflags [join $cxxflaglist]
176 if {! [gcj_jni_compile_c_to_so $cfile $cxxflags]} {
181 set args [list "additional_flags=-fjni"]
182 if {! [gcj_link $main $main $file $args]} {
187 set resultfile [file rootname $file].out
189 if {! [gcj_invoke $main $resultfile $cxxldlibflags]} {
194 # We purposely ignore errors here; we still want to run the other
196 set errname [file rootname [file tail $file]]
197 set gij [libjava_find_gij]
198 # libjava_find_gij will return "" if it couldn't find the
199 # program; in this case we want to skip the test.
200 # If the libraries are not installed yet, we have to pass them via
201 # cxxldlibflags to libjava_invoke.
202 if {$INTERPRETER == "yes" && $gij != ""} {
203 libjava_invoke $errname "gij test" opts $gij \
204 "" $resultfile $cxxldlibflags \
205 -classpath $file $main
208 # When we succeed we remove all our clutter.
209 eval gcj_cleanup [glob -nocomplain -- ${main}.*] \
210 [list $main lib${main}.${so_extension}]
215 # Compile a single C file and produce a binary. OPTIONS is a list of
216 # options to pass to the compiler. Returns 0 on failure, 1 on
218 proc gcj_jni_invocation_compile_c_to_binary {file {options {}}} {
221 verbose "options: $options"
222 set options_cxx $options
225 set filename [file tail $file]
226 set name [file rootname $filename]
228 # Set some darwin specific options
229 if { [istarget "*-*-darwin*"] } {
230 lappend options "additional_flags= -bind_at_load -multiply_defined suppress"
232 # Find the generated header.
233 lappend options "additional_flags=-I. -I.. -I$srcdir/$subdir"
235 # Find jni.h and jni_md.h.
236 lappend options "additional_flags=-I$srcdir/../include -I$srcdir/../classpath/include -fdollars-in-identifiers"
239 lappend options "additional_flags=$options_cxx"
241 set x [libjava_prune_warnings \
242 [target_compile $file $name executable $options]]
244 verbose "target_compile failed: $x" 2
245 fail "$filename compilation"
249 pass "$filename compilation"
253 proc gcj_jni_get_cxxflags_invocation {} {
256 if {$libiconv == ""} {
257 set libiconv "-liconv"
259 if [info exists LIBJAVA] {
260 set libjava $LIBJAVA;
262 set libjava [libjava_find_lib libjava gcj]
264 set cxxflags "$libjava -ljvm"
266 # Darwin needs -liconv linked, otherwise we get some unresolved.
267 # If you're building the compiler with --prefix set to a place
268 # where it's not yet installed, then the linker won't be able to
269 # find the libgcc used by libffi.dylib. We could pass the
270 # -dylib_file option, but that's complicated, and it's much easier
271 # to just make the linker find libgcc using -L options.
272 # Similar logic applies to libgcj.
273 if { [istarget "*-*-darwin*"] } {
274 eval lappend cxxflags "-shared-libgcc -lgcj $libiconv"
277 if { [istarget "*-*-solaris*"] } {
278 lappend cxxflags "-lsocket"
284 # Do all the work for a single invocation API test. Return 0 on
286 proc gcj_jni_invocation_test_one {file} {
292 # The base name. We use it for several purposes.
293 set main [file rootname [file tail $file]]
294 if {! [runtest_file_p $runtests $main]} {
299 # if {! [bytecompile_file $file [pwd]]} {
300 # fail "bytecompile $file"
301 # # FIXME - should use `untested' on all remaining tests.
302 # # But that is hard.
305 # pass "bytecompile $file"
307 set cfile [file rootname $file].c
309 set cxxflags [gcj_jni_get_cxxflags_invocation]
310 if {! [gcj_jni_invocation_compile_c_to_binary $cfile $cxxflags]} {
315 set resultfile [file rootname $file].out
317 set env(CLASSPATH) $file
318 verbose "CLASSPATH = $env(CLASSPATH)"
319 if {! [gcj_invoke $main $resultfile ""]} {
326 # We purposely ignore errors here; we still want to run the other
328 set errname [file rootname [file tail $file]]
330 # When we succeed we remove all our clutter.
331 eval gcj_cleanup [glob -nocomplain -- ${main}.*] \
337 proc gcj_jni_pr29812 {} {
339 global INTERPRETER runtests
341 # Set up a global we need.
344 set b ${srcdir}/${subdir}
346 if {! [runtest_file_p $runtests pr29812]} {
351 if {! [gcj_jni_compile_c_to_so $b/pr29812.c ""]} {
354 if {! [gcj_jni_compile_c_to_so $b/pr29812_injar.c ""]} {
358 set gij [libjava_find_gij]
359 if {$INTERPRETER == "yes" && $gij != ""} {
360 if {! [libjava_invoke pr29812 "gij test" opts $gij \
361 "" $b/pr29812.out "" \
362 -classpath $b/pr29812.jar pr29812 $b/pr29812_injar.jar]} {
367 # When we succeed we remove all our clutter.
368 eval gcj_cleanup [glob -nocomplain -- *pr29812*]
372 proc gcj_jni_run {} {
374 global build_triplet host_triplet
376 # For now we only test JNI on native builds.
377 if {$build_triplet == $host_triplet} {
378 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/*.jar] } srcfiles
380 foreach x $srcfiles {
384 # Run JNI invocation API tests
385 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/invocation/*.jar] } srcfiles
387 foreach x $srcfiles {
388 gcj_jni_invocation_test_one $x
393 verbose "JNI tests not run in cross-compilation environment"