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.gcc;
21  
22  import java.io.File;
23  import java.util.Vector;
24  
25  import org.apache.tools.ant.types.Environment;
26  
27  import com.github.maven_nar.cpptasks.CUtil;
28  import com.github.maven_nar.cpptasks.OptimizationEnum;
29  import com.github.maven_nar.cpptasks.compiler.CommandLineCCompiler;
30  import com.github.maven_nar.cpptasks.compiler.LinkType;
31  import org.apache.tools.ant.util.FileUtils;
32  
33  /**
34   * Abstract base class for compilers that attempt to be command line compatible
35   * with GCC
36   *
37   * @author Adam Murdoch
38   * @author Curt Arnold
39   */
40  public abstract class GccCompatibleCCompiler extends CommandLineCCompiler {
41    private final static String[] headerExtensions = new String[] {
42        ".h", ".hpp", ".inl"
43    };
44    private final static String[] sourceExtensions = new String[] {
45        ".c", ".cc", ".cpp", ".cxx", ".c++", ".i", ".f", ".for", ".f90"
46    };
47  
48    /**
49     * Private constructor. Use GccCCompiler.getInstance() to get singleton
50     * instance of this class.
51     */
52    protected GccCompatibleCCompiler(final String command, final String identifierArg, final boolean libtool,
53        final GccCompatibleCCompiler libtoolCompiler, final boolean newEnvironment, final Environment env) {
54      super(command, identifierArg, sourceExtensions, headerExtensions, libtool ? ".fo" : ".o", libtool, libtoolCompiler,
55          newEnvironment, env);
56    }
57  
58    /**
59     * Private constructor. Use GccCCompiler.getInstance() to get singleton
60     * instance of this class.
61     */
62    protected GccCompatibleCCompiler(final String command, final String identifierArg, final String[] sourceExtensions,
63        final String[] headerExtensions, final boolean libtool, final GccCompatibleCCompiler libtoolCompiler,
64        final boolean newEnvironment, final Environment env) {
65      super(command, identifierArg, sourceExtensions, headerExtensions, libtool ? ".fo" : ".o", libtool, libtoolCompiler,
66          newEnvironment, env);
67    }
68  
69    @Override
70    public void addImpliedArgs(final Vector<String> args, final boolean debug, final boolean multithreaded,
71        final boolean exceptions, final LinkType linkType, final Boolean rtti, final OptimizationEnum optimization) {
72      //
73      // -fPIC is too much trouble
74      // users have to manually add it for
75      // operating systems that make sense
76      //
77      args.addElement("-c");
78      if (debug) {
79        args.addElement("-g");
80      } else {
81        if (optimization != null) {
82          if (optimization.isSize()) {
83            args.addElement("-Os");
84          } else if (optimization.isSpeed()) {
85            if ("full".equals(optimization.getValue())) {
86              args.addElement("-O2");
87            } else {
88              if ("speed".equals(optimization.getValue())) {
89                args.addElement("-O1");
90              } else {
91                args.addElement("-O3");
92              }
93            }
94          }
95        }
96      }
97      if (getIdentifier().contains("mingw")) {
98        if (linkType.isSubsystemConsole()) {
99          args.addElement("-mconsole");
100       }
101       if (linkType.isSubsystemGUI()) {
102         args.addElement("-mwindows");
103       }
104     }
105     // BEGINFREEHEP, tests have been modified
106     if (!exceptions) {
107       args.addElement("-fno-exceptions");
108     }
109     // ENDFREEHEP
110     // BEGINFREEHEP moved to GccCCompiler
111     // if (rtti != null && !rtti.booleanValue()) {
112     // args.addElement("-fno-rtti");
113     // }
114     // ENDFREEHEP
115   }
116 
117   /**
118    * Adds an include path to the command.
119    */
120   public void addIncludePath(final String path, final Vector<String> cmd) {
121     cmd.addElement("-I" + path);
122   }
123 
124   @Override
125   public void addWarningSwitch(final Vector<String> args, final int level) {
126     switch (level) {
127       case 0:
128         args.addElement("-w");
129         break;
130       case 5:
131         args.addElement("-Werror");
132         /* nobreak */
133       case 4:
134         args.addElement("-W");
135         /* nobreak */
136       case 3:
137         args.addElement("-Wall");
138         break;
139     }
140   }
141 
142   @Override
143   protected int getArgumentCountPerInputFile() {
144     return 3;
145   }
146 
147   @Override
148   protected String getInputFileArgument(final File outputDir, final String filename, final int index) {
149     switch (index) {
150       case 0:
151         return "-o";
152       case 1:
153         final String outputFileName = getOutputFileNames(filename, null)[0];
154         final String objectName = new File(outputDir, outputFileName).toString();
155         return objectName;
156     }
157     String relative="";
158       try {
159           relative = FileUtils.getRelativePath(workDir, new File(filename));
160       } catch (Exception ex) {
161       }
162       if (relative.isEmpty()) {
163           return filename;
164       } else {
165           return relative;
166       }
167   }
168 
169   @Override
170   public void getDefineSwitch(final StringBuffer buffer, final String define, final String value) {
171     buffer.append("-D");
172     buffer.append(define);
173     if (value != null && value.length() > 0) {
174       buffer.append('=');
175       buffer.append(value);
176     }
177   }
178 
179   @Override
180   protected File[] getEnvironmentIncludePath() {
181     return CUtil.getPathFromEnvironment("INCLUDE", ":");
182   }
183 
184   // Darren Sargent 22Oct2008 - added overloads to properly handle system paths
185   @Override
186   public String getIncludeDirSwitch(final String includeDir) {
187     return getIncludeDirSwitch(includeDir, false);
188   }
189 
190   @Override
191   public String getIncludeDirSwitch(final String includeDir, final boolean isSystem) {
192     if (isSystem) {
193       return "-isystem" + includeDir;
194     } else {
195       return "-I" + includeDir;
196     }
197   }
198 
199   @Override
200   public void getUndefineSwitch(final StringBuffer buffer, final String define) {
201     buffer.append("-U");
202     buffer.append(define);
203   }
204 }