3 * Copyright 2002-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
.compiler
;
20 import net
.sf
.antcontrib
.cpptasks
.CCTask
;
21 import net
.sf
.antcontrib
.cpptasks
.CompilerParam
;
22 import net
.sf
.antcontrib
.cpptasks
.DependencyInfo
;
23 import net
.sf
.antcontrib
.cpptasks
.ProcessorParam
;
25 import org
.apache
.tools
.ant
.BuildException
;
27 * A configuration for a C++ compiler
31 public final class CommandLineCompilerConfiguration
33 CompilerConfiguration
{
34 private/* final */String
[] args
;
35 private/* final */CommandLineCompiler compiler
;
36 private String
[] endArgs
;
38 // include path from environment variable not
39 // explicitly stated in Ant script
40 private/* final */File
[] envIncludePath
;
41 private String
[] exceptFiles
;
42 private/* final */String identifier
;
43 private/* final */File
[] includePath
;
44 private/* final */String includePathIdentifier
;
45 private boolean isPrecompiledHeaderGeneration
;
46 private/* final */ProcessorParam
[] params
;
47 private/* final */boolean rebuild
;
48 private/* final */File
[] sysIncludePath
;
49 public CommandLineCompilerConfiguration(CommandLineCompiler compiler
,
50 String identifier
, File
[] includePath
, File
[] sysIncludePath
,
51 File
[] envIncludePath
, String includePathIdentifier
, String
[] args
,
52 ProcessorParam
[] params
, boolean rebuild
, String
[] endArgs
) {
53 if (compiler
== null) {
54 throw new NullPointerException("compiler");
56 if (identifier
== null) {
57 throw new NullPointerException("identifier");
59 if (includePathIdentifier
== null) {
60 throw new NullPointerException("includePathIdentifier");
63 this.args
= new String
[0];
65 this.args
= (String
[]) args
.clone();
67 if (includePath
== null) {
68 this.includePath
= new File
[0];
70 this.includePath
= (File
[]) includePath
.clone();
72 if (sysIncludePath
== null) {
73 this.sysIncludePath
= new File
[0];
75 this.sysIncludePath
= (File
[]) sysIncludePath
.clone();
77 if (envIncludePath
== null) {
78 this.envIncludePath
= new File
[0];
80 this.envIncludePath
= (File
[]) envIncludePath
.clone();
82 this.compiler
= compiler
;
83 this.params
= (ProcessorParam
[]) params
.clone();
84 this.rebuild
= rebuild
;
85 this.identifier
= identifier
;
86 this.includePathIdentifier
= includePathIdentifier
;
87 this.endArgs
= (String
[]) endArgs
.clone();
89 isPrecompiledHeaderGeneration
= false;
91 public CommandLineCompilerConfiguration(
92 CommandLineCompilerConfiguration base
, String
[] additionalArgs
,
93 String
[] exceptFiles
, boolean isPrecompileHeaderGeneration
) {
94 compiler
= base
.compiler
;
95 identifier
= base
.identifier
;
96 rebuild
= base
.rebuild
;
97 includePath
= (File
[]) base
.includePath
.clone();
98 sysIncludePath
= (File
[]) base
.sysIncludePath
.clone();
99 endArgs
= (String
[]) base
.endArgs
.clone();
100 envIncludePath
= (File
[]) base
.envIncludePath
.clone();
101 includePathIdentifier
= base
.includePathIdentifier
;
102 if (exceptFiles
!= null) {
103 this.exceptFiles
= (String
[]) exceptFiles
.clone();
105 this.isPrecompiledHeaderGeneration
= isPrecompileHeaderGeneration
;
106 args
= new String
[base
.args
.length
+ additionalArgs
.length
];
107 for (int i
= 0; i
< base
.args
.length
; i
++) {
108 args
[i
] = base
.args
[i
];
110 int index
= base
.args
.length
;
111 for (int i
= 0; i
< additionalArgs
.length
; i
++) {
112 args
[index
++] = additionalArgs
[i
];
115 public int bid(String inputFile
) {
116 int compilerBid
= compiler
.bid(inputFile
);
117 if (compilerBid
> 0 && exceptFiles
!= null) {
118 for (int i
= 0; i
< exceptFiles
.length
; i
++) {
119 if (inputFile
.equals(exceptFiles
[i
])) {
126 public void compile(CCTask task
, File outputDir
, String
[] sourceFiles
,
127 boolean relentless
, ProgressMonitor monitor
) throws BuildException
{
128 if (monitor
!= null) {
132 compiler
.compile(task
, outputDir
, sourceFiles
, args
, endArgs
,
133 relentless
, this, monitor
);
134 if (monitor
!= null) {
135 monitor
.finish(this, true);
137 } catch (BuildException ex
) {
138 if (monitor
!= null) {
139 monitor
.finish(this, false);
146 * This method may be used to get two distinct compiler configurations, one
147 * for compiling the specified file and producing a precompiled header
148 * file, and a second for compiling other files using the precompiled
151 * The last (preferrably only) include directive in the prototype file will
152 * be used to mark the boundary between pre-compiled and normally compiled
156 * A source file (for example, stdafx.cpp) that is used to build
157 * the precompiled header file. @returns null if precompiled
158 * headers are not supported or a two element array containing
159 * the precompiled header generation configuration and the
160 * consuming configuration
163 public CompilerConfiguration
[] createPrecompileConfigurations(
164 File prototype
, String
[] nonPrecompiledFiles
) {
165 if (compiler
instanceof PrecompilingCompiler
) {
166 return ((PrecompilingCompiler
) compiler
)
167 .createPrecompileConfigurations(this, prototype
,
168 nonPrecompiledFiles
);
173 * Returns a string representation of this configuration. Should be
174 * canonical so that equivalent configurations will have equivalent string
177 public String
getIdentifier() {
180 public String
getIncludePathIdentifier() {
181 return includePathIdentifier
;
183 public String
getOutputFileName(String inputFile
) {
184 return compiler
.getOutputFileName(inputFile
);
186 public CompilerParam
getParam(String name
) {
187 for (int i
= 0; i
< params
.length
; i
++) {
188 if (name
.equals(params
[i
].getName()))
189 return (CompilerParam
) params
[i
];
193 public ProcessorParam
[] getParams() {
196 public boolean getRebuild() {
199 public boolean isPrecompileGeneration() {
200 return isPrecompiledHeaderGeneration
;
202 public DependencyInfo
parseIncludes(CCTask task
, File baseDir
, File source
) {
203 return compiler
.parseIncludes(task
, source
, includePath
,
204 sysIncludePath
, envIncludePath
, baseDir
,
205 getIncludePathIdentifier());
207 public String
toString() {
210 public String
[] getPreArguments() {
211 return (String
[]) args
.clone();
213 public String
[] getEndArguments() {
214 return (String
[]) endArgs
.clone();