1 package net
.sf
.antcontrib
.cpptasks
.userdefine
;
4 import java
.util
.Iterator
;
5 import java
.util
.StringTokenizer
;
6 import java
.util
.Vector
;
8 import net
.sf
.antcontrib
.cpptasks
.CCTask
;
9 import net
.sf
.antcontrib
.cpptasks
.CUtil
;
10 import net
.sf
.antcontrib
.cpptasks
.types
.CommandLineArgument
;
11 import net
.sf
.antcontrib
.cpptasks
.types
.ConditionalFileSet
;
12 import net
.sf
.antcontrib
.cpptasks
.types
.LibrarySet
;
14 import org
.apache
.tools
.ant
.BuildException
;
15 import org
.apache
.tools
.ant
.DirectoryScanner
;
16 import org
.apache
.tools
.ant
.Project
;
19 * Copyright 2001-2004 The Ant-Contrib project
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
25 * http://www.apache.org/licenses/LICENSE-2.0
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
33 public class CommandLineUserDefine
{
38 * The follows variable set at child class.
40 String includeFileFlag
= null;
41 String entryPointFlag
= null;
42 String subSystemFlag
= null;
43 String mapFlag
= null;
44 String pdbFlag
= null;
45 String outputFileFlag
= null;
46 String includePathDelimiter
= null;
49 * get lib string if Vendor = "gcc", it should respectively aadd "-(" and ")-"
50 * at library set before and end. This value set at userDefineCompiler class.
52 Vector
<String
> libSetList
= new Vector
<String
>();
53 Vector
<String
> fileList
= new Vector
<String
>();
54 public void command(CCTask cctask
, UserDefineDef userdefine
){
57 Project project
= cctask
.getProject();
58 if(userdefine
.getWorkdir() == null) {
59 workdir
= new File(".");
62 workdir
= userdefine
.getWorkdir();
66 * generate cmdline= command + args + includepath + endargs + outfile
68 Vector args
= new Vector();
69 Vector argsWithoutSpace
= new Vector();
70 Vector endargs
= new Vector();
71 Vector endargsWithoutSpace
= new Vector();
72 Vector includePath
= new Vector();
75 * Generate cmdline = command +
77 * outputflag + outputfile
78 * subsystemFlag + subsystemValue +
79 * includeFlag + includeFile +
80 * includeFileincludpath +
81 * entryPointFlag + entryPointValue +
82 * mapFlag + mapValue +
83 * pdbFlag + pdbValue +
91 CommandLineArgument
[] argument
= userdefine
.getActiveProcessorArgs();
92 for (int j
= 0; j
< argument
.length
; j
++) {
93 if (argument
[j
].getLocation() == 0) {
94 args
.addElement(argument
[j
].getValue());
96 endargs
.addElement(argument
[j
].getValue());
102 String
[] incPath
= userdefine
.getActiveIncludePaths();
103 for (int j
= 0; j
< incPath
.length
; j
++) {
104 if(incPath
[j
].indexOf(' ') >= 0) {
105 includePath
.addElement( includePathDelimiter
+ incPath
[j
]);
106 //includePath.addElement( includePathDelimiter + "\"" + incPath[j] + "\"");
109 includePath
.addElement( includePathDelimiter
+ incPath
[j
]);
113 * Remove space in args and endargs.
115 for ( int i
=0; i
< args
.size(); i
++) {
116 String str
= (String
)args
.get(i
);
117 StringTokenizer st
= new StringTokenizer(str
);
118 while(st
.hasMoreTokens()) {
119 argsWithoutSpace
.addElement(st
.nextToken());
122 for ( int i
=0; i
< endargs
.size(); i
++) {
123 String str
= (String
)endargs
.get(i
);
124 StringTokenizer st
= new StringTokenizer(str
);
125 while(st
.hasMoreTokens()) {
126 endargsWithoutSpace
.addElement(st
.nextToken());
131 if(userdefine
.getOutdir() == null) {
132 outdir
= new File(".");
134 * command + args + endargs + includepath + sourcefile
136 cmdLen
= 1 + argsWithoutSpace
.size() + endargsWithoutSpace
.size() + includePath
.size() + 1;
139 outdir
= userdefine
.getOutdir();
141 * command + args + endargs + includepath + sourcefile + outfile
143 cmdLen
= 1 + argsWithoutSpace
.size() + endargsWithoutSpace
.size() + includePath
.size() + 2;
145 if (includeFileFlag
!= null && includeFileFlag
.trim().length() > 0){
148 if (entryPointFlag
!= null && entryPointFlag
.trim().length() > 0){
151 if (subSystemFlag
!= null && subSystemFlag
.trim().length() > 0){
154 if (mapFlag
!= null && mapFlag
.trim().length() > 0){
157 if (pdbFlag
!= null && pdbFlag
.trim().length() > 0){
160 if (libSetList
!= null && libSetList
.size() > 0){
161 cmdLen
= cmdLen
+ libSetList
.size();
163 if (fileList
!= null){
164 cmdLen
= cmdLen
+ fileList
.size();
167 * In gcc the "cr" flag should follow space then add outputfile name, otherwise
170 if (outputFileFlag
!= null && outputFileFlag
.trim().length() > 0){
171 if (outputFileFlag
.trim().equalsIgnoreCase("-cr")){
179 * for every source file
180 * if file is header file, just skip it (add later)
182 Vector srcSets
= userdefine
.getSrcSets();
183 if (srcSets
.size() == 0) {
184 String
[] cmd
= new String
[cmdLen
- 1];
186 cmd
[index
++] = this.command
;
190 Iterator iter
= argsWithoutSpace
.iterator();
191 while (iter
.hasNext()) {
192 cmd
[index
++] = project
.replaceProperties((String
)iter
.next());
193 //cmd[index++] = (String)iter.next();
196 iter
= endargsWithoutSpace
.iterator();
197 while (iter
.hasNext()) {
198 cmd
[index
++] = (String
)iter
.next();
202 * "OutputFlag + outputFile" as first option follow command.exe.
204 if (outputFileFlag
!= null && outputFileFlag
.trim().length() > 0){
205 if (outputFileFlag
.trim().equalsIgnoreCase("-cr")){
206 cmd
[index
++] = outputFileFlag
;
207 cmd
[index
++] = userdefine
.getOutputFile();
209 cmd
[index
++] = outputFileFlag
+ userdefine
.getOutputFile();
214 * Add fileList to cmd
216 if (fileList
!= null && fileList
.size()> 0){
217 for (int i
= 0; i
< fileList
.size(); i
++){
218 cmd
[index
++] = fileList
.get(i
);
222 if (subSystemFlag
!= null && subSystemFlag
.trim().length() > 0){
223 cmd
[index
++] = subSystemFlag
+ userdefine
.getSubSystemvalue();
225 if (includeFileFlag
!= null && includeFileFlag
.trim().length() > 0){
226 cmd
[index
++] = includeFileFlag
+ userdefine
.getIncludeFile();
229 iter
= includePath
.iterator();
230 while (iter
.hasNext()) {
231 cmd
[index
++] = (String
)iter
.next();
234 if (entryPointFlag
!= null && entryPointFlag
.trim().length() > 0){
236 // If GCC link use __ModuleEntrypoint instead of _ModuleEntryPoint;
238 if (entryPointFlag
.equalsIgnoreCase("-e")){
239 cmd
[index
++] = entryPointFlag
+ "_" + userdefine
.getEntryPointvalue();
241 cmd
[index
++] = entryPointFlag
+ userdefine
.getEntryPointvalue();
245 if (mapFlag
!= null && mapFlag
.trim().length() > 0){
246 cmd
[index
++] = mapFlag
+ userdefine
.getMapvalue();
248 if (pdbFlag
!= null && pdbFlag
.trim().length() > 0){
249 cmd
[index
++] = pdbFlag
+ userdefine
.getPdbvalue();
252 if (userdefine
.getOutdir() != null){
253 // will add code to generate outfile name and flag
254 cmd
[index
++] = "/nologo";
257 if (libSetList
!= null && libSetList
.size() > 0){
258 for (int i
= 0; i
< libSetList
.size(); i
++){
259 cmd
[index
++] = libSetList
.get(i
);
263 // execute the command
264 int retval
= runCommand(cctask
, workdir
, cmd
);
265 // if with monitor, add more code
267 throw new BuildException(this.command
268 + " failed with return code " + retval
,
269 cctask
.getLocation());
274 // if have source file append source file in command land.
276 for (int i
= 0; i
< srcSets
.size(); i
++) {
277 ConditionalFileSet srcSet
= (ConditionalFileSet
) srcSets
279 if (srcSet
.isActive()) {
280 // Find matching source files
281 DirectoryScanner scanner
= srcSet
.getDirectoryScanner(project
);
282 // Check each source file - see if it needs compilation
283 String
[] fileNames
= scanner
.getIncludedFiles();
284 for (int j
= 0; j
< fileNames
.length
; j
++){
285 String
[] cmd
= new String
[cmdLen
];
287 cmd
[index
++] = this.command
;
291 Iterator iter
= argsWithoutSpace
.iterator();
292 while (iter
.hasNext()) {
293 cmd
[index
++] = (String
)iter
.next();
296 iter
= endargsWithoutSpace
.iterator();
297 while (iter
.hasNext()) {
298 cmd
[index
++] = (String
)iter
.next();
302 * Add outputFileFlag and output file to cmd
304 if (outputFileFlag
!= null && outputFileFlag
.length()> 0){
305 if (outputFileFlag
.trim().equalsIgnoreCase("-cr")){
306 cmd
[index
++] = outputFileFlag
;
307 cmd
[index
++] = userdefine
.getOutputFile();
309 cmd
[index
++] = outputFileFlag
+ userdefine
.getOutputFile();
314 * Add fileList to cmd
316 if (fileList
!= null && fileList
.size()> 0){
317 for (int s
= 0; s
< fileList
.size(); s
++){
318 cmd
[index
++] = fileList
.get(s
);
321 if (subSystemFlag
!= null && subSystemFlag
.length()> 0){
322 cmd
[index
++] = subSystemFlag
+ userdefine
.getSubSystemvalue();
324 if (includeFileFlag
!= null && includeFileFlag
.length()> 0){
325 cmd
[index
++] = includeFileFlag
+ userdefine
.getIncludeFile();
328 iter
= includePath
.iterator();
329 while (iter
.hasNext()) {
330 cmd
[index
++] = (String
)iter
.next();
332 if (userdefine
.getOutdir() != null){
333 // will add code to generate outfile name and flag
334 cmd
[index
++] = "/nologo";
337 if (entryPointFlag
!= null && entryPointFlag
.length()> 0){
338 cmd
[index
++] = entryPointFlag
+ userdefine
.getEntryPointvalue();
340 if (mapFlag
!= null && mapFlag
.length() > 0){
341 cmd
[index
++] = mapFlag
+ userdefine
.getMapvalue();
343 if (pdbFlag
!= null && pdbFlag
.length() > 0){
344 cmd
[index
++] = pdbFlag
+ userdefine
.getPdbvalue();
347 if (libSetList
!= null && libSetList
.size() > 0){
348 for (int k
= 0; k
< libSetList
.size(); k
++){
349 cmd
[index
++] = libSetList
.get(k
);
353 // execute the command
354 cmd
[index
++] = scanner
.getBasedir() + "/" + fileNames
[j
];
355 for (int k
= 0; k
< cmd
.length
; k
++){
357 int retval
= runCommand(cctask
, workdir
, cmd
);
358 // if with monitor, add more code
360 throw new BuildException(this.command
361 + " failed with return code " + retval
,
362 cctask
.getLocation());
369 protected int runCommand(CCTask task
, File workingDir
, String
[] cmdline
)
370 throws BuildException
{
371 return CUtil
.runCommand(task
, workingDir
, cmdline
, false, null);
375 protected String
getInputFileArgument(File outputDir
, String filename
,
378 // if there is an embedded space,
379 // must enclose in quotes
380 if (filename
.indexOf(' ') >= 0) {
381 StringBuffer buf
= new StringBuffer("\"");
382 buf
.append(filename
);
384 return buf
.toString();