OSDN Git Service

* lib/libjava.exp (gcj_invoke): Moved...
[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 [libjava_prune_warnings \
19            [target_compile $file $soname executable $options]]
20   if {$x != ""} {
21     verbose "target_compile failed: $x" 2
22     fail "$name.c compilation"
23     return 0
24   }
25
26   pass "$name.c compilation"
27   return 1
28 }
29
30 # Build a header file from a .class file.  Return 0 on failure.
31 proc gcj_jni_build_header {file} {
32   set gcjh [find_gcjh]
33   set file [file rootname $file]
34   set x [string trim [libjava_prune_warnings \
35                         [lindex [local_exec "$gcjh -jni $file" "" "" 300] 1]]]
36   if {$x != ""} {
37     verbose "local_exec failed: $x" 2
38     fail "$file header generation"
39     return 0
40   }
41
42   pass "$file header generation"
43   return 1
44 }
45
46 # Do all the work for a single JNI test.  Return 0 on failure.
47 proc gcj_jni_test_one {file} {
48   global runtests
49
50   # The base name.  We use it for several purposes.
51   set main [file rootname [file tail $file]]
52   if {! [runtest_file_p $runtests $main]} {
53     # Simply skip it.
54     return 1
55   }
56
57   if {! [bytecompile_file $file [pwd]]} {
58     fail "bytecompile $file"
59     # FIXME - should use `untested' on all remaining tests.
60     # But that is hard.
61     return 0
62   }
63   pass "bytecompile $file"
64
65   set bytefile [file rootname [file tail $file]].class
66   if {! [gcj_jni_build_header $bytefile]} {
67     # FIXME
68     return 0
69   }
70
71   set cfile [file rootname $file].c
72   set cxxflags ""
73   set cxxldlibflags {}
74   # If there is no `.c' file, assume there is a `.cc' file.
75   if {! [file exists $cfile]} {
76     set cfile [file rootname $file].cc
77
78     set cxxflaglist {}
79     foreach arg [split [libjava_find_lib libstdc++-v3/src stdc++] " "] {
80       switch -glob -- $arg {
81         "-L*" {
82           set arg [string range $arg 2 end]
83           lappend cxxldlibflags $arg
84           # Strip the `.libs' directory; we link with libtool which
85           # doesn't need it.
86           set arg "-L[file dirname $arg]"
87         }
88       }
89       lappend cxxflaglist $arg
90     }
91
92     lappend cxxflaglist "-lstdc++"
93     set cxxflags [join $cxxflaglist]
94   }
95
96   if {! [gcj_jni_compile_c_to_so $cfile]} {
97     # FIXME
98     return 0
99   }
100
101   # We use -l$main because the .so is named the same as the main
102   # program.
103   set args [list "additional_flags=-fjni -L. -l$main $cxxflags"]
104   if {! [gcj_link $main $main $file $args]} {
105     # FIXME
106     return 0
107   }
108
109   if {! [gcj_invoke $main [file rootname $file].out $cxxldlibflags]} {
110     # FIXME
111     return 0
112   }
113
114   # When we succeed we remove all our clutter.
115   eval gcj_cleanup [glob -nocomplain -- ${main}.*] [list $main lib${main}.so]
116
117   return 1
118 }
119
120 # Run the JNI tests.
121 proc gcj_jni_run {} {
122   global srcdir subdir
123   global build_triplet host_triplet
124
125   # For now we only test JNI on native builds.
126   if {$build_triplet == $host_triplet} {
127     catch { lsort [glob -nocomplain ${srcdir}/${subdir}/*.java] } srcfiles
128
129     foreach x $srcfiles {
130       gcj_jni_test_one $x
131     }
132   } else {
133     verbose "JNI tests not run in cross-compilation environment"
134   }
135 }
136
137 gcj_jni_run