1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
38
39
40
41
42 public abstract class MsvcCompatibleCCompiler extends PrecompilingCommandLineCCompiler {
43 private static String[] mflags = new String[] {
44
45
46
47
48
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);
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
79
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
113
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
131
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 }