summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/CompilerDef.java
blob: 0a92a51512e4a952b04921048770e26e6fdffe2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * 
 * Copyright 2001-2004 The Ant-Contrib project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sf.antcontrib.cpptasks;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Enumeration;
import java.util.Vector;

import net.sf.antcontrib.cpptasks.compiler.CommandLineCompiler;
import net.sf.antcontrib.cpptasks.compiler.Compiler;
import net.sf.antcontrib.cpptasks.compiler.Processor;
import net.sf.antcontrib.cpptasks.gcc.GccCCompiler;
import net.sf.antcontrib.cpptasks.types.CompilerArgument;
import net.sf.antcontrib.cpptasks.types.ConditionalPath;
import net.sf.antcontrib.cpptasks.types.DefineSet;
import net.sf.antcontrib.cpptasks.types.IncludePath;
import net.sf.antcontrib.cpptasks.types.SystemIncludePath;
import net.sf.antcontrib.cpptasks.types.UndefineArgument;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.*;
/**
 * A compiler definition. compiler elements may be placed either as children of
 * a cc element or the project element. A compiler element with an id attribute
 * may be referenced from compiler elements with refid or extends attributes.
 * 
 * @author Adam Murdoch
 */
public final class CompilerDef extends ProcessorDef {
    /**
     * Enumerated attribute with the values "none", "severe", "default",
     * "production", "diagnostic", and "failtask".
     */
    public static class WarningLevel extends EnumeratedAttribute {
        public String[] getValues() {
            return new String[]{"none", "severe", "default", "production",
                    "diagnostic", "aserror"};
        }
    }
    /** The source file sets. */
    private final Vector defineSets = new Vector();
    private Boolean exceptions;
    private Boolean rtti;
    private final Vector includePaths = new Vector();
    private Boolean multithreaded;
    private final Vector precompileDefs = new Vector();
    private final Vector sysIncludePaths = new Vector();
    private OptimizationEnum optimization;
    private int warnings = -1;
    private Boolean defaultflag = new Boolean(true);
    public CompilerDef() {
    }
    /**
     * Adds a compiler command-line arg.
     */
    public void addConfiguredCompilerArg(CompilerArgument arg) {
        if (isReference()) {
            throw noChildrenAllowed();
        }
        addConfiguredProcessorArg(arg);
    }
    /**
     * Adds a compiler command-line arg.
     */
    public void addConfiguredCompilerParam(CompilerParam param) {
        if (isReference()) {
            throw noChildrenAllowed();
        }
        addConfiguredProcessorParam(param);
    }
    /**
     * Adds a defineset.
     */
    public void addConfiguredDefineset(DefineSet defs) {
        if (defs == null) {
            throw new NullPointerException("defs");
        }
        if (isReference()) {
            throw noChildrenAllowed();
        }
        defineSets.addElement(defs);
    }
    /**
     * Creates an include path.
     */
    public IncludePath createIncludePath() {
        Project p = getProject();
        if (p == null) {
            throw new java.lang.IllegalStateException("project must be set");
        }
        if (isReference()) {
            throw noChildrenAllowed();
        }
        IncludePath path = new IncludePath(p);
        includePaths.addElement(path);
        return path;
    }
    /**
     * Add a <includepath>or <sysincludepath> if specify the file 
     * attribute
     * 
     * @throws BuildException
     *             if the specify file not exist
     */
    protected void loadFile(Vector activePath,File file) throws BuildException {
        FileReader fileReader;
        BufferedReader in;
        String str;
        if (! file.exists()){
            throw new BuildException("The file " + file + " is not existed");
        }
        try {
            fileReader = new FileReader(file);
            in = new BufferedReader(fileReader);
            while ( (str = in.readLine()) != null ){                
                if(str.trim() == ""){
                    continue ;
                }
                str = getProject().replaceProperties(str);
                activePath.addElement(str.trim());
            }
        }
        catch(Exception e){
            throw new BuildException(e.getMessage());
        }
    }
    
