1 | /*
|
---|
2 | * Copyright 2000-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import java.util.Enumeration;
|
---|
23 | import java.util.StringTokenizer;
|
---|
24 | import java.util.Vector;
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 | import org.apache.tools.ant.Project;
|
---|
27 | import org.apache.tools.ant.Task;
|
---|
28 | import org.apache.tools.ant.taskdefs.optional.javah.JavahAdapter;
|
---|
29 | import org.apache.tools.ant.taskdefs.optional.javah.JavahAdapterFactory;
|
---|
30 | import org.apache.tools.ant.types.Commandline;
|
---|
31 | import org.apache.tools.ant.types.Path;
|
---|
32 | import org.apache.tools.ant.types.Reference;
|
---|
33 | import org.apache.tools.ant.util.JavaEnvUtils;
|
---|
34 | import org.apache.tools.ant.util.facade.FacadeTaskHelper;
|
---|
35 | import org.apache.tools.ant.util.facade.ImplementationSpecificArgument;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Generates JNI header files using javah.
|
---|
39 | *
|
---|
40 | * This task can take the following arguments:
|
---|
41 | * <ul>
|
---|
42 | * <li>classname - the fully-qualified name of a class</li>
|
---|
43 | * <li>outputFile - Concatenates the resulting header or source files for all
|
---|
44 | * the classes listed into this file</li>
|
---|
45 | * <li>destdir - Sets the directory where javah saves the header files or the
|
---|
46 | * stub files</li>
|
---|
47 | * <li>classpath</li>
|
---|
48 | * <li>bootclasspath</li>
|
---|
49 | * <li>force - Specifies that output files should always be written
|
---|
50 | (JDK1.2 only)</li>
|
---|
51 | * <li>old - Specifies that old JDK1.0-style header files should be generated
|
---|
52 | * (otherwise output file contain JNI-style native method
|
---|
53 | * function prototypes) (JDK1.2 only)</li>
|
---|
54 | * <li>stubs - generate C declarations from the Java object file (used with old)</li>
|
---|
55 | * <li>verbose - causes javah to print a message to stdout concerning the status
|
---|
56 | * of the generated files</li>
|
---|
57 | * <li>extdirs - Override location of installed extensions</li>
|
---|
58 | * </ul>
|
---|
59 | * Of these arguments, either <b>outputFile</b> or <b>destdir</b> is required,
|
---|
60 | * but not both. More than one classname may be specified, using a comma-separated
|
---|
61 | * list or by using <code><class name="xxx"></code> elements within the task.
|
---|
62 | * <p>
|
---|
63 | * When this task executes, it will generate C header and source files that
|
---|
64 | * are needed to implement native methods.
|
---|
65 | *
|
---|
66 | */
|
---|
67 |
|
---|
68 | public class Javah extends Task {
|
---|
69 |
|
---|
70 | private Vector classes = new Vector(2);
|
---|
71 | private String cls;
|
---|
72 | private File destDir;
|
---|
73 | private Path classpath = null;
|
---|
74 | private File outputFile = null;
|
---|
75 | private boolean verbose = false;
|
---|
76 | private boolean force = false;
|
---|
77 | private boolean old = false;
|
---|
78 | private boolean stubs = false;
|
---|
79 | private Path bootclasspath;
|
---|
80 | //private Path extdirs;
|
---|
81 | private static String lSep = System.getProperty("line.separator");
|
---|
82 | private FacadeTaskHelper facade = null;
|
---|
83 |
|
---|
84 | public Javah() {
|
---|
85 | facade = new FacadeTaskHelper(JavahAdapterFactory.getDefault());
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * the fully-qualified name of the class (or classes, separated by commas).
|
---|
90 | */
|
---|
91 | public void setClass(String cls) {
|
---|
92 | this.cls = cls;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Adds class to process.
|
---|
97 | */
|
---|
98 | public ClassArgument createClass() {
|
---|
99 | ClassArgument ga = new ClassArgument();
|
---|
100 | classes.addElement(ga);
|
---|
101 | return ga;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public class ClassArgument {
|
---|
105 | private String name;
|
---|
106 |
|
---|
107 | public ClassArgument() {
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void setName(String name) {
|
---|
111 | this.name = name;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public String getName() {
|
---|
115 | return name;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Names of the classes to process.
|
---|
121 | *
|
---|
122 | * @since Ant 1.6.3
|
---|
123 | */
|
---|
124 | public String[] getClasses() {
|
---|
125 | ArrayList al = new ArrayList();
|
---|
126 | if (cls != null) {
|
---|
127 | StringTokenizer tok = new StringTokenizer(cls, ",", false);
|
---|
128 | while (tok.hasMoreTokens()) {
|
---|
129 | al.add(tok.nextToken().trim());
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | Enumeration e = classes.elements();
|
---|
134 | while (e.hasMoreElements()) {
|
---|
135 | ClassArgument arg = (ClassArgument) e.nextElement();
|
---|
136 | al.add(arg.getName());
|
---|
137 | }
|
---|
138 | return (String[]) al.toArray(new String[0]);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Set the destination directory into which the Java source
|
---|
143 | * files should be compiled.
|
---|
144 | */
|
---|
145 | public void setDestdir(File destDir) {
|
---|
146 | this.destDir = destDir;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * The destination directory, if any.
|
---|
151 | *
|
---|
152 | * @since Ant 1.6.3
|
---|
153 | */
|
---|
154 | public File getDestdir() {
|
---|
155 | return destDir;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * the classpath to use.
|
---|
160 | */
|
---|
161 | public void setClasspath(Path src) {
|
---|
162 | if (classpath == null) {
|
---|
163 | classpath = src;
|
---|
164 | } else {
|
---|
165 | classpath.append(src);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Path to use for classpath.
|
---|
171 | */
|
---|
172 | public Path createClasspath() {
|
---|
173 | if (classpath == null) {
|
---|
174 | classpath = new Path(getProject());
|
---|
175 | }
|
---|
176 | return classpath.createPath();
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Adds a reference to a classpath defined elsewhere.
|
---|
181 | * @todo this needs to be documented in the HTML docs
|
---|
182 | */
|
---|
183 | public void setClasspathRef(Reference r) {
|
---|
184 | createClasspath().setRefid(r);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * The classpath to use.
|
---|
189 | *
|
---|
190 | * @since Ant 1.6.3
|
---|
191 | */
|
---|
192 | public Path getClasspath() {
|
---|
193 | return classpath;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * location of bootstrap class files.
|
---|
198 | */
|
---|
199 | public void setBootclasspath(Path src) {
|
---|
200 | if (bootclasspath == null) {
|
---|
201 | bootclasspath = src;
|
---|
202 | } else {
|
---|
203 | bootclasspath.append(src);
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Adds path to bootstrap class files.
|
---|
209 | */
|
---|
210 | public Path createBootclasspath() {
|
---|
211 | if (bootclasspath == null) {
|
---|
212 | bootclasspath = new Path(getProject());
|
---|
213 | }
|
---|
214 | return bootclasspath.createPath();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Adds a reference to a classpath defined elsewhere.
|
---|
219 | * @todo this needs to be documented in the HTML
|
---|
220 | */
|
---|
221 | public void setBootClasspathRef(Reference r) {
|
---|
222 | createBootclasspath().setRefid(r);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * The bootclasspath to use.
|
---|
227 | *
|
---|
228 | * @since Ant 1.6.3
|
---|
229 | */
|
---|
230 | public Path getBootclasspath() {
|
---|
231 | return bootclasspath;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Concatenates the resulting header or source files for all
|
---|
236 | * the classes listed into this file.
|
---|
237 | */
|
---|
238 | public void setOutputFile(File outputFile) {
|
---|
239 | this.outputFile = outputFile;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * The destination file, if any.
|
---|
244 | *
|
---|
245 | * @since Ant 1.6.3
|
---|
246 | */
|
---|
247 | public File getOutputfile() {
|
---|
248 | return outputFile;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * If true, output files should always be written (JDK1.2 only).
|
---|
253 | */
|
---|
254 | public void setForce(boolean force) {
|
---|
255 | this.force = force;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Whether output files should always be written.
|
---|
260 | *
|
---|
261 | * @since Ant 1.6.3
|
---|
262 | */
|
---|
263 | public boolean getForce() {
|
---|
264 | return force;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * If true, specifies that old JDK1.0-style header files should be
|
---|
269 | * generated.
|
---|
270 | * (otherwise output file contain JNI-style native method function prototypes) (JDK1.2 only)
|
---|
271 | */
|
---|
272 | public void setOld(boolean old) {
|
---|
273 | this.old = old;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Whether old JDK1.0-style header files should be generated.
|
---|
278 | *
|
---|
279 | * @since Ant 1.6.3
|
---|
280 | */
|
---|
281 | public boolean getOld() {
|
---|
282 | return old;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * If true, generate C declarations from the Java object file (used with old).
|
---|
287 | */
|
---|
288 | public void setStubs(boolean stubs) {
|
---|
289 | this.stubs = stubs;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Whether C declarations from the Java object file should be generated.
|
---|
294 | *
|
---|
295 | * @since Ant 1.6.3
|
---|
296 | */
|
---|
297 | public boolean getStubs() {
|
---|
298 | return stubs;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * If true, causes Javah to print a message concerning
|
---|
303 | * the status of the generated files.
|
---|
304 | */
|
---|
305 | public void setVerbose(boolean verbose) {
|
---|
306 | this.verbose = verbose;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Whether verbose output should get generated.
|
---|
311 | *
|
---|
312 | * @since Ant 1.6.3
|
---|
313 | */
|
---|
314 | public boolean getVerbose() {
|
---|
315 | return verbose;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Choose the implementation for this particular task.
|
---|
320 | * @param impl the name of the implemenation
|
---|
321 | * @since Ant 1.6.3
|
---|
322 | */
|
---|
323 | public void setImplementation(String impl) {
|
---|
324 | if ("default".equals(impl)) {
|
---|
325 | facade.setImplementation(JavahAdapterFactory.getDefault());
|
---|
326 | } else {
|
---|
327 | facade.setImplementation(impl);
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Adds an implementation specific command-line argument.
|
---|
333 | * @return a ImplementationSpecificArgument to be configured
|
---|
334 | *
|
---|
335 | * @since Ant 1.6.3
|
---|
336 | */
|
---|
337 | public ImplementationSpecificArgument createArg() {
|
---|
338 | ImplementationSpecificArgument arg =
|
---|
339 | new ImplementationSpecificArgument();
|
---|
340 | facade.addImplementationArgument(arg);
|
---|
341 | return arg;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Returns the (implementation specific) settings given as nested
|
---|
346 | * arg elements.
|
---|
347 | *
|
---|
348 | * @since Ant 1.6.3
|
---|
349 | */
|
---|
350 | public String[] getCurrentArgs() {
|
---|
351 | return facade.getArgs();
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Execute the task
|
---|
356 | *
|
---|
357 | * @throws BuildException is there is a problem in the task execution.
|
---|
358 | */
|
---|
359 | public void execute() throws BuildException {
|
---|
360 | // first off, make sure that we've got a srcdir
|
---|
361 |
|
---|
362 | if ((cls == null) && (classes.size() == 0)) {
|
---|
363 | throw new BuildException("class attribute must be set!",
|
---|
364 | getLocation());
|
---|
365 | }
|
---|
366 |
|
---|
367 | if ((cls != null) && (classes.size() > 0)) {
|
---|
368 | throw new BuildException("set class attribute or class element, "
|
---|
369 | + "not both.", getLocation());
|
---|
370 | }
|
---|
371 |
|
---|
372 | if (destDir != null) {
|
---|
373 | if (!destDir.isDirectory()) {
|
---|
374 | throw new BuildException("destination directory \"" + destDir
|
---|
375 | + "\" does not exist or is not a directory", getLocation());
|
---|
376 | }
|
---|
377 | if (outputFile != null) {
|
---|
378 | throw new BuildException("destdir and outputFile are mutually "
|
---|
379 | + "exclusive", getLocation());
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (classpath == null) {
|
---|
384 | classpath = (new Path(getProject())).concatSystemClasspath("last");
|
---|
385 | } else {
|
---|
386 | classpath = classpath.concatSystemClasspath("ignore");
|
---|
387 | }
|
---|
388 |
|
---|
389 | JavahAdapter ad =
|
---|
390 | JavahAdapterFactory.getAdapter(facade.getImplementation(),
|
---|
391 | this);
|
---|
392 | if (!ad.compile(this)) {
|
---|
393 | throw new BuildException("compilation failed");
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Logs the compilation parameters, adds the files to compile and logs the
|
---|
399 | * "niceSourceList"
|
---|
400 | */
|
---|
401 | public void logAndAddFiles(Commandline cmd) {
|
---|
402 | logAndAddFilesToCompile(cmd);
|
---|
403 | }
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Logs the compilation parameters, adds the files to compile and logs the
|
---|
407 | * "niceSourceList"
|
---|
408 | */
|
---|
409 | protected void logAndAddFilesToCompile(Commandline cmd) {
|
---|
410 | log("Compilation " + cmd.describeArguments(),
|
---|
411 | Project.MSG_VERBOSE);
|
---|
412 |
|
---|
413 | StringBuffer niceClassList = new StringBuffer();
|
---|
414 | String[] c = getClasses();
|
---|
415 | for (int i = 0; i < c.length; i++) {
|
---|
416 | cmd.createArgument().setValue(c[i]);
|
---|
417 | niceClassList.append(" " + c[i] + lSep);
|
---|
418 | }
|
---|
419 |
|
---|
420 | StringBuffer prefix = new StringBuffer("Class");
|
---|
421 | if (c.length > 1) {
|
---|
422 | prefix.append("es");
|
---|
423 | }
|
---|
424 | prefix.append(" to be compiled:");
|
---|
425 | prefix.append(lSep);
|
---|
426 |
|
---|
427 | log(prefix.toString() + niceClassList.toString(), Project.MSG_VERBOSE);
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|