]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/userdefine/UserDefineDef.java
Added code to check if "cmd" attribute is valid or not. This is to make error report...
[mirror_edk2.git] / Tools / Java / Source / Cpptasks / net / sf / antcontrib / cpptasks / userdefine / UserDefineDef.java
CommitLineData
878ddf1f 1/*\r
2 * \r
3 * Copyright 2002-2006 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
17package net.sf.antcontrib.cpptasks.userdefine;\r
18\r
19import java.io.BufferedReader;\r
20import java.io.File;\r
21import java.io.FileReader;\r
509693cc 22import java.util.Iterator;\r
23import java.util.LinkedHashSet;\r
24import java.util.Set;\r
878ddf1f 25import java.util.Vector;\r
26\r
27import org.apache.tools.ant.BuildException;\r
28import org.apache.tools.ant.Project;\r
878ddf1f 29\r
30import net.sf.antcontrib.cpptasks.ProcessorDef;\r
878ddf1f 31import net.sf.antcontrib.cpptasks.types.ConditionalPath;\r
32import net.sf.antcontrib.cpptasks.types.IncludePath;\r
33import net.sf.antcontrib.cpptasks.types.LibrarySet;\r
34\r
e6225e3c 35/**\r
36 * A userdefinedef definition. userdefine elements may be placed either as\r
37 * children of a cc element or the project element. A userdefine element with an\r
38 * id attribute may be referenced by userdefine elements with refid or extends\r
39 * attributes.\r
40 * \r
41 */\r
42public class UserDefineDef extends ProcessorDef {\r
43\r
44 public UserDefineDef () {\r
45 }\r
46\r
878ddf1f 47 private String type = "CC";\r
e6225e3c 48\r
509693cc 49 private String family = "MSFT";\r
e6225e3c 50\r
509693cc 51 private String cmd;\r
e6225e3c 52\r
53 private String includePathDelimiter;\r
54\r
509693cc 55 private String outputDelimiter;\r
e6225e3c 56\r
878ddf1f 57 private File workdir;\r
e6225e3c 58\r
59 private Vector includePaths = new Vector();\r
60\r
509693cc 61 private String outputFile;\r
e6225e3c 62\r
63 private Vector allLibraries = new Vector();\r
32c8b5a9 64 \r
65 private String dpath = null;\r
e6225e3c 66\r
509693cc 67 public void addLibset(LibrarySet libset) {\r
68 if (isReference()) {\r
69 throw noChildrenAllowed();\r
70 }\r
71 if (libset == null) {\r
72 throw new NullPointerException("libset");\r
73 }\r
e6225e3c 74\r
75 allLibraries.add(libset);\r
509693cc 76 }\r
e6225e3c 77\r
878ddf1f 78 public void execute() throws org.apache.tools.ant.BuildException {\r
79 throw new org.apache.tools.ant.BuildException(\r
80 "Not an actual task, but looks like one for documentation purposes");\r
81 }\r
82\r
878ddf1f 83 public void addConfiguredArgument(UserDefineArgument arg) {\r
84 if (isReference()) {\r
85 throw noChildrenAllowed();\r
86 }\r
87 addConfiguredProcessorArg(arg);\r
88 }\r
e6225e3c 89\r
878ddf1f 90 /**\r
91 * Creates an include path.\r
92 */\r
93 public IncludePath createIncludePath() {\r
94 Project p = getProject();\r
878ddf1f 95 if (isReference()) {\r
96 throw noChildrenAllowed();\r
97 }\r
98 IncludePath path = new IncludePath(p);\r
99 includePaths.addElement(path);\r
100 return path;\r
101 }\r
e6225e3c 102\r
878ddf1f 103 /**\r
104 * Add a <includepath> if specify the file attribute\r
105 * \r
e6225e3c 106 * @param activePath\r
107 * Active Path Vector\r
108 * @param file\r
109 * File with multiple path\r
878ddf1f 110 * @throws BuildException\r
111 * if the specify file not exist\r
112 */\r
113 protected void loadFile(Vector activePath, File file) throws BuildException {\r
114 FileReader fileReader;\r
115 BufferedReader in;\r
116 String str;\r
117 if (!file.exists()) {\r
118 throw new BuildException("The file " + file + " is not existed");\r
119 }\r
120 try {\r
121 fileReader = new FileReader(file);\r
122 in = new BufferedReader(fileReader);\r
123 while ((str = in.readLine()) != null) {\r
e6225e3c 124 if (str.trim().endsWith("")) {\r
878ddf1f 125 continue;\r
126 }\r
127 str = getProject().replaceProperties(str);\r
128 activePath.addElement(str.trim());\r
129 }\r
130 } catch (Exception e) {\r
131 throw new BuildException(e.getMessage());\r
132 }\r
133 }\r
e6225e3c 134\r
878ddf1f 135 /**\r
136 * Returns the specific include path.\r
e6225e3c 137 * \r
138 * @return All active include paths\r
878ddf1f 139 */\r
140 public String[] getActiveIncludePaths() {\r
141 if (isReference()) {\r
142 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,\r
143 "UserDefineDef")).getActiveIncludePaths();\r
144 }\r
145 return getActivePaths(includePaths);\r
146 }\r
e6225e3c 147\r
878ddf1f 148 private String[] getActivePaths(Vector paths) {\r
149 Project p = getProject();\r
878ddf1f 150 Vector activePaths = new Vector(paths.size());\r
e6225e3c 151 int length = paths.size();\r
152 for (int i = 0; i < length; i++) {\r
878ddf1f 153 ConditionalPath path = (ConditionalPath) paths.elementAt(i);\r
154 if (path.isActive(p)) {\r
155 if (path.getFile() == null) {\r
156 String[] pathEntries = path.list();\r
157 for (int j = 0; j < pathEntries.length; j++) {\r
158 activePaths.addElement(pathEntries[j]);\r
159 }\r
160 } else {\r
161 loadFile(activePaths, path.getFile());\r
162 }\r
163 }\r
164 }\r
165 String[] pathNames = new String[activePaths.size()];\r
166 activePaths.copyInto(pathNames);\r
167 return pathNames;\r
168 }\r
e6225e3c 169\r
170 /**\r
171 * Get include path delimiter.\r
172 * \r
173 * @return Include Path Delimiter\r
174 */\r
175 public String getIncludePathDelimiter() {\r
878ddf1f 176 if (isReference()) {\r
177 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,\r
e6225e3c 178 "UserDefineDef")).getIncludePathDelimiter();\r
878ddf1f 179 }\r
e6225e3c 180 return includePathDelimiter;\r
878ddf1f 181 }\r
182\r
e6225e3c 183 /**\r
184 * Set include path delimiter.\r
185 * \r
186 * @param includePathDelimiter\r
187 * include path delimiter\r
188 */\r
189 public void setIncludePathDelimiter(String includePathDelimiter) {\r
878ddf1f 190 if (isReference()) {\r
191 throw tooManyAttributes();\r
192 }\r
e6225e3c 193 this.includePathDelimiter = includePathDelimiter;\r
878ddf1f 194 }\r
195\r
e6225e3c 196 /**\r
197 * Get type.\r
198 * \r
199 * @return type\r
200 */\r
509693cc 201 public String getType() {\r
878ddf1f 202 if (isReference()) {\r
203 return ((UserDefineDef) getCheckedRef(UserDefineDef.class,\r
509693cc 204 "UserDefineDef")).getType();\r
878ddf1f 205 }\r
509693cc 206 return type;\r
878ddf1f 207 }\r
208\r
e6225e3c 209 /**\r
210 * Set type.\r
211 * \r
212 * @param type\r
213 * Type\r
214 */\r
509693cc 215 public void setType(String type) {\r
878ddf1f 216 if (isReference()) {\r
217 throw tooManyAttributes();\r
218 }\r
509693cc 219 this.type = type;\r
878ddf1f 220 }\r
221\r
509693cc 222 public String getCmd() {\r
223 return cmd;\r
878ddf1f 224 }\r
225\r
509693cc 226 public void setCmd(String cmd) {\r
878ddf1f 227 if (isReference()) {\r
228 throw tooManyAttributes();\r
229 }\r
63cdbe79 230 if (cmd == null || cmd.trim().length() == 0) {\r
231 throw new BuildException("cmd attribute is empty!");\r
232 }\r
233 File cmdProgram = new File(cmd);\r
234 if (cmdProgram.isDirectory()) {\r
235 throw new BuildException(cmd + " is not valid or executable!");\r
236 }\r
509693cc 237 this.cmd = cmd;\r
878ddf1f 238 }\r
239\r
509693cc 240 public String getFamily() {\r
241 return family;\r
878ddf1f 242 }\r
243\r
509693cc 244 public void setFamily(String family) {\r
878ddf1f 245 if (isReference()) {\r
246 throw tooManyAttributes();\r
247 }\r
509693cc 248 this.family = family;\r
878ddf1f 249 }\r
250\r
509693cc 251 public String getOutputFile() {\r
252 return outputFile;\r
878ddf1f 253 }\r
254\r
509693cc 255 public void setOutputFile(String outputFile) {\r
878ddf1f 256 if (isReference()) {\r
257 throw tooManyAttributes();\r
258 }\r
509693cc 259 this.outputFile = outputFile;\r
878ddf1f 260 }\r
261\r
262 public File getWorkdir() {\r
878ddf1f 263 return workdir;\r
264 }\r
265\r
266 public void setWorkdir(File workdir) {\r
267 if (isReference()) {\r
268 throw tooManyAttributes();\r
269 }\r
270 this.workdir = workdir;\r
271 }\r
509693cc 272\r
e6225e3c 273 public String[] getLibset() {\r
509693cc 274 Set libs = new LinkedHashSet();\r
e6225e3c 275 Iterator iter = allLibraries.iterator();\r
509693cc 276 while (iter.hasNext()) {\r
e6225e3c 277 LibrarySet librarySet = (LibrarySet) iter.next();\r
509693cc 278 File basedir = librarySet.getDir(getProject());\r
279 String[] libStrArray = librarySet.getLibs();\r
e6225e3c 280 for (int i = 0; i < libStrArray.length; i++) {\r
509693cc 281 if (basedir != null) {\r
282 File libFile = new File(libStrArray[i]);\r
283 if (libFile.isAbsolute()) {\r
284 libs.add(libFile.getPath());\r
e6225e3c 285 } else {\r
286 libs.add(basedir.getPath() + File.separatorChar\r
287 + libFile.getPath());\r
509693cc 288 }\r
e6225e3c 289 } else {\r
509693cc 290 libs.add(libStrArray[i]);\r
291 }\r
878ddf1f 292 }\r
293 }\r
e6225e3c 294 return (String[]) libs.toArray(new String[libs.size()]);\r
878ddf1f 295 }\r
509693cc 296\r
297 public String getOutputDelimiter() {\r
298 return outputDelimiter;\r
878ddf1f 299 }\r
509693cc 300\r
301 public void setOutputDelimiter(String outputDelimiter) {\r
302 this.outputDelimiter = outputDelimiter;\r
878ddf1f 303 }\r
509693cc 304\r
32c8b5a9 305 public String getDpath() {\r
306 return dpath;\r
307 }\r
308\r
309 public void setDpath(String dpath) {\r
310 this.dpath = dpath;\r
311 }\r
312\r
878ddf1f 313}\r