]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/userdefine/CommandLineUserDefine.java
Update cpptaks to support LIBPATH and INCLUDEPATH which will defined in TOOLS_DEF...
[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
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
51f94863 223 Environment newEnv = new Environment();\r
e6225e3c 224 \r
51f94863 225 if (userdefine.getDpath() != null && userdefine.getDpath().trim().length() != 0) {\r
226 String existPath = System.getenv(getPathName("PATH"));\r
227 \r
32c8b5a9 228 Variable var = new Variable();\r
51f94863 229 var.setKey(getPathName("PATH"));\r
32c8b5a9 230 var.setPath(new Path(project, userdefine.getDpath() + ";" + existPath));\r
231 newEnv.addVariable(var);\r
32c8b5a9 232 }\r
233 \r
51f94863 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
32c8b5a9 253\r
509693cc 254 if (retval != 0) {\r
e6225e3c 255 throw new BuildException(userdefine.getCmd()\r
256 + " failed with return code " + retval, cctask\r
257 .getLocation());\r
509693cc 258 }\r
878ddf1f 259 }\r
32c8b5a9 260 \r
51f94863 261 private String getPathName(String variableName) {\r
32c8b5a9 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
51f94863 269 if(key.equalsIgnoreCase(variableName)) {\r
32c8b5a9 270 pathName = key;\r
271 break ;\r
272 }\r
273 }\r
274 return pathName;\r
275 }\r
e6225e3c 276\r
32c8b5a9 277 protected int runCommand(CCTask task, File workingDir, String[] cmdline, Environment env)\r
878ddf1f 278 throws BuildException {\r
32c8b5a9 279 //\r
280 // Write command to File\r
281 //\r
282 return CUtil.runCommand(task, workingDir, cmdline, false, env);\r
878ddf1f 283\r
284 }\r
878ddf1f 285}\r