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
15 # dylib, else we assume it's .so
17 if { [istarget "*-*-darwin*"] } {
18 set so_extension "dylib"
19 set so_flag "-dynamiclib"
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"
40 lappend options "additional_flags=-I$srcdir/../include"
43 lappend options "additional_flags=$options_cxx"
45 set x [libjava_prune_warnings \
46 [target_compile $file $soname executable $options]]
48 verbose "target_compile failed: $x" 2
49 fail "$filename compilation"
53 pass "$filename compilation"
57 # Build a header file from a .class file. Return 0 on failure.
58 proc gcj_jni_build_header {file} {
60 set file [file rootname $file]
61 set x [string trim [libjava_prune_warnings \
62 [lindex [local_exec "$gcjh -jni $file" "" "" 300] 1]]]
64 verbose "local_exec failed: $x" 2
65 fail "$file header generation"
69 pass "$file header generation"
73 # Do all the work for a single JNI test. Return 0 on failure.
74 proc gcj_jni_test_one {file} {
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 { [istarget "*-*-darwin*"] } {
84 set so_extension "dylib"
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]} {
96 if {! [bytecompile_file $file [pwd]]} {
97 fail "bytecompile $file"
98 # FIXME - should use `untested' on all remaining tests.
102 pass "bytecompile $file"
104 set bytefile [file rootname [file tail $file]].class
105 if {! [gcj_jni_build_header $bytefile]} {
110 set cfile [file rootname $file].c
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
118 foreach arg [split [libjava_find_lib libstdc++-v3/src stdc++] " "] {
119 switch -glob -- $arg {
121 set arg [string range $arg 2 end]
122 lappend cxxldlibflags $arg
123 # Strip the `.libs' directory; we link with libtool which
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++
137 if { [istarget "*-*-darwin*"] } {
138 lappend cxxflaglist "-lgcc_s -lstdc++"
140 lappend cxxflaglist "-lstdc++"
143 set cxxflags [join $cxxflaglist]
146 if {! [gcj_jni_compile_c_to_so $cfile $cxxflags]} {
151 set args [list "additional_flags=-fjni"]
152 if {! [gcj_link $main $main $file $args]} {
157 set resultfile [file rootname $file].out
159 if {! [gcj_invoke $main $resultfile $cxxldlibflags]} {
164 # We purposely ignore errors here; we still want to run the other
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}]
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
187 proc gcj_jni_invocation_compile_c_to_binary {file {options {}}} {
190 verbose "options: $options"
191 set options_cxx $options
194 set filename [file tail $file]
195 set name [file rootname $filename]
197 # Set some darwin specific options
198 if { [istarget "*-*-darwin*"] } {
199 lappend options "additional_flags=-multiply_defined suppress"
201 # Find the generated header.
202 lappend options "additional_flags=-I. -I.."
205 lappend options "additional_flags=-I$srcdir/../include"
208 lappend options "additional_flags=$options_cxx"
210 set x [libjava_prune_warnings \
211 [target_compile $file $name executable $options]]
213 verbose "target_compile failed: $x" 2
214 fail "$filename compilation"
218 pass "$filename compilation"
222 # Do all the work for a single invocation API test. Return 0 on
224 proc gcj_jni_invocation_test_one {file} {
229 # The base name. We use it for several purposes.
230 set main [file rootname [file tail $file]]
231 if {! [runtest_file_p $runtests $main]} {
236 if {! [bytecompile_file $file [pwd]]} {
237 fail "bytecompile $file"
238 # FIXME - should use `untested' on all remaining tests.
242 pass "bytecompile $file"
244 set cfile [file rootname $file].c
245 # Darwin needs -liconv linked, otherwise we get some unresolved.
246 if { [istarget "*-*-darwin*"] } {
247 set cxxflags "-L../.libs -lgcj -liconv"
249 set cxxflags "-L../.libs -lgcj"
252 if {! [gcj_jni_invocation_compile_c_to_binary $cfile $cxxflags]} {
257 set resultfile [file rootname $file].out
259 if {! [gcj_invoke $main $resultfile ""]} {
264 # We purposely ignore errors here; we still want to run the other
266 set errname [file rootname [file tail $file]]
268 # When we succeed we remove all our clutter.
269 eval gcj_cleanup [glob -nocomplain -- ${main}.*] \
276 proc gcj_jni_run {} {
278 global build_triplet host_triplet
280 # For now we only test JNI on native builds.
281 if {$build_triplet == $host_triplet} {
282 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/*.java] } srcfiles
284 foreach x $srcfiles {
288 # Run JNI invocation API tests
289 catch { lsort [glob -nocomplain ${srcdir}/${subdir}/invocation/*.java] } srcfiles
291 foreach x $srcfiles {
292 gcj_jni_invocation_test_one $x
295 verbose "JNI tests not run in cross-compilation environment"