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