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