]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /*\r |
2 | * \r | |
3 | * Copyright 2001-2004 The Ant-Contrib project\r | |
4 | *\r | |
5 | * Licensed under the Apache License, Version 2.0 (the "License");\r | |
6 | * you may not use this file except in compliance with the License.\r | |
7 | * You may obtain a copy of the License at\r | |
8 | *\r | |
9 | * http://www.apache.org/licenses/LICENSE-2.0\r | |
10 | *\r | |
11 | * Unless required by applicable law or agreed to in writing, software\r | |
12 | * distributed under the License is distributed on an "AS IS" BASIS,\r | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r | |
14 | * See the License for the specific language governing permissions and\r | |
15 | * limitations under the License.\r | |
16 | */\r | |
17 | package net.sf.antcontrib.cpptasks;\r | |
18 | import java.io.BufferedReader;\r | |
19 | import java.io.File;\r | |
20 | import java.io.FileReader;\r | |
21 | import java.util.Enumeration;\r | |
22 | import java.util.Vector;\r | |
23 | \r | |
24 | import net.sf.antcontrib.cpptasks.compiler.CommandLineCompiler;\r | |
25 | import net.sf.antcontrib.cpptasks.compiler.Compiler;\r | |
26 | import net.sf.antcontrib.cpptasks.compiler.Processor;\r | |
27 | import net.sf.antcontrib.cpptasks.gcc.GccCCompiler;\r | |
28 | import net.sf.antcontrib.cpptasks.types.CompilerArgument;\r | |
29 | import net.sf.antcontrib.cpptasks.types.ConditionalPath;\r | |
30 | import net.sf.antcontrib.cpptasks.types.DefineSet;\r | |
31 | import net.sf.antcontrib.cpptasks.types.IncludePath;\r | |
32 | import net.sf.antcontrib.cpptasks.types.SystemIncludePath;\r | |
33 | import net.sf.antcontrib.cpptasks.types.UndefineArgument;\r | |
34 | \r | |
35 | import org.apache.tools.ant.BuildException;\r | |
36 | import org.apache.tools.ant.types.EnumeratedAttribute;\r | |
37 | import org.apache.tools.ant.*;\r | |
38 | /**\r | |
39 | * A compiler definition. compiler elements may be placed either as children of\r | |
40 | * a cc element or the project element. A compiler element with an id attribute\r | |
41 | * may be referenced from compiler elements with refid or extends attributes.\r | |
42 | * \r | |
43 | * @author Adam Murdoch\r | |
44 | */\r | |
45 | public final class CompilerDef extends ProcessorDef {\r | |
46 | /**\r | |
47 | * Enumerated attribute with the values "none", "severe", "default",\r | |
48 | * "production", "diagnostic", and "failtask".\r | |
49 | */\r | |
50 | public static class WarningLevel extends EnumeratedAttribute {\r | |
51 | public String[] getValues() {\r | |
52 | return new String[]{"none", "severe", "default", "production",\r | |
53 | "diagnostic", "aserror"};\r | |
54 | }\r | |
55 | }\r | |
56 | /** The source file sets. */\r | |
57 | private final Vector defineSets = new Vector();\r | |
58 | private Boolean exceptions;\r | |
59 | private Boolean rtti;\r | |
60 | private final Vector includePaths = new Vector();\r | |
61 | private Boolean multithreaded;\r | |
62 | private final Vector precompileDefs = new Vector();\r | |
63 | private final Vector sysIncludePaths = new Vector();\r | |
64 | private OptimizationEnum optimization;\r | |
65 | private int warnings = -1;\r | |
66 | private Boolean defaultflag = new Boolean(true);\r | |
67 | public CompilerDef() {\r | |
68 | }\r | |
69 | /**\r | |
70 | * Adds a compiler command-line arg.\r | |
71 | */\r | |
72 | public void addConfiguredCompilerArg(CompilerArgument arg) {\r | |
73 | if (isReference()) {\r | |
74 | throw noChildrenAllowed();\r | |
75 | }\r | |
76 | addConfiguredProcessorArg(arg);\r | |
77 | }\r | |
78 | /**\r | |
79 | * Adds a compiler command-line arg.\r | |
80 | */\r | |
81 | public void addConfiguredCompilerParam(CompilerParam param) {\r | |
82 | if (isReference()) {\r | |
83 | throw noChildrenAllowed();\r | |
84 | }\r | |
85 | addConfiguredProcessorParam(param);\r | |
86 | }\r | |
87 | /**\r | |
88 | * Adds a defineset.\r | |
89 | */\r | |
90 | public void addConfiguredDefineset(DefineSet defs) {\r | |
91 | if (defs == null) {\r | |
92 | throw new NullPointerException("defs");\r | |
93 | }\r | |
94 | if (isReference()) {\r | |
95 | throw noChildrenAllowed();\r | |
96 | }\r | |
97 | defineSets.addElement(defs);\r | |
98 | }\r | |
99 | /**\r | |
100 | * Creates an include path.\r | |
101 | */\r | |
102 | public IncludePath createIncludePath() {\r | |
103 | Project p = getProject();\r | |
104 | if (p == null) {\r | |
105 | throw new java.lang.IllegalStateException("project must be set");\r | |
106 | }\r | |
107 | if (isReference()) {\r | |
108 | throw noChildrenAllowed();\r | |
109 | }\r | |
110 | IncludePath path = new IncludePath(p);\r | |
111 | includePaths.addElement(path);\r | |
112 | return path;\r | |
113 | }\r | |
114 | /**\r | |
115 | * Add a <includepath>or <sysincludepath> if specify the file \r | |
116 | * attribute\r | |
117 | * \r | |
118 | * @throws BuildException\r | |
119 | * if the specify file not exist\r | |
120 | */\r | |
121 | protected void loadFile(Vector activePath,File file) throws BuildException {\r | |
122 | FileReader fileReader;\r | |
123 | BufferedReader in;\r | |
124 | String str;\r | |
125 | if (! file.exists()){\r | |
126 | throw new BuildException("The file " + file + " is not existed");\r | |
127 | }\r | |
128 | try {\r | |
129 | fileReader = new FileReader(file);\r | |
130 | in = new BufferedReader(fileReader);\r | |
131 | while ( (str = in.readLine()) != null ){ \r | |
132 | if(str.trim() == ""){\r | |
133 | continue ;\r | |
134 | }\r | |
135 | str = getProject().replaceProperties(str);\r | |
136 | activePath.addElement(str.trim());\r | |
137 | }\r | |
138 | }\r | |
139 | catch(Exception e){\r | |
140 | throw new BuildException(e.getMessage());\r | |
141 | }\r | |
142 | }\r | |
143 | \r | |
144 | /**\r | |
145 | * Specifies precompilation prototype file and exclusions.\r | |
146 | * \r | |
147 | */\r | |
148 | public PrecompileDef createPrecompile() throws BuildException {\r | |
149 | Project p = getProject();\r | |
150 | if (isReference()) {\r | |
151 | throw noChildrenAllowed();\r | |
152 | }\r | |
153 | PrecompileDef precomp = new PrecompileDef();\r | |
154 | precomp.setProject(p);\r | |
155 | precompileDefs.addElement(precomp);\r | |
156 | return precomp;\r | |
157 | }\r | |
158 | /**\r | |
159 | * Creates a system include path. Locations and timestamps of files located\r | |
160 | * using the system include paths are not used in dependency analysis.\r | |
161 | * \r | |
162 | * \r | |
163 | * Standard include locations should not be specified. The compiler\r | |
164 | * adapters should recognized the settings from the appropriate environment\r | |
165 | * variables or configuration files.\r | |
166 | */\r | |
167 | public SystemIncludePath createSysIncludePath() {\r | |
168 | Project p = getProject();\r | |
169 | if (p == null) {\r | |
170 | throw new java.lang.IllegalStateException("project must be set");\r | |
171 | }\r | |
172 | if (isReference()) {\r | |
173 | throw noChildrenAllowed();\r | |
174 | }\r | |
175 | SystemIncludePath path = new SystemIncludePath(p);\r | |
176 | sysIncludePaths.addElement(path);\r | |
177 | return path;\r | |
178 | }\r | |
179 | public void execute() throws org.apache.tools.ant.BuildException {\r | |
180 | throw new org.apache.tools.ant.BuildException(\r | |
181 | "Not an actual task, but looks like one for documentation purposes");\r | |
182 | }\r | |
183 | public UndefineArgument[] getActiveDefines() {\r | |
184 | Project p = getProject();\r | |
185 | if (p == null) {\r | |
186 | throw new java.lang.IllegalStateException(\r | |
187 | "project must be set before this call");\r | |
188 | }\r | |
189 | if (isReference()) {\r | |
190 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
191 | "CompilerDef")).getActiveDefines();\r | |
192 | }\r | |
193 | Vector actives = new Vector();\r | |
194 | for (int i = 0; i < defineSets.size(); i++) {\r | |
195 | DefineSet currentSet = (DefineSet) defineSets.elementAt(i);\r | |
196 | UndefineArgument[] defines = currentSet.getDefines();\r | |
197 | for (int j = 0; j < defines.length; j++) {\r | |
198 | if (defines[j].isActive(p)) {\r | |
199 | actives.addElement(defines[j]);\r | |
200 | }\r | |
201 | }\r | |
202 | }\r | |
203 | UndefineArgument[] retval = new UndefineArgument[actives.size()];\r | |
204 | actives.copyInto(retval);\r | |
205 | return retval;\r | |
206 | }\r | |
207 | /**\r | |
208 | * Returns the compiler-specific include path.\r | |
209 | */\r | |
210 | public String[] getActiveIncludePaths() {\r | |
211 | if (isReference()) {\r | |
212 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
213 | "CompilerDef")).getActiveIncludePaths();\r | |
214 | }\r | |
215 | return getActivePaths(includePaths);\r | |
216 | }\r | |
217 | private String[] getActivePaths(Vector paths) {\r | |
218 | Project p = getProject();\r | |
219 | if (p == null) {\r | |
220 | throw new java.lang.IllegalStateException("project not set");\r | |
221 | }\r | |
222 | Vector activePaths = new Vector(paths.size());\r | |
223 | for (int i = 0; i < paths.size(); i++) {\r | |
224 | ConditionalPath path = (ConditionalPath) paths.elementAt(i);\r | |
225 | if (path.isActive(p)) {\r | |
226 | if (path.getFile() == null) {\r | |
227 | String[] pathEntries = path.list();\r | |
228 | for (int j = 0; j < pathEntries.length; j++) {\r | |
229 | activePaths.addElement(pathEntries[j]);\r | |
230 | }\r | |
231 | }\r | |
232 | else {\r | |
233 | loadFile(activePaths, path.getFile());\r | |
234 | }\r | |
235 | }\r | |
236 | }\r | |
237 | String[] pathNames = new String[activePaths.size()];\r | |
238 | activePaths.copyInto(pathNames);\r | |
239 | return pathNames;\r | |
240 | }\r | |
241 | public PrecompileDef getActivePrecompile(CompilerDef ccElement) {\r | |
242 | if (isReference()) {\r | |
243 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
244 | "CompilerDef")).getActivePrecompile(ccElement);\r | |
245 | }\r | |
246 | PrecompileDef current = null;\r | |
247 | Enumeration enumPrecompilerDef = precompileDefs.elements();\r | |
248 | while (enumPrecompilerDef.hasMoreElements()) {\r | |
249 | current = (PrecompileDef) enumPrecompilerDef.nextElement();\r | |
250 | if (current.isActive()) {\r | |
251 | return current;\r | |
252 | }\r | |
253 | }\r | |
254 | CompilerDef extendedDef = (CompilerDef) getExtends();\r | |
255 | if (extendedDef != null) {\r | |
256 | current = extendedDef.getActivePrecompile(null);\r | |
257 | if (current != null) {\r | |
258 | return current;\r | |
259 | }\r | |
260 | }\r | |
261 | if (ccElement != null && getInherit()) {\r | |
262 | return ccElement.getActivePrecompile(null);\r | |
263 | }\r | |
264 | return null;\r | |
265 | }\r | |
266 | public String[] getActiveSysIncludePaths() {\r | |
267 | if (isReference()) {\r | |
268 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
269 | "CompilerDef")).getActiveSysIncludePaths();\r | |
270 | }\r | |
271 | return getActivePaths(sysIncludePaths);\r | |
272 | }\r | |
273 | public final boolean getExceptions(CompilerDef[] defaultProviders, int index) {\r | |
274 | if (isReference()) {\r | |
275 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
276 | "CompilerDef")).getExceptions(defaultProviders, index);\r | |
277 | }\r | |
278 | if (exceptions != null) {\r | |
279 | return exceptions.booleanValue();\r | |
280 | } else {\r | |
281 | if (defaultProviders != null && index < defaultProviders.length) {\r | |
282 | return defaultProviders[index].getExceptions(defaultProviders,\r | |
283 | index + 1);\r | |
284 | }\r | |
285 | }\r | |
286 | return false;\r | |
287 | }\r | |
288 | public final Boolean getRtti(CompilerDef[] defaultProviders, int index) {\r | |
289 | if (isReference()) {\r | |
290 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
291 | "CompilerDef")).getRtti(defaultProviders, index);\r | |
292 | }\r | |
293 | if (rtti != null) {\r | |
294 | return rtti;\r | |
295 | } else {\r | |
296 | if (defaultProviders != null && index < defaultProviders.length) {\r | |
297 | return defaultProviders[index].getRtti(defaultProviders,\r | |
298 | index + 1);\r | |
299 | }\r | |
300 | }\r | |
301 | return null;\r | |
302 | }\r | |
303 | public final Boolean getDefaultflag(CompilerDef[] defaultProviders, int index) {\r | |
304 | if (isReference()) {\r | |
305 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
306 | "CompilerDef")).getDefaultflag(defaultProviders, index);\r | |
307 | }\r | |
308 | return defaultflag;\r | |
309 | }\r | |
310 | public final OptimizationEnum getOptimization(CompilerDef[] defaultProviders, int index) {\r | |
311 | if (isReference()) {\r | |
312 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
313 | "CompilerDef")).getOptimization(defaultProviders, index);\r | |
314 | }\r | |
315 | if (optimization != null) {\r | |
316 | return optimization;\r | |
317 | } else {\r | |
318 | if (defaultProviders != null && index < defaultProviders.length) {\r | |
319 | return defaultProviders[index].getOptimization(defaultProviders,\r | |
320 | index + 1);\r | |
321 | }\r | |
322 | }\r | |
323 | return null;\r | |
324 | }\r | |
325 | \r | |
326 | public boolean getMultithreaded(CompilerDef[] defaultProviders, int index) {\r | |
327 | if (isReference()) {\r | |
328 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
329 | "CompilerDef")).getMultithreaded(defaultProviders, index);\r | |
330 | }\r | |
331 | if (multithreaded != null) {\r | |
332 | return multithreaded.booleanValue();\r | |
333 | } else {\r | |
334 | if (defaultProviders != null && index < defaultProviders.length) {\r | |
335 | return defaultProviders[index].getMultithreaded(\r | |
336 | defaultProviders, index + 1);\r | |
337 | }\r | |
338 | }\r | |
339 | return true;\r | |
340 | }\r | |
341 | public Processor getProcessor() {\r | |
342 | Processor processor = super.getProcessor();\r | |
343 | if (processor == null) {\r | |
344 | processor = GccCCompiler.getInstance();\r | |
345 | }\r | |
346 | if (getLibtool() && processor instanceof CommandLineCompiler) {\r | |
347 | CommandLineCompiler compiler = (CommandLineCompiler) processor;\r | |
348 | processor = compiler.getLibtoolCompiler();\r | |
349 | }\r | |
350 | return processor;\r | |
351 | }\r | |
352 | public int getWarnings(CompilerDef[] defaultProviders, int index) {\r | |
353 | if (isReference()) {\r | |
354 | return ((CompilerDef) getCheckedRef(CompilerDef.class,\r | |
355 | "CompilerDef")).getWarnings(defaultProviders, index);\r | |
356 | }\r | |
357 | if (warnings == -1) {\r | |
358 | if (defaultProviders != null && index < defaultProviders.length) {\r | |
359 | return defaultProviders[index].getWarnings(defaultProviders,\r | |
360 | index + 1);\r | |
361 | }\r | |
362 | }\r | |
363 | return warnings;\r | |
364 | }\r | |
365 | /**\r | |
366 | * Sets the default compiler adapter. Use the "name" attribute when the\r | |
367 | * compiler is a supported compiler.\r | |
368 | * \r | |
369 | * @param classname\r | |
370 | * fully qualified classname which implements CompilerAdapter\r | |
371 | */\r | |
372 | public void setClassname(String classname) throws BuildException {\r | |
373 | if (isReference()) {\r | |
374 | throw tooManyAttributes();\r | |
375 | }\r | |
376 | super.setClassname(classname);\r | |
377 | Processor proc = getProcessor();\r | |
378 | if (!(proc instanceof Compiler)) {\r | |
379 | throw new BuildException(classname + " does not implement Compiler");\r | |
380 | }\r | |
381 | }\r | |
382 | /**\r | |
383 | * Enables or disables exception support.\r | |
384 | * \r | |
385 | * @param exceptions\r | |
386 | * if true, exceptions are supported.\r | |
387 | * \r | |
388 | */\r | |
389 | public void setExceptions(boolean exceptions) {\r | |
390 | if (isReference()) {\r | |
391 | throw tooManyAttributes();\r | |
392 | }\r | |
393 | this.exceptions = booleanValueOf(exceptions);\r | |
394 | }\r | |
395 | \r | |
396 | /**\r | |
397 | * Enables or disables run-time type information.\r | |
398 | * \r | |
399 | * @param rtti\r | |
400 | * if true, run-time type information is supported.\r | |
401 | * \r | |
402 | */\r | |
403 | public void setRtti(boolean rtti) {\r | |
404 | if (isReference()) {\r | |
405 | throw tooManyAttributes();\r | |
406 | }\r | |
407 | this.rtti = booleanValueOf(rtti);\r | |
408 | }\r | |
409 | \r | |
410 | /**\r | |
411 | * Enables or disables generation of multithreaded code. Unless specified,\r | |
412 | * multithreaded code generation is enabled.\r | |
413 | * \r | |
414 | * @param multi\r | |
415 | * If true, generated code may be multithreaded.\r | |
416 | */\r | |
417 | public void setMultithreaded(boolean multithreaded) {\r | |
418 | if (isReference()) {\r | |
419 | throw tooManyAttributes();\r | |
420 | }\r | |
421 | this.multithreaded = booleanValueOf(multithreaded);\r | |
422 | }\r | |
423 | /**\r | |
424 | * Sets compiler type.\r | |
425 | * \r | |
426 | * \r | |
427 | * <table width="100%" border="1"> <thead>Supported compilers </thead>\r | |
428 | * <tr>\r | |
429 | * <td>gcc (default)</td>\r | |
430 | * <td>GCC C++ compiler</td>\r | |
431 | * </tr>\r | |
432 | * <tr>\r | |
433 | * <td>g++</td>\r | |
434 | * <td>GCC C++ compiler</td>\r | |
435 | * </tr>\r | |
436 | * <tr>\r | |
437 | * <td>c++</td>\r | |
438 | * <td>GCC C++ compiler</td>\r | |
439 | * </tr>\r | |
440 | * <tr>\r | |
441 | * <td>g77</td>\r | |
442 | * <td>GNU Fortran compiler</td>\r | |
443 | * </tr>\r | |
444 | * <tr>\r | |
445 | * <td>msvc</td>\r | |
446 | * <td>Microsoft Visual C++</td>\r | |
447 | * </tr>\r | |
448 | * <tr>\r | |
449 | * <td>bcc</td>\r | |
450 | * <td>Borland C++ Compiler</td>\r | |
451 | * </tr>\r | |
452 | * <tr>\r | |
453 | * <td>msrc</td>\r | |
454 | * <td>Microsoft Resource Compiler</td>\r | |
455 | * </tr>\r | |
456 | * <tr>\r | |
457 | * <td>brc</td>\r | |
458 | * <td>Borland Resource Compiler</td>\r | |
459 | * </tr>\r | |
460 | * <tr>\r | |
461 | * <td>df</td>\r | |
462 | * <td>Compaq Visual Fortran Compiler</td>\r | |
463 | * </tr>\r | |
464 | * <tr>\r | |
465 | * <td>midl</td>\r | |
466 | * <td>Microsoft MIDL Compiler</td>\r | |
467 | * </tr>\r | |
468 | * <tr>\r | |
469 | * <td>icl</td>\r | |
470 | * <td>Intel C++ compiler for Windows (IA-32)</td>\r | |
471 | * </tr>\r | |
472 | * <tr>\r | |
473 | * <td>ecl</td>\r | |
474 | * <td>Intel C++ compiler for Windows (IA-64)</td>\r | |
475 | * </tr>\r | |
476 | * <tr>\r | |
477 | * <td>icc</td>\r | |
478 | * <td>Intel C++ compiler for Linux (IA-32)</td>\r | |
479 | * </tr>\r | |
480 | * <tr>\r | |
481 | * <td>ecc</td>\r | |
482 | * <td>Intel C++ compiler for Linux (IA-64)</td>\r | |
483 | * </tr>\r | |
484 | * <tr>\r | |
485 | * <td>CC</td>\r | |
486 | * <td>Sun ONE C++ compiler</td>\r | |
487 | * </tr>\r | |
488 | * <tr>\r | |
489 | * <td>aCC</td>\r | |
490 | * <td>HP aC++ C++ Compiler</td>\r | |
491 | * </tr>\r | |
492 | * <tr>\r | |
493 | * <td>os390</td>\r | |
494 | * <td>OS390 C Compiler</td>\r | |
495 | * </tr>\r | |
496 | * <tr>\r | |
497 | * <td>os400</td>\r | |
498 | * <td>Icc Compiler</td>\r | |
499 | * </tr>\r | |
500 | * <tr>\r | |
501 | * <td>sunc89</td>\r | |
502 | * <td>Sun C89 C Compiler</td>\r | |
503 | * </tr>\r | |
504 | * <tr>\r | |
505 | * <td>xlC</td>\r | |
506 | * <td>VisualAge C Compiler</td>\r | |
507 | * </tr>\r | |
508 | * </table>\r | |
509 | * \r | |
510 | */\r | |
511 | public void setName(CompilerEnum name) throws BuildException {\r | |
512 | if (isReference()) {\r | |
513 | throw tooManyAttributes();\r | |
514 | }\r | |
515 | Compiler compiler = name.getCompiler();\r | |
516 | setProcessor(compiler);\r | |
517 | }\r | |
518 | protected void setProcessor(Processor proc) throws BuildException {\r | |
519 | try {\r | |
520 | super.setProcessor((Compiler) proc);\r | |
521 | } catch (ClassCastException ex) {\r | |
522 | throw new BuildException(ex);\r | |
523 | }\r | |
524 | }\r | |
525 | /**\r | |
526 | * Enumerated attribute with the values "none", "severe", "default",\r | |
527 | * "production", "diagnostic", and "failtask".\r | |
528 | */\r | |
529 | public void setWarnings(CompilerDef.WarningLevel level) {\r | |
530 | warnings = level.getIndex();\r | |
531 | }\r | |
532 | /**\r | |
533 | * Sets optimization level.\r | |
534 | * \r | |
535 | * @param value optimization level\r | |
536 | */\r | |
537 | public void setOptimize(OptimizationEnum value) {\r | |
538 | if (isReference()) {\r | |
539 | throw tooManyAttributes();\r | |
540 | }\r | |
541 | this.optimization = value;\r | |
542 | }\r | |
543 | /**\r | |
544 | * Enables or disables default flags.\r | |
545 | * \r | |
546 | * @param defaultflag\r | |
547 | * if true, default flags will add to command line.\r | |
548 | * \r | |
549 | */\r | |
550 | public void setDefaultflag(boolean defaultflag) {\r | |
551 | if (isReference()) {\r | |
552 | throw tooManyAttributes();\r | |
553 | }\r | |
554 | this.defaultflag = booleanValueOf(defaultflag);\r | |
555 | }\r | |
556 | }\r |