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