Added code to print out file name in GCC building because GCC doesn't output its...
[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 if (isGccCommand) {
161 System.out.println("[" + userdefine.getType() + "] " + fileNames[j]);
162 }
163 }
164 }
165 }
166
167 String[] fileNames = (String[])allSrcFiles.toArray(new String[allSrcFiles.size()]);
168 String[] cmd = new String[cmdLen - 1 + fileNames.length];
169 int index = 0;
170 cmd[index++] = userdefine.getCmd();
171
172 Iterator iter = argsWithoutSpace.iterator();
173 while (iter.hasNext()) {
174 cmd[index++] = project.replaceProperties((String)iter.next());
175 }
176
177 iter = endargsWithoutSpace.iterator();
178 while (iter.hasNext()) {
179 cmd[index++] = project.replaceProperties((String)iter.next());
180 }
181
182 //
183 // Add outputFileFlag and output file to cmd
184 //
185 if (outputDelimiter != null && userdefine.getOutputFile() != null && outputDelimiter.length()> 0){
186 if (outputDelimiter.trim().equalsIgnoreCase("-cr")){
187 cmd[index++] = outputDelimiter;
188 cmd[index++] = userdefine.getOutputFile();
189 }else {
190 cmd[index++] = outputDelimiter + userdefine.getOutputFile();
191 }
192 }
193
194 iter = includePath.iterator();
195 while (iter.hasNext()) {
196 cmd[index++] = (String)iter.next();
197 }
198
199 if (libSet != null && libSet.length > 0){
200 if (isGccCommand) {
201 cmd[index++] = "-(";
202 }
203 for (int k = 0; k < libSet.length ; k++){
204 cmd[index++] = libSet[k];
205 }
206 if (isGccCommand) {
207 cmd[index++] = "-)";
208 }
209 }
210 for (int j = 0; j < fileNames.length; j++){
211 // execute the command
212 cmd[index++] = fileNames[j];
213 }
214
215 int retval = runCommand(cctask, workdir, cmd);
216 // if with monitor, add more code
217 if (retval != 0) {
218 throw new BuildException(userdefine.getCmd()
219 + " failed with return code " + retval,
220 cctask.getLocation());
221 }
222 }
223
224 protected int runCommand(CCTask task, File workingDir, String[] cmdline)
225 throws BuildException {
226 return CUtil.runCommand(task, workingDir, cmdline, false, null);
227
228 }
229
230 protected String getInputFileArgument(File outputDir, String filename,
231 int index) {
232 //
233 // if there is an embedded space,
234 // must enclose in quotes
235 if (filename.indexOf(' ') >= 0) {
236 StringBuffer buf = new StringBuffer("\"");
237 buf.append(filename);
238 buf.append("\"");
239 return buf.toString();
240 }
241 return filename;
242 }
243
244 }