1 18 19 package org.apache.tools.ant.taskdefs.compilers; 20 21 24 import java.io.File ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Location; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.taskdefs.Execute; 32 import org.apache.tools.ant.taskdefs.Javac; 33 import org.apache.tools.ant.taskdefs.LogStreamHandler; 34 import org.apache.tools.ant.types.Commandline; 35 import org.apache.tools.ant.types.Path; 36 import org.apache.tools.ant.util.FileUtils; 37 import org.apache.tools.ant.util.StringUtils; 38 import org.apache.tools.ant.util.JavaEnvUtils; 39 import org.apache.tools.ant.taskdefs.condition.Os; 40 41 47 public abstract class DefaultCompilerAdapter implements CompilerAdapter { 48 50 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 51 52 protected Path src; 53 protected File destDir; 54 protected String encoding; 55 protected boolean debug = false; 56 protected boolean optimize = false; 57 protected boolean deprecation = false; 58 protected boolean depend = false; 59 protected boolean verbose = false; 60 protected String target; 61 protected Path bootclasspath; 62 protected Path extdirs; 63 protected Path compileClasspath; 64 protected Path compileSourcepath; 65 protected Project project; 66 protected Location location; 67 protected boolean includeAntRuntime; 68 protected boolean includeJavaRuntime; 69 protected String memoryInitialSize; 70 protected String memoryMaximumSize; 71 72 protected File [] compileList; 73 protected Javac attributes; 74 75 protected static final String lSep = StringUtils.LINE_SEP; 78 79 82 88 public void setJavac(Javac attributes) { 89 this.attributes = attributes; 90 src = attributes.getSrcdir(); 91 destDir = attributes.getDestdir(); 92 encoding = attributes.getEncoding(); 93 debug = attributes.getDebug(); 94 optimize = attributes.getOptimize(); 95 deprecation = attributes.getDeprecation(); 96 depend = attributes.getDepend(); 97 verbose = attributes.getVerbose(); 98 target = attributes.getTarget(); 99 bootclasspath = attributes.getBootclasspath(); 100 extdirs = attributes.getExtdirs(); 101 compileList = attributes.getFileList(); 102 compileClasspath = attributes.getClasspath(); 103 compileSourcepath = attributes.getSourcepath(); 104 project = attributes.getProject(); 105 location = attributes.getLocation(); 106 includeAntRuntime = attributes.getIncludeantruntime(); 107 includeJavaRuntime = attributes.getIncludejavaruntime(); 108 memoryInitialSize = attributes.getMemoryInitialSize(); 109 memoryMaximumSize = attributes.getMemoryMaximumSize(); 110 } 111 112 117 public Javac getJavac() { 118 return attributes; 119 } 120 121 126 protected Project getProject() { 127 return project; 128 } 129 130 134 protected Path getCompileClasspath() { 135 Path classpath = new Path(project); 136 137 140 if (destDir != null) { 141 classpath.setLocation(destDir); 142 } 143 144 147 Path cp = compileClasspath; 148 if (cp == null) { 149 cp = new Path(project); 150 } 151 if (includeAntRuntime) { 152 classpath.addExisting(cp.concatSystemClasspath("last")); 153 } else { 154 classpath.addExisting(cp.concatSystemClasspath("ignore")); 155 } 156 157 if (includeJavaRuntime) { 158 classpath.addJavaRuntime(); 159 } 160 161 return classpath; 162 } 163 164 169 protected Commandline setupJavacCommandlineSwitches(Commandline cmd) { 170 return setupJavacCommandlineSwitches(cmd, false); 171 } 172 173 180 protected Commandline setupJavacCommandlineSwitches(Commandline cmd, 181 boolean useDebugLevel) { 182 Path classpath = getCompileClasspath(); 183 Path sourcepath = null; 186 if (compileSourcepath != null) { 187 sourcepath = compileSourcepath; 188 } else { 189 sourcepath = src; 190 } 191 192 String memoryParameterPrefix = assumeJava11() ? "-J-" : "-J-X"; 193 if (memoryInitialSize != null) { 194 if (!attributes.isForkedJavac()) { 195 attributes.log("Since fork is false, ignoring " 196 + "memoryInitialSize setting.", 197 Project.MSG_WARN); 198 } else { 199 cmd.createArgument().setValue(memoryParameterPrefix 200 + "ms" + memoryInitialSize); 201 } 202 } 203 204 if (memoryMaximumSize != null) { 205 if (!attributes.isForkedJavac()) { 206 attributes.log("Since fork is false, ignoring " 207 + "memoryMaximumSize setting.", 208 Project.MSG_WARN); 209 } else { 210 cmd.createArgument().setValue(memoryParameterPrefix 211 + "mx" + memoryMaximumSize); 212 } 213 } 214 215 if (attributes.getNowarn()) { 216 cmd.createArgument().setValue("-nowarn"); 217 } 218 219 if (deprecation) { 220 cmd.createArgument().setValue("-deprecation"); 221 } 222 223 if (destDir != null) { 224 cmd.createArgument().setValue("-d"); 225 cmd.createArgument().setFile(destDir); 226 } 227 228 cmd.createArgument().setValue("-classpath"); 229 230 if (assumeJava11()) { 233 Path cp = new Path(project); 234 235 Path bp = getBootClassPath(); 236 if (bp.size() > 0) { 237 cp.append(bp); 238 } 239 240 if (extdirs != null) { 241 cp.addExtdirs(extdirs); 242 } 243 cp.append(classpath); 244 cp.append(sourcepath); 245 cmd.createArgument().setPath(cp); 246 } else { 247 cmd.createArgument().setPath(classpath); 248 if (sourcepath.size() > 0) { 251 cmd.createArgument().setValue("-sourcepath"); 252 cmd.createArgument().setPath(sourcepath); 253 } 254 if (target != null) { 255 cmd.createArgument().setValue("-target"); 256 cmd.createArgument().setValue(target); 257 } 258 259 Path bp = getBootClassPath(); 260 if (bp.size() > 0) { 261 cmd.createArgument().setValue("-bootclasspath"); 262 cmd.createArgument().setPath(bp); 263 } 264 265 if (extdirs != null && extdirs.size() > 0) { 266 cmd.createArgument().setValue("-extdirs"); 267 cmd.createArgument().setPath(extdirs); 268 } 269 } 270 271 if (encoding != null) { 272 cmd.createArgument().setValue("-encoding"); 273 cmd.createArgument().setValue(encoding); 274 } 275 if (debug) { 276 if (useDebugLevel && !assumeJava11()) { 277 String debugLevel = attributes.getDebugLevel(); 278 if (debugLevel != null) { 279 cmd.createArgument().setValue("-g:" + debugLevel); 280 } else { 281 cmd.createArgument().setValue("-g"); 282 } 283 } else { 284 cmd.createArgument().setValue("-g"); 285 } 286 } else if (getNoDebugArgument() != null) { 287 cmd.createArgument().setValue(getNoDebugArgument()); 288 } 289 if (optimize) { 290 cmd.createArgument().setValue("-O"); 291 } 292 293 if (depend) { 294 if (assumeJava11()) { 295 cmd.createArgument().setValue("-depend"); 296 } else if (assumeJava12()) { 297 cmd.createArgument().setValue("-Xdepend"); 298 } else { 299 attributes.log("depend attribute is not supported by the " 300 + "modern compiler", Project.MSG_WARN); 301 } 302 } 303 304 if (verbose) { 305 cmd.createArgument().setValue("-verbose"); 306 } 307 308 addCurrentCompilerArgs(cmd); 309 310 return cmd; 311 } 312 313 319 protected Commandline setupModernJavacCommandlineSwitches(Commandline cmd) { 320 setupJavacCommandlineSwitches(cmd, true); 321 if (attributes.getSource() != null && !assumeJava13()) { 322 cmd.createArgument().setValue("-source"); 323 String source = attributes.getSource(); 324 if ((assumeJava14() || assumeJava15()) 325 && (source.equals("1.1") || source.equals("1.2"))) { 326 cmd.createArgument().setValue("1.3"); 329 } else { 330 cmd.createArgument().setValue(source); 331 } 332 } else if ((assumeJava15() || assumeJava16()) 333 && attributes.getTarget() != null) { 334 String t = attributes.getTarget(); 335 if (t.equals("1.1") || t.equals("1.2") || t.equals("1.3") 336 || t.equals("1.4")) { 337 String s = t; 338 if (t.equals("1.1")) { 339 s = "1.2"; 341 } 342 attributes.log("", Project.MSG_WARN); 343 attributes.log(" WARNING", Project.MSG_WARN); 344 attributes.log("", Project.MSG_WARN); 345 attributes.log("The -source switch defaults to 1.5 in JDK 1.5 and 1.6.", 346 Project.MSG_WARN); 347 attributes.log("If you specify -target " + t 348 + " you now must also specify -source " + s 349 + ".", Project.MSG_WARN); 350 attributes.log("Ant will implicitly add -source " + s 351 + " for you. Please change your build file.", 352 Project.MSG_WARN); 353 cmd.createArgument().setValue("-source"); 354 cmd.createArgument().setValue(s); 355 } 356 } 357 return cmd; 358 } 359 360 365 protected Commandline setupModernJavacCommand() { 366 Commandline cmd = new Commandline(); 367 setupModernJavacCommandlineSwitches(cmd); 368 369 logAndAddFilesToCompile(cmd); 370 return cmd; 371 } 372 373 377 protected Commandline setupJavacCommand() { 378 return setupJavacCommand(false); 379 } 380 381 387 protected Commandline setupJavacCommand(boolean debugLevelCheck) { 388 Commandline cmd = new Commandline(); 389 setupJavacCommandlineSwitches(cmd, debugLevelCheck); 390 logAndAddFilesToCompile(cmd); 391 return cmd; 392 } 393 394 399 protected void logAndAddFilesToCompile(Commandline cmd) { 400 attributes.log("Compilation " + cmd.describeArguments(), 401 Project.MSG_VERBOSE); 402 403 StringBuffer niceSourceList = new StringBuffer ("File"); 404 if (compileList.length != 1) { 405 niceSourceList.append("s"); 406 } 407 niceSourceList.append(" to be compiled:"); 408 409 niceSourceList.append(StringUtils.LINE_SEP); 410 411 for (int i = 0; i < compileList.length; i++) { 412 String arg = compileList[i].getAbsolutePath(); 413 cmd.createArgument().setValue(arg); 414 niceSourceList.append(" "); 415 niceSourceList.append(arg); 416 niceSourceList.append(StringUtils.LINE_SEP); 417 } 418 419 attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE); 420 } 421 422 431 protected int executeExternalCompile(String [] args, int firstFileName) { 432 return executeExternalCompile(args, firstFileName, true); 433 } 434 435 450 protected int executeExternalCompile(String [] args, int firstFileName, 451 boolean quoteFiles) { 452 String [] commandArray = null; 453 File tmpFile = null; 454 455 try { 456 463 if (Commandline.toString(args).length() > 4096 464 && firstFileName >= 0) { 465 PrintWriter out = null; 466 try { 467 tmpFile = FILE_UTILS.createTempFile( 468 "files", "", getJavac().getTempdir()); 469 tmpFile.deleteOnExit(); 470 out = new PrintWriter (new FileWriter (tmpFile)); 471 for (int i = firstFileName; i < args.length; i++) { 472 if (quoteFiles && args[i].indexOf(" ") > -1) { 473 args[i] = args[i].replace(File.separatorChar, '/'); 474 out.println("\"" + args[i] + "\""); 475 } else { 476 out.println(args[i]); 477 } 478 } 479 out.flush(); 480 commandArray = new String [firstFileName + 1]; 481 System.arraycopy(args, 0, commandArray, 0, firstFileName); 482 commandArray[firstFileName] = "@" + tmpFile; 483 } catch (IOException e) { 484 throw new BuildException("Error creating temporary file", 485 e, location); 486 } finally { 487 FileUtils.close(out); 488 } 489 } else { 490 commandArray = args; 491 } 492 493 try { 494 Execute exe = new Execute( 495 new LogStreamHandler(attributes, 496 Project.MSG_INFO, 497 Project.MSG_WARN)); 498 if (Os.isFamily("openvms")) { 499 exe.setVMLauncher(true); 502 } 503 exe.setAntRun(project); 504 exe.setWorkingDirectory(project.getBaseDir()); 505 exe.setCommandline(commandArray); 506 exe.execute(); 507 return exe.getExitValue(); 508 } catch (IOException e) { 509 throw new BuildException("Error running " + args[0] 510 + " compiler", e, location); 511 } 512 } finally { 513 if (tmpFile != null) { 514 tmpFile.delete(); 515 } 516 } 517 } 518 519 525 protected void addExtdirsToClasspath(Path classpath) { 526 classpath.addExtdirs(extdirs); 527 } 528 529 533 protected void addCurrentCompilerArgs(Commandline cmd) { 534 cmd.addArguments(getJavac().getCurrentCompilerArgs()); 535 } 536 537 542 protected boolean assumeJava11() { 543 return "javac1.1".equals(attributes.getCompilerVersion()); 544 } 545 546 551 protected boolean assumeJava12() { 552 return "javac1.2".equals(attributes.getCompilerVersion()) 553 || ("classic".equals(attributes.getCompilerVersion()) 554 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)) 555 || ("extJavac".equals(attributes.getCompilerVersion()) 556 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)); 557 } 558 559 564 protected boolean assumeJava13() { 565 return "javac1.3".equals(attributes.getCompilerVersion()) 566 || ("classic".equals(attributes.getCompilerVersion()) 567 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) 568 || ("modern".equals(attributes.getCompilerVersion()) 569 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) 570 || ("extJavac".equals(attributes.getCompilerVersion()) 571 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)); 572 } 573 574 579 protected boolean assumeJava14() { 580 return "javac1.4".equals(attributes.getCompilerVersion()) 581 || ("classic".equals(attributes.getCompilerVersion()) 582 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4)) 583 || ("modern".equals(attributes.getCompilerVersion()) 584 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4)) 585 || ("extJavac".equals(attributes.getCompilerVersion()) 586 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4)); 587 } 588 589 594 protected boolean assumeJava15() { 595 return "javac1.5".equals(attributes.getCompilerVersion()) 596 || ("classic".equals(attributes.getCompilerVersion()) 597 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5)) 598 || ("modern".equals(attributes.getCompilerVersion()) 599 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5)) 600 || ("extJavac".equals(attributes.getCompilerVersion()) 601 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5)); 602 } 603 604 609 protected boolean assumeJava16() { 610 return "javac1.6".equals(attributes.getCompilerVersion()) 611 || ("classic".equals(attributes.getCompilerVersion()) 612 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_6)) 613 || ("modern".equals(attributes.getCompilerVersion()) 614 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_6)) 615 || ("extJavac".equals(attributes.getCompilerVersion()) 616 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_6)); 617 } 618 619 626 protected Path getBootClassPath() { 627 Path bp = new Path(project); 628 if (bootclasspath != null) { 629 bp.append(bootclasspath); 630 } 631 return bp.concatSystemBootClasspath("ignore"); 632 } 633 634 644 protected String getNoDebugArgument() { 645 return assumeJava11() ? null : "-g:none"; 646 } 647 } 648 649 | Popular Tags |