summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/sf/antcontrib/cpptasks/ProcessorDef.java
blob: 54a3406d61044dff74fe4f9acbd7b3336f5980fd (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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
 * 
 * Copyright 2002-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.File;
import java.lang.reflect.Method;
import java.util.Vector;
import net.sf.antcontrib.cpptasks.compiler.LinkType;
import net.sf.antcontrib.cpptasks.compiler.Processor;
import net.sf.antcontrib.cpptasks.compiler.ProcessorConfiguration;
import net.sf.antcontrib.cpptasks.types.CommandLineArgument;
import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.DataType;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Reference;
/**
 * An abstract compiler/linker definition.
 * 
 * @author Curt Arnold
 */
public abstract class ProcessorDef extends DataType {
    /**
     * Returns the equivalent Boolean object for the specified value
     * 
     * Equivalent to Boolean.valueOf in JDK 1.4
     * 
     * @param val
     *            boolean value
     * @return Boolean.TRUE or Boolean.FALSE
     */
    protected static Boolean booleanValueOf(boolean val) {
        if (val) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
    /**
     * if true, targets will be built for debugging
     */
    private Boolean debug;
    private Environment env = null;
    /**
     * Reference for "extends" processor definition
     */
    private Reference extendsRef = null;
    /**
     * Name of property that must be present or definition will be ignored. May
     * be null.
     */
    private String ifProp;
    /**
     * if true, processor definition inherits values from containing cc
     * element
     */
    private boolean inherit;
    private Boolean libtool = null;
    protected boolean newEnvironment = false;
    /**
     * Processor.
     */
    private Processor processor;
    /**
     * Collection of <compilerarg>or <linkerarg>contained by definition
     */
    private final Vector processorArgs = new Vector();
    /**
     * Collection of <compilerparam>or <linkerparam>contained by definition
     */
    private final Vector processorParams = new Vector();
    /**
     * if true, all targets will be unconditionally rebuilt
     */
    private Boolean rebuild;
    /**
     * Collection of <fileset>contained by definition
     */
    private final Vector srcSets = new Vector();
    /**
     * Name of property that if present will cause definition to be ignored.
     * May be null.
     */
    private String unlessProp;
    /**
     * Constructor
     *  
     */
    protected ProcessorDef() throws NullPointerException {
        inherit = true;
    }
    /**
     * Adds a <compilerarg>or <linkerarg>
     * 
     * @param arg
     *            command line argument, must not be null
     * @throws NullPointerException
     *             if arg is null
     * @throws BuildException
     *             if this definition is a reference
     */
    protected void addConfiguredProcessorArg(CommandLineArgument arg)
            throws NullPointerException, BuildException {
        if (arg == null) {
            throw new NullPointerException("arg");
        }
        if (isReference()) {
            throw noChildrenAllowed();
        }
        processorArgs.addElement(arg);
    }
    /**
     * Adds a <compilerarg>or <linkerarg>
     * 
     * @param param
     *            command line argument, must not be null
     * @throws NullPointerException
     *             if arg is null
     * @throws BuildException
     *             if this definition is a reference
     */
    protected void addConfiguredProcessorParam(ProcessorParam param)
            throws NullPointerException, BuildException {
        if (param == null) {
            throw new NullPointerException("param");
        }
        if (isReference()) {
            throw noChildrenAllowed();
        }
        processorParams.addElement(param);
    }
    /**
     * Add an environment variable to the launched process.
     */
    public void addEnv(Environment.Variable var) {
        if (env == null) {
            env = new Environment();
        }
        env.addVariable(var);
    }
    /**
     * Adds a source file set.
     * 
     * Files in these set will be processed by this configuration and will not
     * participate in the auction.
     * 
     * @param srcSet
     *            Fileset identifying files that should be processed by this
     *            processor
     * @throws BuildException
     *             if processor definition is a reference
     */
    public void addFileset(ConditionalFileSet srcSet) throws BuildException {
        if (isReference()) {
            throw noChildrenAllowed();
        }
        srcSet.setProject(getProject());
        srcSets.addElement(srcSet);
    }
    /**
     * Creates a configuration
     * 
     * @param baseDef
     *            reference to def from containing cc element, may be null
     * @return configuration
     *  
     */
    public ProcessorConfiguration createConfiguration(CCTask task,
            LinkType linkType, ProcessorDef baseDef, 
			TargetDef targetPlatform, VersionInfo versionInfo) {
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).createConfiguration(task, linkType,
                    baseDef, targetPlatform, versionInfo);
        }
        ProcessorDef[] defaultProviders = getDefaultProviders(baseDef);
        Processor proc = getProcessor(linkType);
        return proc.createConfiguration(task, linkType, defaultProviders, this, targetPlatform, versionInfo);
    }
    /**
     * Prepares list of processor arguments ( compilerarg, linkerarg ) that
     * are active for the current project settings.
     * 
     * @return active compiler arguments
     */
    public CommandLineArgument[] getActiveProcessorArgs() {
    	Project p = getProject();
        if (p == null) {
            throw new java.lang.IllegalStateException("project must be set");
        }
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).getActiveProcessorArgs();
        }
        Vector activeArgs = new Vector(processorArgs.size());
        for (int i = 0; i < processorArgs.size(); i++) {
            CommandLineArgument arg = (CommandLineArgument) processorArgs
                    .elementAt(i);
            if (arg.isActive(p)) {
                activeArgs.addElement(arg);
            }
        }
        CommandLineArgument[] array = new CommandLineArgument[activeArgs.size()];
        activeArgs.copyInto(array);
        return array;
    }
    /**
     * Prepares list of processor arguments ( compilerarg, linkerarg) that
     * are active for the current project settings.
     * 
     * @return active compiler arguments
     */
    public ProcessorParam[] getActiveProcessorParams() {
    	Project p = getProject();
        if (p == null) {
            throw new java.lang.IllegalStateException("project must be set");
        }
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).getActiveProcessorParams();
        }
        Vector activeParams = new Vector(processorParams.size());
        for (int i = 0; i < processorParams.size(); i++) {
            ProcessorParam param = (ProcessorParam) processorParams
                    .elementAt(i);
            if (param.isActive(p)) {
                activeParams.addElement(param);
            }
        }
        ProcessorParam[] array = new ProcessorParam[activeParams.size()];
        activeParams.copyInto(array);
        return array;
    }
    /**
     * Gets boolean indicating debug build
     * 
     * @param defaultProviders
     *            array of ProcessorDef's in descending priority
     * @param index
     *            index to first element in array that should be considered
     * @return if true, built targets for debugging
     */
    public boolean getDebug(ProcessorDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).getDebug(defaultProviders, index);
        }
        if (debug != null) {
            return debug.booleanValue();
        } else {
            if (defaultProviders != null && index < defaultProviders.length) {
                return defaultProviders[index].getDebug(defaultProviders,
                        index + 1);
            }
        }
        return false;
    }
    /**
     * Creates an chain of objects which provide default values in descending
     * order of significance.
     * 
     * @param baseDef
     *            corresponding ProcessorDef from CCTask, will be last element
     *            in array unless inherit = false
     * @return default provider array
     *  
     */
    protected final ProcessorDef[] getDefaultProviders(ProcessorDef baseDef) {
        ProcessorDef extendsDef = getExtends();
        Vector chain = new Vector();
        while (extendsDef != null && !chain.contains(extendsDef)) {
            chain.addElement(extendsDef);
            extendsDef = extendsDef.getExtends();
        }
        if (baseDef != null && getInherit()) {
            chain.addElement(baseDef);
        }
        ProcessorDef[] defaultProviders = new ProcessorDef[chain.size()];
        chain.copyInto(defaultProviders);
        return defaultProviders;
    }
    /**
     * Gets the ProcessorDef specified by the extends attribute
     * 
     * @return Base ProcessorDef, null if extends is not specified
     * @throws BuildException
     *             if reference is not same type object
     */
    public ProcessorDef getExtends() throws BuildException {
        if (extendsRef != null) {
            Object obj = extendsRef.getReferencedObject(getProject());
            if (!getClass().isInstance(obj)) {
                throw new BuildException("Referenced object "
                        + extendsRef.getRefId() + " not correct type, is "
                        + obj.getClass().getName() + " should be "
                        + getClass().getName());
            }
            return (ProcessorDef) obj;
        }
        return null;
    }
    /**
     * Gets the inherit attribute. If the inherit value is true, this processor
     * definition will inherit default values from the containing cc element.
     * 
     * @return if true then properties from the containing <cc>element are
     *         used.
     */
    public final boolean getInherit() {
        return inherit;
    }
    public boolean getLibtool() {
        if (libtool != null) {
            return libtool.booleanValue();
        }
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).getLibtool();
        }
        ProcessorDef extendsDef = getExtends();
        if (extendsDef != null) {
            return extendsDef.getLibtool();
        }
        return false;
    }
    /**
     * Obtains the appropriate processor (compiler, linker)
     * 
     * @return processor
     */
    protected Processor getProcessor() {
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).getProcessor();
        }
        //
        //   if a processor has not been explicitly set
        //      then may be set by an extended definition
        if (processor == null) {
            ProcessorDef extendsDef = getExtends();
            if (extendsDef != null) {
                return extendsDef.getProcessor();
            }
        }
        return processor;
    }

    /**
     * Obtains the appropriate processor (compiler, linker) based on the 
     * LinkType.
     *
     * @return processor
     */
    protected Processor getProcessor(LinkType linkType) {
      // by default ignore the linkType.
      return getProcessor();
    }
    /**
     * Gets a boolean value indicating whether all targets must be rebuilt
     * regardless of dependency analysis.
     * 
     * @param defaultProviders
     *            array of ProcessorDef's in descending priority
     * @param index
     *            index to first element in array that should be considered
     * @return true if all targets should be rebuilt.
     */
    public boolean getRebuild(ProcessorDef[] defaultProviders, int index) {
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).getRebuild(defaultProviders, index);
        }
        if (rebuild != null) {
            return rebuild.booleanValue();
        } else {
            if (defaultProviders != null && index < defaultProviders.length) {
                return defaultProviders[index].getRebuild(defaultProviders,
                        index + 1);
            }
        }
        return false;
    }
    /**
     * Returns true if the processor definition contains embedded file set
     * definitions
     * 
     * @return true if processor definition contains embedded filesets
     */
    public boolean hasFileSets() {
        if (isReference()) {
            return ((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).hasFileSets();
        }
        return srcSets.size() > 0;
    }
    /**
     * Determine if this def should be used.
     * 
     * Definition will be active if the "if" variable (if specified) is set and
     * the "unless" variable (if specified) is not set and that all reference
     * or extended definitions are active
     * 
     * @return true if processor is active
     * @throws IllegalStateException
     *             if not properly initialized
     * @throws BuildException
     *             if "if" or "unless" variable contains suspicious values
     *             "false" or "no" which indicates possible confusion
     */
    public boolean isActive() throws BuildException, IllegalStateException {
        Project project = getProject();
        if (!CUtil.isActive(project, ifProp, unlessProp)) {
            return false;
        }
        if (isReference()) {
            if (!((ProcessorDef) getCheckedRef(ProcessorDef.class,
                    "ProcessorDef")).isActive()) {
                return false;
            }
        }
        //
        //  walk through any extended definitions
        //
        ProcessorDef[] defaultProviders = getDefaultProviders(null);
        for (int i = 0; i < defaultProviders.length; i++) {
            if (!defaultProviders[i].isActive()) {
                return false;
            }
        }
        return true;
    }
    /**
     * Sets the class name for the adapter. Use the "name" attribute when the
     * tool is supported.
     * 
     * @param className
     *            full class name
     *  
     */
    public void setClassname(String className) throws BuildException {
        Object proc = null;
        try {
            Class implClass = ProcessorDef.class.getClassLoader().loadClass(
                    className);
            try {
                Method getInstance = implClass.getMethod("getInstance",
                        new Class[0]);
                proc = getInstance.invoke(null, new Object[0]);
            } catch (Exception ex) {
                proc = implClass.newInstance();
            }
        } catch (Exception ex) {
            throw new BuildException(ex);
        }
        setProcessor((Processor) proc);
    }
    /**
     * If set true, all targets will be built for debugging.
     * 
     * @param debug
     *            true if targets should be built for debugging
     * @throws BuildException
     *             if processor definition is a reference
     */
    public void setDebug(boolean debug) throws BuildException {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.debug = booleanValueOf(debug);
    }
    /**
     * Sets a description of the current data type.
     */
    public void setDescription(String desc) {
        super.setDescription(desc);
    }
    /**
     * Specifies that this element extends the element with id attribute with a
     * matching value. The configuration will be constructed from the settings
     * of this element, element referenced by extends, and the containing cc
     * element.
     * 
     * @param extendsRef
     *            Reference to the extended processor definition.
     * @throws BuildException
     *             if this processor definition is a reference
     */
    public void setExtends(Reference extendsRef) throws BuildException {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.extendsRef = extendsRef;
    }
    /**
     * Sets an id that can be used to reference this element.
     * 
     * @param id
     *            id
     */
    public void setId(String id) {
        //
        //  this is actually accomplished by a different
        //     mechanism, but we can document it
        //
    }
    /**
     * Sets the property name for the 'if' condition.
     * 
     * The configuration will be ignored unless the property is defined.
     * 
     * The value of the property is insignificant, but values that would imply
     * misinterpretation ("false", "no") will throw an exception when
     * evaluated.
     * 
     * @param propName
     *            name of property
     */
    public void setIf(String propName) {
        ifProp = propName;
    }
    /**
     * If inherit has the default value of true, defines, includes and other
     * settings from the containing cc element will be inherited.
     * 
     * @param inherit
     *            new value
     * @throws BuildException
     *             if processor definition is a reference
     */
    public void setInherit(boolean inherit) throws BuildException {
        if (isReference()) {
            throw super.tooManyAttributes();
        }
        this.inherit = inherit;
    }
    /**
     * Set use of libtool.
     * 
     * If set to true, the "libtool " will be prepended to the command line
     * 
     * @param libtool
     *            If true, use libtool.
     */
    public void setLibtool(boolean libtool) {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.libtool = booleanValueOf(libtool);
    }
    /**
     * Do not propagate old environment when new environment variables are
     * specified.
     */
    public void setNewenvironment(boolean newenv) {
        newEnvironment = newenv;
    }
    /**
     * Sets the processor
     * 
     * @param processor
     *            processor, may not be null.
     * @throws BuildException
     *             if ProcessorDef is a reference
     * @throws NullPointerException
     *             if processor is null
     */
    protected void setProcessor(Processor processor) throws BuildException,
            NullPointerException {
        if (processor == null) {
            throw new NullPointerException("processor");
        }
        if (isReference()) {
            throw super.tooManyAttributes();
        }
        if (env == null && !newEnvironment) {
            this.processor = processor;
        } else {
            this.processor = processor.changeEnvironment(newEnvironment, env);
        }
    }
    /**
     * If set true, all targets will be unconditionally rebuilt.
     * 
     * @param rebuild
     *            if true, rebuild all targets.
     * @throws BuildException
     *             if processor definition is a reference
     */
    public void setRebuild(boolean rebuild) throws BuildException {
        if (isReference()) {
            throw tooManyAttributes();
        }
        this.rebuild = booleanValueOf(rebuild);
    }
    /**
     * Specifies that this element should behave as if the content of the
     * element with the matching id attribute was inserted at this location. If
     * specified, no other attributes or child content should be specified,
     * other than "if", "unless" and "description".
     * 
     * @param ref
     *            Reference to other element
     *  
     */
    public void setRefid(org.apache.tools.ant.types.Reference ref) {
        super.setRefid(ref);
    }
    /**
     * Set the property name for the 'unless' condition.
     * 
     * If named property is set, the configuration will be ignored.
     * 
     * The value of the property is insignificant, but values that would imply
     * misinterpretation ("false", "no") of the behavior will throw an
     * exception when evaluated.
     * 
     * @param propName
     *            name of property
     */
    public void setUnless(String propName) {
        unlessProp = propName;
    }
    /**
     * This method calls the FileVistor's visit function for every file in the
     * processors definition
     * 
     * @param visitor
     *            object whose visit method is called for every file
     */
    public void visitFiles(FileVisitor visitor) {
    	Project p = getProject();
        if (p == null) {
            throw new java.lang.IllegalStateException(
                    "project must be set before this call");
        }
        if (isReference()) {
            ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef"))
                    .visitFiles(visitor);
        }
        //
        //   if this processor extends another,
        //      visit its files first
        //
        ProcessorDef extendsDef = getExtends();
        if (extendsDef != null) {
            extendsDef.visitFiles(visitor);
        }
        
        for (int i = 0; i < srcSets.size(); i++) {
            ConditionalFileSet srcSet = (ConditionalFileSet) srcSets
                    .elementAt(i);
            if (srcSet.isActive()) {
                // Find matching source files
                DirectoryScanner scanner = srcSet.getDirectoryScanner(p);
                // Check each source file - see if it needs compilation
                String[] fileNames = scanner.getIncludedFiles();
                File parentDir = scanner.getBasedir();
                for (int j = 0; j < fileNames.length; j++) {
                    String currentFile = fileNames[j];
                    visitor.visit(parentDir, currentFile);
                }
            }
        }
    }
}