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.borland;
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.OptimizationEnum;
28  import com.github.maven_nar.cpptasks.compiler.CommandLineCompilerConfiguration;
29  import com.github.maven_nar.cpptasks.compiler.CompilerConfiguration;
30  import com.github.maven_nar.cpptasks.compiler.LinkType;
31  import com.github.maven_nar.cpptasks.compiler.Linker;
32  import com.github.maven_nar.cpptasks.compiler.PrecompilingCommandLineCCompiler;
33  import com.github.maven_nar.cpptasks.compiler.Processor;
34  import org.apache.tools.ant.util.FileUtils;
35  
36  /**
37   * Adapter for the Borland(r) C/C++ compiler.
38   *
39   * @author Curt Arnold
40   */
41  public class BorlandCCompiler extends PrecompilingCommandLineCCompiler {
42    private static final String[] headerExtensions = new String[] {
43        ".h", ".hpp", ".inl"
44    };
45    private static final String[] sourceExtensions = new String[] {
46        ".c", ".cc", ".cpp", ".cxx", ".c++"
47    };
48    private static final BorlandCCompiler instance = new BorlandCCompiler(false, null);
49  
50    public static BorlandCCompiler getInstance() {
51      return instance;
52    }
53  
54    private BorlandCCompiler(final boolean newEnvironment, final Environment env) {
55      super("bcc32", "--version", sourceExtensions, headerExtensions, ".obj", false, null, newEnvironment, env);
56    }
57  
58    @Override
59    protected void addImpliedArgs(final Vector<String> args, final boolean debug, final boolean multithreaded,
60        final boolean exceptions, final LinkType linkType, final Boolean rtti, final OptimizationEnum optimization) {
61      args.addElement("-c");
62      //
63      // turn off compiler autodependency since
64      // we do it ourselves
65      args.addElement("-X");
66      if (exceptions) {
67        args.addElement("-x");
68      } else {
69        args.addElement("-x-");
70      }
71      if (multithreaded) {
72        args.addElement("-tWM");
73      }
74      if (debug) {
75        args.addElement("-Od");
76        args.addElement("-v");
77      } else {
78        if (optimization != null) {
79          if (optimization.isSpeed()) {
80            args.addElement("-O1");
81          } else {
82            if (optimization.isSpeed()) {
83              args.addElement("-O2");
84            } else {
85              if (optimization.isNoOptimization()) {
86                args.addElement("-Od");
87              }
88            }
89          }
90        }
91      }
92      if (rtti != null && !rtti.booleanValue()) {
93        args.addElement("-RT-");
94      }
95    }
96  
97    @Override
98    protected void addWarningSwitch(final Vector<String> args, final int level) {
99      BorlandProcessor.addWarningSwitch(args, level);
100   }
101 
102   @Override
103   public Processor changeEnvironment(final boolean newEnvironment, final Environment env) {
104     if (newEnvironment || env != null) {
105       return new BorlandCCompiler(newEnvironment, env);
106     }
107     return this;
108   }
109 
110   @Override
111   protected CompilerConfiguration createPrecompileGeneratingConfig(final CommandLineCompilerConfiguration baseConfig,
112       final File prototype, final String lastInclude) {
113     final String[] additionalArgs = new String[] {
114         "-H=" + lastInclude, "-Hc"
115     };
116     return new CommandLineCompilerConfiguration(baseConfig, additionalArgs, null, true);
117   }
118 
119   @Override
120   protected CompilerConfiguration createPrecompileUsingConfig(final CommandLineCompilerConfiguration baseConfig,
121       final File prototype, final String lastInclude, final String[] exceptFiles) {
122     final String[] additionalArgs = new String[] {
123       "-Hu"
124     };
125     return new CommandLineCompilerConfiguration(baseConfig, additionalArgs, exceptFiles, false);
126   }
127 
128   @Override
129   protected int getArgumentCountPerInputFile() {
130     return 3;
131   }
132 
133   @Override
134   protected String getInputFileArgument(final File outputDir, final String filename, final int index) {
135     switch (index) {
136       case 0:
137         return "-o";
138       case 1:
139         final String outputFileName = getOutputFileNames(filename, null)[0];
140         final String objectName = new File(outputDir, outputFileName).toString();
141         return objectName;
142     }
143     String relative="";
144     try {
145       relative = FileUtils.getRelativePath(workDir, new File(filename));
146     } catch (Exception ex) {
147     }
148     if (relative.isEmpty()) {
149       return filename;
150     } else {
151       return relative;
152     }
153   }
154 
155   @Override
156   protected void getDefineSwitch(final StringBuffer buffer, final String define, final String value) {
157     BorlandProcessor.getDefineSwitch(buffer, define, value);
158   }
159 
160   @Override
161   protected File[] getEnvironmentIncludePath() {
162     return BorlandProcessor.getEnvironmentPath("bcc32", 'I', new String[] {
163       "..\\include"
164     });
165   }
166 
167   @Override
168   protected String getIncludeDirSwitch(final String includeDir) {
169     return BorlandProcessor.getIncludeDirSwitch("-I", includeDir);
170   }
171 
172   @Override
173   public Linker getLinker(final LinkType type) {
174     return BorlandLinker.getInstance().getLinker(type);
175   }
176 
177   @Override
178   public int getMaximumCommandLength() {
179     return 1024;
180   }
181 
182   @Override
183   protected void getUndefineSwitch(final StringBuffer buffer, final String define) {
184     BorlandProcessor.getUndefineSwitch(buffer, define);
185   }
186 }