3 * Copyright 2001-2004 The Ant-Contrib project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package net
.sf
.antcontrib
.cpptasks
.userdefine
;
20 import java
.util
.Iterator
;
21 import java
.util
.LinkedHashSet
;
23 import java
.util
.StringTokenizer
;
24 import java
.util
.Vector
;
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
;
31 import org
.apache
.tools
.ant
.BuildException
;
32 import org
.apache
.tools
.ant
.DirectoryScanner
;
33 import org
.apache
.tools
.ant
.Project
;
35 public class CommandLineUserDefine
{
37 String includePathDelimiter
= null;
39 String outputDelimiter
= null;
41 public void command(CCTask cctask
, UserDefineDef userdefine
){
42 boolean isGccCommand
= userdefine
.getFamily().equalsIgnoreCase("GCC");
44 Project project
= cctask
.getProject();
45 if(userdefine
.getWorkdir() == null) {
46 workdir
= new File(".");
49 workdir
= userdefine
.getWorkdir();
53 // generate cmdline= command + args + includepath + endargs + outfile
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();
62 // Generate cmdline = command +
64 // outputflag + outputfile
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());
77 endargs
.addElement(argument
[j
].getValue());
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] + "\"");
91 includePath
.addElement( includePathDelimiter
+ incPath
[j
]);
96 // Remove space in args and endargs.
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());
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());
115 // command + args + endargs + includepath + sourcefile
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
;
122 cmdLen
+= 2; // we need -( and -) to group libs for GCC
127 // In gcc the "cr" flag should follow space then add outputfile name, otherwise
128 // it will pop error.
130 if (outputDelimiter
!= null && userdefine
.getOutputFile() != null && outputDelimiter
.trim().length() > 0){
131 if (outputDelimiter
.trim().equalsIgnoreCase("-cr")){
139 // for every source file
140 // if file is header file, just skip it (add later)
142 Vector srcSets
= userdefine
.getSrcSets();
143 // System.out.println("##" + userdefine.getSrcSets());
146 // if have source file append source file in command land.
148 Set allSrcFiles
= new LinkedHashSet();
149 for (int i
= 0; i
< srcSets
.size(); i
++) {
150 ConditionalFileSet srcSet
= (ConditionalFileSet
) srcSets
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
]);
161 System
.out
.println("[" + userdefine
.getType() + "] " + fileNames
[j
]);
167 String
[] fileNames
= (String
[])allSrcFiles
.toArray(new String
[allSrcFiles
.size()]);
168 String
[] cmd
= new String
[cmdLen
- 1 + fileNames
.length
];
170 cmd
[index
++] = userdefine
.getCmd();
172 Iterator iter
= argsWithoutSpace
.iterator();
173 while (iter
.hasNext()) {
174 cmd
[index
++] = project
.replaceProperties((String
)iter
.next());
177 iter
= endargsWithoutSpace
.iterator();
178 while (iter
.hasNext()) {
179 cmd
[index
++] = project
.replaceProperties((String
)iter
.next());
183 // Add outputFileFlag and output file to cmd
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();
190 cmd
[index
++] = outputDelimiter
+ userdefine
.getOutputFile();
194 iter
= includePath
.iterator();
195 while (iter
.hasNext()) {
196 cmd
[index
++] = (String
)iter
.next();
199 if (libSet
!= null && libSet
.length
> 0){
203 for (int k
= 0; k
< libSet
.length
; k
++){
204 cmd
[index
++] = libSet
[k
];
210 for (int j
= 0; j
< fileNames
.length
; j
++){
211 // execute the command
212 cmd
[index
++] = fileNames
[j
];
215 int retval
= runCommand(cctask
, workdir
, cmd
);
216 // if with monitor, add more code
218 throw new BuildException(userdefine
.getCmd()
219 + " failed with return code " + retval
,
220 cctask
.getLocation());
224 protected int runCommand(CCTask task
, File workingDir
, String
[] cmdline
)
225 throws BuildException
{
226 return CUtil
.runCommand(task
, workingDir
, cmdline
, false, null);
230 protected String
getInputFileArgument(File outputDir
, String filename
,
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
);
239 return buf
.toString();