]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/userdefine/CommandLineUserDefine.java
Change to new XML Schema.
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / userdefine / CommandLineUserDefine.java
1 package net.sf.antcontrib.cpptasks.userdefine;
2
3 import java.io.File;
4 import java.util.Iterator;
5 import java.util.StringTokenizer;
6 import java.util.Vector;
7
8 import net.sf.antcontrib.cpptasks.CCTask;
9 import net.sf.antcontrib.cpptasks.CUtil;
10 import net.sf.antcontrib.cpptasks.types.CommandLineArgument;
11 import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;
12 import net.sf.antcontrib.cpptasks.types.LibrarySet;
13
14 import org.apache.tools.ant.BuildException;
15 import org.apache.tools.ant.DirectoryScanner;
16 import org.apache.tools.ant.Project;
17 /*
18 *
19 * Copyright 2001-2004 The Ant-Contrib project
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 */
33 public class CommandLineUserDefine {
34
35 String command;
36
37 /*
38 * The follows variable set at child class.
39 */
40 String includeFileFlag = null;
41 String entryPointFlag = null;
42 String subSystemFlag = null;
43 String mapFlag = null;
44 String pdbFlag = null;
45 String outputFileFlag = null;
46 String includePathDelimiter = null;
47
48 /*
49 * get lib string if Vendor = "gcc", it should respectively aadd "-(" and ")-"
50 * at library set before and end. This value set at userDefineCompiler class.
51 */
52 Vector<String> libSetList = new Vector<String>();
53 Vector<String> fileList = new Vector<String>();
54 public void command(CCTask cctask, UserDefineDef userdefine){
55 File workdir;
56 File outdir;
57 Project project = cctask.getProject();
58 if(userdefine.getWorkdir() == null) {
59 workdir = new File(".");
60 }
61 else {
62 workdir = userdefine.getWorkdir();
63 }
64
65 /*
66 * generate cmdline= command + args + includepath + endargs + outfile
67 */
68 Vector args = new Vector();
69 Vector argsWithoutSpace = new Vector();
70 Vector endargs = new Vector();
71 Vector endargsWithoutSpace = new Vector();
72 Vector includePath = new Vector();
73
74 /*
75 * Generate cmdline = command +
76 * general args +
77 * outputflag + outputfile
78 * subsystemFlag + subsystemValue +
79 * includeFlag + includeFile +
80 * includeFileincludpath +
81 * entryPointFlag + entryPointValue +
82 * mapFlag + mapValue +
83 * pdbFlag + pdbValue +
84 * endargs +
85 *
86 *
87 */
88 /*
89 * get Args.
90 */
91 CommandLineArgument[] argument = userdefine.getActiveProcessorArgs();
92 for (int j = 0; j < argument.length; j++) {
93 if (argument[j].getLocation() == 0) {
94 args.addElement(argument[j].getValue());
95 } else {
96 endargs.addElement(argument[j].getValue());
97 }
98 }
99 /*
100 * get include path.
101 */
102 String[] incPath = userdefine.getActiveIncludePaths();
103 for (int j = 0; j < incPath.length; j++) {
104 if(incPath[j].indexOf(' ') >= 0) {
105 includePath.addElement( includePathDelimiter + incPath[j]);
106 //includePath.addElement( includePathDelimiter + "\"" + incPath[j] + "\"");
107 }
108 else {
109 includePath.addElement( includePathDelimiter + incPath[j]);
110 }
111 }
112 /*
113 * Remove space in args and endargs.
114 */
115 for ( int i=0; i < args.size(); i++) {
116 String str = (String)args.get(i);
117 StringTokenizer st = new StringTokenizer(str);
118 while(st.hasMoreTokens()) {
119 argsWithoutSpace.addElement(st.nextToken());
120 }
121 }
122 for ( int i=0; i < endargs.size(); i++) {
123 String str = (String)endargs.get(i);
124 StringTokenizer st = new StringTokenizer(str);
125 while(st.hasMoreTokens()) {
126 endargsWithoutSpace.addElement(st.nextToken());
127 }
128 }
129
130 int cmdLen = 0;
131 if(userdefine.getOutdir() == null) {
132 outdir = new File(".");
133 /*
134 * command + args + endargs + includepath + sourcefile
135 */
136 cmdLen = 1 + argsWithoutSpace.size() + endargsWithoutSpace.size() + includePath.size() + 1;
137 }
138 else {
139 outdir = userdefine.getOutdir();
140 /*
141 * command + args + endargs + includepath + sourcefile + outfile
142 */
143 cmdLen = 1 + argsWithoutSpace.size() + endargsWithoutSpace.size() + includePath.size() + 2;
144 }
145 if (includeFileFlag != null && includeFileFlag.trim().length() > 0){
146 cmdLen++;
147 }
148 if (entryPointFlag != null && entryPointFlag.trim().length() > 0){
149 cmdLen++;
150 }
151 if (subSystemFlag != null && subSystemFlag.trim().length() > 0){
152 cmdLen++;
153 }
154 if (mapFlag != null && mapFlag.trim().length() > 0){
155 cmdLen++;
156 }
157 if (pdbFlag != null && pdbFlag.trim().length() > 0){
158 cmdLen++;
159 }
160 if (libSetList != null && libSetList.size() > 0){
161 cmdLen = cmdLen + libSetList.size();
162 }
163 if (fileList != null){
164 cmdLen = cmdLen + fileList.size();
165 }
166 /*
167 * In gcc the "cr" flag should follow space then add outputfile name, otherwise
168 * it will pop error.
169 */
170 if (outputFileFlag != null && outputFileFlag.trim().length() > 0){
171 if (outputFileFlag.trim().equalsIgnoreCase("-cr")){
172 cmdLen = cmdLen + 2;
173 }else {
174 cmdLen++;
175 }
176
177 }
178 /*
179 * for every source file
180 * if file is header file, just skip it (add later)
181 */
182 Vector srcSets = userdefine.getSrcSets();
183 if (srcSets.size() == 0) {
184 String[] cmd = new String[cmdLen - 1];
185 int index = 0;
186 cmd[index++] = this.command;
187
188
189
190 Iterator iter = argsWithoutSpace.iterator();
191 while (iter.hasNext()) {
192 cmd[index++] = project.replaceProperties((String)iter.next());
193 //cmd[index++] = (String)iter.next();
194 }
195
196 iter = endargsWithoutSpace.iterator();
197 while (iter.hasNext()) {
198 cmd[index++] = (String)iter.next();
199 }
200
201 /*
202 * "OutputFlag + outputFile" as first option follow command.exe.
203 */
204 if (outputFileFlag != null && outputFileFlag.trim().length() > 0){
205 if (outputFileFlag.trim().equalsIgnoreCase("-cr")){
206 cmd[index++] = outputFileFlag;
207 cmd[index++] = userdefine.getOutputFile();
208 }else {
209 cmd[index++] = outputFileFlag + userdefine.getOutputFile();
210 }
211 }
212
213 /*
214 * Add fileList to cmd
215 */
216 if (fileList != null && fileList.size()> 0){
217 for (int i = 0; i < fileList.size(); i++){
218 cmd[index++] = fileList.get(i);
219 }
220 }
221
222 if (subSystemFlag != null && subSystemFlag.trim().length() > 0){
223 cmd[index++] = subSystemFlag + userdefine.getSubSystemvalue();
224 }
225 if (includeFileFlag != null && includeFileFlag.trim().length() > 0){
226 cmd[index++] = includeFileFlag + userdefine.getIncludeFile();
227 }
228
229 iter = includePath.iterator();
230 while (iter.hasNext()) {
231 cmd[index++] = (String)iter.next();
232 }
233
234 if (entryPointFlag != null && entryPointFlag.trim().length() > 0){
235 //
236 // If GCC link use __ModuleEntrypoint instead of _ModuleEntryPoint;
237 //
238 if (entryPointFlag.equalsIgnoreCase("-e")){
239 cmd[index++] = entryPointFlag + "_" + userdefine.getEntryPointvalue();
240 } else {
241 cmd[index++] = entryPointFlag + userdefine.getEntryPointvalue();
242 }
243
244 }
245 if (mapFlag != null && mapFlag.trim().length() > 0){
246 cmd[index++] = mapFlag + userdefine.getMapvalue();
247 }
248 if (pdbFlag != null && pdbFlag.trim().length() > 0){
249 cmd[index++] = pdbFlag + userdefine.getPdbvalue();
250 }
251
252 if (userdefine.getOutdir() != null){
253 // will add code to generate outfile name and flag
254 cmd[index++] = "/nologo";
255 }
256
257 if (libSetList != null && libSetList.size() > 0){
258 for (int i = 0; i < libSetList.size(); i++){
259 cmd[index++] = libSetList.get(i);
260 }
261 }
262
263 // execute the command
264 int retval = runCommand(cctask, workdir, cmd);
265 // if with monitor, add more code
266 if (retval != 0) {
267 throw new BuildException(this.command
268 + " failed with return code " + retval,
269 cctask.getLocation());
270 }
271 }
272
273 //
274 // if have source file append source file in command land.
275 //
276 for (int i = 0; i < srcSets.size(); i++) {
277 ConditionalFileSet srcSet = (ConditionalFileSet) srcSets
278 .elementAt(i);
279 if (srcSet.isActive()) {
280 // Find matching source files
281 DirectoryScanner scanner = srcSet.getDirectoryScanner(project);
282 // Check each source file - see if it needs compilation
283 String[] fileNames = scanner.getIncludedFiles();
284 for (int j = 0; j < fileNames.length; j++){
285 String[] cmd = new String[cmdLen];
286 int index = 0;
287 cmd[index++] = this.command;
288
289
290
291 Iterator iter = argsWithoutSpace.iterator();
292 while (iter.hasNext()) {
293 cmd[index++] = (String)iter.next();
294 }
295
296 iter = endargsWithoutSpace.iterator();
297 while (iter.hasNext()) {
298 cmd[index++] = (String)iter.next();
299 }
300
301 /*
302 * Add outputFileFlag and output file to cmd
303 */
304 if (outputFileFlag != null && outputFileFlag.length()> 0){
305 if (outputFileFlag.trim().equalsIgnoreCase("-cr")){
306 cmd[index++] = outputFileFlag;
307 cmd[index++] = userdefine.getOutputFile();
308 }else {
309 cmd[index++] = outputFileFlag + userdefine.getOutputFile();
310 }
311 }
312
313 /*
314 * Add fileList to cmd
315 */
316 if (fileList != null && fileList.size()> 0){
317 for (int s = 0; s < fileList.size(); s++){
318 cmd[index++] = fileList.get(s);
319 }
320 }
321 if (subSystemFlag != null && subSystemFlag.length()> 0){
322 cmd[index++] = subSystemFlag + userdefine.getSubSystemvalue();
323 }
324 if (includeFileFlag != null && includeFileFlag.length()> 0){
325 cmd[index++] = includeFileFlag + userdefine.getIncludeFile();
326 }
327
328 iter = includePath.iterator();
329 while (iter.hasNext()) {
330 cmd[index++] = (String)iter.next();
331 }
332 if (userdefine.getOutdir() != null){
333 // will add code to generate outfile name and flag
334 cmd[index++] = "/nologo";
335 }
336
337 if (entryPointFlag != null && entryPointFlag.length()> 0){
338 cmd[index++] = entryPointFlag + userdefine.getEntryPointvalue();
339 }
340 if (mapFlag != null && mapFlag.length() > 0){
341 cmd[index++] = mapFlag + userdefine.getMapvalue();
342 }
343 if (pdbFlag != null && pdbFlag.length() > 0){
344 cmd[index++] = pdbFlag + userdefine.getPdbvalue();
345 }
346
347 if (libSetList != null && libSetList.size() > 0){
348 for (int k = 0; k < libSetList.size(); k++){
349 cmd[index++] = libSetList.get(k);
350 }
351 }
352
353 // execute the command
354 cmd[index++] = scanner.getBasedir() + "/" + fileNames[j];
355 for (int k = 0; k < cmd.length; k++){
356 }
357 int retval = runCommand(cctask, workdir, cmd);
358 // if with monitor, add more code
359 if (retval != 0) {
360 throw new BuildException(this.command
361 + " failed with return code " + retval,
362 cctask.getLocation());
363 }
364 }
365 }
366 }
367 }
368
369 protected int runCommand(CCTask task, File workingDir, String[] cmdline)
370 throws BuildException {
371 return CUtil.runCommand(task, workingDir, cmdline, false, null);
372
373 }
374
375 protected String getInputFileArgument(File outputDir, String filename,
376 int index) {
377 //
378 // if there is an embedded space,
379 // must enclose in quotes
380 if (filename.indexOf(' ') >= 0) {
381 StringBuffer buf = new StringBuffer("\"");
382 buf.append(filename);
383 buf.append("\"");
384 return buf.toString();
385 }
386 return filename;
387 }
388
389 }