]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/gcc/GccCCompiler.java
Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / Source / Cpptasks / net / sf / antcontrib / cpptasks / gcc / GccCCompiler.java
1 /*
2 *
3 * Copyright 2001-2004 The Ant-Contrib project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package net.sf.antcontrib.cpptasks.gcc;
18 import java.io.File;
19 import java.util.Vector;
20
21 import net.sf.antcontrib.cpptasks.CUtil;
22 import net.sf.antcontrib.cpptasks.compiler.LinkType;
23 import net.sf.antcontrib.cpptasks.compiler.Linker;
24 import net.sf.antcontrib.cpptasks.compiler.Processor;
25 import net.sf.antcontrib.cpptasks.parser.CParser;
26 import net.sf.antcontrib.cpptasks.parser.FortranParser;
27 import net.sf.antcontrib.cpptasks.parser.Parser;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.types.Environment;
31 import net.sf.antcontrib.cpptasks.OptimizationEnum;
32
33 /**
34 * Adapter for the GCC C/C++ compiler
35 *
36 * @author Adam Murdoch
37 */
38 public final class GccCCompiler extends GccCompatibleCCompiler {
39 private final static String[] sourceExtensions = new String[]{".c", /* C */
40 ".cc", /* C++ */
41 ".cpp", /* C++ */
42 ".cxx", /* C++ */
43 ".c++", /* C++ */
44 ".i", /* preprocessed C */
45 ".ii", /* preprocessed C++ */
46 ".f", /* FORTRAN */
47 ".for", /* FORTRAN */
48 ".m", /* Objective-C */
49 ".mm", /* Objected-C++ */
50 ".s" /* Assembly */
51 };
52 private final static String[] headerExtensions = new String[]{".h", ".hpp",
53 ".inl"};
54 private static final GccCCompiler cppInstance = new GccCCompiler("c++",
55 sourceExtensions, headerExtensions, false,
56 new GccCCompiler("c++", sourceExtensions, headerExtensions, true,
57 null, false, null), false, null);
58 private static final GccCCompiler g77Instance = new GccCCompiler("g77",
59 sourceExtensions, headerExtensions, false,
60 new GccCCompiler("g77", sourceExtensions, headerExtensions, true,
61 null, false, null), false, null);
62 private static final GccCCompiler gppInstance = new GccCCompiler("g++",
63 sourceExtensions, headerExtensions, false,
64 new GccCCompiler("g++", sourceExtensions, headerExtensions, true,
65 null, false, null), false, null);
66 private static final GccCCompiler instance = new GccCCompiler("gcc",
67 sourceExtensions, headerExtensions, false,
68 new GccCCompiler("gcc", sourceExtensions, headerExtensions, true,
69 null, false, null), false, null);
70 /**
71 * Gets c++ adapter
72 */
73 public static GccCCompiler getCppInstance() {
74 return cppInstance;
75 }
76 /**
77 * Gets g77 adapter
78 */
79 public static GccCCompiler getG77Instance() {
80 return g77Instance;
81 }
82 /**
83 * Gets gpp adapter
84 */
85 public static GccCCompiler getGppInstance() {
86 return gppInstance;
87 }
88 /**
89 * Gets gcc adapter
90 */
91 public static GccCCompiler getInstance() {
92 return instance;
93 }
94 private String identifier;
95 private File[] includePath;
96 private boolean isPICMeaningful = true;
97 /**
98 * Private constructor. Use GccCCompiler.getInstance() to get singleton
99 * instance of this class.
100 */
101 private GccCCompiler(String command, String[] sourceExtensions,
102 String[] headerExtensions, boolean isLibtool,
103 GccCCompiler libtoolCompiler, boolean newEnvironment,
104 Environment env) {
105 super(command, null, sourceExtensions, headerExtensions, isLibtool,
106 libtoolCompiler, newEnvironment, env);
107 isPICMeaningful = System.getProperty("os.name").indexOf("Windows") < 0;
108 }
109 public void addImpliedArgs(final Vector args,
110 final boolean debug,
111 final boolean multithreaded,
112 final boolean exceptions,
113 final LinkType linkType,
114 final Boolean rtti,
115 final OptimizationEnum optimization,
116 final Boolean defaultflag) {
117 super.addImpliedArgs(args, debug, multithreaded,
118 exceptions, linkType, rtti, optimization, defaultflag);
119 if (isPICMeaningful && linkType.isSharedLibrary()) {
120 args.addElement("-fPIC");
121 }
122 }
123 public Processor changeEnvironment(boolean newEnvironment, Environment env) {
124 if (newEnvironment || env != null) {
125 return new GccCCompiler(getCommand(), this.getSourceExtensions(),
126 this.getHeaderExtensions(), this.getLibtool(),
127 (GccCCompiler) this.getLibtoolCompiler(), newEnvironment,
128 env);
129 }
130 return this;
131 }
132 /**
133 * Create parser to determine dependencies.
134 *
135 * Will create appropriate parser (C++, FORTRAN) based on file extension.
136 *
137 */
138 protected Parser createParser(File source) {
139 if (source != null) {
140 String sourceName = source.getName();
141 int lastDot = sourceName.lastIndexOf('.');
142 if (lastDot >= 0 && lastDot + 1 < sourceName.length()) {
143 char afterDot = sourceName.charAt(lastDot + 1);
144 if (afterDot == 'f' || afterDot == 'F') {
145 return new FortranParser();
146 }
147 }
148 }
149 return new CParser();
150 }
151 public File[] getEnvironmentIncludePath() {
152 if (includePath == null) {
153 //
154 // construct default include path from machine id and version id
155 //
156 String[] defaultInclude = new String[1];
157 StringBuffer buf = new StringBuffer("/lib/");
158 buf.append(GccProcessor.getMachine());
159 buf.append('/');
160 buf.append(GccProcessor.getVersion());
161 buf.append("/include");
162 defaultInclude[0] = buf.toString();
163 //
164 // read specs file and look for -istart and -idirafter
165 //
166 String[] specs = GccProcessor.getSpecs();
167 String[][] optionValues = GccProcessor.parseSpecs(specs, "*cpp:",
168 new String[]{"-isystem ", "-idirafter "});
169 //
170 // if no entries were found, then use a default path
171 //
172 if (optionValues[0].length == 0 && optionValues[1].length == 0) {
173 optionValues[0] = new String[]{"/usr/local/include",
174 "/usr/include", "/usr/include/win32api"};
175 }
176 //
177 // remove mingw entries.
178 // For MinGW compiles this will mean the
179 // location of the sys includes will be
180 // wrong in dependencies.xml
181 // but that should have no significant effect
182 for (int i = 0; i < optionValues.length; i++) {
183 for (int j = 0; j < optionValues[i].length; j++) {
184 if (optionValues[i][j].indexOf("mingw") > 0) {
185 optionValues[i][j] = null;
186 }
187 }
188 }
189 //
190 // if cygwin then
191 // we have to prepend location of gcc32
192 // and .. to start of absolute filenames to
193 // have something that will exist in the
194 // windows filesystem
195 if (GccProcessor.isCygwin()) {
196 GccProcessor.convertCygwinFilenames(optionValues[0]);
197 GccProcessor.convertCygwinFilenames(optionValues[1]);
198 GccProcessor.convertCygwinFilenames(defaultInclude);
199 }
200 int count = CUtil.checkDirectoryArray(optionValues[0]);
201 count += CUtil.checkDirectoryArray(optionValues[1]);
202 count += CUtil.checkDirectoryArray(defaultInclude);
203 includePath = new File[count];
204 int index = 0;
205 for (int i = 0; i < optionValues.length; i++) {
206 for (int j = 0; j < optionValues[i].length; j++) {
207 if (optionValues[i][j] != null) {
208 includePath[index++] = new File(optionValues[i][j]);
209 }
210 }
211 }
212 for (int i = 0; i < defaultInclude.length; i++) {
213 if (defaultInclude[i] != null) {
214 includePath[index++] = new File(defaultInclude[i]);
215 }
216 }
217 }
218 return (File[]) includePath.clone();
219 }
220 public String getIdentifier() throws BuildException {
221 if (identifier == null) {
222 StringBuffer buf;
223 if (getLibtool()) {
224 buf = new StringBuffer("libtool ");
225 } else {
226 buf = new StringBuffer(' ');
227 }
228 buf.append(getCommand());
229 buf.append(' ');
230 buf.append(GccProcessor.getVersion());
231 buf.append(' ');
232 buf.append(GccProcessor.getMachine());
233 identifier = buf.toString();
234 }
235 return identifier;
236 }
237 public Linker getLinker(LinkType linkType) {
238 return GccLinker.getInstance().getLinker(linkType);
239 }
240 public int getMaximumCommandLength() {
241 return Integer.MAX_VALUE;
242 }
243 }