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