]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/gcc/cross/GccCCompiler.java
Changed spelling to manifest
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / gcc / cross / GccCCompiler.java
1 /*
2 *
3 * Copyright 2001-2004 The Ant-Contrib project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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 package net.sf.antcontrib.cpptasks.gcc.cross;
18 import java.io.File;
19 import java.util.Vector;
20 import net.sf.antcontrib.cpptasks.CCTask;
21 import net.sf.antcontrib.cpptasks.CUtil;
22 import net.sf.antcontrib.cpptasks.CompilerParam;
23 import net.sf.antcontrib.cpptasks.compiler.CommandLineCompilerConfiguration;
24 import net.sf.antcontrib.cpptasks.compiler.LinkType;
25 import net.sf.antcontrib.cpptasks.compiler.Linker;
26 import net.sf.antcontrib.cpptasks.compiler.Processor;
27 import net.sf.antcontrib.cpptasks.compiler.ProgressMonitor;
28 import net.sf.antcontrib.cpptasks.gcc.GccCompatibleCCompiler;
29 import net.sf.antcontrib.cpptasks.parser.CParser;
30 import net.sf.antcontrib.cpptasks.parser.FortranParser;
31 import net.sf.antcontrib.cpptasks.parser.Parser;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.types.Environment;
34 import net.sf.antcontrib.cpptasks.OptimizationEnum;
35
36 /**
37 * Adapter for the GCC C/C++ compiler
38 *
39 * @author Adam Murdoch
40 */
41 public final class GccCCompiler extends GccCompatibleCCompiler {
42 private final static String[] headerExtensions = new String[]{".h", ".hpp",
43 ".inl"};
44 private final static String[] sourceExtensions = new String[]{".c", /* C */
45 ".cc", /* C++ */
46 ".cpp", /* C++ */
47 ".cxx", /* C++ */
48 ".c++", /* C++ */
49 ".i", /* preprocessed C */
50 ".ii", /* preprocessed C++ */
51 ".f", /* FORTRAN */
52 ".for", /* FORTRAN */
53 ".m", /* Objective-C */
54 ".mm", /* Objected-C++ */
55 ".s" /* Assembly */
56 };
57 private static final GccCCompiler cppInstance = new GccCCompiler("c++",
58 sourceExtensions, headerExtensions, false,
59 new GccCCompiler("c++", sourceExtensions, headerExtensions, true,
60 null, false, null), false, null);
61 private static final GccCCompiler g77Instance = new GccCCompiler("g77",
62 sourceExtensions, headerExtensions, false,
63 new GccCCompiler("g77", sourceExtensions, headerExtensions, true,
64 null, false, null), false, null);
65 private static final GccCCompiler gppInstance = new GccCCompiler("g++",
66 sourceExtensions, headerExtensions, false,
67 new GccCCompiler("g++", sourceExtensions, headerExtensions, true,
68 null, false, null), false, null);
69 private static final GccCCompiler instance = new GccCCompiler("gcc",
70 sourceExtensions, headerExtensions, false,
71 new GccCCompiler("gcc", sourceExtensions, headerExtensions, true,
72 null, false, null), false, null);
73 /**
74 * Gets c++ adapter
75 */
76 public static GccCCompiler getCppInstance() {
77 return cppInstance;
78 }
79 /**
80 * Gets g77 adapter
81 */
82 public static GccCCompiler getG77Instance() {
83 return g77Instance;
84 }
85 /**
86 * Gets gpp adapter
87 */
88 public static GccCCompiler getGppInstance() {
89 return gppInstance;
90 }
91 /**
92 * Gets gcc adapter
93 */
94 public static GccCCompiler getInstance() {
95 return instance;
96 }
97 private String identifier;
98 private File[] includePath;
99 private boolean isPICMeaningful = true;
100 /**
101 * Private constructor. Use GccCCompiler.getInstance() to get singleton
102 * instance of this class.
103 */
104 private GccCCompiler(String command, String[] sourceExtensions,
105 String[] headerExtensions, boolean isLibtool,
106 GccCCompiler libtoolCompiler, boolean newEnvironment,
107 Environment env) {
108 super(command, null, sourceExtensions, headerExtensions, isLibtool,
109 libtoolCompiler, newEnvironment, env);
110 isPICMeaningful = System.getProperty("os.name").indexOf("Windows") < 0;
111 }
112 public void addImpliedArgs(final Vector args,
113 final boolean debug,
114 final boolean multithreaded,
115 final boolean exceptions,
116 final LinkType linkType,
117 final Boolean rtti,
118 final OptimizationEnum optimization,
119 final Boolean defaultflag) {
120 super.addImpliedArgs(args, debug, multithreaded,
121 exceptions, linkType, rtti, optimization, defaultflag);
122 if (isPICMeaningful && linkType.isSharedLibrary()) {
123 args.addElement("-fPIC");
124 }
125 }
126 public Processor changeEnvironment(boolean newEnvironment, Environment env) {
127 if (newEnvironment || env != null) {
128 return new GccCCompiler(getCommand(), this.getSourceExtensions(),
129 this.getHeaderExtensions(), this.getLibtool(),
130 (GccCCompiler) this.getLibtoolCompiler(), newEnvironment,
131 env);
132 }
133 return this;
134 }
135 protected Object clone() throws CloneNotSupportedException {
136 GccCCompiler clone = (GccCCompiler) super.clone();
137 return clone;
138 }
139 public void compile(CCTask task, File outputDir, String[] sourceFiles,
140 String[] args, String[] endArgs, boolean relentless,
141 CommandLineCompilerConfiguration config, ProgressMonitor monitor)
142 throws BuildException {
143 try {
144 GccCCompiler clone = (GccCCompiler) this.clone();
145 CompilerParam param = config.getParam("target");
146 if (param != null)
147 clone.setCommand(param.getValue() + "-" + this.getCommand());
148 clone.supercompile(task, outputDir, sourceFiles, args, endArgs,
149 relentless, config, monitor);
150 } catch (CloneNotSupportedException e) {
151 supercompile(task, outputDir, sourceFiles, args, endArgs,
152 relentless, config, monitor);
153 }
154 }
155 /**
156 * Create parser to determine dependencies.
157 *
158 * Will create appropriate parser (C++, FORTRAN) based on file extension.
159 *
160 */
161 protected Parser createParser(File source) {
162 if (source != null) {
163 String sourceName = source.getName();
164 int lastDot = sourceName.lastIndexOf('.');
165 if (lastDot >= 0 && lastDot + 1 < sourceName.length()) {
166 char afterDot = sourceName.charAt(lastDot + 1);
167 if (afterDot == 'f' || afterDot == 'F') {
168 return new FortranParser();
169 }
170 }
171 }
172 return new CParser();
173 }
174 public File[] getEnvironmentIncludePath() {
175 if (includePath == null) {
176 //
177 // construct default include path from machine id and version id
178 //
179 String[] defaultInclude = new String[1];
180 StringBuffer buf = new StringBuffer("/lib/");
181 buf.append(GccProcessor.getMachine());
182 buf.append('/');
183 buf.append(GccProcessor.getVersion());
184 buf.append("/include");
185 defaultInclude[0] = buf.toString();
186 //
187 // read specs file and look for -istart and -idirafter
188 //
189 String[] specs = GccProcessor.getSpecs();
190 String[][] optionValues = GccProcessor.parseSpecs(specs, "*cpp:",
191 new String[]{"-isystem ", "-idirafter "});
192 //
193 // if no entries were found, then use a default path
194 //
195 if (optionValues[0].length == 0 && optionValues[1].length == 0) {
196 optionValues[0] = new String[]{"/usr/local/include",
197 "/usr/include", "/usr/include/win32api"};
198 }
199 //
200 // remove mingw entries.
201 // For MinGW compiles this will mean the
202 // location of the sys includes will be
203 // wrong in dependencies.xml
204 // but that should have no significant effect
205 for (int i = 0; i < optionValues.length; i++) {
206 for (int j = 0; j < optionValues[i].length; j++) {
207 if (optionValues[i][j].indexOf("mingw") > 0) {
208 optionValues[i][j] = null;
209 }
210 }
211 }
212 //
213 // if cygwin then
214 // we have to prepend location of gcc32
215 // and .. to start of absolute filenames to
216 // have something that will exist in the
217 // windows filesystem
218 if (GccProcessor.isCygwin()) {
219 GccProcessor.convertCygwinFilenames(optionValues[0]);
220 GccProcessor.convertCygwinFilenames(optionValues[1]);
221 GccProcessor.convertCygwinFilenames(defaultInclude);
222 }
223 int count = CUtil.checkDirectoryArray(optionValues[0]);
224 count += CUtil.checkDirectoryArray(optionValues[1]);
225 count += CUtil.checkDirectoryArray(defaultInclude);
226 includePath = new File[count];
227 int index = 0;
228 for (int i = 0; i < optionValues.length; i++) {
229 for (int j = 0; j < optionValues[i].length; j++) {
230 if (optionValues[i][j] != null) {
231 includePath[index++] = new File(optionValues[i][j]);
232 }
233 }
234 }
235 for (int i = 0; i < defaultInclude.length; i++) {
236 if (defaultInclude[i] != null) {
237 includePath[index++] = new File(defaultInclude[i]);
238 }
239 }
240 }
241 return (File[]) includePath.clone();
242 }
243 public String getIdentifier() throws BuildException {
244 if (identifier == null) {
245 StringBuffer buf;
246 if (getLibtool()) {
247 buf = new StringBuffer("libtool ");
248 } else {
249 buf = new StringBuffer(' ');
250 }
251 buf.append(getCommand());
252 buf.append(' ');
253 buf.append(GccProcessor.getVersion());
254 buf.append(' ');
255 buf.append(GccProcessor.getMachine());
256 identifier = buf.toString();
257 }
258 return identifier;
259 }
260 public Linker getLinker(LinkType linkType) {
261 return GccLinker.getInstance().getLinker(linkType);
262 }
263 public int getMaximumCommandLength() {
264 return Integer.MAX_VALUE;
265 }
266 private void supercompile(CCTask task, File outputDir,
267 String[] sourceFiles, String[] args, String[] endArgs,
268 boolean relentless, CommandLineCompilerConfiguration config,
269 ProgressMonitor monitor) throws BuildException {
270 super.compile(task, outputDir, sourceFiles, args, endArgs, relentless,
271 config, monitor);
272 }
273 }