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