3 * Copyright 2001-2004 The Ant-Contrib project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package net
.sf
.antcontrib
.cpptasks
;
18 import java
.io
.BufferedReader
;
20 import java
.io
.FileReader
;
21 import java
.util
.Enumeration
;
22 import java
.util
.Vector
;
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
;
35 import org
.apache
.tools
.ant
.BuildException
;
36 import org
.apache
.tools
.ant
.types
.EnumeratedAttribute
;
37 import org
.apache
.tools
.ant
.*;
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.
43 * @author Adam Murdoch
45 public final class CompilerDef
extends ProcessorDef
{
47 * Enumerated attribute with the values "none", "severe", "default",
48 * "production", "diagnostic", and "failtask".
50 public static class WarningLevel
extends EnumeratedAttribute
{
51 public String
[] getValues() {
52 return new String
[]{"none", "severe", "default", "production",
53 "diagnostic", "aserror"};
56 /** The source file sets. */
57 private final Vector defineSets
= new Vector();
58 private Boolean exceptions
;
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() {
70 * Adds a compiler command-line arg.
72 public void addConfiguredCompilerArg(CompilerArgument arg
) {
74 throw noChildrenAllowed();
76 addConfiguredProcessorArg(arg
);
79 * Adds a compiler command-line arg.
81 public void addConfiguredCompilerParam(CompilerParam param
) {
83 throw noChildrenAllowed();
85 addConfiguredProcessorParam(param
);
90 public void addConfiguredDefineset(DefineSet defs
) {
92 throw new NullPointerException("defs");
95 throw noChildrenAllowed();
97 defineSets
.addElement(defs
);
100 * Creates an include path.
102 public IncludePath
createIncludePath() {
103 Project p
= getProject();
105 throw new java
.lang
.IllegalStateException("project must be set");
108 throw noChildrenAllowed();
110 IncludePath path
= new IncludePath(p
);
111 includePaths
.addElement(path
);
115 * Add a <includepath>or <sysincludepath> if specify the file
118 * @throws BuildException
119 * if the specify file not exist
121 protected void loadFile(Vector activePath
,File file
) throws BuildException
{
122 FileReader fileReader
;
125 if (! file
.exists()){
126 throw new BuildException("The file " + file
+ " is not existed");
129 fileReader
= new FileReader(file
);
130 in
= new BufferedReader(fileReader
);
131 while ( (str
= in
.readLine()) != null ){
132 if(str
.trim() == ""){
135 str
= getProject().replaceProperties(str
);
136 activePath
.addElement(str
.trim());
140 throw new BuildException(e
.getMessage());
145 * Specifies precompilation prototype file and exclusions.
148 public PrecompileDef
createPrecompile() throws BuildException
{
149 Project p
= getProject();
151 throw noChildrenAllowed();
153 PrecompileDef precomp
= new PrecompileDef();
154 precomp
.setProject(p
);
155 precompileDefs
.addElement(precomp
);
159 * Creates a system include path. Locations and timestamps of files located
160 * using the system include paths are not used in dependency analysis.
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.
167 public SystemIncludePath
createSysIncludePath() {
168 Project p
= getProject();
170 throw new java
.lang
.IllegalStateException("project must be set");
173 throw noChildrenAllowed();
175 SystemIncludePath path
= new SystemIncludePath(p
);
176 sysIncludePaths
.addElement(path
);
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");
183 public UndefineArgument
[] getActiveDefines() {
184 Project p
= getProject();
186 throw new java
.lang
.IllegalStateException(
187 "project must be set before this call");
190 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
191 "CompilerDef")).getActiveDefines();
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
]);
203 UndefineArgument
[] retval
= new UndefineArgument
[actives
.size()];
204 actives
.copyInto(retval
);
208 * Returns the compiler-specific include path.
210 public String
[] getActiveIncludePaths() {
212 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
213 "CompilerDef")).getActiveIncludePaths();
215 return getActivePaths(includePaths
);
217 private String
[] getActivePaths(Vector paths
) {
218 Project p
= getProject();
220 throw new java
.lang
.IllegalStateException("project not set");
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
]);
233 loadFile(activePaths
, path
.getFile());
237 String
[] pathNames
= new String
[activePaths
.size()];
238 activePaths
.copyInto(pathNames
);
241 public PrecompileDef
getActivePrecompile(CompilerDef ccElement
) {
243 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
244 "CompilerDef")).getActivePrecompile(ccElement
);
246 PrecompileDef current
= null;
247 Enumeration enumPrecompilerDef
= precompileDefs
.elements();
248 while (enumPrecompilerDef
.hasMoreElements()) {
249 current
= (PrecompileDef
) enumPrecompilerDef
.nextElement();
250 if (current
.isActive()) {
254 CompilerDef extendedDef
= (CompilerDef
) getExtends();
255 if (extendedDef
!= null) {
256 current
= extendedDef
.getActivePrecompile(null);
257 if (current
!= null) {
261 if (ccElement
!= null && getInherit()) {
262 return ccElement
.getActivePrecompile(null);
266 public String
[] getActiveSysIncludePaths() {
268 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
269 "CompilerDef")).getActiveSysIncludePaths();
271 return getActivePaths(sysIncludePaths
);
273 public final boolean getExceptions(CompilerDef
[] defaultProviders
, int index
) {
275 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
276 "CompilerDef")).getExceptions(defaultProviders
, index
);
278 if (exceptions
!= null) {
279 return exceptions
.booleanValue();
281 if (defaultProviders
!= null && index
< defaultProviders
.length
) {
282 return defaultProviders
[index
].getExceptions(defaultProviders
,
288 public final Boolean
getRtti(CompilerDef
[] defaultProviders
, int index
) {
290 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
291 "CompilerDef")).getRtti(defaultProviders
, index
);
296 if (defaultProviders
!= null && index
< defaultProviders
.length
) {
297 return defaultProviders
[index
].getRtti(defaultProviders
,
303 public final Boolean
getDefaultflag(CompilerDef
[] defaultProviders
, int index
) {
305 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
306 "CompilerDef")).getDefaultflag(defaultProviders
, index
);
310 public final OptimizationEnum
getOptimization(CompilerDef
[] defaultProviders
, int index
) {
312 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
313 "CompilerDef")).getOptimization(defaultProviders
, index
);
315 if (optimization
!= null) {
318 if (defaultProviders
!= null && index
< defaultProviders
.length
) {
319 return defaultProviders
[index
].getOptimization(defaultProviders
,
326 public boolean getMultithreaded(CompilerDef
[] defaultProviders
, int index
) {
328 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
329 "CompilerDef")).getMultithreaded(defaultProviders
, index
);
331 if (multithreaded
!= null) {
332 return multithreaded
.booleanValue();
334 if (defaultProviders
!= null && index
< defaultProviders
.length
) {
335 return defaultProviders
[index
].getMultithreaded(
336 defaultProviders
, index
+ 1);
341 public Processor
getProcessor() {
342 Processor processor
= super.getProcessor();
343 if (processor
== null) {
344 processor
= GccCCompiler
.getInstance();
346 if (getLibtool() && processor
instanceof CommandLineCompiler
) {
347 CommandLineCompiler compiler
= (CommandLineCompiler
) processor
;
348 processor
= compiler
.getLibtoolCompiler();
352 public int getWarnings(CompilerDef
[] defaultProviders
, int index
) {
354 return ((CompilerDef
) getCheckedRef(CompilerDef
.class,
355 "CompilerDef")).getWarnings(defaultProviders
, index
);
357 if (warnings
== -1) {
358 if (defaultProviders
!= null && index
< defaultProviders
.length
) {
359 return defaultProviders
[index
].getWarnings(defaultProviders
,
366 * Sets the default compiler adapter. Use the "name" attribute when the
367 * compiler is a supported compiler.
370 * fully qualified classname which implements CompilerAdapter
372 public void setClassname(String classname
) throws BuildException
{
374 throw tooManyAttributes();
376 super.setClassname(classname
);
377 Processor proc
= getProcessor();
378 if (!(proc
instanceof Compiler
)) {
379 throw new BuildException(classname
+ " does not implement Compiler");
383 * Enables or disables exception support.
386 * if true, exceptions are supported.
389 public void setExceptions(boolean exceptions
) {
391 throw tooManyAttributes();
393 this.exceptions
= booleanValueOf(exceptions
);
397 * Enables or disables run-time type information.
400 * if true, run-time type information is supported.
403 public void setRtti(boolean rtti
) {
405 throw tooManyAttributes();
407 this.rtti
= booleanValueOf(rtti
);
411 * Enables or disables generation of multithreaded code. Unless specified,
412 * multithreaded code generation is enabled.
415 * If true, generated code may be multithreaded.
417 public void setMultithreaded(boolean multithreaded
) {
419 throw tooManyAttributes();
421 this.multithreaded
= booleanValueOf(multithreaded
);
424 * Sets compiler type.
427 * <table width="100%" border="1"> <thead>Supported compilers </thead>
429 * <td>gcc (default)</td>
430 * <td>GCC C++ compiler</td>
434 * <td>GCC C++ compiler</td>
438 * <td>GCC C++ compiler</td>
442 * <td>GNU Fortran compiler</td>
446 * <td>Microsoft Visual C++</td>
450 * <td>Borland C++ Compiler</td>
454 * <td>Microsoft Resource Compiler</td>
458 * <td>Borland Resource Compiler</td>
462 * <td>Compaq Visual Fortran Compiler</td>
466 * <td>Microsoft MIDL Compiler</td>
470 * <td>Intel C++ compiler for Windows (IA-32)</td>
474 * <td>Intel C++ compiler for Windows (IA-64)</td>
478 * <td>Intel C++ compiler for Linux (IA-32)</td>
482 * <td>Intel C++ compiler for Linux (IA-64)</td>
486 * <td>Sun ONE C++ compiler</td>
490 * <td>HP aC++ C++ Compiler</td>
494 * <td>OS390 C Compiler</td>
498 * <td>Icc Compiler</td>
502 * <td>Sun C89 C Compiler</td>
506 * <td>VisualAge C Compiler</td>
511 public void setName(CompilerEnum name
) throws BuildException
{
513 throw tooManyAttributes();
515 Compiler compiler
= name
.getCompiler();
516 setProcessor(compiler
);
518 protected void setProcessor(Processor proc
) throws BuildException
{
520 super.setProcessor((Compiler
) proc
);
521 } catch (ClassCastException ex
) {
522 throw new BuildException(ex
);
526 * Enumerated attribute with the values "none", "severe", "default",
527 * "production", "diagnostic", and "failtask".
529 public void setWarnings(CompilerDef
.WarningLevel level
) {
530 warnings
= level
.getIndex();
533 * Sets optimization level.
535 * @param value optimization level
537 public void setOptimize(OptimizationEnum value
) {
539 throw tooManyAttributes();
541 this.optimization
= value
;
544 * Enables or disables default flags.
547 * if true, default flags will add to command line.
550 public void setDefaultflag(boolean defaultflag
) {
552 throw tooManyAttributes();
554 this.defaultflag
= booleanValueOf(defaultflag
);