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 org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Project;
  20. import org.apache.tools.ant.types.Commandline;
  21. import org.apache.tools.ant.types.Path;
  22. /**
  23. * The implementation of the gcj compiler.
  24. * This is primarily a cut-and-paste from the jikes.
  25. *
  26. * @since Ant 1.4
  27. */
  28. public class Gcj extends DefaultCompilerAdapter {
  29. /**
  30. * Performs a compile using the gcj compiler.
  31. */
  32. public boolean execute() throws BuildException {
  33. Commandline cmd;
  34. attributes.log("Using gcj compiler", Project.MSG_VERBOSE);
  35. cmd = setupGCJCommand();
  36. int firstFileName = cmd.size();
  37. logAndAddFilesToCompile(cmd);
  38. return
  39. executeExternalCompile(cmd.getCommandline(), firstFileName) == 0;
  40. }
  41. protected Commandline setupGCJCommand() {
  42. Commandline cmd = new Commandline();
  43. Path classpath = new Path(project);
  44. // gcj doesn't support bootclasspath dir (-bootclasspath)
  45. // so we'll emulate it for compatibility and convenience.
  46. if (bootclasspath != null) {
  47. classpath.append(bootclasspath);
  48. }
  49. // gcj doesn't support an extension dir (-extdir)
  50. // so we'll emulate it for compatibility and convenience.
  51. classpath.addExtdirs(extdirs);
  52. if (bootclasspath == null || bootclasspath.size() == 0) {
  53. // no bootclasspath, therefore, get one from the java runtime
  54. includeJavaRuntime = true;
  55. }
  56. classpath.append(getCompileClasspath());
  57. // Gcj has no option for source-path so we
  58. // will add it to classpath.
  59. if (compileSourcepath != null) {
  60. classpath.append(compileSourcepath);
  61. } else {
  62. classpath.append(src);
  63. }
  64. String exec = getJavac().getExecutable();
  65. cmd.setExecutable(exec == null ? "gcj" : exec);
  66. if (destDir != null) {
  67. cmd.createArgument().setValue("-d");
  68. cmd.createArgument().setFile(destDir);
  69. if (!destDir.exists() && !destDir.mkdirs()) {
  70. throw new BuildException("Can't make output directories. "
  71. + "Maybe permission is wrong. ");
  72. }
  73. }
  74. cmd.createArgument().setValue("-classpath");
  75. cmd.createArgument().setPath(classpath);
  76. if (encoding != null) {
  77. cmd.createArgument().setValue("--encoding=" + encoding);
  78. }
  79. if (debug) {
  80. cmd.createArgument().setValue("-g1");
  81. }
  82. if (optimize) {
  83. cmd.createArgument().setValue("-O");
  84. }
  85. /**
  86. * gcj should be set for generate class.
  87. * ... if no 'compile to native' argument is passed
  88. */
  89. if (!isNativeBuild()) {
  90. cmd.createArgument().setValue("-C");
  91. }
  92. addCurrentCompilerArgs(cmd);
  93. return cmd;
  94. }
  95. /**
  96. * Whether any of the arguments given via <compilerarg>
  97. * implies that compilation to native code is requested.
  98. *
  99. * @since Ant 1.6.2
  100. */
  101. public boolean isNativeBuild() {
  102. boolean nativeBuild = false;
  103. String[] additionalArguments = getJavac().getCurrentCompilerArgs();
  104. int argsLength=0;
  105. while (!nativeBuild && argsLength < additionalArguments.length) {
  106. int conflictLength = 0;
  107. while (!nativeBuild
  108. && conflictLength < CONFLICT_WITH_DASH_C.length) {
  109. nativeBuild = (additionalArguments[argsLength].startsWith
  110. (CONFLICT_WITH_DASH_C[conflictLength]));
  111. conflictLength++;
  112. }
  113. argsLength++;
  114. }
  115. return nativeBuild;
  116. }
  117. private static final String [] CONFLICT_WITH_DASH_C = {
  118. "-o" , "--main=", "-D", "-fjni", "-L"
  119. };
  120. }