001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.javah;
019:
020: import java.io.File;
021:
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.launch.Locator;
024: import org.apache.tools.ant.taskdefs.ExecuteJava;
025: import org.apache.tools.ant.taskdefs.optional.Javah;
026: import org.apache.tools.ant.types.Commandline;
027: import org.apache.tools.ant.types.Path;
028:
029: /**
030: * Adapter to com.sun.tools.javah.oldjavah.Main or com.sun.tools.javah.Main.
031: *
032: * @since Ant 1.6.3
033: */
034: public class SunJavah implements JavahAdapter {
035:
036: /** the name of the javah adapter - sun */
037: public static final String IMPLEMENTATION_NAME = "sun";
038:
039: /**
040: * Performs the actual compilation.
041: * @param javah the calling javah task.
042: * @return true if the compilation was successful.
043: * @throws BuildException if there is an error.
044: * @since Ant 1.6.3
045: */
046: public boolean compile(Javah javah) throws BuildException {
047: Commandline cmd = setupJavahCommand(javah);
048: ExecuteJava ej = new ExecuteJava();
049: Class c = null;
050: try {
051: try {
052: // first search for the "old" javah class in 1.4.2 tools.jar
053: c = Class.forName("com.sun.tools.javah.oldjavah.Main");
054: } catch (ClassNotFoundException cnfe) {
055: // assume older than 1.4.2 tools.jar
056: c = Class.forName("com.sun.tools.javah.Main");
057: }
058: } catch (ClassNotFoundException ex) {
059: throw new BuildException("Can't load javah", ex, javah
060: .getLocation());
061: }
062: cmd.setExecutable(c.getName());
063: ej.setJavaCommand(cmd);
064: File f = Locator.getClassSource(c);
065: if (f != null) {
066: ej.setClasspath(new Path(javah.getProject(), f.getPath()));
067: }
068: return ej.fork(javah) == 0;
069: }
070:
071: private Commandline setupJavahCommand(Javah javah) {
072: Commandline cmd = new Commandline();
073:
074: if (javah.getDestdir() != null) {
075: cmd.createArgument().setValue("-d");
076: cmd.createArgument().setFile(javah.getDestdir());
077: }
078:
079: if (javah.getOutputfile() != null) {
080: cmd.createArgument().setValue("-o");
081: cmd.createArgument().setFile(javah.getOutputfile());
082: }
083:
084: if (javah.getClasspath() != null) {
085: cmd.createArgument().setValue("-classpath");
086: cmd.createArgument().setPath(javah.getClasspath());
087: }
088:
089: if (javah.getVerbose()) {
090: cmd.createArgument().setValue("-verbose");
091: }
092: if (javah.getOld()) {
093: cmd.createArgument().setValue("-old");
094: }
095: if (javah.getForce()) {
096: cmd.createArgument().setValue("-force");
097: }
098: if (javah.getStubs() && !javah.getOld()) {
099: throw new BuildException(
100: "stubs only available in old mode.", javah
101: .getLocation());
102: }
103:
104: if (javah.getStubs()) {
105: cmd.createArgument().setValue("-stubs");
106: }
107: Path bcp = new Path(javah.getProject());
108: if (javah.getBootclasspath() != null) {
109: bcp.append(javah.getBootclasspath());
110: }
111: bcp = bcp.concatSystemBootClasspath("ignore");
112: if (bcp.size() > 0) {
113: cmd.createArgument().setValue("-bootclasspath");
114: cmd.createArgument().setPath(bcp);
115: }
116:
117: cmd.addArguments(javah.getCurrentArgs());
118:
119: javah.logAndAddFiles(cmd);
120: return cmd;
121: }
122:
123: }
|