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