source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 21.5 KB
Line 
1/*
2 * Copyright 2001-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
18package org.apache.tools.ant.taskdefs.compilers;
19
20import java.io.File;
21import java.io.FileWriter;
22import java.io.IOException;
23import java.io.PrintWriter;
24import org.apache.tools.ant.BuildException;
25import org.apache.tools.ant.Location;
26import org.apache.tools.ant.Project;
27import org.apache.tools.ant.taskdefs.Execute;
28import org.apache.tools.ant.taskdefs.Javac;
29import org.apache.tools.ant.taskdefs.LogStreamHandler;
30import org.apache.tools.ant.types.Commandline;
31import org.apache.tools.ant.types.Path;
32import org.apache.tools.ant.util.FileUtils;
33import org.apache.tools.ant.util.JavaEnvUtils;
34
35/**
36 * This is the default implementation for the CompilerAdapter interface.
37 * Currently, this is a cut-and-paste of the original javac task.
38 *
39 * @since Ant 1.3
40 */
41public abstract class DefaultCompilerAdapter implements CompilerAdapter {
42
43 /* jdg - TODO - all these attributes are currently protected, but they
44 * should probably be private in the near future.
45 */
46
47 protected Path src;
48 protected File destDir;
49 protected String encoding;
50 protected boolean debug = false;
51 protected boolean optimize = false;
52 protected boolean deprecation = false;
53 protected boolean depend = false;
54 protected boolean verbose = false;
55 protected String target;
56 protected Path bootclasspath;
57 protected Path extdirs;
58 protected Path compileClasspath;
59 protected Path compileSourcepath;
60 protected Project project;
61 protected Location location;
62 protected boolean includeAntRuntime;
63 protected boolean includeJavaRuntime;
64 protected String memoryInitialSize;
65 protected String memoryMaximumSize;
66
67 protected File[] compileList;
68 protected static final String lSep = System.getProperty("line.separator");
69 protected Javac attributes;
70
71 private FileUtils fileUtils = FileUtils.newFileUtils();
72
73 /**
74 * Set the Javac instance which contains the configured compilation
75 * attributes.
76 *
77 * @param attributes a configured Javac task.
78 */
79 public void setJavac(Javac attributes) {
80 this.attributes = attributes;
81 src = attributes.getSrcdir();
82 destDir = attributes.getDestdir();
83 encoding = attributes.getEncoding();
84 debug = attributes.getDebug();
85 optimize = attributes.getOptimize();
86 deprecation = attributes.getDeprecation();
87 depend = attributes.getDepend();
88 verbose = attributes.getVerbose();
89 target = attributes.getTarget();
90 bootclasspath = attributes.getBootclasspath();
91 extdirs = attributes.getExtdirs();
92 compileList = attributes.getFileList();
93 compileClasspath = attributes.getClasspath();
94 compileSourcepath = attributes.getSourcepath();
95 project = attributes.getProject();
96 location = attributes.getLocation();
97 includeAntRuntime = attributes.getIncludeantruntime();
98 includeJavaRuntime = attributes.getIncludejavaruntime();
99 memoryInitialSize = attributes.getMemoryInitialSize();
100 memoryMaximumSize = attributes.getMemoryMaximumSize();
101 }
102
103 /**
104 * Get the Javac task instance associated with this compiler adapter
105 *
106 * @return the configured Javac task instance used by this adapter.
107 */
108 public Javac getJavac() {
109 return attributes;
110 }
111
112 /**
113 * @since Ant 1.6
114 */
115 protected Project getProject() {
116 return project;
117 }
118
119 /**
120 * Builds the compilation classpath.
121 *
122 */
123 protected Path getCompileClasspath() {
124 Path classpath = new Path(project);
125
126 // add dest dir to classpath so that previously compiled and
127 // untouched classes are on classpath
128
129 if (destDir != null) {
130 classpath.setLocation(destDir);
131 }
132
133 // Combine the build classpath with the system classpath, in an
134 // order determined by the value of build.sysclasspath
135
136 Path cp = compileClasspath;
137 if (cp == null) {
138 cp = new Path(project);
139 }
140 if (includeAntRuntime) {
141 classpath.addExisting(cp.concatSystemClasspath("last"));
142 } else {
143 classpath.addExisting(cp.concatSystemClasspath("ignore"));
144 }
145
146 if (includeJavaRuntime) {
147 classpath.addJavaRuntime();
148 }
149
150 return classpath;
151 }
152
153 protected Commandline setupJavacCommandlineSwitches(Commandline cmd) {
154 return setupJavacCommandlineSwitches(cmd, false);
155 }
156
157 /**
158 * Does the command line argument processing common to classic and
159 * modern. Doesn't add the files to compile.
160 */
161 protected Commandline setupJavacCommandlineSwitches(Commandline cmd,
162 boolean useDebugLevel) {
163 Path classpath = getCompileClasspath();
164 // For -sourcepath, use the "sourcepath" value if present.
165 // Otherwise default to the "srcdir" value.
166 Path sourcepath = null;
167 if (compileSourcepath != null) {
168 sourcepath = compileSourcepath;
169 } else {
170 sourcepath = src;
171 }
172
173 String memoryParameterPrefix = assumeJava11() ? "-J-" : "-J-X";
174 if (memoryInitialSize != null) {
175 if (!attributes.isForkedJavac()) {
176 attributes.log("Since fork is false, ignoring "
177 + "memoryInitialSize setting.",
178 Project.MSG_WARN);
179 } else {
180 cmd.createArgument().setValue(memoryParameterPrefix
181 + "ms" + memoryInitialSize);
182 }
183 }
184
185 if (memoryMaximumSize != null) {
186 if (!attributes.isForkedJavac()) {
187 attributes.log("Since fork is false, ignoring "
188 + "memoryMaximumSize setting.",
189 Project.MSG_WARN);
190 } else {
191 cmd.createArgument().setValue(memoryParameterPrefix
192 + "mx" + memoryMaximumSize);
193 }
194 }
195
196 if (attributes.getNowarn()) {
197 cmd.createArgument().setValue("-nowarn");
198 }
199
200 if (deprecation == true) {
201 cmd.createArgument().setValue("-deprecation");
202 }
203
204 if (destDir != null) {
205 cmd.createArgument().setValue("-d");
206 cmd.createArgument().setFile(destDir);
207 }
208
209 cmd.createArgument().setValue("-classpath");
210
211 // Just add "sourcepath" to classpath ( for JDK1.1 )
212 // as well as "bootclasspath" and "extdirs"
213 if (assumeJava11()) {
214 Path cp = new Path(project);
215 /*
216 * XXX - This doesn't mix very well with build.systemclasspath,
217 */
218 if (bootclasspath != null) {
219 cp.append(bootclasspath);
220 }
221 if (extdirs != null) {
222 cp.addExtdirs(extdirs);
223 }
224 cp.append(classpath);
225 cp.append(sourcepath);
226 cmd.createArgument().setPath(cp);
227 } else {
228 cmd.createArgument().setPath(classpath);
229 // If the buildfile specifies sourcepath="", then don't
230 // output any sourcepath.
231 if (sourcepath.size() > 0) {
232 cmd.createArgument().setValue("-sourcepath");
233 cmd.createArgument().setPath(sourcepath);
234 }
235 if (target != null) {
236 cmd.createArgument().setValue("-target");
237 cmd.createArgument().setValue(target);
238 }
239 if (bootclasspath != null && bootclasspath.size() > 0) {
240 cmd.createArgument().setValue("-bootclasspath");
241 cmd.createArgument().setPath(bootclasspath);
242 }
243 if (extdirs != null && extdirs.size() > 0) {
244 cmd.createArgument().setValue("-extdirs");
245 cmd.createArgument().setPath(extdirs);
246 }
247 }
248
249 if (encoding != null) {
250 cmd.createArgument().setValue("-encoding");
251 cmd.createArgument().setValue(encoding);
252 }
253 if (debug) {
254 if (useDebugLevel && !assumeJava11()) {
255 String debugLevel = attributes.getDebugLevel();
256 if (debugLevel != null) {
257 cmd.createArgument().setValue("-g:" + debugLevel);
258 } else {
259 cmd.createArgument().setValue("-g");
260 }
261 } else {
262 cmd.createArgument().setValue("-g");
263 }
264 } else if (getNoDebugArgument() != null) {
265 cmd.createArgument().setValue(getNoDebugArgument());
266 }
267 if (optimize) {
268 cmd.createArgument().setValue("-O");
269 }
270
271 if (depend) {
272 if (assumeJava11()) {
273 cmd.createArgument().setValue("-depend");
274 } else if (assumeJava12()) {
275 cmd.createArgument().setValue("-Xdepend");
276 } else {
277 attributes.log("depend attribute is not supported by the "
278 + "modern compiler", Project.MSG_WARN);
279 }
280 }
281
282 if (verbose) {
283 cmd.createArgument().setValue("-verbose");
284 }
285
286 addCurrentCompilerArgs(cmd);
287
288 return cmd;
289 }
290
291 /**
292 * Does the command line argument processing for modern. Doesn't
293 * add the files to compile.
294 */
295 protected Commandline setupModernJavacCommandlineSwitches(Commandline cmd) {
296 setupJavacCommandlineSwitches(cmd, true);
297 if (attributes.getSource() != null && !assumeJava13()) {
298 cmd.createArgument().setValue("-source");
299 String source = attributes.getSource();
300 if ((assumeJava14() || assumeJava15())
301 && (source.equals("1.1") || source.equals("1.2"))) {
302 // support for -source 1.1 and -source 1.2 has been
303 // added with JDK 1.4.2 - and isn't present in 1.5.0 either
304 cmd.createArgument().setValue("1.3");
305 } else {
306 cmd.createArgument().setValue(source);
307 }
308 } else if (assumeJava15() && attributes.getTarget() != null) {
309 String t = attributes.getTarget();
310 if (t.equals("1.1") || t.equals("1.2") || t.equals("1.3")
311 || t.equals("1.4")) {
312 String s = t;
313 if (t.equals("1.1")) {
314 // 1.5.0 doesn't support -source 1.1
315 s = "1.2";
316 }
317 attributes.log("", Project.MSG_WARN);
318 attributes.log(" WARNING", Project.MSG_WARN);
319 attributes.log("", Project.MSG_WARN);
320 attributes.log("The -source switch defaults to 1.5 in JDK 1.5.",
321 Project.MSG_WARN);
322 attributes.log("If you specify -target " + t
323 + " you now must also specify -source " + s
324 + ".", Project.MSG_WARN);
325 attributes.log("Ant will implicitly add -source " + s
326 + " for you. Please change your build file.",
327 Project.MSG_WARN);
328 cmd.createArgument().setValue("-source");
329 cmd.createArgument().setValue(s);
330 }
331 }
332 return cmd;
333 }
334
335 /**
336 * Does the command line argument processing for modern and adds
337 * the files to compile as well.
338 */
339 protected Commandline setupModernJavacCommand() {
340 Commandline cmd = new Commandline();
341 setupModernJavacCommandlineSwitches(cmd);
342
343 logAndAddFilesToCompile(cmd);
344 return cmd;
345 }
346
347 protected Commandline setupJavacCommand() {
348 return setupJavacCommand(false);
349 }
350
351 /**
352 * Does the command line argument processing for classic and adds
353 * the files to compile as well.
354 */
355 protected Commandline setupJavacCommand(boolean debugLevelCheck) {
356 Commandline cmd = new Commandline();
357 setupJavacCommandlineSwitches(cmd, debugLevelCheck);
358 logAndAddFilesToCompile(cmd);
359 return cmd;
360 }
361
362 /**
363 * Logs the compilation parameters, adds the files to compile and logs the
364 * "niceSourceList"
365 */
366 protected void logAndAddFilesToCompile(Commandline cmd) {
367 attributes.log("Compilation " + cmd.describeArguments(),
368 Project.MSG_VERBOSE);
369
370 StringBuffer niceSourceList = new StringBuffer("File");
371 if (compileList.length != 1) {
372 niceSourceList.append("s");
373 }
374 niceSourceList.append(" to be compiled:");
375
376 niceSourceList.append(lSep);
377
378 for (int i = 0; i < compileList.length; i++) {
379 String arg = compileList[i].getAbsolutePath();
380 cmd.createArgument().setValue(arg);
381 niceSourceList.append(" " + arg + lSep);
382 }
383
384 attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
385 }
386
387 /**
388 * Do the compile with the specified arguments.
389 * @param args - arguments to pass to process on command line
390 * @param firstFileName - index of the first source file in args,
391 * if the index is negative, no temporary file will ever be
392 * created, but this may hit the command line length limit on your
393 * system.
394 */
395 protected int executeExternalCompile(String[] args, int firstFileName) {
396 return executeExternalCompile(args, firstFileName, true);
397 }
398
399 /**
400 * Do the compile with the specified arguments.
401 * @param args - arguments to pass to process on command line
402 * @param firstFileName - index of the first source file in args,
403 * if the index is negative, no temporary file will ever be
404 * created, but this may hit the command line length limit on your
405 * system.
406 * @param quoteFiles - if set to true, filenames containing
407 * spaces will be quoted when they appear in the external file.
408 * This is necessary when running JDK 1.4's javac and probably
409 * others.
410 *
411 * @since Ant 1.6
412 */
413 protected int executeExternalCompile(String[] args, int firstFileName,
414 boolean quoteFiles) {
415 String[] commandArray = null;
416 File tmpFile = null;
417
418 try {
419 /*
420 * Many system have been reported to get into trouble with
421 * long command lines - no, not only Windows ;-).
422 *
423 * POSIX seems to define a lower limit of 4k, so use a temporary
424 * file if the total length of the command line exceeds this limit.
425 */
426 if (Commandline.toString(args).length() > 4096
427 && firstFileName >= 0) {
428 PrintWriter out = null;
429 try {
430 tmpFile = fileUtils.createTempFile(
431 "files", "", getJavac().getTempdir());
432 tmpFile.deleteOnExit();
433 out = new PrintWriter(new FileWriter(tmpFile));
434 for (int i = firstFileName; i < args.length; i++) {
435 if (quoteFiles && args[i].indexOf(" ") > -1) {
436 args[i] = args[i].replace(File.separatorChar, '/');
437 out.println("\"" + args[i] + "\"");
438 } else {
439 out.println(args[i]);
440 }
441 }
442 out.flush();
443 commandArray = new String[firstFileName + 1];
444 System.arraycopy(args, 0, commandArray, 0, firstFileName);
445 commandArray[firstFileName] = "@" + tmpFile;
446 } catch (IOException e) {
447 throw new BuildException("Error creating temporary file",
448 e, location);
449 } finally {
450 if (out != null) {
451 try {
452 out.close();
453 } catch (Throwable t) {
454 // ignore
455 }
456 }
457 }
458 } else {
459 commandArray = args;
460 }
461
462 try {
463 Execute exe = new Execute(
464 new LogStreamHandler(attributes,
465 Project.MSG_INFO,
466 Project.MSG_WARN));
467 exe.setAntRun(project);
468 exe.setWorkingDirectory(project.getBaseDir());
469 exe.setCommandline(commandArray);
470 exe.execute();
471 return exe.getExitValue();
472 } catch (IOException e) {
473 throw new BuildException("Error running " + args[0]
474 + " compiler", e, location);
475 }
476 } finally {
477 if (tmpFile != null) {
478 tmpFile.delete();
479 }
480 }
481 }
482
483 /**
484 * @deprecated use org.apache.tools.ant.types.Path#addExtdirs instead
485 */
486 protected void addExtdirsToClasspath(Path classpath) {
487 classpath.addExtdirs(extdirs);
488 }
489
490 /**
491 * Adds the command line arguments specific to the current implementation.
492 */
493 protected void addCurrentCompilerArgs(Commandline cmd) {
494 cmd.addArguments(getJavac().getCurrentCompilerArgs());
495 }
496
497 /**
498 * Shall we assume JDK 1.1 command line switches?
499 * @since Ant 1.5
500 */
501 protected boolean assumeJava11() {
502 return "javac1.1".equals(attributes.getCompilerVersion())
503 || ("classic".equals(attributes.getCompilerVersion())
504 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1))
505 || ("extJavac".equals(attributes.getCompilerVersion())
506 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1));
507 }
508
509 /**
510 * Shall we assume JDK 1.2 command line switches?
511 * @since Ant 1.5
512 */
513 protected boolean assumeJava12() {
514 return "javac1.2".equals(attributes.getCompilerVersion())
515 || ("classic".equals(attributes.getCompilerVersion())
516 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2))
517 || ("extJavac".equals(attributes.getCompilerVersion())
518 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2));
519 }
520
521 /**
522 * Shall we assume JDK 1.3 command line switches?
523 * @since Ant 1.5
524 */
525 protected boolean assumeJava13() {
526 return "javac1.3".equals(attributes.getCompilerVersion())
527 || ("classic".equals(attributes.getCompilerVersion())
528 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3))
529 || ("modern".equals(attributes.getCompilerVersion())
530 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3))
531 || ("extJavac".equals(attributes.getCompilerVersion())
532 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3));
533 }
534
535 /**
536 * Shall we assume JDK 1.4 command line switches?
537 * @since Ant 1.6.3
538 */
539 protected boolean assumeJava14() {
540 return "javac1.4".equals(attributes.getCompilerVersion())
541 || ("classic".equals(attributes.getCompilerVersion())
542 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4))
543 || ("modern".equals(attributes.getCompilerVersion())
544 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4))
545 || ("extJavac".equals(attributes.getCompilerVersion())
546 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4));
547 }
548
549 /**
550 * Shall we assume JDK 1.5 command line switches?
551 * @return true if JDK 1.5
552 * @since Ant 1.6.3
553 */
554 protected boolean assumeJava15() {
555 return "javac1.5".equals(attributes.getCompilerVersion())
556 || ("classic".equals(attributes.getCompilerVersion())
557 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5))
558 || ("modern".equals(attributes.getCompilerVersion())
559 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5))
560 || ("extJavac".equals(attributes.getCompilerVersion())
561 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5));
562 }
563
564 /**
565 * The argument the compiler wants to see if the debug attribute
566 * has been set to false.
567 *
568 * <p>A return value of <code>null</code> means no argument at all.</p>
569 *
570 * @return "-g:none" unless we expect to invoke a JDK 1.1 compiler.
571 *
572 * @since Ant 1.6.3
573 */
574 protected String getNoDebugArgument() {
575 return assumeJava11() ? null : "-g:none";
576 }
577}
578
Note: See TracBrowser for help on using the repository browser.