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