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;
21  
22  import java.io.File;
23  import java.lang.reflect.Method;
24  import java.util.Vector;
25  
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.DirectoryScanner;
28  import org.apache.tools.ant.Project;
29  import org.apache.tools.ant.types.DataType;
30  import org.apache.tools.ant.types.Environment;
31  import org.apache.tools.ant.types.Reference;
32  
33  import com.github.maven_nar.cpptasks.compiler.LinkType;
34  import com.github.maven_nar.cpptasks.compiler.Processor;
35  import com.github.maven_nar.cpptasks.compiler.ProcessorConfiguration;
36  import com.github.maven_nar.cpptasks.types.CommandLineArgument;
37  import com.github.maven_nar.cpptasks.types.ConditionalFileSet;
38  
39  
40  
41  
42  
43  
44  public abstract class ProcessorDef extends DataType {
45    
46  
47  
48  
49  
50  
51  
52  
53  
54    protected static Boolean booleanValueOf(final boolean val) {
55      if (val) {
56        return Boolean.TRUE;
57      }
58      return Boolean.FALSE;
59    }
60  
61    
62  
63  
64    private Boolean debug;
65    private Environment env = null;
66    
67  
68  
69    private Reference extendsRef = null;
70    
71  
72  
73  
74    private String ifProp;
75    
76  
77  
78  
79    private boolean inherit;
80    private Boolean libtool = null;
81    protected boolean newEnvironment = false;
82    
83  
84  
85    private Processor processor;
86    
87  
88  
89    private final Vector processorArgs = new Vector();
90    
91  
92  
93    private final Vector processorParams = new Vector();
94    
95  
96  
97    private Boolean rebuild;
98    
99  
100 
101   private final Vector srcSets = new Vector();
102   
103 
104 
105 
106   private String unlessProp;
107 
108   
109 
110 
111 
112   protected ProcessorDef() throws NullPointerException {
113     this.inherit = true;
114   }
115 
116   
117 
118 
119 
120 
121 
122 
123 
124 
125 
126   protected void addConfiguredProcessorArg(final CommandLineArgument arg) throws NullPointerException, BuildException {
127     if (arg == null) {
128       throw new NullPointerException("arg");
129     }
130     if (isReference()) {
131       throw noChildrenAllowed();
132     }
133     this.processorArgs.addElement(arg);
134   }
135 
136   
137 
138 
139 
140 
141 
142 
143 
144 
145 
146   protected void addConfiguredProcessorParam(final ProcessorParam param) throws NullPointerException, BuildException {
147     if (param == null) {
148       throw new NullPointerException("param");
149     }
150     if (isReference()) {
151       throw noChildrenAllowed();
152     }
153     this.processorParams.addElement(param);
154   }
155 
156   
157 
158 
159   public void addEnv(final Environment.Variable var) {
160     if (this.env == null) {
161       this.env = new Environment();
162       this.newEnvironment = true;
163       if (this.processor != null) {
164         
165         setProcessor(this.processor);
166       }
167     }
168     this.env.addVariable(var);
169   }
170 
171   
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183   public void addFileset(final ConditionalFileSet srcSet) throws BuildException {
184     if (isReference()) {
185       throw noChildrenAllowed();
186     }
187     srcSet.setProject(getProject());
188     this.srcSets.addElement(srcSet);
189   }
190 
191   
192 
193 
194 
195 
196 
197 
198 
199   public ProcessorConfiguration createConfiguration(final CCTask task, final LinkType linkType,
200       final ProcessorDef baseDef, final TargetDef targetPlatform, final VersionInfo versionInfo) {
201     if (isReference()) {
202       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).createConfiguration(task, linkType,
203           baseDef, targetPlatform, versionInfo);
204     }
205     final ProcessorDef[] defaultProviders = getDefaultProviders(baseDef);
206     final Processor proc = getProcessor(linkType);
207     return proc.createConfiguration(task, linkType, defaultProviders, this, targetPlatform, versionInfo);
208   }
209 
210   
211 
212 
213 
214 
215 
216   public CommandLineArgument[] getActiveProcessorArgs() {
217     final Project p = getProject();
218     if (p == null) {
219       throw new java.lang.IllegalStateException("project must be set");
220     }
221     if (isReference()) {
222       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getActiveProcessorArgs();
223     }
224     final Vector activeArgs = new Vector(this.processorArgs.size());
225     for (int i = 0; i < this.processorArgs.size(); i++) {
226       final CommandLineArgument arg = (CommandLineArgument) this.processorArgs.elementAt(i);
227       if (arg.isActive(p)) {
228         activeArgs.addElement(arg);
229       }
230     }
231     final CommandLineArgument[] array = new CommandLineArgument[activeArgs.size()];
232     activeArgs.copyInto(array);
233     return array;
234   }
235 
236   
237 
238 
239 
240 
241 
242   public ProcessorParam[] getActiveProcessorParams() {
243     final Project p = getProject();
244     if (p == null) {
245       throw new java.lang.IllegalStateException("project must be set");
246     }
247     if (isReference()) {
248       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getActiveProcessorParams();
249     }
250     final Vector activeParams = new Vector(this.processorParams.size());
251     for (int i = 0; i < this.processorParams.size(); i++) {
252       final ProcessorParam param = (ProcessorParam) this.processorParams.elementAt(i);
253       if (param.isActive(p)) {
254         activeParams.addElement(param);
255       }
256     }
257     final ProcessorParam[] array = new ProcessorParam[activeParams.size()];
258     activeParams.copyInto(array);
259     return array;
260   }
261 
262   
263 
264 
265 
266 
267 
268 
269 
270 
271   public boolean getDebug(final ProcessorDef[] defaultProviders, final int index) {
272     if (isReference()) {
273       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getDebug(defaultProviders, index);
274     }
275     if (this.debug != null) {
276       return this.debug.booleanValue();
277     } else {
278       if (defaultProviders != null && index < defaultProviders.length) {
279         return defaultProviders[index].getDebug(defaultProviders, index + 1);
280       }
281     }
282     return false;
283   }
284 
285   
286 
287 
288 
289 
290 
291 
292 
293 
294 
295   protected final ProcessorDef[] getDefaultProviders(final ProcessorDef baseDef) {
296     ProcessorDef extendsDef = getExtends();
297     final Vector chain = new Vector();
298     while (extendsDef != null && !chain.contains(extendsDef)) {
299       chain.addElement(extendsDef);
300       extendsDef = extendsDef.getExtends();
301     }
302     if (baseDef != null && getInherit()) {
303       chain.addElement(baseDef);
304     }
305     final ProcessorDef[] defaultProviders = new ProcessorDef[chain.size()];
306     chain.copyInto(defaultProviders);
307     return defaultProviders;
308   }
309 
310   
311 
312 
313 
314 
315 
316 
317   public Environment getEnv() {
318     return this.env;
319   }
320 
321   
322 
323 
324 
325 
326 
327 
328   public ProcessorDef getExtends() throws BuildException {
329     if (this.extendsRef != null) {
330       final Object obj = this.extendsRef.getReferencedObject(getProject());
331       if (!getClass().isInstance(obj)) {
332         throw new BuildException("Referenced object " + this.extendsRef.getRefId() + " not correct type, is "
333             + obj.getClass().getName() + " should be " + getClass().getName());
334       }
335       return (ProcessorDef) obj;
336     }
337     return null;
338   }
339 
340   
341 
342 
343 
344 
345 
346 
347   public final boolean getInherit() {
348     return this.inherit;
349   }
350 
351   public boolean getLibtool() {
352     if (this.libtool != null) {
353       return this.libtool.booleanValue();
354     }
355     if (isReference()) {
356       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getLibtool();
357     }
358     final ProcessorDef extendsDef = getExtends();
359     if (extendsDef != null) {
360       return extendsDef.getLibtool();
361     }
362     return false;
363   }
364 
365   
366 
367 
368 
369 
370   protected Processor getProcessor() {
371     if (isReference()) {
372       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getProcessor();
373     }
374     
375     
376     
377     if (this.processor == null) {
378       final ProcessorDef extendsDef = getExtends();
379       if (extendsDef != null) {
380         return extendsDef.getProcessor();
381       }
382     }
383     return this.processor;
384   }
385 
386   
387 
388 
389 
390 
391 
392   protected Processor getProcessor(final LinkType linkType) {
393     
394     return getProcessor();
395   }
396 
397   
398 
399 
400 
401 
402 
403 
404 
405 
406 
407   public boolean getRebuild(final ProcessorDef[] defaultProviders, final int index) {
408     if (isReference()) {
409       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getRebuild(defaultProviders, index);
410     }
411     if (this.rebuild != null) {
412       return this.rebuild.booleanValue();
413     } else {
414       if (defaultProviders != null && index < defaultProviders.length) {
415         return defaultProviders[index].getRebuild(defaultProviders, index + 1);
416       }
417     }
418     return false;
419   }
420 
421   
422 
423 
424 
425 
426 
427   public boolean hasFileSets() {
428     if (isReference()) {
429       return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).hasFileSets();
430     }
431     return this.srcSets.size() > 0;
432   }
433 
434   
435 
436 
437 
438 
439 
440 
441 
442 
443 
444 
445 
446 
447 
448   public boolean isActive() throws BuildException, IllegalStateException {
449     final Project project = getProject();
450     if (!CUtil.isActive(project, this.ifProp, this.unlessProp)) {
451       return false;
452     }
453     if (isReference()) {
454       if (!((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).isActive()) {
455         return false;
456       }
457     }
458     
459     
460     
461     final ProcessorDef[] defaultProviders = getDefaultProviders(null);
462     for (final ProcessorDef defaultProvider : defaultProviders) {
463       if (!defaultProvider.isActive()) {
464         return false;
465       }
466     }
467     return true;
468   }
469 
470   
471 
472 
473 
474 
475 
476 
477 
478   public void setClassname(final String className) throws BuildException {
479     Object proc = null;
480     try {
481       final Class implClass = ProcessorDef.class.getClassLoader().loadClass(className);
482       try {
483         final Method getInstance = implClass.getMethod("getInstance");
484         proc = getInstance.invoke(null);
485       } catch (final Exception ex) {
486         proc = implClass.newInstance();
487       }
488     } catch (final Exception ex) {
489       throw new BuildException(ex);
490     }
491     setProcessor((Processor) proc);
492   }
493 
494   
495 
496 
497 
498 
499 
500 
501 
502   public void setDebug(final boolean debug) throws BuildException {
503     if (isReference()) {
504       throw tooManyAttributes();
505     }
506     this.debug = booleanValueOf(debug);
507   }
508 
509   
510 
511 
512   @Override
513   public void setDescription(final String desc) {
514     super.setDescription(desc);
515   }
516 
517   
518 
519 
520 
521 
522 
523 
524 
525 
526 
527 
528   public void setExtends(final Reference extendsRef) throws BuildException {
529     if (isReference()) {
530       throw tooManyAttributes();
531     }
532     this.extendsRef = extendsRef;
533   }
534 
535   
536 
537 
538 
539 
540 
541   public void setId(final String id) {
542     
543     
544     
545     
546   }
547 
548   
549 
550 
551 
552 
553 
554 
555 
556 
557 
558 
559 
560   public void setIf(final String propName) {
561     this.ifProp = propName;
562   }
563 
564   
565 
566 
567 
568 
569 
570 
571 
572 
573   public void setInherit(final boolean inherit) throws BuildException {
574     if (isReference()) {
575       throw super.tooManyAttributes();
576     }
577     this.inherit = inherit;
578   }
579 
580   
581 
582 
583 
584 
585 
586 
587 
588   public void setLibtool(final boolean libtool) {
589     if (isReference()) {
590       throw tooManyAttributes();
591     }
592     this.libtool = booleanValueOf(libtool);
593   }
594 
595   
596 
597 
598 
599   public void setNewenvironment(final boolean newenv) {
600     this.newEnvironment = newenv;
601   }
602   public boolean isNewEnvironment() {
603     return this.newEnvironment;
604   }
605 
606   
607 
608 
609 
610 
611 
612 
613 
614 
615 
616   protected void setProcessor(final Processor processor) throws BuildException, NullPointerException {
617     if (processor == null) {
618       throw new NullPointerException("processor");
619     }
620     if (isReference()) {
621       throw super.tooManyAttributes();
622     }
623     if (this.env == null && !this.newEnvironment) {
624       this.processor = processor;
625     } else {
626       this.processor = processor.changeEnvironment(this.newEnvironment, this.env);
627     }
628   }
629 
630   
631 
632 
633 
634 
635 
636 
637 
638   public void setRebuild(final boolean rebuild) throws BuildException {
639     if (isReference()) {
640       throw tooManyAttributes();
641     }
642     this.rebuild = booleanValueOf(rebuild);
643   }
644 
645   
646 
647 
648 
649 
650 
651 
652 
653 
654 
655   @Override
656   public void setRefid(final org.apache.tools.ant.types.Reference ref) {
657     super.setRefid(ref);
658   }
659 
660   
661 
662 
663 
664 
665 
666 
667 
668 
669 
670 
671 
672   public void setUnless(final String propName) {
673     this.unlessProp = propName;
674   }
675 
676   
677 
678 
679 
680 
681 
682 
683   public void visitFiles(final FileVisitor visitor) {
684     final Project p = getProject();
685     if (p == null) {
686       throw new java.lang.IllegalStateException("project must be set before this call");
687     }
688     if (isReference()) {
689       ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).visitFiles(visitor);
690     }
691     
692     
693     
694     
695     final ProcessorDef extendsDef = getExtends();
696     if (extendsDef != null) {
697       extendsDef.visitFiles(visitor);
698     }
699 
700     for (int i = 0; i < this.srcSets.size(); i++) {
701       final ConditionalFileSet srcSet = (ConditionalFileSet) this.srcSets.elementAt(i);
702       if (srcSet.isActive()) {
703         
704         final DirectoryScanner scanner = srcSet.getDirectoryScanner(p);
705         
706         final String[] fileNames = scanner.getIncludedFiles();
707         final File parentDir = scanner.getBasedir();
708         for (final String currentFile : fileNames) {
709           visitor.visit(parentDir, currentFile);
710         }
711       }
712     }
713   }
714 }