1e4c0ce2b7e34ad34fc5209bb9cc6b5e54af3964
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / userdefine / CommandLineUserDefine.java
1 /*
2 *
3 * Copyright 2001-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.userdefine;
18
19 import java.io.File;
20 import java.util.Iterator;
21 import java.util.LinkedHashSet;
22 import java.util.Set;
23 import java.util.StringTokenizer;
24 import java.util.Vector;
25
26 import net.sf.antcontrib.cpptasks.CCTask;
27 import net.sf.antcontrib.cpptasks.CUtil;
28 import net.sf.antcontrib.cpptasks.types.CommandLineArgument;
29 import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.DirectoryScanner;
33 import org.apache.tools.ant.Project;
34
35 public class CommandLineUserDefine {
36
37 String includePathDelimiter = null;
38
39 String outputDelimiter = null;
40
41 public void command(CCTask cctask, UserDefineDef userdefine){
42 boolean isGccCommand = userdefine.getFamily().equalsIgnoreCase("GCC");
43 File workdir;
44 Project project = cctask.getProject();
45 if(userdefine.getWorkdir() == null) {
46 workdir = new File(".");
47 }
48 else {
49 workdir = userdefine.getWorkdir();
50 }
51
52 //
53 // generate cmdline= command + args + includepath + endargs + outfile
54 //
55 Vector args = new Vector();
56 Vector argsWithoutSpace = new Vector();
57 Vector endargs = new Vector();
58 Vector endargsWithoutSpace = new Vector();
59 Vector includePath = new Vector();
60
61 //
62 // Generate cmdline = command +
63 // general args +
64 // outputflag + outputfile
65 // includpath +
66 // endargs +
67 //
68
69 //
70 // get Args.
71 //
72 CommandLineArgument[] argument = userdefine.getActiveProcessorArgs();
73 for (int j = 0; j < argument.length; j++) {
74 if (argument[j].getLocation() == 0) {
75 args.addElement(argument[j].getValue());
76 } else {
77 endargs.addElement(argument[j].getValue());
78 }
79 }
80
81 //
82 // get include path.
83 //
84 String[] incPath = userdefine.getActiveIncludePaths();
85 for (int j = 0; j < incPath.length; j++) {
86 if(incPath[j].indexOf(' ') >= 0) {
87 includePath.addElement( includePathDelimiter + incPath[j]);
88 //includePath.addElement( includePathDelimiter + "\"" + incPath[j] + "\"");
89 }
90 else {
91 includePath.addElement( includePathDelimiter + incPath[j]);
92 }
93 }
94
95 //
96 // Remove space in args and endargs.
97 //
98 for ( int i=0; i < args.size(); i++) {
99 String str = (String)args.get(i);
100 StringTokenizer st = new StringTokenizer(str, " \t");
101 while(st.hasMoreTokens()) {
102 argsWithoutSpace.addElement(st.nextToken());
103 }
104 }
105 for ( int i=0; i < endargs.size(); i++) {
106 String str = (String)endargs.get(i);
107 StringTokenizer st = new StringTokenizer(str, " \t");
108 while(st.hasMoreTokens()) {
109 endargsWithoutSpace.addElement(st.nextToken());
110 }
111 }
112
113 int cmdLen = 0;
114 //
115 // command + args + endargs + includepath + sourcefile
116 //
117 cmdLen = 1 + argsWithoutSpace.size() + endargsWithoutSpace.size() + includePath.size() + 1;
118 String[] libSet = userdefine.get_libset();
119 if (libSet != null && libSet.length > 0){
120 cmdLen = cmdLen + libSet.length;
121 if (isGccCommand) {
122 cmdLen += 2; // we need -( and -) to group libs for GCC
123 }
124 }
125
126 //
127 // In gcc the "cr" flag should follow space then add outputfile name, otherwise
128 // it will pop error.
129 // TBD
130 if (outputDelimiter != null && userdefine.getOutputFile() != null && outputDelimiter.trim().length() > 0){
131 if (outputDelimiter.trim().equalsIgnoreCase("-cr")){
132 cmdLen = cmdLen + 2;
133 }else {
134 cmdLen++;
135 }
136 }
137
138 //
139 // for every source file
140 // if file is header file, just skip it (add later)
141 //
142 Vector srcSets = userdefine.getSrcSets();
143 // System.out.println("##" + userdefine.getSrcSets());
144
145 //
146 // if have source file append source file in command land.
147 //
148 Set allSrcFiles = new LinkedHashSet();
149 for (int i = 0; i < srcSets.size(); i++) {
150 ConditionalFileSet srcSet = (ConditionalFileSet) srcSets
151 .elementAt(i);
152 if (srcSet.isActive()) {
153 // Find matching source files
154 DirectoryScanner scanner = srcSet.getDirectoryScanner(project);
155 // Check each source file - see if it needs compilation
156 String[] fileNames = scanner.getIncludedFiles();
157 for (int j = 0; j < fileNames.length; j++){
158 // execute the command
159 allSrcFiles.add(scanner.getBasedir() + "/" + fileNames[j]);
160 }
161 }
162 }
163
164 String[] fileNames = (String[])allSrcFiles.toArray(new String[allSrcFiles.size()]);
165 String[] cmd = new String[cmdLen - 1 + fileNames.length];
166 int index = 0;
167 cmd[index++] = userdefine.getCmd();
168
169 Iterator iter = argsWithoutSpace.iterator();
170 while (iter.hasNext()) {
171 cmd[index++] = project.replaceProperties((String)iter.next());
172 }
173
174 iter = endargsWithoutSpace.iterator();
175 while (iter.hasNext()) {
176 cmd[index++] = project.replaceProperties((String)iter.next());
177 }
178
179 //
180 // Add outputFileFlag and output file to cmd
181 //
182 if (outputDelimiter != null && userdefine.getOutputFile() != null && outputDelimiter.length()> 0){
183 if (outputDelimiter.trim().equalsIgnoreCase("-cr")){
184 cmd[index++] = outputDelimiter;
185 cmd[index++] = userdefine.getOutputFile();
186 }else {
187 cmd[index++] = outputDelimiter + userdefine.getOutputFile();
188 }
189 }
190
191 iter = includePath.iterator();
192 while (iter.hasNext()) {
193 cmd[index++] = (String)iter.next();
194 }
195
196 if (libSet != null && libSet.length > 0){
197 if (isGccCommand) {
198 cmd[index++] = "-(";
199 }
200 for (int k = 0; k < libSet.length ; k++){
201 cmd[index++] = libSet[k];
202 }
203 if (isGccCommand) {
204 cmd[index++] = "-)";
205 }
206 }
207 for (int j = 0; j < fileNames.length; j++){
208 // execute the command
209 cmd[index++] = fileNames[j];
210 }
211
212 int retval = runCommand(cctask, workdir, cmd);
213 // if with monitor, add more code
214 if (retval != 0) {
215 throw new BuildException(userdefine.getCmd()
216 + " failed with return code " + retval,
217 cctask.getLocation());
218 }
219 }
220
221 protected int runCommand(CCTask task, File workingDir, String[] cmdline)
222 throws BuildException {
223 return CUtil.runCommand(task, workingDir, cmdline, false, null);
224
225 }
226
227 protected String getInputFileArgument(File outputDir, String filename,
228 int index) {
229 //
230 // if there is an embedded space,
231 // must enclose in quotes
232 if (filename.indexOf(' ') >= 0) {
233 StringBuffer buf = new StringBuffer("\"");
234 buf.append(filename);
235 buf.append("\"");
236 return buf.toString();
237 }
238 return filename;
239 }
240
241 }