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
;
18 import java
.io
.BufferedReader
;
20 import java
.io
.FileReader
;
21 import java
.io
.IOException
;
22 import java
.io
.Reader
;
24 import net
.sf
.antcontrib
.cpptasks
.parser
.Parser
;
26 import org
.apache
.tools
.ant
.BuildException
;
27 import org
.apache
.tools
.ant
.types
.Environment
;
29 * A command line C compiler that can utilize precompilation of header files
33 public abstract class PrecompilingCommandLineCompiler
35 CommandLineCompiler
implements PrecompilingCompiler
{
36 protected PrecompilingCommandLineCompiler(String command
,
37 String identifierArg
, String
[] sourceExtensions
,
38 String
[] headerExtensions
, String outputSuffix
, boolean libtool
,
39 PrecompilingCommandLineCompiler libtoolCompiler
,
40 boolean newEnvironment
, Environment env
) {
41 super(command
, identifierArg
, sourceExtensions
, headerExtensions
,
42 outputSuffix
, libtool
, libtoolCompiler
, newEnvironment
, env
);
46 * This method may be used to get two distinct compiler configurations, one
47 * for compiling the specified file and producing a precompiled header
48 * file, and a second for compiling other files using the precompiled
51 * The last (preferrably only) include directive in the prototype file will
52 * be used to mark the boundary between pre-compiled and normally compiled
58 * A source file (for example, stdafx.cpp) that is used to build
59 * the precompiled header file. @returns null if precompiled
60 * headers are not supported or a two element array containing
61 * the precompiled header generation configuration and the
62 * consuming configuration
65 public CompilerConfiguration
[] createPrecompileConfigurations(
66 CompilerConfiguration config
, File prototype
, String
[] exceptFiles
) {
68 // cast should success or someone is passing us a configuration
69 // that was prepared by another processor
71 CommandLineCompilerConfiguration cmdLineConfig
= (CommandLineCompilerConfiguration
) config
;
73 // parse prototype file to determine last header
75 Parser parser
= createParser(prototype
);
78 Reader reader
= new BufferedReader(new FileReader(prototype
));
80 includes
= parser
.getIncludes();
81 } catch (IOException ex
) {
82 throw new BuildException(
83 "Error parsing precompiled header protoype: "
84 + prototype
.toString() + ":" + ex
.toString());
86 if (includes
.length
== 0) {
87 throw new BuildException("Precompiled header prototype: "
88 + prototype
.toString()
89 + " does not contain any include directives.");
91 CompilerConfiguration
[] configs
= new CompilerConfiguration
[2];
92 configs
[0] = createPrecompileGeneratingConfig(cmdLineConfig
, prototype
,
94 configs
[1] = createPrecompileUsingConfig(cmdLineConfig
, prototype
,
95 includes
[0], exceptFiles
);
98 abstract protected CompilerConfiguration
createPrecompileGeneratingConfig(
99 CommandLineCompilerConfiguration baseConfig
, File prototype
,
101 abstract protected CompilerConfiguration
createPrecompileUsingConfig(
102 CommandLineCompilerConfiguration baseConfig
, File prototype
,
103 String lastInclude
, String
[] exceptFiles
);