View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks.compiler;
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileReader;
25  import java.io.IOException;
26  import java.io.Reader;
27  
28  import org.apache.tools.ant.BuildException;
29  import org.apache.tools.ant.types.Environment;
30  
31  import com.github.maven_nar.cpptasks.parser.Parser;
32  
33  /**
34   * A command line C compiler that can utilize precompilation of header files
35   *
36   * @author Curt Arnold
37   */
38  public abstract class PrecompilingCommandLineCompiler extends CommandLineCompiler implements PrecompilingCompiler {
39    protected PrecompilingCommandLineCompiler(final String command, final String identifierArg,
40        final String[] sourceExtensions, final String[] headerExtensions, final String outputSuffix,
41        final boolean libtool, final PrecompilingCommandLineCompiler libtoolCompiler, final boolean newEnvironment,
42        final Environment env) {
43      super(command, identifierArg, sourceExtensions, headerExtensions, outputSuffix, libtool, libtoolCompiler,
44          newEnvironment, env);
45    }
46  
47    /**
48     * 
49     * This method may be used to get two distinct compiler configurations, one
50     * for compiling the specified file and producing a precompiled header
51     * file, and a second for compiling other files using the precompiled
52     * header file.
53     * 
54     * The last (preferrably only) include directive in the prototype file will
55     * be used to mark the boundary between pre-compiled and normally compiled
56     * headers.
57     * 
58     * @param config
59     *          base configuration
60     * @param prototype
61     *          A source file (for example, stdafx.cpp) that is used to build
62     *          the precompiled header file. @returns null if precompiled
63     *          headers are not supported or a two element array containing
64     *          the precompiled header generation configuration and the
65     *          consuming configuration
66     * 
67     */
68    @Override
69    public CompilerConfiguration[] createPrecompileConfigurations(final CompilerConfiguration config,
70        final File prototype, final String[] exceptFiles) {
71      //
72      // cast should success or someone is passing us a configuration
73      // that was prepared by another processor
74      //
75      final CommandLineCompilerConfiguration cmdLineConfig = (CommandLineCompilerConfiguration) config;
76      //
77      // parse prototype file to determine last header
78      //
79      final Parser parser = createParser(prototype);
80      String[] includes;
81      try {
82        final Reader reader = new BufferedReader(new FileReader(prototype));
83        parser.parse(reader);
84        includes = parser.getIncludes();
85      } catch (final IOException ex) {
86        throw new BuildException("Error parsing precompiled header protoype: " + prototype.toString() + ":"
87            + ex.toString());
88      }
89      if (includes.length == 0) {
90        throw new BuildException("Precompiled header prototype: " + prototype.toString()
91            + " does not contain any include directives.");
92      }
93      final CompilerConfiguration[] configs = new CompilerConfiguration[2];
94      configs[0] = createPrecompileGeneratingConfig(cmdLineConfig, prototype, includes[0]);
95      configs[1] = createPrecompileUsingConfig(cmdLineConfig, prototype, includes[0], exceptFiles);
96      return configs;
97    }
98  
99    abstract protected CompilerConfiguration createPrecompileGeneratingConfig(
100       CommandLineCompilerConfiguration baseConfig, File prototype, String lastInclude);
101 
102   abstract protected CompilerConfiguration createPrecompileUsingConfig(CommandLineCompilerConfiguration baseConfig,
103       File prototype, String lastInclude, String[] exceptFiles);
104 }