KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > compilers > Gcj


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.compilers;
20
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.types.Commandline;
25 import org.apache.tools.ant.types.Path;
26
27 /**
28  * The implementation of the gcj compiler.
29  * This is primarily a cut-and-paste from the jikes.
30  *
31  * @since Ant 1.4
32  */

33 public class Gcj extends DefaultCompilerAdapter {
34
35     /**
36      * Performs a compile using the gcj compiler.
37      * @return true if the compilation succeeded
38      * @throws BuildException on error
39      */

40     public boolean execute() throws BuildException {
41         Commandline cmd;
42         attributes.log("Using gcj compiler", Project.MSG_VERBOSE);
43         cmd = setupGCJCommand();
44
45         int firstFileName = cmd.size();
46         logAndAddFilesToCompile(cmd);
47
48         return
49             executeExternalCompile(cmd.getCommandline(), firstFileName) == 0;
50     }
51
52     /**
53      * Set up the gcj commandline.
54      * @return the command line
55      */

56     protected Commandline setupGCJCommand() {
57         Commandline cmd = new Commandline();
58         Path classpath = new Path(project);
59
60         // gcj doesn't support bootclasspath dir (-bootclasspath)
61
// so we'll emulate it for compatibility and convenience.
62
Path p = getBootClassPath();
63         if (p.size() > 0) {
64             classpath.append(p);
65         }
66
67         // gcj doesn't support an extension dir (-extdir)
68
// so we'll emulate it for compatibility and convenience.
69
classpath.addExtdirs(extdirs);
70
71         classpath.append(getCompileClasspath());
72
73         // Gcj has no option for source-path so we
74
// will add it to classpath.
75
if (compileSourcepath != null) {
76             classpath.append(compileSourcepath);
77         } else {
78             classpath.append(src);
79         }
80
81         String JavaDoc exec = getJavac().getExecutable();
82         cmd.setExecutable(exec == null ? "gcj" : exec);
83
84         if (destDir != null) {
85             cmd.createArgument().setValue("-d");
86             cmd.createArgument().setFile(destDir);
87
88             if (!destDir.exists() && !destDir.mkdirs()) {
89                 throw new BuildException("Can't make output directories. "
90                                          + "Maybe permission is wrong. ");
91             }
92         }
93
94         cmd.createArgument().setValue("-classpath");
95         cmd.createArgument().setPath(classpath);
96
97         if (encoding != null) {
98             cmd.createArgument().setValue("--encoding=" + encoding);
99         }
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 JavaDoc[] additionalArguments = getJavac().getCurrentCompilerArgs();
129         int argsLength = 0;
130         while (!nativeBuild && argsLength < additionalArguments.length) {
131             int conflictLength = 0;
132             while (!nativeBuild
133                    && conflictLength < CONFLICT_WITH_DASH_C.length) {
134                 nativeBuild = (additionalArguments[argsLength].startsWith
135                                (CONFLICT_WITH_DASH_C[conflictLength]));
136                 conflictLength++;
137             }
138             argsLength++;
139         }
140         return nativeBuild;
141     }
142
143     private static final String JavaDoc [] CONFLICT_WITH_DASH_C = {
144         "-o" , "--main=", "-D", "-fjni", "-L"
145     };
146
147 }
148
Popular Tags