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