001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.compilers;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.types.Commandline;
024: import org.apache.tools.ant.types.Path;
025:
026: /**
027: * The implementation of the gcj compiler.
028: * This is primarily a cut-and-paste from the jikes.
029: *
030: * @since Ant 1.4
031: */
032: public class Gcj extends DefaultCompilerAdapter {
033:
034: /**
035: * Performs a compile using the gcj compiler.
036: * @return true if the compilation succeeded
037: * @throws BuildException on error
038: */
039: public boolean execute() throws BuildException {
040: Commandline cmd;
041: attributes.log("Using gcj compiler", Project.MSG_VERBOSE);
042: cmd = setupGCJCommand();
043:
044: int firstFileName = cmd.size();
045: logAndAddFilesToCompile(cmd);
046:
047: return executeExternalCompile(cmd.getCommandline(),
048: firstFileName) == 0;
049: }
050:
051: /**
052: * Set up the gcj commandline.
053: * @return the command line
054: */
055: protected Commandline setupGCJCommand() {
056: Commandline cmd = new Commandline();
057: Path classpath = new Path(project);
058:
059: // gcj doesn't support bootclasspath dir (-bootclasspath)
060: // so we'll emulate it for compatibility and convenience.
061: Path p = getBootClassPath();
062: if (p.size() > 0) {
063: classpath.append(p);
064: }
065:
066: // gcj doesn't support an extension dir (-extdir)
067: // so we'll emulate it for compatibility and convenience.
068: classpath.addExtdirs(extdirs);
069:
070: classpath.append(getCompileClasspath());
071:
072: // Gcj has no option for source-path so we
073: // will add it to classpath.
074: if (compileSourcepath != null) {
075: classpath.append(compileSourcepath);
076: } else {
077: classpath.append(src);
078: }
079:
080: String exec = getJavac().getExecutable();
081: cmd.setExecutable(exec == null ? "gcj" : exec);
082:
083: if (destDir != null) {
084: cmd.createArgument().setValue("-d");
085: cmd.createArgument().setFile(destDir);
086:
087: if (!destDir.exists() && !destDir.mkdirs()) {
088: throw new BuildException(
089: "Can't make output directories. "
090: + "Maybe permission is wrong. ");
091: }
092: }
093:
094: cmd.createArgument().setValue("-classpath");
095: cmd.createArgument().setPath(classpath);
096:
097: if (encoding != null) {
098: cmd.createArgument().setValue("--encoding=" + encoding);
099: }
100: if (debug) {
101: cmd.createArgument().setValue("-g1");
102: }
103: if (optimize) {
104: cmd.createArgument().setValue("-O");
105: }
106:
107: /**
108: * gcj should be set for generate class.
109: * ... if no 'compile to native' argument is passed
110: */
111: if (!isNativeBuild()) {
112: cmd.createArgument().setValue("-C");
113: }
114:
115: addCurrentCompilerArgs(cmd);
116:
117: return cmd;
118: }
119:
120: /**
121: * Whether any of the arguments given via <compilerarg>
122: * implies that compilation to native code is requested.
123: * @return true if compilation to native code is requested
124: * @since Ant 1.6.2
125: */
126: public boolean isNativeBuild() {
127: boolean nativeBuild = false;
128: String[] additionalArguments = getJavac()
129: .getCurrentCompilerArgs();
130: int argsLength = 0;
131: while (!nativeBuild && argsLength < additionalArguments.length) {
132: int conflictLength = 0;
133: while (!nativeBuild
134: && conflictLength < CONFLICT_WITH_DASH_C.length) {
135: nativeBuild = (additionalArguments[argsLength]
136: .startsWith(CONFLICT_WITH_DASH_C[conflictLength]));
137: conflictLength++;
138: }
139: argsLength++;
140: }
141: return nativeBuild;
142: }
143:
144: private static final String[] CONFLICT_WITH_DASH_C = { "-o",
145: "--main=", "-D", "-fjni", "-L" };
146:
147: }
|