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  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  public abstract class AbstractCCompiler
37      extends AbstractCompiler
38  {
39      
40  
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  
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 }