878ddf1f |
1 | /*\r |
2 | * \r |
3 | * Copyright 2001-2004 The Ant-Contrib project\r |
4 | *\r |
5 | * Licensed under the Apache License, Version 2.0 (the "License");\r |
6 | * you may not use this file except in compliance with the License.\r |
7 | * You may obtain a copy of the License at\r |
8 | *\r |
9 | * http://www.apache.org/licenses/LICENSE-2.0\r |
10 | *\r |
11 | * Unless required by applicable law or agreed to in writing, software\r |
12 | * distributed under the License is distributed on an "AS IS" BASIS,\r |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r |
14 | * See the License for the specific language governing permissions and\r |
15 | * limitations under the License.\r |
16 | */\r |
509693cc |
17 | package net.sf.antcontrib.cpptasks.userdefine;\r |
18 | \r |
19 | import java.io.File;\r |
20 | import java.util.Iterator;\r |
21 | import java.util.LinkedHashSet;\r |
22 | import java.util.Set;\r |
23 | import java.util.StringTokenizer;\r |
24 | import java.util.Vector;\r |
25 | \r |
26 | import net.sf.antcontrib.cpptasks.CCTask;\r |
27 | import net.sf.antcontrib.cpptasks.CUtil;\r |
28 | import net.sf.antcontrib.cpptasks.types.CommandLineArgument;\r |
29 | import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;\r |
30 | \r |
31 | import org.apache.tools.ant.BuildException;\r |
32 | import org.apache.tools.ant.DirectoryScanner;\r |
33 | import org.apache.tools.ant.Project;\r |
34 | \r |
878ddf1f |
35 | public class CommandLineUserDefine {\r |
36 | \r |
878ddf1f |
37 | String includePathDelimiter = null;\r |
38 | \r |
509693cc |
39 | String outputDelimiter = null;\r |
40 | \r |
878ddf1f |
41 | public void command(CCTask cctask, UserDefineDef userdefine){\r |
bf0ce309 |
42 | boolean isGccCommand = userdefine.getFamily().equalsIgnoreCase("GCC");\r |
878ddf1f |
43 | File workdir;\r |
878ddf1f |
44 | Project project = cctask.getProject();\r |
45 | if(userdefine.getWorkdir() == null) {\r |
46 | workdir = new File(".");\r |
47 | }\r |
48 | else {\r |
49 | workdir = userdefine.getWorkdir();\r |
50 | } \r |
51 | \r |
509693cc |
52 | //\r |
53 | // generate cmdline= command + args + includepath + endargs + outfile\r |
54 | // \r |
878ddf1f |
55 | Vector args = new Vector();\r |
56 | Vector argsWithoutSpace = new Vector();\r |
57 | Vector endargs = new Vector();\r |
58 | Vector endargsWithoutSpace = new Vector();\r |
59 | Vector includePath = new Vector();\r |
60 | \r |
509693cc |
61 | //\r |
62 | // Generate cmdline = command + \r |
63 | // general args + \r |
64 | // outputflag + outputfile\r |
65 | // includpath + \r |
66 | // endargs + \r |
67 | // \r |
68 | \r |
69 | //\r |
70 | // get Args.\r |
71 | //\r |
878ddf1f |
72 | CommandLineArgument[] argument = userdefine.getActiveProcessorArgs();\r |
73 | for (int j = 0; j < argument.length; j++) {\r |
74 | if (argument[j].getLocation() == 0) {\r |
75 | args.addElement(argument[j].getValue());\r |
76 | } else {\r |
77 | endargs.addElement(argument[j].getValue());\r |
78 | }\r |
79 | }\r |
509693cc |
80 | \r |
81 | //\r |
82 | // get include path.\r |
83 | //\r |
878ddf1f |
84 | String[] incPath = userdefine.getActiveIncludePaths();\r |
85 | for (int j = 0; j < incPath.length; j++) {\r |
86 | if(incPath[j].indexOf(' ') >= 0) {\r |
87 | includePath.addElement( includePathDelimiter + incPath[j]);\r |
88 | //includePath.addElement( includePathDelimiter + "\"" + incPath[j] + "\"");\r |
89 | }\r |
90 | else {\r |
91 | includePath.addElement( includePathDelimiter + incPath[j]);\r |
92 | }\r |
93 | }\r |
509693cc |
94 | \r |
95 | //\r |
96 | // Remove space in args and endargs.\r |
97 | //\r |
878ddf1f |
98 | for ( int i=0; i < args.size(); i++) {\r |
99 | String str = (String)args.get(i);\r |
509693cc |
100 | StringTokenizer st = new StringTokenizer(str, " \t");\r |
878ddf1f |
101 | while(st.hasMoreTokens()) {\r |
102 | argsWithoutSpace.addElement(st.nextToken());\r |
103 | }\r |
104 | }\r |
105 | for ( int i=0; i < endargs.size(); i++) {\r |
106 | String str = (String)endargs.get(i);\r |
509693cc |
107 | StringTokenizer st = new StringTokenizer(str, " \t");\r |
878ddf1f |
108 | while(st.hasMoreTokens()) {\r |
109 | endargsWithoutSpace.addElement(st.nextToken());\r |
110 | }\r |
111 | }\r |
112 | \r |
113 | int cmdLen = 0;\r |
509693cc |
114 | //\r |
115 | // command + args + endargs + includepath + sourcefile\r |
116 | //\r |
117 | cmdLen = 1 + argsWithoutSpace.size() + endargsWithoutSpace.size() + includePath.size() + 1;\r |
118 | String[] libSet = userdefine.get_libset();\r |
119 | if (libSet != null && libSet.length > 0){\r |
120 | cmdLen = cmdLen + libSet.length;\r |
bf0ce309 |
121 | if (isGccCommand) {\r |
122 | cmdLen += 2; // we need -( and -) to group libs for GCC\r |
123 | }\r |
878ddf1f |
124 | }\r |
509693cc |
125 | \r |
126 | //\r |
127 | // In gcc the "cr" flag should follow space then add outputfile name, otherwise\r |
128 | // it will pop error. \r |
129 | // TBD\r |
130 | if (outputDelimiter != null && userdefine.getOutputFile() != null && outputDelimiter.trim().length() > 0){\r |
131 | if (outputDelimiter.trim().equalsIgnoreCase("-cr")){\r |
878ddf1f |
132 | cmdLen = cmdLen + 2;\r |
133 | }else {\r |
134 | cmdLen++;\r |
135 | }\r |
878ddf1f |
136 | }\r |
509693cc |
137 | \r |
138 | //\r |
139 | // for every source file\r |
140 | // if file is header file, just skip it (add later)\r |
141 | //\r |
878ddf1f |
142 | Vector srcSets = userdefine.getSrcSets();\r |
509693cc |
143 | // System.out.println("##" + userdefine.getSrcSets());\r |
878ddf1f |
144 | \r |
145 | //\r |
146 | // if have source file append source file in command land.\r |
147 | //\r |
509693cc |
148 | Set allSrcFiles = new LinkedHashSet();\r |
878ddf1f |
149 | for (int i = 0; i < srcSets.size(); i++) {\r |
150 | ConditionalFileSet srcSet = (ConditionalFileSet) srcSets\r |
151 | .elementAt(i);\r |
152 | if (srcSet.isActive()) {\r |
153 | // Find matching source files\r |
154 | DirectoryScanner scanner = srcSet.getDirectoryScanner(project);\r |
155 | // Check each source file - see if it needs compilation\r |
156 | String[] fileNames = scanner.getIncludedFiles();\r |
157 | for (int j = 0; j < fileNames.length; j++){\r |
878ddf1f |
158 | // execute the command\r |
509693cc |
159 | allSrcFiles.add(scanner.getBasedir() + "/" + fileNames[j]);\r |
9e63db5e |
160 | if (isGccCommand) {\r |
161 | System.out.println("[" + userdefine.getType() + "] " + fileNames[j]);\r |
162 | }\r |
878ddf1f |
163 | }\r |
164 | }\r |
165 | }\r |
509693cc |
166 | \r |
167 | String[] fileNames = (String[])allSrcFiles.toArray(new String[allSrcFiles.size()]);\r |
168 | String[] cmd = new String[cmdLen - 1 + fileNames.length];\r |
169 | int index = 0;\r |
170 | cmd[index++] = userdefine.getCmd();\r |
171 | \r |
172 | Iterator iter = argsWithoutSpace.iterator();\r |
173 | while (iter.hasNext()) {\r |
174 | cmd[index++] = project.replaceProperties((String)iter.next());\r |
175 | }\r |
176 | \r |
177 | iter = endargsWithoutSpace.iterator();\r |
178 | while (iter.hasNext()) {\r |
179 | cmd[index++] = project.replaceProperties((String)iter.next());\r |
180 | }\r |
181 | \r |
182 | //\r |
183 | // Add outputFileFlag and output file to cmd\r |
184 | //\r |
185 | if (outputDelimiter != null && userdefine.getOutputFile() != null && outputDelimiter.length()> 0){\r |
186 | if (outputDelimiter.trim().equalsIgnoreCase("-cr")){\r |
187 | cmd[index++] = outputDelimiter;\r |
188 | cmd[index++] = userdefine.getOutputFile();\r |
189 | }else {\r |
190 | cmd[index++] = outputDelimiter + userdefine.getOutputFile();\r |
191 | }\r |
192 | }\r |
193 | \r |
194 | iter = includePath.iterator();\r |
195 | while (iter.hasNext()) {\r |
196 | cmd[index++] = (String)iter.next();\r |
197 | }\r |
198 | \r |
199 | if (libSet != null && libSet.length > 0){\r |
bf0ce309 |
200 | if (isGccCommand) {\r |
201 | cmd[index++] = "-(";\r |
202 | }\r |
509693cc |
203 | for (int k = 0; k < libSet.length ; k++){\r |
204 | cmd[index++] = libSet[k];\r |
205 | }\r |
bf0ce309 |
206 | if (isGccCommand) {\r |
207 | cmd[index++] = "-)";\r |
208 | }\r |
509693cc |
209 | }\r |
210 | for (int j = 0; j < fileNames.length; j++){\r |
211 | // execute the command\r |
212 | cmd[index++] = fileNames[j];\r |
213 | }\r |
214 | \r |
215 | int retval = runCommand(cctask, workdir, cmd);\r |
216 | // if with monitor, add more code\r |
217 | if (retval != 0) {\r |
218 | throw new BuildException(userdefine.getCmd()\r |
219 | + " failed with return code " + retval,\r |
220 | cctask.getLocation());\r |
221 | }\r |
878ddf1f |
222 | }\r |
223 | \r |
224 | protected int runCommand(CCTask task, File workingDir, String[] cmdline)\r |
225 | throws BuildException {\r |
226 | return CUtil.runCommand(task, workingDir, cmdline, false, null);\r |
227 | \r |
228 | }\r |
229 | \r |
230 | protected String getInputFileArgument(File outputDir, String filename,\r |
231 | int index) {\r |
232 | //\r |
233 | // if there is an embedded space,\r |
234 | // must enclose in quotes\r |
235 | if (filename.indexOf(' ') >= 0) {\r |
236 | StringBuffer buf = new StringBuffer("\"");\r |
237 | buf.append(filename);\r |
238 | buf.append("\"");\r |
239 | return buf.toString();\r |
240 | }\r |
241 | return filename;\r |
242 | }\r |
243 | \r |
244 | }\r |