You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Javac.java 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.*;
  56. import java.lang.reflect.Method;
  57. import java.io.*;
  58. import java.util.*;
  59. /**
  60. * Task to compile Java source files. This task can take the following
  61. * arguments:
  62. * <ul>
  63. * <li>sourcedir
  64. * <li>destdir
  65. * <li>deprecation
  66. * <li>classpath
  67. * <li>bootclasspath
  68. * <li>extdirs
  69. * <li>optimize
  70. * <li>debug
  71. * <li>target
  72. * </ul>
  73. * Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are required.
  74. * <p>
  75. * When this task executes, it will recursively scan the sourcedir and
  76. * destdir looking for Java source files to compile. This task makes its
  77. * compile decision based on timestamp. Any other file in the
  78. * sourcedir will be copied to the destdir allowing support files to be
  79. * located properly in the classpath.
  80. *
  81. * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a>
  82. * @author Robin Green <a href="mailto:greenrd@hotmail.com">greenrd@hotmail.com</a>
  83. */
  84. public class Javac extends MatchingTask {
  85. /**
  86. * Integer returned by the "Modern" jdk1.3 compiler to indicate success.
  87. */
  88. private static final int
  89. MODERN_COMPILER_SUCCESS = 0;
  90. private Path src;
  91. private File destDir;
  92. private Path compileClasspath;
  93. private boolean debug = false;
  94. private boolean optimize = false;
  95. private boolean deprecation = false;
  96. private boolean filtering = false;
  97. private String target;
  98. private Path bootclasspath;
  99. private Path extdirs;
  100. protected Vector compileList = new Vector();
  101. protected Hashtable filecopyList = new Hashtable();
  102. /**
  103. * Create a nested <src ...> element for multiple source path
  104. * support.
  105. *
  106. * @return a nexted src element.
  107. */
  108. public Path createSrc() {
  109. if (src != null) {
  110. src = new Path();
  111. }
  112. return src;
  113. }
  114. /**
  115. * Set the source dirs to find the source Java files.
  116. */
  117. public void setSrcdir(Path srcDir) {
  118. if (src == null) {
  119. src = srcDir;
  120. } else {
  121. src.append(srcDir);
  122. }
  123. }
  124. /**
  125. * Set the destination directory into which the Java source
  126. * files should be compiled.
  127. */
  128. public void setDestdir(File destDir) {
  129. this.destDir = destDir;
  130. }
  131. /**
  132. * Set the classpath to be used for this compilation.
  133. */
  134. public void setClasspath(Path classpath) {
  135. if (compileClasspath == null) {
  136. compileClasspath = classpath;
  137. } else {
  138. compileClasspath.append(classpath);
  139. }
  140. }
  141. /**
  142. * Maybe creates a nested classpath element.
  143. */
  144. public Path createClasspath() {
  145. if (compileClasspath == null) {
  146. compileClasspath = new Path();
  147. }
  148. return compileClasspath;
  149. }
  150. /**
  151. * Sets the bootclasspath that will be used to compile the classes
  152. * against.
  153. */
  154. public void setBootclasspath(Path bootclasspath) {
  155. if (this.bootclasspath == null) {
  156. this.bootclasspath = bootclasspath;
  157. } else {
  158. this.bootclasspath.append(bootclasspath);
  159. }
  160. }
  161. /**
  162. * Maybe creates a nested classpath element.
  163. */
  164. public Path createBootclasspath() {
  165. if (bootclasspath == null) {
  166. bootclasspath = new Path();
  167. }
  168. return bootclasspath;
  169. }
  170. /**
  171. * Sets the extension directories that will be used during the
  172. * compilation.
  173. */
  174. public void setExtdirs(Path extdirs) {
  175. if (this.extdirs == null) {
  176. this.extdirs = extdirs;
  177. } else {
  178. this.extdirs.append(extdirs);
  179. }
  180. }
  181. /**
  182. * Maybe creates a nested classpath element.
  183. */
  184. public Path createExtdirs() {
  185. if (extdirs == null) {
  186. extdirs = new Path();
  187. }
  188. return extdirs;
  189. }
  190. /**
  191. * Set the deprecation flag.
  192. */
  193. public void setDeprecation(boolean deprecation) {
  194. this.deprecation = deprecation;
  195. }
  196. /**
  197. * Set the debug flag.
  198. */
  199. public void setDebug(boolean debug) {
  200. this.debug = debug;
  201. }
  202. /**
  203. * Set the optimize flag.
  204. */
  205. public void setOptimize(boolean optimize) {
  206. this.optimize = optimize;
  207. }
  208. /**
  209. * Sets the target VM that the classes will be compiled for. Valid
  210. * strings are "1.1", "1.2", and "1.3".
  211. */
  212. public void setTarget(String target) {
  213. this.target = target;
  214. }
  215. /**
  216. * Set the filtering flag.
  217. */
  218. public void setFiltering(boolean filter) {
  219. filtering = filter;
  220. }
  221. /**
  222. * Executes the task.
  223. */
  224. public void execute() throws BuildException {
  225. // first off, make sure that we've got a srcdir and destdir
  226. if (src == null) {
  227. throw new BuildException("srcdir attribute must be set!");
  228. }
  229. String [] list = src.list();
  230. if (list.length == 0) {
  231. throw new BuildException("srcdir attribute must be set!");
  232. }
  233. if (destDir == null) {
  234. throw new BuildException("destdir attribute must be set!");
  235. }
  236. // scan source directories and dest directory to build up both copy lists and
  237. // compile lists
  238. resetFileLists();
  239. for (int i=0; i<list.length; i++) {
  240. File srcDir = (File)project.resolveFile(list[i]);
  241. if (!srcDir.exists()) {
  242. throw new BuildException("srcdir " + srcDir.getPath() + " does not exist!");
  243. }
  244. DirectoryScanner ds = this.getDirectoryScanner(srcDir);
  245. String[] files = ds.getIncludedFiles();
  246. scanDir(srcDir, destDir, files);
  247. }
  248. // compile the source files
  249. String compiler = project.getProperty("build.compiler");
  250. if (compiler == null) {
  251. if (Project.getJavaVersion().startsWith("1.3")) {
  252. compiler = "modern";
  253. } else {
  254. compiler = "classic";
  255. }
  256. }
  257. if (compileList.size() > 0) {
  258. log("Compiling " + compileList.size() +
  259. " source files to " + destDir);
  260. if (compiler.equalsIgnoreCase("classic")) {
  261. doClassicCompile();
  262. } else if (compiler.equalsIgnoreCase("modern")) {
  263. doModernCompile();
  264. } else if (compiler.equalsIgnoreCase("jikes")) {
  265. doJikesCompile();
  266. } else {
  267. String msg = "Don't know how to use compiler " + compiler;
  268. throw new BuildException(msg);
  269. }
  270. }
  271. // copy the support files
  272. if (filecopyList.size() > 0) {
  273. log("The implicit copying of support files by javac has been deprecated. " +
  274. "Use the copydir task to copy support files explicitly.",
  275. Project.MSG_WARN);
  276. log("Copying " + filecopyList.size() +
  277. " support files to " + destDir.getAbsolutePath());
  278. Enumeration enum = filecopyList.keys();
  279. while (enum.hasMoreElements()) {
  280. String fromFile = (String) enum.nextElement();
  281. String toFile = (String) filecopyList.get(fromFile);
  282. try {
  283. project.copyFile(fromFile, toFile, filtering);
  284. } catch (IOException ioe) {
  285. String msg = "Failed to copy " + fromFile + " to " + toFile
  286. + " due to " + ioe.getMessage();
  287. throw new BuildException(msg);
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Clear the list of files to be compiled and copied..
  294. */
  295. protected void resetFileLists() {
  296. compileList.removeAllElements();
  297. filecopyList.clear();
  298. }
  299. /**
  300. * Scans the directory looking for source files to be compiled and
  301. * support files to be copied. The results are returned in the
  302. * class variables compileList and filecopyList.
  303. */
  304. protected void scanDir(File srcDir, File destDir, String files[]) {
  305. long now = (new Date()).getTime();
  306. for (int i = 0; i < files.length; i++) {
  307. File srcFile = new File(srcDir, files[i]);
  308. if (files[i].endsWith(".java")) {
  309. File classFile = new File(destDir, files[i].substring(0,
  310. files[i].indexOf(".java")) + ".class");
  311. if (srcFile.lastModified() > now) {
  312. log("Warning: file modified in the future: " +
  313. files[i], Project.MSG_WARN);
  314. }
  315. if (srcFile.lastModified() > classFile.lastModified()) {
  316. compileList.addElement(srcFile.getAbsolutePath());
  317. }
  318. } else {
  319. File destFile = new File(destDir, files[i]);
  320. if (srcFile.lastModified() > destFile.lastModified()) {
  321. filecopyList.put(srcFile.getAbsolutePath(),
  322. destFile.getAbsolutePath());
  323. }
  324. }
  325. }
  326. }
  327. /**
  328. * Builds the compilation classpath.
  329. */
  330. // XXX
  331. // we need a way to not use the current classpath.
  332. /**
  333. * @param addRuntime Shall <code>rt.jar</code> or
  334. * <code>classes.zip</code> be added to the classpath.
  335. */
  336. private String getCompileClasspath(boolean addRuntime) {
  337. Path classpath = new Path();
  338. // add dest dir to classpath so that previously compiled and
  339. // untouched classes are on classpath
  340. //classpath.append(sourceDir.getAbsolutePath());
  341. //classpath.append(File.pathSeparator);
  342. classpath.setLocation(destDir.getAbsolutePath());
  343. // add our classpath to the mix
  344. if (compileClasspath != null) {
  345. addExistingToClasspath(classpath,compileClasspath);
  346. }
  347. // add the system classpath
  348. addExistingToClasspath(classpath, Path.systemClasspath);
  349. if (addRuntime) {
  350. if (Project.getJavaVersion() == Project.JAVA_1_1) {
  351. addExistingToClasspath(classpath,
  352. new Path(System.getProperty("java.home")
  353. + File.separator + "lib"
  354. + File.separator
  355. + "classes.zip"));
  356. } else {
  357. // JDK > 1.1 seems to set java.home to the JRE directory.
  358. addExistingToClasspath(classpath,
  359. new Path(System.getProperty("java.home")
  360. + File.separator + "lib"
  361. + File.separator + "rt.jar"));
  362. // Just keep the old version as well and let addExistingToPath
  363. // sort it out.
  364. addExistingToClasspath(classpath,
  365. new Path(System.getProperty("java.home")
  366. + File.separator +"jre"
  367. + File.separator + "lib"
  368. + File.separator + "rt.jar"));
  369. }
  370. }
  371. return classpath.toString();
  372. }
  373. /**
  374. * Takes a classpath-like string, and adds each element of
  375. * this string to a new classpath, if the components exist.
  376. * Components that don't exist, aren't added.
  377. * We do this, because jikes issues warnings for non-existant
  378. * files/dirs in his classpath, and these warnings are pretty
  379. * annoying.
  380. * @param target - target classpath
  381. * @param source - source classpath
  382. * to get file objects.
  383. */
  384. private void addExistingToClasspath(Path target, Path source) {
  385. String[] list = source.list();
  386. for (int i=0; i<list.length; i++) {
  387. File f = project.resolveFile(list[i]);
  388. if (f.exists()) {
  389. target.setLocation(f.getAbsolutePath());
  390. } else {
  391. log("Dropping from classpath: "+
  392. f.getAbsolutePath(), Project.MSG_VERBOSE);
  393. }
  394. }
  395. }
  396. /**
  397. * Peforms a copmile using the classic compiler that shipped with
  398. * JDK 1.1 and 1.2.
  399. */
  400. private void doClassicCompile() throws BuildException {
  401. log("Using classic compiler", Project.MSG_VERBOSE);
  402. String classpath = getCompileClasspath(false);
  403. Vector argList = new Vector();
  404. if (deprecation == true)
  405. argList.addElement("-deprecation");
  406. argList.addElement("-d");
  407. argList.addElement(destDir.getAbsolutePath());
  408. argList.addElement("-classpath");
  409. // Just add "sourcepath" to classpath ( for JDK1.1 )
  410. if (Project.getJavaVersion().startsWith("1.1")) {
  411. argList.addElement(classpath.toString() + File.pathSeparator +
  412. src.toString());
  413. } else {
  414. argList.addElement(classpath.toString());
  415. argList.addElement("-sourcepath");
  416. argList.addElement(src.toString());
  417. if (target != null) {
  418. argList.addElement("-target");
  419. argList.addElement(target);
  420. }
  421. }
  422. if (debug) {
  423. argList.addElement("-g");
  424. }
  425. if (optimize) {
  426. argList.addElement("-O");
  427. }
  428. if (bootclasspath != null) {
  429. argList.addElement("-bootclasspath");
  430. argList.addElement(bootclasspath.toString());
  431. }
  432. if (extdirs != null) {
  433. argList.addElement("-extdirs");
  434. argList.addElement(extdirs.toString());
  435. }
  436. log("Compilation args: " + argList.toString(),
  437. Project.MSG_VERBOSE);
  438. String[] args = new String[argList.size() + compileList.size()];
  439. int counter = 0;
  440. for (int i = 0; i < argList.size(); i++) {
  441. args[i] = (String)argList.elementAt(i);
  442. counter++;
  443. }
  444. // XXX
  445. // should be using system independent line feed!
  446. StringBuffer niceSourceList = new StringBuffer("Files to be compiled:"
  447. + "\r\n");
  448. Enumeration enum = compileList.elements();
  449. while (enum.hasMoreElements()) {
  450. args[counter] = (String)enum.nextElement();
  451. niceSourceList.append(" " + args[counter] + "\r\n");
  452. counter++;
  453. }
  454. log(niceSourceList.toString(), Project.MSG_VERBOSE);
  455. // XXX
  456. // provide the compiler a different message sink - namely our own
  457. ByteArrayOutputStream out = new ByteArrayOutputStream();
  458. sun.tools.javac.Main compiler =
  459. new sun.tools.javac.Main(new TaskOutputStream(this, Project.MSG_WARN), "javac");
  460. if (!compiler.compile(args)) {
  461. throw new BuildException("Compile failed");
  462. }
  463. }
  464. /**
  465. * Performs a compile using the newer compiler that ships with JDK 1.3
  466. */
  467. private void doModernCompile() throws BuildException {
  468. try {
  469. Class.forName("com.sun.tools.javac.Main");
  470. } catch (ClassNotFoundException cnfe) {
  471. doClassicCompile();
  472. return;
  473. }
  474. log("Using modern compiler", Project.MSG_VERBOSE);
  475. String classpath = getCompileClasspath(false);
  476. Vector argList = new Vector();
  477. if (deprecation == true)
  478. argList.addElement("-deprecation");
  479. argList.addElement("-d");
  480. argList.addElement(destDir.getAbsolutePath());
  481. argList.addElement("-classpath");
  482. argList.addElement(classpath.toString());
  483. argList.addElement("-sourcepath");
  484. argList.addElement(src.toString());
  485. if (target != null) {
  486. argList.addElement("-target");
  487. argList.addElement(target);
  488. }
  489. if (debug) {
  490. argList.addElement("-g");
  491. }
  492. if (optimize) {
  493. argList.addElement("-O");
  494. }
  495. if (bootclasspath != null) {
  496. argList.addElement("-bootclasspath");
  497. argList.addElement(bootclasspath.toString());
  498. }
  499. if (extdirs != null) {
  500. argList.addElement("-extdirs");
  501. argList.addElement(extdirs.toString());
  502. }
  503. log("Compilation args: " + argList.toString(),
  504. Project.MSG_VERBOSE);
  505. String[] args = new String[argList.size() + compileList.size()];
  506. int counter = 0;
  507. for (int i = 0; i < argList.size(); i++) {
  508. args[i] = (String)argList.elementAt(i);
  509. counter++;
  510. }
  511. // XXX
  512. // should be using system independent line feed!
  513. StringBuffer niceSourceList = new StringBuffer("Files to be compiled:"
  514. + "\r\n");
  515. Enumeration enum = compileList.elements();
  516. while (enum.hasMoreElements()) {
  517. args[counter] = (String)enum.nextElement();
  518. niceSourceList.append(" " + args[counter] + "\r\n");
  519. counter++;
  520. }
  521. log(niceSourceList.toString(), Project.MSG_VERBOSE);
  522. // This won't build under JDK1.2.2 because the new compiler
  523. // doesn't exist there.
  524. //com.sun.tools.javac.Main compiler = new com.sun.tools.javac.Main();
  525. //if (compiler.compile(args) != 0) {
  526. // Use reflection to be able to build on all JDKs >= 1.1:
  527. try {
  528. Class c = Class.forName ("com.sun.tools.javac.Main");
  529. Object compiler = c.newInstance ();
  530. Method compile = c.getMethod ("compile",
  531. new Class [] {(new String [] {}).getClass ()});
  532. int result = ((Integer) compile.invoke
  533. (compiler, new Object [] {args})) .intValue ();
  534. if (result != MODERN_COMPILER_SUCCESS) {
  535. String msg =
  536. "Compile failed, messages should have been provided.";
  537. throw new BuildException(msg);
  538. }
  539. } catch (Exception ex) {
  540. throw new BuildException (ex);
  541. }
  542. }
  543. /**
  544. * Performs a compile using the Jikes compiler from IBM..
  545. * Mostly of this code is identical to doClassicCompile()
  546. * However, it does not support all options like
  547. * bootclasspath, extdirs, deprecation and so on, because
  548. * there is no option in jikes and I don't understand
  549. * what they should do.
  550. *
  551. * It has been successfully tested with jikes 1.10
  552. *
  553. * @author skanthak@muehlheim.de
  554. */
  555. private void doJikesCompile() throws BuildException {
  556. log("Using jikes compiler", Project.MSG_VERBOSE);
  557. Path classpath = new Path(getCompileClasspath(true));
  558. // Jikes doesn't support an extension dir (-extdir)
  559. // so we'll emulate it for compatibility and convenience.
  560. addExtdirsToClasspath(classpath);
  561. // Jikes has no option for source-path so we
  562. // will add it to classpath.
  563. classpath.append(src);
  564. Vector argList = new Vector();
  565. if (deprecation == true)
  566. argList.addElement("-deprecation");
  567. // We want all output on stdout to make
  568. // parsing easier
  569. argList.addElement("-Xstdout");
  570. argList.addElement("-d");
  571. argList.addElement(destDir.getAbsolutePath());
  572. argList.addElement("-classpath");
  573. argList.addElement(classpath.toString());
  574. if (debug) {
  575. argList.addElement("-g");
  576. }
  577. if (optimize) {
  578. argList.addElement("-O");
  579. }
  580. /**
  581. * XXX
  582. * Perhaps we shouldn't use properties for these
  583. * two options (emacs mode and warnings),
  584. * but include it in the javac directive?
  585. */
  586. /**
  587. * Jikes has the nice feature to print error
  588. * messages in a form readable by emacs, so
  589. * that emcas can directly set the cursor
  590. * to the place, where the error occured.
  591. */
  592. boolean emacsMode = false;
  593. String emacsProperty = project.getProperty("build.compiler.emacs");
  594. if (emacsProperty != null &&
  595. (emacsProperty.equalsIgnoreCase("on") ||
  596. emacsProperty.equalsIgnoreCase("true"))
  597. ) {
  598. emacsMode = true;
  599. }
  600. /**
  601. * Jikes issues more warnings that javac, for
  602. * example, when you have files in your classpath
  603. * that don't exist. As this is often the case, these
  604. * warning can be pretty annoying.
  605. */
  606. boolean warnings = true;
  607. String warningsProperty = project.getProperty("build.compiler.warnings");
  608. if (warningsProperty != null &&
  609. (warningsProperty.equalsIgnoreCase("off") ||
  610. warningsProperty.equalsIgnoreCase("false"))
  611. ) {
  612. warnings = false;
  613. }
  614. if (emacsMode)
  615. argList.addElement("+E");
  616. if (!warnings)
  617. argList.addElement("-nowarn");
  618. log("Compilation args: " + argList.toString(),
  619. Project.MSG_VERBOSE);
  620. String[] args = new String[argList.size() + compileList.size()];
  621. int counter = 0;
  622. for (int i = 0; i < argList.size(); i++) {
  623. args[i] = (String)argList.elementAt(i);
  624. counter++;
  625. }
  626. // XXX
  627. // should be using system independent line feed!
  628. StringBuffer niceSourceList = new StringBuffer("Files to be compiled:"
  629. + "\r\n");
  630. Enumeration enum = compileList.elements();
  631. while (enum.hasMoreElements()) {
  632. args[counter] = (String)enum.nextElement();
  633. niceSourceList.append(" " + args[counter] + "\r\n");
  634. counter++;
  635. }
  636. log(niceSourceList.toString(), Project.MSG_VERBOSE);
  637. // XXX
  638. // provide the compiler a different message sink - namely our own
  639. JikesOutputParser jop = new JikesOutputParser(this, emacsMode);
  640. Jikes compiler = new Jikes(jop,"jikes");
  641. compiler.compile(args);
  642. if (jop.getErrorFlag()) {
  643. String msg = "Compile failed, messages should have been provided.";
  644. throw new BuildException(msg);
  645. }
  646. }
  647. class JarFilenameFilter implements FilenameFilter {
  648. public boolean accept(File dir,String name) {
  649. return name.endsWith(".jar");
  650. }
  651. }
  652. /**
  653. * Emulation of extdirs feature in java >= 1.2.
  654. * This method adds all jar archives in the given
  655. * directories (but not in sub-directories!) to the classpath,
  656. * so that you don't have to specify them all one by one.
  657. * @param classpath - stringbuffer to append jar files to
  658. */
  659. private void addExtdirsToClasspath(Path classpath) {
  660. // FIXME
  661. // Should we scan files recursively? How does
  662. // javac handle this?
  663. if (extdirs != null) {
  664. String[] list = extdirs.list();
  665. for (int j=0; j<list.length; j++) {
  666. File dir = project.resolveFile(list[j]);
  667. String[] files = dir.list(new JarFilenameFilter());
  668. for (int i=0 ; i < files.length ; i++) {
  669. File f = new File(dir,files[i]);
  670. if (f.exists() && f.isFile()) {
  671. classpath.setLocation(f.getAbsolutePath());
  672. }
  673. }
  674. }
  675. }
  676. }
  677. }