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.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
35
36
37
38
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
50
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
60
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
74
75
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
106 if (!exceptions) {
107 args.addElement("-fno-exceptions");
108 }
109
110
111
112
113
114
115 }
116
117
118
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
133 case 4:
134 args.addElement("-W");
135
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
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 }