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