]>
Commit | Line | Data |
---|---|---|
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 | |
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.Map;\r | |
23 | import java.util.Set;\r | |
24 | import java.util.StringTokenizer;\r | |
25 | import java.util.Vector;\r | |
26 | \r | |
27 | import net.sf.antcontrib.cpptasks.CCTask;\r | |
28 | import net.sf.antcontrib.cpptasks.CUtil;\r | |
29 | import net.sf.antcontrib.cpptasks.types.CommandLineArgument;\r | |
30 | import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;\r | |
31 | \r | |
32 | import org.apache.tools.ant.BuildException;\r | |
33 | import org.apache.tools.ant.DirectoryScanner;\r | |
34 | import org.apache.tools.ant.Project;\r | |
35 | import org.apache.tools.ant.types.Environment;\r | |
36 | import org.apache.tools.ant.types.Path;\r | |
37 | import org.apache.tools.ant.types.Environment.Variable;\r | |
38 | \r | |
39 | /**\r | |
40 | * \r | |
41 | */\r | |
42 | public class CommandLineUserDefine {\r | |
43 | \r | |
44 | String includePathDelimiter = null;\r | |
45 | \r | |
46 | String outputDelimiter = null;\r | |
47 | \r | |
48 | private static String pathName = null;\r | |
49 | \r | |
50 | public void command(CCTask cctask, UserDefineDef userdefine) {\r | |
51 | boolean isGccCommand = userdefine.getFamily().equalsIgnoreCase("GCC");\r | |
52 | File workdir;\r | |
53 | Project project = cctask.getProject();\r | |
54 | if (userdefine.getWorkdir() == null) {\r | |
55 | workdir = new File(".");\r | |
56 | } else {\r | |
57 | workdir = userdefine.getWorkdir();\r | |
58 | }\r | |
59 | \r | |
60 | //\r | |
61 | // generate cmdline= command + args + includepath + endargs + outfile\r | |
62 | // \r | |
63 | Vector args = new Vector();\r | |
64 | Vector argsWithoutSpace = new Vector();\r | |
65 | Vector endargs = new Vector();\r | |
66 | Vector endargsWithoutSpace = new Vector();\r | |
67 | Vector includePath = new Vector();\r | |
68 | \r | |
69 | //\r | |
70 | // get Args.\r | |
71 | //\r | |
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 | |
80 | \r | |
81 | //\r | |
82 | // get include path.\r | |
83 | //\r | |
84 | String[] incPath = userdefine.getActiveIncludePaths();\r | |
85 | for (int j = 0; j < incPath.length; j++) {\r | |
86 | includePath.addElement(includePathDelimiter + incPath[j]);\r | |
87 | }\r | |
88 | \r | |
89 | //\r | |
90 | // Remove space in args and endargs.\r | |
91 | //\r | |
92 | for (int i = 0; i < args.size(); i++) {\r | |
93 | String str = (String) args.get(i);\r | |
94 | StringTokenizer st = new StringTokenizer(str, " \t");\r | |
95 | while (st.hasMoreTokens()) {\r | |
96 | argsWithoutSpace.addElement(st.nextToken());\r | |
97 | }\r | |
98 | }\r | |
99 | for (int i = 0; i < endargs.size(); i++) {\r | |
100 | String str = (String) endargs.get(i);\r | |
101 | StringTokenizer st = new StringTokenizer(str, " \t");\r | |
102 | while (st.hasMoreTokens()) {\r | |
103 | endargsWithoutSpace.addElement(st.nextToken());\r | |
104 | }\r | |
105 | }\r | |
106 | \r | |
107 | int cmdLen = 0;\r | |
108 | //\r | |
109 | // command + args + endargs + includepath + sourcefile\r | |
110 | //\r | |
111 | cmdLen = 1 + argsWithoutSpace.size() + endargsWithoutSpace.size()\r | |
112 | + includePath.size() + 1;\r | |
113 | String[] libSet = userdefine.getLibset();\r | |
114 | if (libSet != null && libSet.length > 0) {\r | |
115 | cmdLen = cmdLen + libSet.length;\r | |
116 | if (isGccCommand) {\r | |
117 | cmdLen += 2; // we need -( and -) to group libs for GCC\r | |
118 | }\r | |
119 | }\r | |
120 | \r | |
121 | //\r | |
122 | // In gcc the "cr" flag should follow space then add outputfile name,\r | |
123 | // otherwise\r | |
124 | // it will pop error.\r | |
125 | // TBD\r | |
126 | if (outputDelimiter != null && userdefine.getOutputFile() != null\r | |
127 | && outputDelimiter.trim().length() > 0) {\r | |
128 | if (outputDelimiter.trim().equalsIgnoreCase("-cr")) {\r | |
129 | cmdLen = cmdLen + 2;\r | |
130 | } else {\r | |
131 | cmdLen++;\r | |
132 | }\r | |
133 | }\r | |
134 | \r | |
135 | //\r | |
136 | // for every source file\r | |
137 | // if file is header file, just skip it (add later)\r | |
138 | //\r | |
139 | Vector srcSets = userdefine.getSrcSets();\r | |
140 | \r | |
141 | //\r | |
142 | // if have source file append source file in command line.\r | |
143 | //\r | |
144 | Set allSrcFiles = new LinkedHashSet();\r | |
145 | \r | |
146 | for (int i = 0; i < srcSets.size(); i++) {\r | |
147 | ConditionalFileSet srcSet = (ConditionalFileSet) srcSets\r | |
148 | .elementAt(i);\r | |
149 | if (srcSet.isActive()) {\r | |
150 | //\r | |
151 | // Find matching source files\r | |
152 | //\r | |
153 | DirectoryScanner scanner = srcSet.getDirectoryScanner(project);\r | |
154 | //\r | |
155 | // Check each source file - see if it needs compilation\r | |
156 | //\r | |
157 | String[] fileNames = scanner.getIncludedFiles();\r | |
158 | for (int j = 0; j < fileNames.length; j++) {\r | |
159 | allSrcFiles.add(scanner.getBasedir() + "/" + fileNames[j]);\r | |
160 | if (isGccCommand) {\r | |
161 | System.out.println("[" + userdefine.getType() + "] "\r | |
162 | + fileNames[j]);\r | |
163 | }\r | |
164 | }\r | |
165 | }\r | |
166 | }\r | |
167 | \r | |
168 | String[] fileNames = (String[]) allSrcFiles\r | |
169 | .toArray(new String[allSrcFiles.size()]);\r | |
170 | String[] cmd = new String[cmdLen - 1 + fileNames.length];\r | |
171 | int index = 0;\r | |
172 | cmd[index++] = userdefine.getCmd();\r | |
173 | \r | |
174 | Iterator iter = argsWithoutSpace.iterator();\r | |
175 | while (iter.hasNext()) {\r | |
176 | cmd[index++] = project.replaceProperties((String) iter.next());\r | |
177 | }\r | |
178 | \r | |
179 | iter = endargsWithoutSpace.iterator();\r | |
180 | while (iter.hasNext()) {\r | |
181 | cmd[index++] = project.replaceProperties((String) iter.next());\r | |
182 | }\r | |
183 | \r | |
184 | //\r | |
185 | // Add outputFileFlag and output file to cmd\r | |
186 | //\r | |
187 | if (outputDelimiter != null && userdefine.getOutputFile() != null\r | |
188 | && outputDelimiter.length() > 0) {\r | |
189 | if (outputDelimiter.trim().equalsIgnoreCase("-cr")) {\r | |
190 | cmd[index++] = outputDelimiter;\r | |
191 | cmd[index++] = userdefine.getOutputFile();\r | |
192 | } else {\r | |
193 | cmd[index++] = outputDelimiter + userdefine.getOutputFile();\r | |
194 | }\r | |
195 | }\r | |
196 | \r | |
197 | iter = includePath.iterator();\r | |
198 | while (iter.hasNext()) {\r | |
199 | cmd[index++] = (String) iter.next();\r | |
200 | }\r | |
201 | \r | |
202 | if (libSet != null && libSet.length > 0) {\r | |
203 | if (isGccCommand) {\r | |
204 | cmd[index++] = "-(";\r | |
205 | }\r | |
206 | for (int k = 0; k < libSet.length; k++) {\r | |
207 | cmd[index++] = libSet[k];\r | |
208 | }\r | |
209 | if (isGccCommand) {\r | |
210 | cmd[index++] = "-)";\r | |
211 | }\r | |
212 | }\r | |
213 | for (int j = 0; j < fileNames.length; j++) {\r | |
214 | cmd[index++] = fileNames[j];\r | |
215 | }\r | |
216 | \r | |
217 | // StringBuffer logLine = new StringBuffer();\r | |
218 | // for(int i = 0; i < cmd.length; i++) {\r | |
219 | // logLine.append(cmd[i] + " ");\r | |
220 | // }\r | |
221 | // project.log(logLine.toString(), Project.MSG_VERBOSE);\r | |
222 | \r | |
223 | Environment newEnv = new Environment();\r | |
224 | \r | |
225 | if (userdefine.getDpath() != null && userdefine.getDpath().trim().length() != 0) {\r | |
226 | String existPath = System.getenv(getPathName("PATH"));\r | |
227 | \r | |
228 | Variable var = new Variable();\r | |
229 | var.setKey(getPathName("PATH"));\r | |
230 | var.setPath(new Path(project, userdefine.getDpath() + ";" + existPath));\r | |
231 | newEnv.addVariable(var);\r | |
232 | }\r | |
233 | \r | |
234 | if (userdefine.getLibpath() != null && userdefine.getLibpath().trim().length() != 0) {\r | |
235 | String existPath = System.getenv(getPathName("LIB"));\r | |
236 | \r | |
237 | Variable var = new Variable();\r | |
238 | var.setKey(getPathName("LIB"));\r | |
239 | var.setPath(new Path(project, userdefine.getLibpath() + ";" + existPath));\r | |
240 | newEnv.addVariable(var);\r | |
241 | }\r | |
242 | \r | |
243 | if (userdefine.getInclude() != null && userdefine.getInclude().trim().length() != 0) {\r | |
244 | String existPath = System.getenv(getPathName("INCLUDE"));\r | |
245 | \r | |
246 | Variable var = new Variable();\r | |
247 | var.setKey(getPathName("INCLUDE"));\r | |
248 | var.setPath(new Path(project, userdefine.getInclude() + ";" + existPath));\r | |
249 | newEnv.addVariable(var);\r | |
250 | }\r | |
251 | \r | |
252 | int retval = runCommand(cctask, workdir, cmd, newEnv);\r | |
253 | \r | |
254 | if (retval != 0) {\r | |
255 | throw new BuildException(userdefine.getCmd()\r | |
256 | + " failed with return code " + retval, cctask\r | |
257 | .getLocation());\r | |
258 | }\r | |
259 | }\r | |
260 | \r | |
261 | private String getPathName(String variableName) {\r | |
262 | if (pathName != null) {\r | |
263 | return pathName;\r | |
264 | }\r | |
265 | Map allEnv = System.getenv();\r | |
266 | Iterator iter = allEnv.keySet().iterator();\r | |
267 | while (iter.hasNext()) {\r | |
268 | String key = (String)iter.next();\r | |
269 | if(key.equalsIgnoreCase(variableName)) {\r | |
270 | pathName = key;\r | |
271 | break ;\r | |
272 | }\r | |
273 | }\r | |
274 | return pathName;\r | |
275 | }\r | |
276 | \r | |
277 | protected int runCommand(CCTask task, File workingDir, String[] cmdline, Environment env)\r | |
278 | throws BuildException {\r | |
279 | //\r | |
280 | // Write command to File\r | |
281 | //\r | |
282 | return CUtil.runCommand(task, workingDir, cmdline, false, env);\r | |
283 | \r | |
284 | }\r | |
285 | }\r |