]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/gcc/GccProcessor.java
3 * Copyright 2002-2004 The Ant-Contrib project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package net
.sf
.antcontrib
.cpptasks
.gcc
;
18 import java
.io
.BufferedReader
;
20 import java
.io
.FileReader
;
21 import java
.io
.IOException
;
22 import java
.util
.Vector
;
24 import net
.sf
.antcontrib
.cpptasks
.CUtil
;
25 import net
.sf
.antcontrib
.cpptasks
.compiler
.CaptureStreamHandler
;
27 * A add-in class for Gcc processors
31 public class GccProcessor
{
32 // the results from gcc -dumpmachine
33 private static String machine
;
34 private static String
[] specs
;
35 // the results from gcc -dumpversion
36 private static String version
;
37 private static int addLibraryPatterns(String
[] libnames
, StringBuffer buf
,
38 String prefix
, String extension
, String
[] patterns
, int offset
) {
39 for (int i
= 0; i
< libnames
.length
; i
++) {
42 buf
.append(libnames
[i
]);
43 buf
.append(extension
);
44 patterns
[offset
+ i
] = buf
.toString();
46 return offset
+ libnames
.length
;
49 * Converts absolute Cygwin file or directory names to the corresponding
53 * array of names, some elements may be null, will be changed in
56 public static void convertCygwinFilenames(String
[] names
) {
58 throw new NullPointerException("names");
60 File gccDir
= CUtil
.getExecutableLocation("gcc.exe");
62 String prefix
= gccDir
.getAbsolutePath() + "/..";
63 StringBuffer buf
= new StringBuffer();
64 for (int i
= 0; i
< names
.length
; i
++) {
65 String name
= names
[i
];
66 if (name
!= null && name
.length() > 1 && name
.charAt(0) == '/') {
70 names
[i
] = buf
.toString();
75 public static String
[] getLibraryPatterns(String
[] libnames
) {
76 StringBuffer buf
= new StringBuffer();
77 String
[] patterns
= new String
[libnames
.length
* 2];
78 int offset
= addLibraryPatterns(libnames
, buf
, "lib", ".a", patterns
, 0);
80 offset
= addLibraryPatterns(libnames
, buf
, "lib", ".sl", patterns
,
83 offset
= addLibraryPatterns(libnames
, buf
, "lib", ".so", patterns
,
88 public static String
getMachine() {
89 if (machine
== null) {
90 String
[] args
= new String
[]{"gcc", "-dumpmachine"};
91 String
[] cmdout
= CaptureStreamHandler
.run(args
);
92 if (cmdout
.length
== 0) {
93 machine
= "nomachine";
100 public static String
[] getOutputFileSwitch(String letter
, String outputFile
) {
101 StringBuffer buf
= new StringBuffer();
102 if (outputFile
.indexOf(' ') >= 0) {
104 buf
.append(outputFile
.replace('\\', '/'));
107 buf
.append(outputFile
.replace('\\', '/'));
109 String
[] retval
= new String
[]{letter
, buf
.toString()};
113 * Returns the contents of the gcc specs file.
115 * The implementation locates gcc.exe in the executable path and then
116 * builds a relative path name from the results of -dumpmachine and
117 * -dumpversion. Attempts to use gcc -dumpspecs to provide this information
118 * resulted in stalling on the Execute.run
120 * @returns contents of the specs file
122 public static String
[] getSpecs() {
124 File gccParent
= CUtil
.getExecutableLocation("gcc.exe");
125 if (gccParent
!= null) {
127 // build a relative path like
128 // ../lib/gcc-lib/i686-pc-cygwin/2.95.3-5/specs
130 StringBuffer buf
= new StringBuffer("../lib/gcc-lib/");
131 buf
.append(getMachine());
133 buf
.append(getVersion());
134 buf
.append("/specs");
136 // resolve it relative to the location of gcc.exe
138 String relativePath
= buf
.toString();
139 File specsFile
= new File(gccParent
, relativePath
);
141 // found the specs file
145 // read the lines in the file
147 BufferedReader reader
= new BufferedReader(new FileReader(
149 Vector lines
= new Vector(100);
150 String line
= reader
.readLine();
151 while (line
!= null) {
152 lines
.addElement(line
);
153 line
= reader
.readLine();
155 specs
= new String
[lines
.size()];
156 lines
.copyInto(specs
);
157 } catch (IOException ex
) {
162 specs
= new String
[0];
166 public static String
getVersion() {
167 if (version
== null) {
168 String
[] args
= new String
[]{"gcc", "-dumpversion"};
169 String
[] cmdout
= CaptureStreamHandler
.run(args
);
170 if (cmdout
.length
== 0) {
171 version
= "noversion";
178 public static boolean isCaseSensitive() {
182 * Determines if task is running with cygwin
184 * @return true if cygwin was detected
186 public static boolean isCygwin() {
187 return getMachine().indexOf("cygwin") > 0;
189 private static boolean isHPUX() {
190 String osname
= System
.getProperty("os.name").toLowerCase();
191 if (osname
.indexOf("hp") >= 0 && osname
.indexOf("ux") >= 0) {
198 * Parses the results of the specs file for a specific processor and
201 * @param specsContent
202 * Contents of specs file as returned from getSpecs
203 * @param specSectionStart
204 * start of spec section, for example "*cpp:"
206 * command line switches such as "-istart"
208 public static String
[][] parseSpecs(String
[] specsContent
,
209 String specSectionStart
, String
[] options
) {
210 if (specsContent
== null) {
211 throw new NullPointerException("specsContent");
213 if (specSectionStart
== null) {
214 throw new NullPointerException("specSectionStart");
216 if (options
== null) {
217 throw new NullPointerException("option");
219 String
[][] optionValues
= new String
[options
.length
][];
220 StringBuffer optionValue
= new StringBuffer(40);
221 for (int i
= 0; i
< specsContent
.length
; i
++) {
222 String specLine
= specsContent
[i
];
224 // if start of section then start paying attention
226 if (specLine
.startsWith(specSectionStart
)) {
227 Vector
[] optionVectors
= new Vector
[options
.length
];
228 for (int j
= 0; j
< options
.length
; j
++) {
229 optionVectors
[j
] = new Vector(10);
232 // go to next line and examine contents
233 // and repeat until end of file
235 for (i
++; i
< specsContent
.length
; i
++) {
236 specLine
= specsContent
[i
];
237 for (int j
= 0; j
< options
.length
; j
++) {
238 int optionStart
= specLine
.indexOf(options
[j
]);
239 while (optionStart
>= 0) {
240 optionValue
.setLength(0);
242 // walk rest of line looking for first non
244 // and then next space
245 boolean hasNonBlank
= false;
246 int k
= optionStart
+ options
[j
].length();
247 for (; k
< specLine
.length(); k
++) {
249 // either a blank or a "}" (close of
251 // section will end the path
253 if (specLine
.charAt(k
) == ' '
254 || specLine
.charAt(k
) == '}') {
260 optionValue
.append(specLine
.charAt(k
));
264 // transition back to whitespace
265 // value is over, add it to vector
267 optionVectors
[j
].addElement(optionValue
271 // find next occurance on line
273 optionStart
= specLine
.indexOf(options
[j
], k
);
278 // copy vectors over to option arrays
280 for (int j
= 0; j
< options
.length
; j
++) {
281 optionValues
[j
] = new String
[optionVectors
[j
].size()];
282 optionVectors
[j
].copyInto(optionValues
[j
]);
287 // fill in any missing option values with
288 // a zero-length string array
289 for (int i
= 0; i
< optionValues
.length
; i
++) {
290 String
[] zeroLenArray
= new String
[0];
291 if (optionValues
[i
] == null) {
292 optionValues
[i
] = zeroLenArray
;
297 private GccProcessor() {