]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/gcc/cross/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
.cross
;
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
;
28 * A add-in class for Gcc processors
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
++) {
43 buf
.append(libnames
[i
]);
44 buf
.append(extension
);
45 patterns
[offset
+ i
] = buf
.toString();
47 return offset
+ libnames
.length
;
50 * Converts absolute Cygwin file or directory names to the corresponding
54 * array of names, some elements may be null, will be changed in
57 public static void convertCygwinFilenames(String
[] names
) {
59 throw new NullPointerException("names");
61 File gccDir
= CUtil
.getExecutableLocation("gcc.exe");
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) == '/') {
71 names
[i
] = buf
.toString();
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";
89 public static String
[] getOutputFileSwitch(String letter
, String outputFile
) {
90 StringBuffer buf
= new StringBuffer();
91 if (outputFile
.indexOf(' ') >= 0) {
93 buf
.append(outputFile
.replace('\\', '/'));
96 buf
.append(outputFile
.replace('\\', '/'));
98 String
[] retval
= new String
[]{letter
, buf
.toString()};
102 * Returns the contents of the gcc specs file.
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
109 * @returns contents of the specs file
111 public static String
[] getSpecs() {
113 File gccParent
= CUtil
.getExecutableLocation("gcc.exe");
114 if (gccParent
!= null) {
116 // build a relative path like
117 // ../lib/gcc-lib/i686-pc-cygwin/2.95.3-5/specs
119 StringBuffer buf
= new StringBuffer("../lib/gcc-lib/");
120 buf
.append(getMachine());
122 buf
.append(getVersion());
123 buf
.append("/specs");
125 // resolve it relative to the location of gcc.exe
127 String relativePath
= buf
.toString();
128 File specsFile
= new File(gccParent
, relativePath
);
130 // found the specs file
134 // read the lines in the file
136 BufferedReader reader
= new BufferedReader(new FileReader(
138 Vector lines
= new Vector(100);
139 String line
= reader
.readLine();
140 while (line
!= null) {
141 lines
.addElement(line
);
142 line
= reader
.readLine();
144 specs
= new String
[lines
.size()];
145 lines
.copyInto(specs
);
146 } catch (IOException ex
) {
151 specs
= new String
[0];
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";
167 public static boolean isCaseSensitive() {
171 * Determines if task is running with cygwin
173 * @return true if cygwin was detected
175 public static boolean isCygwin() {
176 return getMachine().indexOf("cygwin") > 0;
178 private static boolean isHPUX() {
179 String osname
= System
.getProperty("os.name").toLowerCase();
180 if (osname
.indexOf("hp") >= 0 && osname
.indexOf("ux") >= 0) {
187 * Parses the results of the specs file for a specific processor and
190 * @param specsContent
191 * Contents of specs file as returned from getSpecs
192 * @param specSectionStart
193 * start of spec section, for example "*cpp:"
195 * command line switches such as "-istart"
197 public static String
[][] parseSpecs(String
[] specsContent
,
198 String specSectionStart
, String
[] options
) {
199 if (specsContent
== null) {
200 throw new NullPointerException("specsContent");
202 if (specSectionStart
== null) {
203 throw new NullPointerException("specSectionStart");
205 if (options
== null) {
206 throw new NullPointerException("option");
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
];
213 // if start of section then start paying attention
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);
221 // go to next line and examine contents
222 // and repeat until end of file
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);
231 // walk rest of line looking for first non
233 // and then next space
234 boolean hasNonBlank
= false;
235 int k
= optionStart
+ options
[j
].length();
236 for (; k
< specLine
.length(); k
++) {
238 // either a blank or a "}" (close of
240 // section will end the path
242 if (specLine
.charAt(k
) == ' '
243 || specLine
.charAt(k
) == '}') {
249 optionValue
.append(specLine
.charAt(k
));
253 // transition back to whitespace
254 // value is over, add it to vector
256 optionVectors
[j
].addElement(optionValue
260 // find next occurance on line
262 optionStart
= specLine
.indexOf(options
[j
], k
);
267 // copy vectors over to option arrays
269 for (int j
= 0; j
< options
.length
; j
++) {
270 optionValues
[j
] = new String
[optionVectors
[j
].size()];
271 optionVectors
[j
].copyInto(optionValues
[j
]);
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
;
286 private GccProcessor() {