]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/CompilerDef.java
Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / Source / Cpptasks / net / sf / antcontrib / cpptasks / CompilerDef.java
1 /*
2 *
3 * Copyright 2001-2004 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;
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileReader;
21 import java.util.Enumeration;
22 import java.util.Vector;
23
24 import net.sf.antcontrib.cpptasks.compiler.CommandLineCompiler;
25 import net.sf.antcontrib.cpptasks.compiler.Compiler;
26 import net.sf.antcontrib.cpptasks.compiler.Processor;
27 import net.sf.antcontrib.cpptasks.gcc.GccCCompiler;
28 import net.sf.antcontrib.cpptasks.types.CompilerArgument;
29 import net.sf.antcontrib.cpptasks.types.ConditionalPath;
30 import net.sf.antcontrib.cpptasks.types.DefineSet;
31 import net.sf.antcontrib.cpptasks.types.IncludePath;
32 import net.sf.antcontrib.cpptasks.types.SystemIncludePath;
33 import net.sf.antcontrib.cpptasks.types.UndefineArgument;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.types.EnumeratedAttribute;
37 import org.apache.tools.ant.*;
38 /**
39 * A compiler definition. compiler elements may be placed either as children of
40 * a cc element or the project element. A compiler element with an id attribute
41 * may be referenced from compiler elements with refid or extends attributes.
42 *
43 * @author Adam Murdoch
44 */
45 public final class CompilerDef extends ProcessorDef {
46 /**
47 * Enumerated attribute with the values "none", "severe", "default",
48 * "production", "diagnostic", and "failtask".
49 */
50 public static class WarningLevel extends EnumeratedAttribute {
51 public String[] getValues() {
52 return new String[]{"none", "severe", "default", "production",
53 "diagnostic", "aserror"};
54 }
55 }
56 /** The source file sets. */
57 private final Vector defineSets = new Vector();
58 private Boolean exceptions;
59 private Boolean rtti;
60 private final Vector includePaths = new Vector();
61 private Boolean multithreaded;
62 private final Vector precompileDefs = new Vector();
63 private final Vector sysIncludePaths = new Vector();
64 private OptimizationEnum optimization;
65 private int warnings = -1;
66 private Boolean defaultflag = new Boolean(true);
67 public CompilerDef() {
68 }
69 /**
70 * Adds a compiler command-line arg.
71 */
72 public void addConfiguredCompilerArg(CompilerArgument arg) {
73 if (isReference()) {
74 throw noChildrenAllowed();
75 }
76 addConfiguredProcessorArg(arg);
77 }
78 /**
79 * Adds a compiler command-line arg.
80 */
81 public void addConfiguredCompilerParam(CompilerParam param) {
82 if (isReference()) {
83 throw noChildrenAllowed();
84 }
85 addConfiguredProcessorParam(param);
86 }
87 /**
88 * Adds a defineset.
89 */
90 public void addConfiguredDefineset(DefineSet defs) {
91 if (defs == null) {
92 throw new NullPointerException("defs");
93 }
94 if (isReference()) {
95 throw noChildrenAllowed();
96 }
97 defineSets.addElement(defs);
98 }
99 /**
100 * Creates an include path.
101 */
102 public IncludePath createIncludePath() {
103 Project p = getProject();
104 if (p == null) {
105 throw new java.lang.IllegalStateException("project must be set");
106 }
107 if (isReference()) {
108 throw noChildrenAllowed();
109 }
110 IncludePath path = new IncludePath(p);
111 includePaths.addElement(path);
112 return path;
113 }
114 /**
115 * Add a <includepath>or <sysincludepath> if specify the file
116 * attribute
117 *
118 * @throws BuildException
119 * if the specify file not exist
120 */
121 protected void loadFile(Vector activePath,File file) throws BuildException {
122 FileReader fileReader;
123 BufferedReader in;
124 String str;
125 if (! file.exists()){
126 throw new BuildException("The file " + file + " is not existed");
127 }
128 try {
129 fileReader = new FileReader(file);
130 in = new BufferedReader(fileReader);
131 while ( (str = in.readLine()) != null ){
132 if(str.trim() == ""){
133 continue ;
134 }
135 str = getProject().replaceProperties(str);
136 activePath.addElement(str.trim());
137 }
138 }
139 catch(Exception e){
140 throw new BuildException(e.getMessage());
141 }
142 }
143
144 /**
145 * Specifies precompilation prototype file and exclusions.
146 *
147 */
148 public PrecompileDef createPrecompile() throws BuildException {
149 Project p = getProject();
150 if (isReference()) {
151 throw noChildrenAllowed();
152 }
153 PrecompileDef precomp = new PrecompileDef();
154 precomp.setProject(p);
155 precompileDefs.addElement(precomp);
156 return precomp;
157 }
158 /**
159 * Creates a system include path. Locations and timestamps of files located
160 * using the system include paths are not used in dependency analysis.
161 *
162 *
163 * Standard include locations should not be specified. The compiler
164 * adapters should recognized the settings from the appropriate environment
165 * variables or configuration files.
166 */
167 public SystemIncludePath createSysIncludePath() {
168 Project p = getProject();
169 if (p == null) {
170 throw new java.lang.IllegalStateException("project must be set");
171 }
172 if (isReference()) {
173 throw noChildrenAllowed();
174 }
175 SystemIncludePath path = new SystemIncludePath(p);
176 sysIncludePaths.addElement(path);
177 return path;
178 }
179 public void execute() throws org.apache.tools.ant.BuildException {
180 throw new org.apache.tools.ant.BuildException(
181 "Not an actual task, but looks like one for documentation purposes");
182 }
183 public UndefineArgument[] getActiveDefines() {
184 Project p = getProject();
185 if (p == null) {
186 throw new java.lang.IllegalStateException(
187 "project must be set before this call");
188 }
189 if (isReference()) {
190 return ((CompilerDef) getCheckedRef(CompilerDef.class,
191 "CompilerDef")).getActiveDefines();
192 }
193 Vector actives = new Vector();
194 for (int i = 0; i < defineSets.size(); i++) {
195 DefineSet currentSet = (DefineSet) defineSets.elementAt(i);
196 UndefineArgument[] defines = currentSet.getDefines();
197 for (int j = 0; j < defines.length; j++) {
198 if (defines[j].isActive(p)) {
199 actives.addElement(defines[j]);
200 }
201 }
202 }
203 UndefineArgument[] retval = new UndefineArgument[actives.size()];
204 actives.copyInto(retval);
205 return retval;
206 }
207 /**
208 * Returns the compiler-specific include path.
209 */
210 public String[] getActiveIncludePaths() {
211 if (isReference()) {
212 return ((CompilerDef) getCheckedRef(CompilerDef.class,
213 "CompilerDef")).getActiveIncludePaths();
214 }
215 return getActivePaths(includePaths);
216 }
217 private String[] getActivePaths(Vector paths) {
218 Project p = getProject();
219 if (p == null) {
220 throw new java.lang.IllegalStateException("project not set");
221 }
222 Vector activePaths = new Vector(paths.size());
223 for (int i = 0; i < paths.size(); i++) {
224 ConditionalPath path = (ConditionalPath) paths.elementAt(i);
225 if (path.isActive(p)) {
226 if (path.getFile() == null) {
227 String[] pathEntries = path.list();
228 for (int j = 0; j < pathEntries.length; j++) {
229 activePaths.addElement(pathEntries[j]);
230 }
231 }
232 else {
233 loadFile(activePaths, path.getFile());
234 }
235 }
236 }
237 String[] pathNames = new String[activePaths.size()];
238 activePaths.copyInto(pathNames);
239 return pathNames;
240 }
241 public PrecompileDef getActivePrecompile(CompilerDef ccElement) {
242 if (isReference()) {
243 return ((CompilerDef) getCheckedRef(CompilerDef.class,
244 "CompilerDef")).getActivePrecompile(ccElement);
245 }
246 PrecompileDef current = null;
247 Enumeration enumPrecompilerDef = precompileDefs.elements();
248 while (enumPrecompilerDef.hasMoreElements()) {
249 current = (PrecompileDef) enumPrecompilerDef.nextElement();
250 if (current.isActive()) {
251 return current;
252 }
253 }
254 CompilerDef extendedDef = (CompilerDef) getExtends();
255 if (extendedDef != null) {
256 current = extendedDef.getActivePrecompile(null);
257 if (current != null) {
258 return current;
259 }
260 }
261 if (ccElement != null && getInherit()) {
262 return ccElement.getActivePrecompile(null);
263 }
264 return null;
265 }
266 public String[] getActiveSysIncludePaths() {
267 if (isReference()) {
268 return ((CompilerDef) getCheckedRef(CompilerDef.class,
269 "CompilerDef")).getActiveSysIncludePaths();
270 }
271 return getActivePaths(sysIncludePaths);
272 }
273 public final boolean getExceptions(CompilerDef[] defaultProviders, int index) {
274 if (isReference()) {
275 return ((CompilerDef) getCheckedRef(CompilerDef.class,
276 "CompilerDef")).getExceptions(defaultProviders, index);
277 }
278 if (exceptions != null) {
279 return exceptions.booleanValue();
280 } else {
281 if (defaultProviders != null && index < defaultProviders.length) {
282 return defaultProviders[index].getExceptions(defaultProviders,
283 index + 1);
284 }
285 }
286 return false;
287 }
288 public final Boolean getRtti(CompilerDef[] defaultProviders, int index) {
289 if (isReference()) {
290 return ((CompilerDef) getCheckedRef(CompilerDef.class,
291 "CompilerDef")).getRtti(defaultProviders, index);
292 }
293 if (rtti != null) {
294 return rtti;
295 } else {
296 if (defaultProviders != null && index < defaultProviders.length) {
297 return defaultProviders[index].getRtti(defaultProviders,
298 index + 1);
299 }
300 }
301 return null;
302 }
303 public final Boolean getDefaultflag(CompilerDef[] defaultProviders, int index) {
304 if (isReference()) {
305 return ((CompilerDef) getCheckedRef(CompilerDef.class,
306 "CompilerDef")).getDefaultflag(defaultProviders, index);
307 }
308 return defaultflag;
309 }
310 public final OptimizationEnum getOptimization(CompilerDef[] defaultProviders, int index) {
311 if (isReference()) {
312 return ((CompilerDef) getCheckedRef(CompilerDef.class,
313 "CompilerDef")).getOptimization(defaultProviders, index);
314 }
315 if (optimization != null) {
316 return optimization;
317 } else {
318 if (defaultProviders != null && index < defaultProviders.length) {
319 return defaultProviders[index].getOptimization(defaultProviders,
320 index + 1);
321 }
322 }
323 return null;
324 }
325
326 public boolean getMultithreaded(CompilerDef[] defaultProviders, int index) {
327 if (isReference()) {
328 return ((CompilerDef) getCheckedRef(CompilerDef.class,
329 "CompilerDef")).getMultithreaded(defaultProviders, index);
330 }
331 if (multithreaded != null) {
332 return multithreaded.booleanValue();
333 } else {
334 if (defaultProviders != null && index < defaultProviders.length) {
335 return defaultProviders[index].getMultithreaded(
336 defaultProviders, index + 1);
337 }
338 }
339 return true;
340 }
341 public Processor getProcessor() {
342 Processor processor = super.getProcessor();
343 if (processor == null) {
344 processor = GccCCompiler.getInstance();
345 }
346 if (getLibtool() && processor instanceof CommandLineCompiler) {
347 CommandLineCompiler compiler = (CommandLineCompiler) processor;
348 processor = compiler.getLibtoolCompiler();
349 }
350 return processor;
351 }
352 public int getWarnings(CompilerDef[] defaultProviders, int index) {
353 if (isReference()) {
354 return ((CompilerDef) getCheckedRef(CompilerDef.class,
355 "CompilerDef")).getWarnings(defaultProviders, index);
356 }
357 if (warnings == -1) {
358 if (defaultProviders != null && index < defaultProviders.length) {
359 return defaultProviders[index].getWarnings(defaultProviders,
360 index + 1);
361 }
362 }
363 return warnings;
364 }
365 /**
366 * Sets the default compiler adapter. Use the "name" attribute when the
367 * compiler is a supported compiler.
368 *
369 * @param classname
370 * fully qualified classname which implements CompilerAdapter
371 */
372 public void setClassname(String classname) throws BuildException {
373 if (isReference()) {
374 throw tooManyAttributes();
375 }
376 super.setClassname(classname);
377 Processor proc = getProcessor();
378 if (!(proc instanceof Compiler)) {
379 throw new BuildException(classname + " does not implement Compiler");
380 }
381 }
382 /**
383 * Enables or disables exception support.
384 *
385 * @param exceptions
386 * if true, exceptions are supported.
387 *
388 */
389 public void setExceptions(boolean exceptions) {
390 if (isReference()) {
391 throw tooManyAttributes();
392 }
393 this.exceptions = booleanValueOf(exceptions);
394 }
395
396 /**
397 * Enables or disables run-time type information.
398 *
399 * @param rtti
400 * if true, run-time type information is supported.
401 *
402 */
403 public void setRtti(boolean rtti) {
404 if (isReference()) {
405 throw tooManyAttributes();
406 }
407 this.rtti = booleanValueOf(rtti);
408 }
409
410 /**
411 * Enables or disables generation of multithreaded code. Unless specified,
412 * multithreaded code generation is enabled.
413 *
414 * @param multi
415 * If true, generated code may be multithreaded.
416 */
417 public void setMultithreaded(boolean multithreaded) {
418 if (isReference()) {
419 throw tooManyAttributes();
420 }
421 this.multithreaded = booleanValueOf(multithreaded);
422 }
423 /**
424 * Sets compiler type.
425 *
426 *
427 * <table width="100%" border="1"> <thead>Supported compilers </thead>
428 * <tr>
429 * <td>gcc (default)</td>
430 * <td>GCC C++ compiler</td>
431 * </tr>
432 * <tr>
433 * <td>g++</td>
434 * <td>GCC C++ compiler</td>
435 * </tr>
436 * <tr>
437 * <td>c++</td>
438 * <td>GCC C++ compiler</td>
439 * </tr>
440 * <tr>
441 * <td>g77</td>
442 * <td>GNU Fortran compiler</td>
443 * </tr>
444 * <tr>
445 * <td>msvc</td>
446 * <td>Microsoft Visual C++</td>
447 * </tr>
448 * <tr>
449 * <td>bcc</td>
450 * <td>Borland C++ Compiler</td>
451 * </tr>
452 * <tr>
453 * <td>msrc</td>
454 * <td>Microsoft Resource Compiler</td>
455 * </tr>
456 * <tr>
457 * <td>brc</td>
458 * <td>Borland Resource Compiler</td>
459 * </tr>
460 * <tr>
461 * <td>df</td>
462 * <td>Compaq Visual Fortran Compiler</td>
463 * </tr>
464 * <tr>
465 * <td>midl</td>
466 * <td>Microsoft MIDL Compiler</td>
467 * </tr>
468 * <tr>
469 * <td>icl</td>
470 * <td>Intel C++ compiler for Windows (IA-32)</td>
471 * </tr>
472 * <tr>
473 * <td>ecl</td>
474 * <td>Intel C++ compiler for Windows (IA-64)</td>
475 * </tr>
476 * <tr>
477 * <td>icc</td>
478 * <td>Intel C++ compiler for Linux (IA-32)</td>
479 * </tr>
480 * <tr>
481 * <td>ecc</td>
482 * <td>Intel C++ compiler for Linux (IA-64)</td>
483 * </tr>
484 * <tr>
485 * <td>CC</td>
486 * <td>Sun ONE C++ compiler</td>
487 * </tr>
488 * <tr>
489 * <td>aCC</td>
490 * <td>HP aC++ C++ Compiler</td>
491 * </tr>
492 * <tr>
493 * <td>os390</td>
494 * <td>OS390 C Compiler</td>
495 * </tr>
496 * <tr>
497 * <td>os400</td>
498 * <td>Icc Compiler</td>
499 * </tr>
500 * <tr>
501 * <td>sunc89</td>
502 * <td>Sun C89 C Compiler</td>
503 * </tr>
504 * <tr>
505 * <td>xlC</td>
506 * <td>VisualAge C Compiler</td>
507 * </tr>
508 * </table>
509 *
510 */
511 public void setName(CompilerEnum name) throws BuildException {
512 if (isReference()) {
513 throw tooManyAttributes();
514 }
515 Compiler compiler = name.getCompiler();
516 setProcessor(compiler);
517 }
518 protected void setProcessor(Processor proc) throws BuildException {
519 try {
520 super.setProcessor((Compiler) proc);
521 } catch (ClassCastException ex) {
522 throw new BuildException(ex);
523 }
524 }
525 /**
526 * Enumerated attribute with the values "none", "severe", "default",
527 * "production", "diagnostic", and "failtask".
528 */
529 public void setWarnings(CompilerDef.WarningLevel level) {
530 warnings = level.getIndex();
531 }
532 /**
533 * Sets optimization level.
534 *
535 * @param value optimization level
536 */
537 public void setOptimize(OptimizationEnum value) {
538 if (isReference()) {
539 throw tooManyAttributes();
540 }
541 this.optimization = value;
542 }
543 /**
544 * Enables or disables default flags.
545 *
546 * @param defaultflag
547 * if true, default flags will add to command line.
548 *
549 */
550 public void setDefaultflag(boolean defaultflag) {
551 if (isReference()) {
552 throw tooManyAttributes();
553 }
554 this.defaultflag = booleanValueOf(defaultflag);
555 }
556 }