Add a --debug=stree option to show Node status. (Kevin Quick)
[scons.git] / test / Java / JAVAH.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import os
28 import string
29 import sys
30 import TestSCons
31
32 python = TestSCons.python
33
34 test = TestSCons.TestSCons()
35
36 test.write('myjavah.py', r"""
37 import sys
38 args = sys.argv[1:]
39 while args:
40     a = args[0]
41     if a == '-d':
42         outdir = args[1]
43         args = args[1:]
44     elif a == '-o':
45         outfile = open(args[1], 'wb')
46         args = args[1:]
47     elif a == '-classpath':
48         args = args[1:]
49     elif a == '-sourcepath':
50         args = args[1:]
51     else:
52         break
53     args = args[1:]
54 for file in args:
55     infile = open(file, 'rb')
56     for l in infile.readlines():
57         if l[:9] != '/*javah*/':
58             outfile.write(l)
59 sys.exit(0)
60 """)
61
62 test.write('SConstruct', """
63 env = Environment(tools = ['javah'],
64                   JAVAH = r'%s myjavah.py')
65 env.JavaH(target = File('test1.h'), source = 'test1.java')
66 """ % (python))
67
68 test.write('test1.java', """\
69 test1.java
70 /*javah*/
71 line 3
72 """)
73
74 test.run(arguments = '.', stderr = None)
75
76 test.must_match('test1.h', "test1.java\nline 3\n")
77
78 if os.path.normcase('.java') == os.path.normcase('.JAVA'):
79
80     test.write('SConstruct', """\
81 env = Environment(tools = ['javah'],
82                   JAVAH = r'%s myjavah.py')
83 env.JavaH(target = File('test2.h'), source = 'test2.JAVA')
84 """ % python)
85
86     test.write('test2.JAVA', """\
87 test2.JAVA
88 /*javah*/
89 line 3
90 """)
91
92     test.run(arguments = '.', stderr = None)
93
94     test.must_match('test2.h', "test2.JAVA\nline 3\n")
95
96
97 if test.detect_tool('javac'):
98     where_javac = test.detect('JAVAC', 'javac')
99 else:
100     import SCons.Environment
101     env = SCons.Environment.Environment()
102     where_javac = env.WhereIs('javac', os.environ['PATH'])
103     if not where_javac:
104         where_javac = env.WhereIs('javac', '/usr/local/j2sdk1.3.1/bin')
105         if not where_javac:
106             print "Could not find Java javac, skipping test(s)."
107             test.pass_test(1)
108
109 if test.detect_tool('javah'):
110     where_javah = test.detect('JAVAH', 'javah')
111 else:
112     import SCons.Environment
113     env = SCons.Environment.Environment()
114     where_javah = env.WhereIs('javah', os.environ['PATH'])
115     if not where_javah:
116         where_javah = env.WhereIs('javah', '/usr/local/j2sdk1.3.1/bin')
117         if not where_javah:
118             print "Could not find Java javah, skipping test(s)."
119             test.pass_test(1)
120
121
122
123 test.write("wrapper.py", """\
124 import os
125 import string
126 import sys
127 open('%s', 'ab').write("wrapper.py %%s\\n" %% string.join(sys.argv[1:]))
128 os.system(string.join(sys.argv[1:], " "))
129 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
130
131 test.write('SConstruct', """
132 foo = Environment(tools = ['javac', 'javah'],
133                   JAVAC = '%(where_javac)s',
134                   JAVAH = '%(where_javah)s')
135 javah = foo.Dictionary('JAVAH')
136 bar = foo.Copy(JAVAH = r'%(python)s wrapper.py ' + javah)
137 foo.Java(target = 'class1', source = 'com/sub/foo')
138 bar_classes = bar.Java(target = 'class2', source = 'com/sub/bar')
139 foo_classes = foo.Java(target = 'class3', source = 'src')
140 foo.JavaH(target = 'outdir1',
141           source = ['class1/com/sub/foo/Example1.class',
142                     'class1/com/other/Example2',
143                     'class1/com/sub/foo/Example3'],
144           JAVACLASSDIR = 'class1')
145 bar.JavaH(target = 'outdir2', source = bar_classes)
146 foo.JavaH(target = File('output.h'), source = foo_classes)
147 foo.Install('class4/com/sub/foo', 'class1/com/sub/foo/Example1.class')
148 foo.JavaH(target = 'outdir4',
149           source = ['class4/com/sub/foo/Example1.class'],
150           JAVACLASSDIR = 'class4')
151 """ % locals())
152
153 test.subdir('com',
154             ['com', 'sub'],
155             ['com', 'sub', 'foo'],
156             ['com', 'sub', 'bar'],
157             'src')
158
159 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
160 package com.sub.foo;
161
162 public class Example1
163 {
164
165      public static void main(String[] args)
166      {
167
168      }
169
170 }
171 """)
172
173 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
174 package com.other;
175
176 public class Example2
177 {
178
179      public static void main(String[] args)
180      {
181
182      }
183
184 }
185 """)
186
187 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
188 package com.sub.foo;
189
190 public class Example3
191 {
192
193      public static void main(String[] args)
194      {
195
196      }
197
198 }
199 """)
200
201 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
202 package com.sub.bar;
203
204 public class Example4
205 {
206
207      public static void main(String[] args)
208      {
209
210      }
211
212 }
213 """)
214
215 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
216 package com.other;
217
218 public class Example5
219 {
220
221      public static void main(String[] args)
222      {
223
224      }
225
226 }
227 """)
228
229 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
230 package com.sub.bar;
231
232 public class Example6
233 {
234
235      public static void main(String[] args)
236      {
237
238      }
239
240 }
241 """)
242
243 test.write(['src', 'Test.java'], """\
244 class Empty {
245 }
246
247 interface Listener {
248   public void execute();
249 }
250
251 public
252 class
253 Test {
254   class Inner {
255     void go() {
256       use(new Listener() {
257         public void execute() {
258           System.out.println("In Inner");
259         }
260       });
261     }
262     String s1 = "class A";
263     String s2 = "new Listener() { }";
264     /* class B */
265     /* new Listener() { } */
266   }
267
268   public static void main(String[] args) {
269     new Test().run();
270   }
271
272   void run() {
273     use(new Listener() {
274       public void execute() {
275         use(new Listener( ) {
276           public void execute() {
277             System.out.println("Inside execute()");
278           }
279         });
280       }
281     });
282
283     new Inner().go();
284   }
285
286   void use(Listener l) {
287     l.execute();
288   }
289 }
290
291 class Private {
292   void run() {
293     new Listener() {
294       public void execute() {
295       }
296     };
297   }
298 }
299 """)
300
301 test.run(arguments = '.')
302
303 test.fail_test(test.read('wrapper.out') != "wrapper.py %(where_javah)s -d outdir2 -classpath class2 com.sub.bar.Example4 com.other.Example5 com.sub.bar.Example6\n" % locals())
304
305 test.must_exist(['outdir1', 'com_sub_foo_Example1.h'])
306 test.must_exist(['outdir1', 'com_other_Example2.h'])
307 test.must_exist(['outdir1', 'com_sub_foo_Example3.h'])
308
309 test.must_exist(['outdir2', 'com_sub_bar_Example4.h'])
310 test.must_exist(['outdir2', 'com_other_Example5.h'])
311 test.must_exist(['outdir2', 'com_sub_bar_Example6.h'])
312
313 test.up_to_date(arguments = '.')
314
315 test.pass_test()