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
){
43 Project project
= cctask
.getProject();
44 if(userdefine
.getWorkdir() == null) {
45 workdir
= new File(".");
48 workdir
= userdefine
.getWorkdir();
52 // generate cmdline= command + args + includepath + endargs + outfile
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();
61 // Generate cmdline = command +
63 // outputflag + outputfile
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());
76 endargs
.addElement(argument
[j
].getValue());
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] + "\"");
90 includePath
.addElement( includePathDelimiter
+ incPath
[j
]);
95 // Remove space in args and endargs.
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());
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());
114 // command + args + endargs + includepath + sourcefile
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
;
123 // In gcc the "cr" flag should follow space then add outputfile name, otherwise
124 // it will pop error.
126 if (outputDelimiter
!= null && userdefine
.getOutputFile() != null && outputDelimiter
.trim().length() > 0){
127 if (outputDelimiter
.trim().equalsIgnoreCase("-cr")){
135 // for every source file
136 // if file is header file, just skip it (add later)
138 Vector srcSets
= userdefine
.getSrcSets();
139 // System.out.println("##" + userdefine.getSrcSets());
142 // if have source file append source file in command land.
144 Set allSrcFiles
= new LinkedHashSet();
145 for (int i
= 0; i
< srcSets
.size(); i
++) {
146 ConditionalFileSet srcSet
= (ConditionalFileSet
) srcSets
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
]);
160 String
[] fileNames
= (String
[])allSrcFiles
.toArray(new String
[allSrcFiles
.size()]);
161 String
[] cmd
= new String
[cmdLen
- 1 + fileNames
.length
];
163 cmd
[index
++] = userdefine
.getCmd();
165 Iterator iter
= argsWithoutSpace
.iterator();
166 while (iter
.hasNext()) {
167 cmd
[index
++] = project
.replaceProperties((String
)iter
.next());
170 iter
= endargsWithoutSpace
.iterator();
171 while (iter
.hasNext()) {
172 cmd
[index
++] = project
.replaceProperties((String
)iter
.next());
176 // Add outputFileFlag and output file to cmd
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();
183 cmd
[index
++] = outputDelimiter
+ userdefine
.getOutputFile();
187 iter
= includePath
.iterator();
188 while (iter
.hasNext()) {
189 cmd
[index
++] = (String
)iter
.next();
192 if (libSet
!= null && libSet
.length
> 0){
193 for (int k
= 0; k
< libSet
.length
; k
++){
194 cmd
[index
++] = libSet
[k
];
197 for (int j
= 0; j
< fileNames
.length
; j
++){
198 // execute the command
199 cmd
[index
++] = fileNames
[j
];
202 int retval
= runCommand(cctask
, workdir
, cmd
);
203 // if with monitor, add more code
205 throw new BuildException(userdefine
.getCmd()
206 + " failed with return code " + retval
,
207 cctask
.getLocation());
211 protected int runCommand(CCTask task
, File workingDir
, String
[] cmdline
)
212 throws BuildException
{
213 return CUtil
.runCommand(task
, workingDir
, cmdline
, false, null);
217 protected String
getInputFileArgument(File outputDir
, String filename
,
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
);
226 return buf
.toString();