View Javadoc
1   package org.codehaus.mojo.natives.c;
2   
3   import java.io.File;
4   import org.codehaus.plexus.util.cli.Commandline;
5   
6   import org.codehaus.mojo.natives.NativeBuildException;
7   import org.codehaus.mojo.natives.compiler.AbstractCompiler;
8   import org.codehaus.mojo.natives.compiler.CompilerConfiguration;
9   import org.codehaus.mojo.natives.parser.Parser;
10  import org.codehaus.mojo.natives.parser.CParser;
11  
12  /*
13   * The MIT License
14   *
15   * Copyright (c) 2004, The Codehaus
16   *
17   * Permission is hereby granted, free of charge, to any person obtaining a copy of
18   * this software and associated documentation files (the "Software"), to deal in
19   * the Software without restriction, including without limitation the rights to
20   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
21   * of the Software, and to permit persons to whom the Software is furnished to do
22   * so, subject to the following conditions:
23   * 
24   * The above copyright notice and this permission notice shall be included in all
25   * copies or substantial portions of the Software.
26   * 
27   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33   * SOFTWARE.
34   */
35  
36  public abstract class AbstractCCompiler
37      extends AbstractCompiler
38  {
39      /**
40       * resuable parser in one Compilation session
41       */
42  
43      private Parser parser = new CParser();
44  
45      protected abstract String getOutputFileOption();
46  
47      protected Parser getParser()
48      {
49          return this.parser;
50      }
51  
52      /**
53       * Setup Compiler Command line
54       */
55      protected Commandline getCommandLine( File srcFile, File destFile, CompilerConfiguration config )
56          throws NativeBuildException
57      {
58  
59          if ( config.getExecutable() == null )
60          {
61              config.setExecutable( "gcc" );
62          }
63  
64          Commandline cl = new Commandline();
65  
66          cl.setExecutable( config.getExecutable() );
67  
68          if ( config.getWorkingDirectory() != null )
69          {
70              cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );
71          }
72  
73          this.setStartOptions( cl, config );
74  
75          this.setIncludePaths( cl, config.getIncludePaths() );
76  
77          this.setIncludePaths( cl, config.getSystemIncludePaths() );
78  
79          this.setMiddleOptions( cl, config );
80  
81          this.setOutputArgs( cl, destFile );
82  
83          this.setSourceArgs( cl, srcFile );
84  
85          this.setEndOptions( cl, config );
86  
87          return cl;
88      }
89  
90      private void setOptions( Commandline cl, String[] options )
91      {
92          if ( options != null )
93          {
94              for ( int i = 0; i < options.length; ++i )
95              {
96                  cl.createArg().setValue( options[i] );
97              }
98          }
99      }
100 
101     private void setStartOptions( Commandline cl, CompilerConfiguration config )
102     {
103         this.setOptions( cl, config.getStartOptions() );
104     }
105 
106     private void setMiddleOptions( Commandline cl, CompilerConfiguration config )
107     {
108         this.setOptions( cl, config.getMiddleOptions() );
109     }
110 
111     private void setEndOptions( Commandline cl, CompilerConfiguration config )
112     {
113         this.setOptions( cl, config.getEndOptions() );
114     }
115 
116     private void setIncludePaths( Commandline cl, File[] includePaths )
117     {
118         if ( includePaths != null )
119         {
120             for ( int i = 0; i < includePaths.length; ++i )
121             {
122                 cl.createArg().setValue( "-I" + includePaths[i].getPath() );
123             }
124         }
125     }
126 
127     private void setOutputArgs( Commandline cl, File outputFile )
128     {
129         String outputFileOption = this.getOutputFileOption();
130 
131         if ( outputFileOption.endsWith( " " ) )
132         {
133             cl.createArg().setValue( outputFileOption.trim() );
134             cl.createArg().setValue( outputFile.getPath() );
135         }
136         else
137         {
138             cl.createArg().setValue( outputFileOption + outputFile.getPath() );
139         }
140     }
141 
142     private void setSourceArgs( Commandline cl, File srcFile )
143     {
144         cl.createArg().setValue( "-c" );
145         cl.createArg().setValue( srcFile.getPath() );
146     }
147 }