1. /*
  2. * Copyright 2001-2004 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. package org.apache.tools.ant.taskdefs.compilers;
  18. import java.lang.reflect.Method;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.types.Commandline;
  22. import org.apache.tools.ant.types.Path;
  23. /**
  24. * The implementation of the Java compiler for KJC.
  25. * This is primarily a cut-and-paste from Jikes.java and
  26. * DefaultCompilerAdapter.
  27. *
  28. * @since Ant 1.4
  29. */
  30. public class Kjc extends DefaultCompilerAdapter {
  31. /**
  32. * Run the compilation.
  33. *
  34. * @exception BuildException if the compilation has problems.
  35. */
  36. public boolean execute() throws BuildException {
  37. attributes.log("Using kjc compiler", Project.MSG_VERBOSE);
  38. Commandline cmd = setupKjcCommand();
  39. try {
  40. Class c = Class.forName("at.dms.kjc.Main");
  41. // Call the compile() method
  42. Method compile = c.getMethod("compile",
  43. new Class [] {String [].class});
  44. Boolean ok =
  45. (Boolean) compile.invoke(null,
  46. new Object[] {cmd.getArguments()});
  47. return ok.booleanValue();
  48. } catch (ClassNotFoundException ex) {
  49. throw new BuildException("Cannot use kjc compiler, as it is not "
  50. + "available. A common solution is to "
  51. + "set the environment variable CLASSPATH "
  52. + "to your kjc archive (kjc.jar).",
  53. location);
  54. } catch (Exception ex) {
  55. if (ex instanceof BuildException) {
  56. throw (BuildException) ex;
  57. } else {
  58. throw new BuildException("Error starting kjc compiler: ",
  59. ex, location);
  60. }
  61. }
  62. }
  63. /**
  64. * setup kjc command arguments.
  65. */
  66. protected Commandline setupKjcCommand() {
  67. Commandline cmd = new Commandline();
  68. // generate classpath, because kjc doesn't support sourcepath.
  69. Path classpath = getCompileClasspath();
  70. if (deprecation == true) {
  71. cmd.createArgument().setValue("-deprecation");
  72. }
  73. if (destDir != null) {
  74. cmd.createArgument().setValue("-d");
  75. cmd.createArgument().setFile(destDir);
  76. }
  77. // generate the clsspath
  78. cmd.createArgument().setValue("-classpath");
  79. Path cp = new Path(project);
  80. // kjc don't have bootclasspath option.
  81. if (bootclasspath != null) {
  82. cp.append(bootclasspath);
  83. }
  84. if (extdirs != null) {
  85. cp.addExtdirs(extdirs);
  86. }
  87. cp.append(classpath);
  88. if (compileSourcepath != null) {
  89. cp.append(compileSourcepath);
  90. } else {
  91. cp.append(src);
  92. }
  93. cmd.createArgument().setPath(cp);
  94. // kjc-1.5A doesn't support -encoding option now.
  95. // but it will be supported near the feature.
  96. if (encoding != null) {
  97. cmd.createArgument().setValue("-encoding");
  98. cmd.createArgument().setValue(encoding);
  99. }
  100. if (debug) {
  101. cmd.createArgument().setValue("-g");
  102. }
  103. if (optimize) {
  104. cmd.createArgument().setValue("-O2");
  105. }
  106. if (verbose) {
  107. cmd.createArgument().setValue("-verbose");
  108. }
  109. addCurrentCompilerArgs(cmd);
  110. logAndAddFilesToCompile(cmd);
  111. return cmd;
  112. }
  113. }