-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathProcessorDef.java
739 lines (698 loc) · 21.9 KB
/
ProcessorDef.java
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
* #%L
* Native ARchive plugin for Maven
* %%
* Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
* %%
* 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.
* #L%
*/
package com.github.maven_nar.cpptasks;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Vector;
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;
import com.github.maven_nar.cpptasks.compiler.LinkType;
import com.github.maven_nar.cpptasks.compiler.Processor;
import com.github.maven_nar.cpptasks.compiler.ProcessorConfiguration;
import com.github.maven_nar.cpptasks.types.CommandLineArgument;
import com.github.maven_nar.cpptasks.types.ConditionalFileSet;
/**
* 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(final 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 {
this.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(final CommandLineArgument arg) throws NullPointerException, BuildException {
if (arg == null) {
throw new NullPointerException("arg");
}
if (isReference()) {
throw noChildrenAllowed();
}
this.processorArgs.addElement(arg);
}
/**
* Remove 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 boolean removeConfiguredProcessorArg(final CommandLineArgument arg) throws NullPointerException, BuildException {
if (arg == null) {
throw new NullPointerException("arg");
}
if (isReference()) {
throw noChildrenAllowed();
}
return this.processorArgs.removeElement(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(final ProcessorParam param) throws NullPointerException, BuildException {
if (param == null) {
throw new NullPointerException("param");
}
if (isReference()) {
throw noChildrenAllowed();
}
this.processorParams.addElement(param);
}
/**
* Add an environment variable to the launched process.
*/
public void addEnv(final Environment.Variable var) {
if (this.env == null) {
this.env = new Environment();
this.newEnvironment = true;
if (this.processor != null) {
// Change the environment in the processor
setProcessor(this.processor);
}
}
this.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(final ConditionalFileSet srcSet) throws BuildException {
if (isReference()) {
throw noChildrenAllowed();
}
srcSet.setProject(getProject());
this.srcSets.addElement(srcSet);
}
/**
* Creates a configuration
*
* @param baseDef
* reference to def from containing cc element, may be null
* @return configuration
*
*/
public ProcessorConfiguration createConfiguration(final CCTask task, final LinkType linkType,
final ProcessorDef baseDef, final TargetDef targetPlatform, final VersionInfo versionInfo) {
if (isReference()) {
return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).createConfiguration(task, linkType,
baseDef, targetPlatform, versionInfo);
}
final ProcessorDef[] defaultProviders = getDefaultProviders(baseDef);
final 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() {
final Project p = getProject();
if (p == null) {
throw new java.lang.IllegalStateException("project must be set");
}
if (isReference()) {
return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getActiveProcessorArgs();
}
final Vector activeArgs = new Vector(this.processorArgs.size());
for (int i = 0; i < this.processorArgs.size(); i++) {
final CommandLineArgument arg = (CommandLineArgument) this.processorArgs.elementAt(i);
if (arg.isActive(p)) {
activeArgs.addElement(arg);
}
}
final 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() {
final Project p = getProject();
if (p == null) {
throw new java.lang.IllegalStateException("project must be set");
}
if (isReference()) {
return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getActiveProcessorParams();
}
final Vector activeParams = new Vector(this.processorParams.size());
for (int i = 0; i < this.processorParams.size(); i++) {
final ProcessorParam param = (ProcessorParam) this.processorParams.elementAt(i);
if (param.isActive(p)) {
activeParams.addElement(param);
}
}
final 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(final ProcessorDef[] defaultProviders, final int index) {
if (isReference()) {
return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getDebug(defaultProviders, index);
}
if (this.debug != null) {
return this.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(final ProcessorDef baseDef) {
ProcessorDef extendsDef = getExtends();
final Vector chain = new Vector();
while (extendsDef != null && !chain.contains(extendsDef)) {
chain.addElement(extendsDef);
extendsDef = extendsDef.getExtends();
}
if (baseDef != null && getInherit()) {
chain.addElement(baseDef);
}
final ProcessorDef[] defaultProviders = new ProcessorDef[chain.size()];
chain.copyInto(defaultProviders);
return defaultProviders;
}
/**
* Because linkers have multiples and none of them share the environment
* settings here is a hack to give direct access to copy it just before
* running
*
* @return
*/
public Environment getEnv() {
return this.env;
}
/**
* Provide direct access to copy of processorArgs for validation testing.
*
* @return processorArgs
*/
public Vector getProcessorArgs() { return this.processorArgs; }
/**
* 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 (this.extendsRef != null) {
final Object obj = this.extendsRef.getReferencedObject(getProject());
if (!getClass().isInstance(obj)) {
throw new BuildException("Referenced object " + this.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 this.inherit;
}
public boolean getLibtool() {
if (this.libtool != null) {
return this.libtool.booleanValue();
}
if (isReference()) {
return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getLibtool();
}
final 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 (this.processor == null) {
final ProcessorDef extendsDef = getExtends();
if (extendsDef != null) {
return extendsDef.getProcessor();
}
}
return this.processor;
}
/**
* Obtains the appropriate processor (compiler, linker) based on the
* LinkType.
*
* @return processor
*/
protected Processor getProcessor(final 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(final ProcessorDef[] defaultProviders, final int index) {
if (isReference()) {
return ((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).getRebuild(defaultProviders, index);
}
if (this.rebuild != null) {
return this.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 this.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 {
final Project project = getProject();
if (!CUtil.isActive(project, this.ifProp, this.unlessProp)) {
return false;
}
if (isReference() && !((ProcessorDef) getCheckedRef(ProcessorDef.class, "ProcessorDef")).isActive()) {
return false;
}
//
// walk through any extended definitions
//
final ProcessorDef[] defaultProviders = getDefaultProviders(null);
for (final ProcessorDef defaultProvider : defaultProviders) {
if (!defaultProvider.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(final String className) throws BuildException {
Object proc = null;
try {
final Class implClass = ProcessorDef.class.getClassLoader().loadClass(className);
try {
final Method getInstance = implClass.getMethod("getInstance");
proc = getInstance.invoke(null);
} catch (final Exception ex) {
proc = implClass.newInstance();
}
} catch (final 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(final boolean debug) throws BuildException {
if (isReference()) {
throw tooManyAttributes();
}
this.debug = booleanValueOf(debug);
}
/**
* Sets a description of the current data type.
*/
@Override
public void setDescription(final 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(final 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(final 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(final String propName) {
this.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(final 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(final boolean libtool) {
if (isReference()) {
throw tooManyAttributes();
}
this.libtool = booleanValueOf(libtool);
}
/**
* Do not propagate old environment when new environment variables are
* specified.
*/
public void setNewenvironment(final boolean newenv) {
this.newEnvironment = newenv;
}
public boolean isNewEnvironment() {
return this.newEnvironment;
}
/**
* 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(final Processor processor) throws BuildException, NullPointerException {
if (processor == null) {
throw new NullPointerException("processor");
}
if (isReference()) {
throw super.tooManyAttributes();
}
if (this.env == null && !this.newEnvironment) {
this.processor = processor;
} else {
this.processor = processor.changeEnvironment(this.newEnvironment, this.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(final 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
*
*/
@Override
public void setRefid(final 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(final String propName) {
this.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(final FileVisitor visitor) {
final 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
//
final ProcessorDef extendsDef = getExtends();
if (extendsDef != null) {
extendsDef.visitFiles(visitor);
}
for (int i = 0; i < this.srcSets.size(); i++) {
final ConditionalFileSet srcSet = (ConditionalFileSet) this.srcSets.elementAt(i);
if (srcSet.isActive()) {
// Find matching source files
final DirectoryScanner scanner = srcSet.getDirectoryScanner(p);
// Check each source file - see if it needs compilation
final String[] fileNames = scanner.getIncludedFiles();
final File parentDir = scanner.getBasedir();
for (final String currentFile : fileNames) {
visitor.visit(parentDir, currentFile);
}
}
}
}
}