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.msvc;
21  
22  import java.io.File;
23  import java.util.Vector;
24  
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.types.Environment;
27  
28  import com.github.maven_nar.cpptasks.CUtil;
29  import com.github.maven_nar.cpptasks.OptimizationEnum;
30  import com.github.maven_nar.cpptasks.compiler.CommandLineCompilerConfiguration;
31  import com.github.maven_nar.cpptasks.compiler.CompilerConfiguration;
32  import com.github.maven_nar.cpptasks.compiler.LinkType;
33  import com.github.maven_nar.cpptasks.compiler.PrecompilingCommandLineCCompiler;
34  import org.apache.tools.ant.util.FileUtils;
35  
36  /**
37   * An abstract base class for compilers that are basically command line
38   * compatible with Microsoft(r) C/C++ Optimizing Compiler
39   *
40   * @author Curt Arnold
41   */
42  public abstract class MsvcCompatibleCCompiler extends PrecompilingCommandLineCCompiler {
43    private static String[] mflags = new String[] {
44        //
45        // first four are single-threaded
46        // (runtime=static,debug=false), (..,debug=true),
47        // (runtime=dynamic,debug=true), (..,debug=false), (not supported)
48        // next four are multi-threaded, same sequence
49        "/ML", "/MLd", null, null, "/MT", "/MTd", "/MD", "/MDd"
50    };
51  
52    protected MsvcCompatibleCCompiler(final String command, final String identifierArg, final boolean newEnvironment,
53        final Environment env) {
54      super(command, identifierArg, new String[] {
55          ".c", ".cc", ".cpp", ".cxx", ".c++"
56      }, new String[] {
57          ".h", ".hpp", ".inl"
58      }, ".obj", false, null, newEnvironment, env);
59    }
60  
61    protected void addDebugSwitch(final Vector<String> args) {
62      args.addElement("/Zi");
63      args.addElement("/Od");
64      args.addElement("/RTC1");
65      args.addElement("/D_DEBUG");
66    }
67  
68    protected void addPathSwitch(final Vector<String> args) {
69      args.addElement("/Fd" + objDir.getAbsolutePath() + File.separator); // vc[version].pdb
70    }
71  
72    @Override
73    protected void addImpliedArgs(final Vector<String> args, final boolean debug, final boolean multithreaded,
74        final boolean exceptions, final LinkType linkType, final Boolean rtti, final OptimizationEnum optimization) {
75      args.addElement("/c");
76      args.addElement("/nologo");
77      if (exceptions) {
78        // changed to eliminate warning on VC 2005, should support VC 6 and later
79        // use /GX to support VC5 - 2005 (with warning)
80        args.addElement("/EHsc");
81      }
82      int mindex = 0;
83      if (multithreaded) {
84        mindex += 4;
85      }
86      final boolean staticRuntime = linkType.isStaticRuntime();
87      if (!staticRuntime) {
88        mindex += 2;
89      }
90      if (debug) {
91        mindex += 1;
92        addDebugSwitch(args);
93      } else {
94        if (optimization != null) {
95          if (optimization.isSize()) {
96            args.addElement("/O1");
97          }
98          if (optimization.isSpeed()) {
99            args.addElement("/O2");
100         }
101       }
102       args.addElement("/DNDEBUG");
103     }
104     final String mflag = mflags[mindex];
105     if (mflag == null) {
106       throw new BuildException("multithread='false' and runtime='dynamic' not supported");
107     }
108     args.addElement(mflag);
109     if (rtti != null && rtti.booleanValue()) {
110       args.addElement("/GR");
111     } else {
112       // added by Darren Sargent, 21Mar2008 -- /GR is default so need
113       // /GR- to disable it
114       args.addElement("/GR-");
115     }
116     addPathSwitch(args);
117   }
118 
119   @Override
120   protected void addWarningSwitch(final Vector<String> args, final int level) {
121     MsvcProcessor.addWarningSwitch(args, level);
122   }
123 
124   @Override
125   protected CompilerConfiguration createPrecompileGeneratingConfig(final CommandLineCompilerConfiguration baseConfig,
126       final File prototype, final String lastInclude) {
127     final String[] additionalArgs = new String[] {
128         "/Fp" + CUtil.getBasename(prototype) + ".pch", "/Yc"
129     };
130     // FREEHEP FIXME we may need /Yd here, but only in debug mode, how do we
131     // find out?
132     return new CommandLineCompilerConfiguration(baseConfig, additionalArgs, null, true);
133   }
134 
135   @Override
136   protected CompilerConfiguration createPrecompileUsingConfig(final CommandLineCompilerConfiguration baseConfig,
137       final File prototype, final String lastInclude, final String[] exceptFiles) {
138     final String[] additionalArgs = new String[] {
139         "/Fp" + CUtil.getBasename(prototype) + ".pch", "/Yu" + lastInclude
140     };
141 
142     return new CommandLineCompilerConfiguration(baseConfig, additionalArgs, exceptFiles, false);
143   }
144 
145   @Override
146   protected int getArgumentCountPerInputFile() {
147     return 2;
148   }
149 
150   @Override
151   protected String getInputFileArgument(final File outputDir, final String filename, final int index) {
152     if (index == 0) {
153       final String outputFileName = getOutputFileNames(filename, null)[0];
154       final String fullOutputName = new File(outputDir, outputFileName).toString();
155       return "/Fo" + fullOutputName;
156     }
157 
158     String relative="";
159       try {
160         relative = FileUtils.getRelativePath(workDir, new File(filename));
161       } catch (Exception ex) {
162       }
163       if (relative.isEmpty()) {
164           return filename;
165       } else {
166           return relative;
167       }
168   }
169 
170   @Override
171   protected void getDefineSwitch(final StringBuffer buffer, final String define, final String value) {
172     MsvcProcessor.getDefineSwitch(buffer, define, value);
173   }
174 
175   @Override
176   protected File[] getEnvironmentIncludePath() {
177     return CUtil.getPathFromEnvironment("INCLUDE", ";");
178   }
179 
180   @Override
181   protected String getIncludeDirSwitch(final String includeDir) {
182     return MsvcProcessor.getIncludeDirSwitch(includeDir);
183   }
184 
185   @Override
186   protected void getUndefineSwitch(final StringBuffer buffer, final String define) {
187     MsvcProcessor.getUndefineSwitch(buffer, define);
188   }
189 }