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