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:
019: package org.apache.tools.ant.taskdefs.compilers;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.types.Commandline;
024: import org.apache.tools.ant.types.Path;
025:
026: /**
027: * The implementation of the jikes compiler.
028: * This is primarily a cut-and-paste from the original javac task before it
029: * was refactored.
030: *
031: * @since Ant 1.3
032: */
033: public class Jikes extends DefaultCompilerAdapter {
034:
035: /**
036: * Performs a compile using the Jikes compiler from IBM.
037: * Mostly of this code is identical to doClassicCompile()
038: * However, it does not support all options like
039: * extdirs, deprecation and so on, because
040: * there is no option in jikes and I don't understand
041: * what they should do.
042: *
043: * It has been successfully tested with jikes >1.10.
044: * @return true if the compilation succeeded
045: * @throws BuildException on error
046: */
047: public boolean execute() throws BuildException {
048: attributes.log("Using jikes compiler", Project.MSG_VERBOSE);
049:
050: Commandline cmd = new Commandline();
051:
052: // For -sourcepath, use the "sourcepath" value if present.
053: // Otherwise default to the "srcdir" value.
054: Path sourcepath = null;
055: if (compileSourcepath != null) {
056: sourcepath = compileSourcepath;
057: } else {
058: sourcepath = src;
059: }
060: // If the buildfile specifies sourcepath="", then don't
061: // output any sourcepath.
062: if (sourcepath.size() > 0) {
063: cmd.createArgument().setValue("-sourcepath");
064: cmd.createArgument().setPath(sourcepath);
065: }
066:
067: Path classpath = new Path(project);
068:
069: if (bootclasspath == null || bootclasspath.size() == 0) {
070: // no bootclasspath, therefore, get one from the java runtime
071: includeJavaRuntime = true;
072: } else {
073: // there is a bootclasspath stated. By default, the
074: // includeJavaRuntime is false. If the user has stated a
075: // bootclasspath and said to include the java runtime, it's on
076: // their head!
077: }
078: classpath.append(getCompileClasspath());
079:
080: // if the user has set JIKESPATH we should add the contents as well
081: String jikesPath = System.getProperty("jikes.class.path");
082: if (jikesPath != null) {
083: classpath.append(new Path(project, jikesPath));
084: }
085:
086: if (extdirs != null && extdirs.size() > 0) {
087: cmd.createArgument().setValue("-extdirs");
088: cmd.createArgument().setPath(extdirs);
089: }
090:
091: String exec = getJavac().getExecutable();
092: cmd.setExecutable(exec == null ? "jikes" : exec);
093:
094: if (deprecation) {
095: cmd.createArgument().setValue("-deprecation");
096: }
097:
098: if (destDir != null) {
099: cmd.createArgument().setValue("-d");
100: cmd.createArgument().setFile(destDir);
101: }
102:
103: cmd.createArgument().setValue("-classpath");
104: cmd.createArgument().setPath(classpath);
105:
106: if (encoding != null) {
107: cmd.createArgument().setValue("-encoding");
108: cmd.createArgument().setValue(encoding);
109: }
110: if (debug) {
111: String debugLevel = attributes.getDebugLevel();
112: if (debugLevel != null) {
113: cmd.createArgument().setValue("-g:" + debugLevel);
114: } else {
115: cmd.createArgument().setValue("-g");
116: }
117: } else {
118: cmd.createArgument().setValue("-g:none");
119: }
120: if (optimize) {
121: cmd.createArgument().setValue("-O");
122: }
123: if (verbose) {
124: cmd.createArgument().setValue("-verbose");
125: }
126: if (depend) {
127: cmd.createArgument().setValue("-depend");
128: }
129:
130: if (target != null) {
131: cmd.createArgument().setValue("-target");
132: cmd.createArgument().setValue(target);
133: }
134:
135: /**
136: * XXX
137: * Perhaps we shouldn't use properties for these
138: * three options (emacs mode, warnings and pedantic),
139: * but include it in the javac directive?
140: */
141:
142: /**
143: * Jikes has the nice feature to print error
144: * messages in a form readable by emacs, so
145: * that emacs can directly set the cursor
146: * to the place, where the error occurred.
147: */
148: String emacsProperty = project
149: .getProperty("build.compiler.emacs");
150: if (emacsProperty != null && Project.toBoolean(emacsProperty)) {
151: cmd.createArgument().setValue("+E");
152: }
153:
154: /**
155: * Jikes issues more warnings that javac, for
156: * example, when you have files in your classpath
157: * that don't exist. As this is often the case, these
158: * warning can be pretty annoying.
159: */
160: String warningsProperty = project
161: .getProperty("build.compiler.warnings");
162: if (warningsProperty != null) {
163: attributes.log(
164: "!! the build.compiler.warnings property is "
165: + "deprecated. !!", Project.MSG_WARN);
166: attributes.log("!! Use the nowarn attribute instead. !!",
167: Project.MSG_WARN);
168: if (!Project.toBoolean(warningsProperty)) {
169: cmd.createArgument().setValue("-nowarn");
170: }
171: }
172: if (attributes.getNowarn()) {
173: cmd.createArgument().setValue("-nowarn");
174: }
175:
176: /**
177: * Jikes can issue pedantic warnings.
178: */
179: String pedanticProperty = project
180: .getProperty("build.compiler.pedantic");
181: if (pedanticProperty != null
182: && Project.toBoolean(pedanticProperty)) {
183: cmd.createArgument().setValue("+P");
184: }
185:
186: /**
187: * Jikes supports something it calls "full dependency
188: * checking", see the jikes documentation for differences
189: * between -depend and +F.
190: */
191: String fullDependProperty = project
192: .getProperty("build.compiler.fulldepend");
193: if (fullDependProperty != null
194: && Project.toBoolean(fullDependProperty)) {
195: cmd.createArgument().setValue("+F");
196: }
197:
198: if (attributes.getSource() != null) {
199: cmd.createArgument().setValue("-source");
200: String source = attributes.getSource();
201: if (source.equals("1.1") || source.equals("1.2")) {
202: // support for -source 1.1 and -source 1.2 has been
203: // added with JDK 1.4.2, Jikes doesn't like it
204: attributes.log("Jikes doesn't support '-source "
205: + source + "', will use '-source 1.3' instead");
206: cmd.createArgument().setValue("1.3");
207: } else {
208: cmd.createArgument().setValue(source);
209: }
210: }
211:
212: addCurrentCompilerArgs(cmd);
213:
214: int firstFileName = cmd.size();
215:
216: Path boot = getBootClassPath();
217: if (boot.size() > 0) {
218: cmd.createArgument().setValue("-bootclasspath");
219: cmd.createArgument().setPath(boot);
220: }
221:
222: logAndAddFilesToCompile(cmd);
223:
224: return executeExternalCompile(cmd.getCommandline(),
225: firstFileName) == 0;
226: }
227:
228: }
|