]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/gcc/cross/sparc_sun_solaris2/GccProcessor.java
Changed spelling to manifest
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / gcc / cross / sparc_sun_solaris2 / GccProcessor.java
CommitLineData
878ddf1f 1/*\r
2 * \r
3 * Copyright 2002-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.sparc_sun_solaris2;\r
18import java.io.BufferedReader;\r
19import java.io.File;\r
20import java.io.FileReader;\r
21import java.io.IOException;\r
22import java.util.Vector;\r
23\r
24import net.sf.antcontrib.cpptasks.CUtil;\r
25import net.sf.antcontrib.cpptasks.compiler.CaptureStreamHandler;\r
26import net.sf.antcontrib.cpptasks.types.LibraryTypeEnum;\r
27\r
28/**\r
29 * A add-in class for Gcc processors\r
30 * \r
31 * \r
32 */\r
33public class GccProcessor {\r
34 // the results from gcc -dumpmachine\r
35 private static String machine;\r
36 private static String[] specs;\r
37 // the results from gcc -dumpversion\r
38 private static String version;\r
39 private static int addLibraryPatterns(String[] libnames, StringBuffer buf,\r
40 String prefix, String extension, String[] patterns, int offset) {\r
41 for (int i = 0; i < libnames.length; i++) {\r
42 buf.setLength(0);\r
43 buf.append(prefix);\r
44 buf.append(libnames[i]);\r
45 buf.append(extension);\r
46 patterns[offset + i] = buf.toString();\r
47 }\r
48 return offset + libnames.length;\r
49 }\r
50 /**\r
51 * Converts absolute Cygwin file or directory names to the corresponding\r
52 * Win32 name.\r
53 * \r
54 * @param names\r
55 * array of names, some elements may be null, will be changed in\r
56 * place.\r
57 */\r
58 public static void convertCygwinFilenames(String[] names) {\r
59 if (names == null) {\r
60 throw new NullPointerException("names");\r
61 }\r
62 File gccDir = CUtil.getExecutableLocation(GccCCompiler.CMD_PREFIX\r
63 + "gcc.exe");\r
64 if (gccDir != null) {\r
65 String prefix = gccDir.getAbsolutePath() + "/..";\r
66 StringBuffer buf = new StringBuffer();\r
67 for (int i = 0; i < names.length; i++) {\r
68 String name = names[i];\r
69 if (name != null && name.length() > 1 && name.charAt(0) == '/') {\r
70 buf.setLength(0);\r
71 buf.append(prefix);\r
72 buf.append(name);\r
73 names[i] = buf.toString();\r
74 }\r
75 }\r
76 }\r
77 }\r
78 public static String[] getLibraryPatterns(String[] libnames, LibraryTypeEnum libType) {\r
79 StringBuffer buf = new StringBuffer();\r
80 String[] patterns = new String[libnames.length * 2];\r
81 int offset = addLibraryPatterns(libnames, buf, "lib", ".a", patterns, 0);\r
82 if (isHPUX()) {\r
83 offset = addLibraryPatterns(libnames, buf, "lib", ".sl", patterns,\r
84 offset);\r
85 } else {\r
86 offset = addLibraryPatterns(libnames, buf, "lib", ".so", patterns,\r
87 offset);\r
88 }\r
89 return patterns;\r
90 }\r
91 public static String getMachine() {\r
92 if (machine == null) {\r
93 String[] args = new String[]{GccCCompiler.CMD_PREFIX + "gcc",\r
94 "-dumpmachine"};\r
95 String[] cmdout = CaptureStreamHandler.run(args);\r
96 if (cmdout.length == 0) {\r
97 machine = "nomachine";\r
98 } else {\r
99 machine = cmdout[0];\r
100 }\r
101 }\r
102 return machine;\r
103 }\r
104 public static String[] getOutputFileSwitch(String letter, String outputFile) {\r
105 StringBuffer buf = new StringBuffer();\r
106 if (outputFile.indexOf(' ') >= 0) {\r
107 buf.append('"');\r
108 buf.append(outputFile.replace('\\', '/'));\r
109 buf.append('"');\r
110 } else {\r
111 buf.append(outputFile.replace('\\', '/'));\r
112 }\r
113 String[] retval = new String[]{letter, buf.toString()};\r
114 return retval;\r
115 }\r
116 /**\r
117 * Returns the contents of the gcc specs file.\r
118 * \r
119 * The implementation locates gcc.exe in the executable path and then\r
120 * builds a relative path name from the results of -dumpmachine and\r
121 * -dumpversion. Attempts to use gcc -dumpspecs to provide this information\r
122 * resulted in stalling on the Execute.run\r
123 * \r
124 * @returns contents of the specs file\r
125 */\r
126 public static String[] getSpecs() {\r
127 if (specs == null) {\r
128 File gccParent = CUtil\r
129 .getExecutableLocation(GccCCompiler.CMD_PREFIX + "gcc.exe");\r
130 if (gccParent != null) {\r
131 //\r
132 // build a relative path like\r
133 // ../lib/gcc-lib/i686-pc-cygwin/2.95.3-5/specs\r
134 //\r
135 StringBuffer buf = new StringBuffer("../lib/gcc-lib/");\r
136 buf.append(getMachine());\r
137 buf.append('/');\r
138 buf.append(getVersion());\r
139 buf.append("/specs");\r
140 //\r
141 // resolve it relative to the location of gcc.exe\r
142 //\r
143 String relativePath = buf.toString();\r
144 File specsFile = new File(gccParent, relativePath);\r
145 //\r
146 // found the specs file\r
147 //\r
148 try {\r
149 //\r
150 // read the lines in the file\r
151 //\r
152 BufferedReader reader = new BufferedReader(new FileReader(\r
153 specsFile));\r
154 Vector lines = new Vector(100);\r
155 String line = reader.readLine();\r
156 while (line != null) {\r
157 lines.addElement(line);\r
158 line = reader.readLine();\r
159 }\r
160 specs = new String[lines.size()];\r
161 lines.copyInto(specs);\r
162 } catch (IOException ex) {\r
163 }\r
164 }\r
165 }\r
166 if (specs == null) {\r
167 specs = new String[0];\r
168 }\r
169 return specs;\r
170 }\r
171 public static String getVersion() {\r
172 if (version == null) {\r
173 String[] args = new String[]{GccCCompiler.CMD_PREFIX + "gcc",\r
174 "-dumpversion"};\r
175 String[] cmdout = CaptureStreamHandler.run(args);\r
176 if (cmdout.length == 0) {\r
177 version = "noversion";\r
178 } else {\r
179 version = cmdout[0];\r
180 }\r
181 }\r
182 return version;\r
183 }\r
184 public static boolean isCaseSensitive() {\r
185 return true;\r
186 }\r
187 /**\r
188 * Determines if task is running with cygwin\r
189 * \r
190 * @return true if cygwin was detected\r
191 */\r
192 public static boolean isCygwin() {\r
193 return getMachine().indexOf("cygwin") > 0;\r
194 }\r
195 private static boolean isHPUX() {\r
196 String osname = System.getProperty("os.name").toLowerCase();\r
197 if (osname.indexOf("hp") >= 0 && osname.indexOf("ux") >= 0) {\r
198 return true;\r
199 }\r
200 return false;\r
201 }\r
202 /**\r
203 * \r
204 * Parses the results of the specs file for a specific processor and\r
205 * options\r
206 * \r
207 * @param specsContent\r
208 * Contents of specs file as returned from getSpecs\r
209 * @param specSectionStart\r
210 * start of spec section, for example "*cpp:"\r
211 * @param options\r
212 * command line switches such as "-istart"\r
213 */\r
214 public static String[][] parseSpecs(String[] specsContent,\r
215 String specSectionStart, String[] options) {\r
216 if (specsContent == null) {\r
217 throw new NullPointerException("specsContent");\r
218 }\r
219 if (specSectionStart == null) {\r
220 throw new NullPointerException("specSectionStart");\r
221 }\r
222 if (options == null) {\r
223 throw new NullPointerException("option");\r
224 }\r
225 String[][] optionValues = new String[options.length][];\r
226 StringBuffer optionValue = new StringBuffer(40);\r
227 for (int i = 0; i < specsContent.length; i++) {\r
228 String specLine = specsContent[i];\r
229 //\r
230 // if start of section then start paying attention\r
231 //\r
232 if (specLine.startsWith(specSectionStart)) {\r
233 Vector[] optionVectors = new Vector[options.length];\r
234 for (int j = 0; j < options.length; j++) {\r
235 optionVectors[j] = new Vector(10);\r
236 }\r
237 //\r
238 // go to next line and examine contents\r
239 // and repeat until end of file\r
240 //\r
241 for (i++; i < specsContent.length; i++) {\r
242 specLine = specsContent[i];\r
243 for (int j = 0; j < options.length; j++) {\r
244 int optionStart = specLine.indexOf(options[j]);\r
245 while (optionStart >= 0) {\r
246 optionValue.setLength(0);\r
247 //\r
248 // walk rest of line looking for first non\r
249 // whitespace\r
250 // and then next space\r
251 boolean hasNonBlank = false;\r
252 int k = optionStart + options[j].length();\r
253 for (; k < specLine.length(); k++) {\r
254 //\r
255 // either a blank or a "}" (close of\r
256 // conditional)\r
257 // section will end the path\r
258 //\r
259 if (specLine.charAt(k) == ' '\r
260 || specLine.charAt(k) == '}') {\r
261 if (hasNonBlank) {\r
262 break;\r
263 }\r
264 } else {\r
265 hasNonBlank = true;\r
266 optionValue.append(specLine.charAt(k));\r
267 }\r
268 }\r
269 //\r
270 // transition back to whitespace\r
271 // value is over, add it to vector\r
272 if (hasNonBlank) {\r
273 optionVectors[j].addElement(optionValue\r
274 .toString());\r
275 }\r
276 //\r
277 // find next occurance on line\r
278 //\r
279 optionStart = specLine.indexOf(options[j], k);\r
280 }\r
281 }\r
282 }\r
283 //\r
284 // copy vectors over to option arrays\r
285 //\r
286 for (int j = 0; j < options.length; j++) {\r
287 optionValues[j] = new String[optionVectors[j].size()];\r
288 optionVectors[j].copyInto(optionValues[j]);\r
289 }\r
290 }\r
291 }\r
292 //\r
293 // fill in any missing option values with\r
294 // a zero-length string array\r
295 for (int i = 0; i < optionValues.length; i++) {\r
296 String[] zeroLenArray = new String[0];\r
297 if (optionValues[i] == null) {\r
298 optionValues[i] = zeroLenArray;\r
299 }\r
300 }\r
301 return optionValues;\r
302 }\r
303 private GccProcessor() {\r
304 }\r
305}\r