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