    /**
     * Specifies precompilation prototype file and exclusions.
     *  
     */
    public PrecompileDef createPrecompile() throws BuildException {
    	Project p = getProject();
        if (isReference()) {
            throw noChildrenAllowed();
        }
        PrecompileDef precomp = new PrecompileDef();
        precomp.setProject(p);
        precompileDefs.addElement(precomp);
        return precomp;
    }
    /**
     * Creates a system include path. Locations and timestamps of files located
     * using the system include paths are not used in dependency analysis.
     * 
     * 
     * Standard include locations should not be specified. The compiler
     * adapters should recognized the settings from the appropriate environment
     * variables or configuration files.
     */
    public SystemIncludePath createSysIncludePath() {
    	Project p = getProject();
        if (p == null) {
            throw new java.lang.IllegalStateException("project must be set");
        }
        if (isReference()) {
            throw noChildrenAllowed();
        }
        SystemIncludePath path = new SystemIncludePath(p);
        sysIncludePaths.addElement(path);
        return path;
    }
    public void execute() throws org.apache.tools.ant.BuildException {
        throw new org.apache.tools.ant.BuildException(
                "Not an actual task, but looks like one for documentation purposes");
    }
    public UndefineArgument[] getActiveDefines() {
    	Project p = getProject();
        if (p == null) {
            throw new java.lang.IllegalStateException(
                    "project must be set before this call");
        }
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getActiveDefines();
        }
        Vector actives = new Vector();
        for (int i = 0; i < defineSets.size(); i++) {
            DefineSet currentSet = (DefineSet) defineSets.elementAt(i);
            UndefineArgument[] defines = currentSet.getDefines();
            for (int j = 0; j < defines.length; j++) {
                if (defines[j].isActive(p)) {
                    actives.addElement(defines[j]);
                }
            }
        }
        UndefineArgument[] retval = new UndefineArgument[actives.size()];
        actives.copyInto(retval);
        return retval;
    }
    /**
     * Returns the compiler-specific include path.
     */
    public String[] getActiveIncludePaths() {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getActiveIncludePaths();
        }
        return getActivePaths(includePaths);
    }
    private String[] getActivePaths(Vector paths) {
    	Project p = getProject();
        if (p == null) {
            throw new java.lang.IllegalStateException("project not set");
        }
        Vector activePaths = new Vector(paths.size());
        for (int i = 0; i < paths.size(); i++) {
            ConditionalPath path = (ConditionalPath) paths.elementAt(i);
            if (path.isActive(p)) {
                if (path.getFile() == null) {
                    String[] pathEntries = path.list();
                    for (int j = 0; j < pathEntries.length; j++) {
                        activePaths.addElement(pathEntries[j]);
                    }
                }
                else {
                    loadFile(activePaths, path.getFile());
                }
            }
        }
        String[] pathNames = new String[activePaths.size()];
        activePaths.copyInto(pathNames);
        return pathNames;
    }
    public PrecompileDef getActivePrecompile(CompilerDef ccElement) {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getActivePrecompile(ccElement);
        }
        PrecompileDef current = null;
        Enumeration enumPrecompilerDef = precompileDefs.elements();
        while (enumPrecompilerDef.hasMoreElements()) {
            current = (PrecompileDef) enumPrecompilerDef.nextElement();
            if (current.isActive()) {
                return current;
            }
        }
        CompilerDef extendedDef = (CompilerDef) getExtends();
        if (extendedDef != null) {
            current = extendedDef.getActivePrecompile(null);
            if (current != null) {
                return current;
            }
        }
        if (ccElement != null && getInherit()) {
            return ccElement.getActivePrecompile(null);
        }
        return null;
    }
    public String[] getActiveSysIncludePaths() {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getActiveSysIncludePaths();
        }
        return getActivePaths(sysIncludePaths);
    }
    public final boolean getExceptions(CompilerDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getExceptions(defaultProviders, index);
        }
        if (exceptions != null) {
            return exceptions.booleanValue();
        } else {
            if (defaultProviders != null && index < defaultProviders.length) {
                return defaultProviders[index].getExceptions(defaultProviders,
                        index + 1);
            }
        }
        return false;
    }
    public final Boolean getRtti(CompilerDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getRtti(defaultProviders, index);
        }
        if (rtti != null) {
            return rtti;
        } else {
            if (defaultProviders != null && index < defaultProviders.length) {
                return defaultProviders[index].getRtti(defaultProviders,
                        index + 1);
            }
        }
        return null;
    }
    public final Boolean getDefaultflag(CompilerDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                  "CompilerDef")).getDefaultflag(defaultProviders, index);
        }
        return defaultflag;
    }
    public final OptimizationEnum getOptimization(CompilerDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getOptimization(defaultProviders, index);
        }
        if (optimization != null) {
            return optimization;
        } else {
            if (defaultProviders != null && index < defaultProviders.length) {
                return defaultProviders[index].getOptimization(defaultProviders,
                        index + 1);
            }
        }
        return null;
    }
    
    public boolean getMultithreaded(CompilerDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getMultithreaded(defaultProviders, index);
        }
        if (multithreaded != null) {
            return multithreaded.booleanValue();
        } else {
            if (defaultProviders != null && index < defaultProviders.length) {
                return defaultProviders[index].getMultithreaded(
                        defaultProviders, index + 1);
            }
        }
        return true;
    }
    public Processor getProcessor() {
        Processor processor = super.getProcessor();
        if (processor == null) {
            processor = GccCCompiler.getInstance();
        }
        if (getLibtool() && processor instanceof CommandLineCompiler) {
            CommandLineCompiler compiler = (CommandLineCompiler) processor;
            processor = compiler.getLibtoolCompiler();
        }
        return processor;
    }
    public int getWarnings(CompilerDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((CompilerDef) getCheckedRef(CompilerDef.class,
                    "CompilerDef")).getWarnings(defaultProviders, index);
        }
        if (warnings == -1) {
            if (defaultProviders != null && index < defaultProviders.length) {
                return defaultProviders[index].getWarnings(defaultProviders,
                        index + 1);
            }
        }
        return warnings;
    }
    /**
     * Sets the default compiler adapter. Use the "name" attribute when the
     * compiler is a supported compiler.
     * 
     * @param classname
     *            fully qualified classname which implements CompilerAdapter
     */
    public void setClassname(String classname) throws BuildException {
        if (isReference()) {
            throw tooManyAttributes();
        }
        super.setClassname(classname);
        Processor proc = getProcessor();
        if (!(proc instanceof Compiler)) {
            throw new BuildException(classname + " does not implement Compiler");
        }
    }
    /**
     * Enables or disables exception support.
     * 
     * @param exceptions
     *            if true, exceptions are supported.
     *  
     */
    public void setExceptions(boolean exceptions) {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.exceptions = booleanValueOf(exceptions);
    }

    /**
     * Enables or disables run-time type information.
     * 
     * @param rtti
     *            if true, run-time type information is supported.
     *  
     */
    public void setRtti(boolean rtti) {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.rtti = booleanValueOf(rtti);
    }
    
    /**
     * Enables or disables generation of multithreaded code. Unless specified,
     * multithreaded code generation is enabled.
     * 
     * @param multi
     *            If true, generated code may be multithreaded.
     */
    public void setMultithreaded(boolean multithreaded) {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.multithreaded = booleanValueOf(multithreaded);
    }
    /**
     * Sets compiler type.
     * 
     * 
     * <table width="100%" border="1"> <thead>Supported compilers </thead>
     * <tr>
     * <td>gcc (default)</td>
     * <td>GCC C++ compiler</td>
     * </tr>
     * <tr>
     * <td>g++</td>
     * <td>GCC C++ compiler</td>
     * </tr>
     * <tr>
     * <td>c++</td>
     * <td>GCC C++ compiler</td>
     * </tr>
     * <tr>
     * <td>g77</td>
     * <td>GNU Fortran compiler</td>
     * </tr>
     * <tr>
     * <td>msvc</td>
     * <td>Microsoft Visual C++</td>
     * </tr>
     * <tr>
     * <td>bcc</td>
     * <td>Borland C++ Compiler</td>
     * </tr>
     * <tr>
     * <td>msrc</td>
     * <td>Microsoft Resource Compiler</td>
     * </tr>
     * <tr>
     * <td>brc</td>
     * <td>Borland Resource Compiler</td>
     * </tr>
     * <tr>
     * <td>df</td>
     * <td>Compaq Visual Fortran Compiler</td>
     * </tr>
     * <tr>
     * <td>midl</td>
     * <td>Microsoft MIDL Compiler</td>
     * </tr>
     * <tr>
     * <td>icl</td>
     * <td>Intel C++ compiler for Windows (IA-32)</td>
     * </tr>
     * <tr>
     * <td>ecl</td>
     * <td>Intel C++ compiler for Windows (IA-64)</td>
     * </tr>
     * <tr>
     * <td>icc</td>
     * <td>Intel C++ compiler for Linux (IA-32)</td>
     * </tr>
     * <tr>
     * <td>ecc</td>
     * <td>Intel C++ compiler for Linux (IA-64)</td>
     * </tr>
     * <tr>
     * <td>CC</td>
     * <td>Sun ONE C++ compiler</td>
     * </tr>
     * <tr>
     * <td>aCC</td>
     * <td>HP aC++ C++ Compiler</td>
     * </tr>
     * <tr>
     * <td>os390</td>
     * <td>OS390 C Compiler</td>
     * </tr>
     * <tr>
     * <td>os400</td>
     * <td>Icc Compiler</td>
     * </tr>
     * <tr>
     * <td>sunc89</td>
     * <td>Sun C89 C Compiler</td>
     * </tr>
     * <tr>
     * <td>xlC</td>
     * <td>VisualAge C Compiler</td>
     * </tr>
     * </table>
     *  
     */
    public void setName(CompilerEnum name) throws BuildException {
        if (isReference()) {
            throw tooManyAttributes();
        }
        Compiler compiler = name.getCompiler();
        setProcessor(compiler);
    }
    protected void setProcessor(Processor proc) throws BuildException {
        try {
            super.setProcessor((Compiler) proc);
        } catch (ClassCastException ex) {
            throw new BuildException(ex);
        }
    }
    /**
     * Enumerated attribute with the values "none", "severe", "default",
     * "production", "diagnostic", and "failtask".
     */
    public void setWarnings(CompilerDef.WarningLevel level) {
        warnings = level.getIndex();
    }
    /**
     * Sets optimization level.
     * 
     * @param value optimization level
     */
    public void setOptimize(OptimizationEnum value) {
    	if (isReference()) {
    		throw tooManyAttributes();
    	}
    	this.optimization = value;
    }
    /**
     * Enables or disables default flags.
     * 
     * @param defaultflag
     *            if true, default flags will add to command line.
     *  
     */
    public void setDefaultflag(boolean defaultflag) {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.defaultflag = booleanValueOf(defaultflag);
    }
}