View Javadoc
1   package org.codehaus.mojo.natives.mingw;
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  public abstract class AbstractGccCompiler extends AbstractCompiler {
36  
37      /**
38       * resuable parser in one Compilation session
39       */
40  
41      private Parser parser = new CParser();
42  
43      protected abstract String getOutputFileOption();
44  
45      protected Parser getParser() {
46          return this.parser;
47      }
48  
49      /**
50       * Setup Compiler Command line
51       */
52      protected Commandline getCommandLine(File srcFile, File destFile, CompilerConfiguration config)
53              throws NativeBuildException {
54  
55          if (config.getExecutable() == null) {
56              config.setExecutable("gcc");
57          }
58  
59          Commandline cl = new Commandline();
60  
61          cl.setExecutable(config.getExecutable());
62  
63          if (config.getWorkingDirectory() != null) {
64              cl.setWorkingDirectory(config.getWorkingDirectory().getPath());
65          }
66  
67          this.setStartOptions(cl, config);
68  
69          this.setIncludePaths(cl, config.getIncludePaths());
70  
71          this.setIncludePaths(cl, config.getSystemIncludePaths());
72  
73          this.setMiddleOptions(cl, config);
74  
75          this.setOutputArgs(cl, destFile);
76  
77          this.setSourceArgs(cl, srcFile);
78  
79          this.setEndOptions(cl, config);
80  
81          return cl;
82      }
83  
84      private void setOptions(Commandline cl, String[] options) {
85          if (options != null) {
86              for (int i = 0; i < options.length; ++i) {
87                  cl.createArg().setValue(options[i]);
88              }
89          }
90      }
91  
92      private void setStartOptions(Commandline cl, CompilerConfiguration config) {
93          this.setOptions(cl, config.getStartOptions());
94      }
95  
96      private void setMiddleOptions(Commandline cl, CompilerConfiguration config) {
97          this.setOptions(cl, config.getMiddleOptions());
98      }
99  
100     private void setEndOptions(Commandline cl, CompilerConfiguration config) {
101         this.setOptions(cl, config.getEndOptions());
102     }
103 
104     private void setIncludePaths(Commandline cl, File[] includePaths) {
105         if (includePaths != null) {
106             for (int i = 0; i < includePaths.length; ++i) {
107                 cl.createArg().setValue("-I" + includePaths[i].getPath());
108             }
109         }
110     }
111 
112     private void setOutputArgs(Commandline cl, File outputFile) {
113         String outputFileOption = this.getOutputFileOption();
114 
115         if (outputFileOption.endsWith(" ")) {
116             cl.createArg().setValue(outputFileOption.trim());
117             cl.createArg().setValue(outputFile.getPath());
118         } else {
119             cl.createArg().setValue(outputFileOption + outputFile.getPath());
120         }
121     }
122 
123     private void setSourceArgs(Commandline cl, File srcFile) {
124         cl.createArg().setValue("-c");
125         cl.createArg().setValue(srcFile.getPath());
126     }
127 }