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