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