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.io.FileFilter;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Comparator;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.Hashtable;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.TreeMap;
34 import java.util.Vector;
35
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.Project;
38 import org.apache.tools.ant.Task;
39 import org.apache.tools.ant.types.Environment;
40 import org.apache.commons.io.FilenameUtils;
41
42 import com.github.maven_nar.cpptasks.compiler.CompilerConfiguration;
43 import com.github.maven_nar.cpptasks.compiler.LinkType;
44 import com.github.maven_nar.cpptasks.compiler.Linker;
45 import com.github.maven_nar.cpptasks.compiler.LinkerConfiguration;
46 import com.github.maven_nar.cpptasks.compiler.Processor;
47 import com.github.maven_nar.cpptasks.compiler.ProcessorConfiguration;
48 import com.github.maven_nar.cpptasks.compiler.AbstractCompiler;
49 import com.github.maven_nar.cpptasks.compiler.CommandLineCompilerConfiguration;
50 import com.github.maven_nar.cpptasks.ide.ProjectDef;
51 import com.github.maven_nar.cpptasks.types.CompilerArgument;
52 import com.github.maven_nar.cpptasks.types.ConditionalFileSet;
53 import com.github.maven_nar.cpptasks.types.DefineSet;
54 import com.github.maven_nar.cpptasks.types.IncludePath;
55 import com.github.maven_nar.cpptasks.types.LibrarySet;
56 import com.github.maven_nar.cpptasks.types.LinkerArgument;
57 import com.github.maven_nar.cpptasks.types.SystemIncludePath;
58 import com.github.maven_nar.cpptasks.types.SystemLibrarySet;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class CCTask extends Task {
74
75
76 class Core extends Thread {
77 private final CCTask task;
78 private final CompilerConfiguration config;
79 private final File objDir;
80 private final List<String> sourceFiles;
81 private final boolean relentless;
82 private final CCTaskProgressMonitor monitor;
83 private Exception compileException;
84
85 Core(final CCTask task, final int coreNo, final CompilerConfiguration config, final File objDir,
86 final List<String> set, final boolean relentless, final CCTaskProgressMonitor monitor) {
87 super("Core " + coreNo);
88 this.task = task;
89 this.config = config;
90 this.objDir = objDir;
91 this.sourceFiles = set;
92 this.relentless = relentless;
93 this.monitor = monitor;
94 }
95
96 public Exception getException() {
97 return this.compileException;
98 }
99
100 @Override
101 public void run() {
102 super.run();
103 try {
104 String[] sources = new String[this.sourceFiles.size()];
105 sources = this.sourceFiles.toArray(sources);
106
107 this.config.compile(this.task, this.objDir, sources, this.relentless, this.monitor);
108 } catch (final Exception ex) {
109 if (this.compileException == null) {
110 this.compileException = ex;
111 }
112 }
113 }
114 }
115
116
117 class Progress extends Thread {
118
119 private boolean stop = false;
120 private final File objDir;
121 private final int rebuildCount;
122
123 public Progress(final File objDir, final int rebuildCount) {
124 this.objDir = objDir;
125 this.rebuildCount = rebuildCount;
126 }
127
128 public void exit() {
129 this.stop = true;
130 }
131
132 @Override
133 public void run() {
134 if (this.rebuildCount < 10) {
135 return;
136 }
137 try {
138 final FileFilter updatedFiles = new FileFilter() {
139 private final long startTime = System.currentTimeMillis();
140
141 @Override
142 public boolean accept(final File file) {
143 return file.lastModified() > this.startTime && !file.getName().endsWith(".xml");
144 }
145 };
146 while (!this.stop) {
147 System.err.print("\r" + this.objDir.listFiles(updatedFiles).length + " / " + this.rebuildCount
148 + " files compiled...");
149 System.err.print("\r");
150 System.err.flush();
151 if (!this.stop) {
152 Thread.sleep(5000);
153 }
154 }
155 } catch (final InterruptedException e) {
156 }
157 System.err.print("\r ");
158 System.err.print("\r");
159 System.err.flush();
160 log(Integer.toString(this.rebuildCount) + " files were compiled.");
161 }
162
163 }
164
165 private static class ProjectFileCollector implements FileVisitor {
166 private final List<File> files;
167
168
169
170
171
172
173
174 public ProjectFileCollector(final List<File> files) {
175 this.files = files;
176 }
177
178
179
180
181
182
183
184
185
186 @Override
187 public void visit(final File parentDir, final String filename) {
188 this.files.add(new File(parentDir, filename));
189 }
190 }
191
192 private static class SystemLibraryCollector implements FileVisitor {
193 private final Hashtable<String, File> libraries;
194 private final Linker linker;
195
196 public SystemLibraryCollector(final Linker linker, final Hashtable<String, File> libraries) {
197 this.linker = linker;
198 this.libraries = libraries;
199 }
200
201 @Override
202 public void visit(final File basedir, final String filename) {
203 if (this.linker.bid(filename) > 0) {
204 final File libfile = new File(basedir, filename);
205 final String key = this.linker.getLibraryKey(libfile);
206 this.libraries.put(key, libfile);
207 }
208 }
209 }
210
211 private static final ProcessorConfiguration[] EMPTY_CONFIG_ARRAY = new ProcessorConfiguration[0];
212
213
214
215
216
217 public static Map<CompilerConfiguration, Vector<TargetInfo>> getTargetsToBuildByConfiguration(
218 final Map<String, TargetInfo> targets) {
219 final Map<CompilerConfiguration, Vector<TargetInfo>> targetsByConfig = new HashMap<>();
220 for (final TargetInfo target : targets.values()) {
221 if (target.getRebuild()) {
222
223 Vector<TargetInfo> targetsForSameConfig = targetsByConfig.get(target.getConfiguration());
224 if (targetsForSameConfig != null) {
225 targetsForSameConfig.addElement(target);
226 } else {
227 targetsForSameConfig = new Vector<>();
228 targetsForSameConfig.addElement(target);
229 targetsByConfig.put((CompilerConfiguration) target.getConfiguration(), targetsForSameConfig);
230 }
231 }
232 }
233 return targetsByConfig;
234 }
235
236
237 private int maxCores = 0;
238 private boolean ordered = false;
239
240 private final Vector<CompilerDef> _compilers = new Vector<>();
241
242
243
244 private final Vector _libsets = new Vector();
245
246 private final Vector<LinkerDef> _linkers = new Vector<>();
247
248 private File _objDir;
249
250 private File _outfile;
251
252 private final Vector<TargetDef> targetPlatforms = new Vector<>();
253
254 private final Vector<DistributerDef> distributers = new Vector<>();
255 private final Vector<VersionInfo> versionInfos = new Vector<>();
256
257 private final Vector<ProjectDef> projects = new Vector<>();
258
259 private boolean projectsOnly = false;
260
261 private boolean decorateLinkerOptions = true;
262
263
264
265
266 protected boolean failOnError = true;
267
268
269
270
271
272 private final CompilerDef compilerDef = new CompilerDef();
273
274 private String dataset;
275
276
277
278
279
280
281
282
283 private int dependencyDepth = -1;
284
285
286
287
288 private final LinkerDef linkerDef = new LinkerDef();
289
290
291
292
293 private final LinkType linkType = new LinkType();
294
295
296
297
298 private String outputFileProperty;
299
300
301
302
303 private boolean relentless;
304
305
306
307
308 private int commandLogLevel = Project.MSG_VERBOSE;
309
310 public CCTask() {
311 }
312
313
314
315
316
317
318
319
320
321 public void addConfiguredCompiler(final CompilerDef compiler) {
322 if (compiler == null) {
323 throw new NullPointerException("compiler");
324 }
325 compiler.setProject(getProject());
326 this._compilers.addElement(compiler);
327 }
328
329
330
331
332
333
334 public void addConfiguredCompilerArg(final CompilerArgument arg) {
335 this.compilerDef.addConfiguredCompilerArg(arg);
336 }
337
338
339
340
341
342
343
344
345 public void addConfiguredDefineset(final DefineSet defs) {
346 this.compilerDef.addConfiguredDefineset(defs);
347 }
348
349
350
351
352
353
354
355
356
357 public void addConfiguredDistributer(final DistributerDef distributer) {
358 if (distributer == null) {
359 throw new NullPointerException("distributer");
360 }
361 distributer.setProject(getProject());
362 this.distributers.addElement(distributer);
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376 public void addConfiguredLinker(final LinkerDef linker) {
377 if (linker == null) {
378 throw new NullPointerException("linker");
379 }
380 linker.setProject(getProject());
381 this._linkers.addElement(linker);
382 }
383
384
385
386
387
388 public void addConfiguredLinkerArg(final LinkerArgument arg) {
389 this.linkerDef.addConfiguredLinkerArg(arg);
390 }
391
392
393
394
395
396
397
398
399
400 public void addConfiguredTarget(final TargetDef target) {
401 if (target == null) {
402 throw new NullPointerException("target");
403 }
404 target.setProject(getProject());
405 this.targetPlatforms.addElement(target);
406 }
407
408
409
410
411
412
413 public void addConfiguredVersioninfo(final VersionInfo newVersionInfo) {
414 newVersionInfo.setProject(this.getProject());
415 this.versionInfos.addElement(newVersionInfo);
416 }
417
418
419
420
421 public void addEnv(final Environment.Variable var) {
422 this.compilerDef.addEnv(var);
423 for (int i = 0; i < this._compilers.size(); i++) {
424 final CompilerDef currentCompilerDef = this._compilers.elementAt(i);
425 currentCompilerDef.addEnv(var);
426 }
427 this.linkerDef.addEnv(var);
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441 public void addFileset(final ConditionalFileSet srcSet) {
442 this.compilerDef.addFileset(srcSet);
443 }
444
445
446
447
448
449
450
451
452
453
454
455
456 public void addLibset(final LibrarySet libset) {
457 if (libset == null) {
458 throw new NullPointerException("libset");
459 }
460 this.linkerDef.addLibset(libset);
461 }
462
463
464
465
466
467
468
469 public void addProject(final ProjectDef projectDef) {
470 if (projectDef == null) {
471 throw new NullPointerException("projectDef");
472 }
473 this.projects.addElement(projectDef);
474 }
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492 public void addSyslibset(final SystemLibrarySet libset) {
493 if (libset == null) {
494 throw new NullPointerException("libset");
495 }
496 this.linkerDef.addSyslibset(libset);
497 }
498
499
500
501
502
503
504
505
506 protected int checkForChangedIncludeFiles(final Map<String, TargetInfo> targets) {
507 int potentialTargets = 0;
508 int definiteTargets = 0;
509 Iterator<TargetInfo> targetEnum = targets.values().iterator();
510 while (targetEnum.hasNext()) {
511 final TargetInfo target = targetEnum.next();
512 if (!target.getRebuild()) {
513 potentialTargets++;
514 } else {
515 definiteTargets++;
516 }
517 }
518
519
520
521
522 if (potentialTargets > 0) {
523 log("Starting dependency analysis for " + Integer.toString(potentialTargets) + " files.");
524 final DependencyTable dependencyTable = new DependencyTable(this._objDir);
525 try {
526 dependencyTable.load();
527 } catch (final Exception ex) {
528 log("Problem reading dependencies.xml: " + ex.toString());
529 }
530 targetEnum = targets.values().iterator();
531 while (targetEnum.hasNext()) {
532 final TargetInfo target = targetEnum.next();
533 if (!target.getRebuild()) {
534 if (dependencyTable.needsRebuild(this, target, this.dependencyDepth)) {
535 target.mustRebuild();
536 }
537 }
538 }
539 dependencyTable.commit(this);
540 }
541
542
543
544 int currentTargets = 0;
545 targetEnum = targets.values().iterator();
546 while (targetEnum.hasNext()) {
547 final TargetInfo target = targetEnum.next();
548 if (target.getRebuild()) {
549 currentTargets++;
550 }
551 }
552 if (potentialTargets > 0) {
553 log(Integer.toString(potentialTargets - currentTargets + definiteTargets) + " files are up to date.");
554 log(Integer.toString(currentTargets - definiteTargets) + " files to be recompiled from dependency analysis.");
555 }
556 log(Integer.toString(currentTargets) + " total files to be compiled.");
557 return currentTargets;
558 }
559
560 protected LinkerConfiguration collectExplicitObjectFiles(final Vector<File> objectFiles,
561 final Vector<File> sysObjectFiles, final VersionInfo versionInfo) {
562
563
564
565
566 ProcessorConfiguration linkerConfig = null;
567 LinkerDef selectedLinkerDef = null;
568 Linker selectedLinker = null;
569 final Hashtable<String, File> sysLibraries = new Hashtable<>();
570 final TargetDef targetPlatform = getTargetPlatform();
571 FileVisitor objCollector = null;
572 FileVisitor sysLibraryCollector = null;
573 for (int i = 0; i < this._linkers.size(); i++) {
574 final LinkerDef currentLinkerDef = this._linkers.elementAt(i);
575 if (currentLinkerDef.isActive()) {
576 selectedLinkerDef = currentLinkerDef;
577 selectedLinker = currentLinkerDef.getProcessor().getLinker(this.linkType);
578
579
580
581 if (selectedLinker != null) {
582 linkerConfig = currentLinkerDef.createConfiguration(this, this.linkType, this.linkerDef, targetPlatform,
583 versionInfo);
584 if (linkerConfig != null) {
585
586
587
588 objCollector = new ObjectFileCollector(selectedLinker, objectFiles);
589 sysLibraryCollector = new SystemLibraryCollector(selectedLinker, sysLibraries);
590
591
592
593
594
595 if (currentLinkerDef.hasFileSets()) {
596 currentLinkerDef.visitFiles(objCollector);
597 }
598
599
600
601 selectedLinkerDef.visitUserLibraries(selectedLinker, objCollector);
602 }
603 break;
604 }
605 }
606 }
607 if (linkerConfig == null) {
608 linkerConfig = this.linkerDef.createConfiguration(this, this.linkType, null, targetPlatform, versionInfo);
609 selectedLinker = this.linkerDef.getProcessor().getLinker(this.linkType);
610 objCollector = new ObjectFileCollector(selectedLinker, objectFiles);
611 sysLibraryCollector = new SystemLibraryCollector(selectedLinker, sysLibraries);
612 }
613
614
615
616
617 if (selectedLinkerDef == null || selectedLinkerDef.getInherit()) {
618 this.linkerDef.visitUserLibraries(selectedLinker, objCollector);
619 this.linkerDef.visitSystemLibraries(selectedLinker, sysLibraryCollector);
620 }
621
622
623
624
625
626 if (selectedLinkerDef != null) {
627
628
629
630
631 selectedLinkerDef.visitSystemLibraries(selectedLinker, sysLibraryCollector);
632 }
633
634
635
636
637 final Enumeration<File> sysLibEnum = sysLibraries.elements();
638 while (sysLibEnum.hasMoreElements()) {
639 sysObjectFiles.addElement(sysLibEnum.nextElement());
640 }
641 return (LinkerConfiguration) linkerConfig;
642 }
643
644
645
646
647
648
649
650 public IncludePath createIncludePath() {
651 return this.compilerDef.createIncludePath();
652 }
653
654
655
656
657
658
659 public PrecompileDef createPrecompile() throws BuildException {
660 return this.compilerDef.createPrecompile();
661 }
662
663
664
665
666
667
668
669
670
671
672
673
674
675 public SystemIncludePath createSysIncludePath() {
676 return this.compilerDef.createSysIncludePath();
677 }
678
679
680
681
682
683
684
685
686
687 @Override
688 public void execute() throws BuildException {
689
690
691
692 if (this._objDir == null) {
693 if (this._outfile != null) {
694 this._objDir = new File(this._outfile.getParent());
695 } else {
696 this._objDir = new File(".");
697 }
698 }
699
700
701
702
703 if (!this._objDir.exists()) {
704 throw new BuildException("Object directory does not exist");
705 }
706 final TargetHistoryTable objHistory = new TargetHistoryTable(this, this._objDir);
707
708
709
710
711 VersionInfo versionInfo = null;
712 final Enumeration<VersionInfo> versionEnum = this.versionInfos.elements();
713 while (versionEnum.hasMoreElements()) {
714 versionInfo = versionEnum.nextElement();
715 versionInfo = versionInfo.merge();
716 if (versionInfo.isActive()) {
717 break;
718 } else {
719 versionInfo = null;
720 }
721 }
722
723
724
725
726
727 final Vector<File> objectFiles = new Vector<>();
728 final Vector<File> sysObjectFiles = new Vector<>();
729 final LinkerConfiguration linkerConfig = collectExplicitObjectFiles(objectFiles, sysObjectFiles, versionInfo);
730
731
732
733
734
735 final Map<String, TargetInfo> targets = getTargets(linkerConfig, objectFiles, versionInfo, this._outfile);
736 TargetInfo linkTarget = null;
737
738
739
740
741 if (this._outfile != null) {
742 linkTarget = getLinkTarget(linkerConfig, objectFiles, sysObjectFiles, targets, versionInfo);
743 }
744
745 if (this.projects.size() > 0) {
746 final List<File> files = new ArrayList<>();
747 final ProjectFileCollector matcher = new ProjectFileCollector(files);
748 for (int i = 0; i < this._compilers.size(); i++) {
749 final CompilerDef currentCompilerDef = this._compilers.elementAt(i);
750 if (currentCompilerDef.isActive()) {
751 if (currentCompilerDef.hasFileSets()) {
752 currentCompilerDef.visitFiles(matcher);
753 }
754 }
755 }
756 this.compilerDef.visitFiles(matcher);
757
758 final Enumeration<ProjectDef> iter = this.projects.elements();
759 while (iter.hasMoreElements()) {
760 final ProjectDef projectDef = iter.nextElement();
761 if (projectDef.isActive()) {
762 projectDef.execute(this, files, targets, linkTarget);
763 }
764 }
765 }
766 if (this.projectsOnly) {
767 return;
768 }
769
770
771
772
773
774
775 objHistory.markForRebuild(targets);
776 final CCTaskProgressMonitor monitor = new CCTaskProgressMonitor(objHistory, versionInfo);
777
778
779
780 final int rebuildCount = checkForChangedIncludeFiles(targets);
781 if (rebuildCount > 0) {
782 BuildException compileException = null;
783
784
785
786 final Map<CompilerConfiguration, Vector<TargetInfo>> targetsByConfig = getTargetsToBuildByConfiguration(targets);
787
788
789
790
791 final ArrayList<Vector<TargetInfo>> targetVectorsPreComp = new ArrayList<>();
792 final ArrayList<Vector<TargetInfo>> targetVectors = new ArrayList<>();
793
794 int index = 0;
795 for (final Map.Entry<CompilerConfiguration, Vector<TargetInfo>> targetsForConfig : targetsByConfig.entrySet()) {
796
797
798
799 final CompilerConfiguration config = targetsForConfig.getKey();
800 if (config.isPrecompileGeneration()) {
801 targetVectorsPreComp.add(targetsForConfig.getValue());
802 } else {
803 targetVectors.add(targetsForConfig.getValue());
804 }
805 }
806
807
808 final Progress progress = new Progress(getObjdir(), rebuildCount);
809 progress.start();
810
811
812 compileException = runTargetPool(monitor, compileException, targetVectorsPreComp);
813 if (compileException == null || this.relentless)
814 compileException = runTargetPool(monitor, compileException, targetVectors);
815
816
817 progress.exit();
818 try {
819 progress.join();
820 } catch (final InterruptedException ex) {
821 }
822
823
824
825
826
827
828 try {
829 objHistory.commit();
830 } catch (final IOException ex) {
831 this.log("Error writing history.xml: " + ex.toString());
832 }
833
834
835
836
837
838
839
840 if (compileException != null) {
841 if (this.failOnError) {
842 throw compileException;
843 } else {
844 log(compileException.getMessage(), Project.MSG_ERR);
845 return;
846 }
847 }
848 }
849
850
851
852
853
854
855
856 if (this.dependencyDepth >= 0) {
857 throw new BuildException("All files at depth " + Integer.toString(this.dependencyDepth)
858 + " from changes successfully compiled.\n"
859 + "Remove or change dependencyDepth to -1 to perform full compilation.");
860 }
861
862
863
864
865 if (linkTarget != null) {
866
867
868
869 final TargetHistoryTable linkHistory = getLinkHistory(objHistory);
870
871
872
873 linkHistory.markForRebuild(linkTarget);
874
875
876
877 final File output = linkTarget.getOutput();
878 if (linkTarget.getRebuild()) {
879 final LinkerConfiguration linkConfig = (LinkerConfiguration) linkTarget.getConfiguration();
880
881 log("Linking...");
882 log("Starting link {" + linkConfig.getIdentifier() + "}");
883
884 if (this.failOnError) {
885 linkConfig.link(this, linkTarget);
886 } else {
887 try {
888 linkConfig.link(this, linkTarget);
889 } catch (final BuildException ex) {
890 log(ex.getMessage(), Project.MSG_ERR);
891 return;
892 }
893 }
894 if (this.outputFileProperty != null) {
895 getProject().setProperty(this.outputFileProperty, output.getAbsolutePath());
896 }
897 linkHistory.update(linkTarget);
898 try {
899 linkHistory.commit();
900 } catch (final IOException ex) {
901 log("Error writing link history.xml: " + ex.toString());
902 }
903 } else {
904 if (this.outputFileProperty != null) {
905 getProject().setProperty(this.outputFileProperty, output.getAbsolutePath());
906 }
907 }
908 }
909 }
910
911 private BuildException runTargetPool(final CCTaskProgressMonitor monitor, BuildException compileException,
912 final ArrayList<Vector<TargetInfo>> targetVectors) {
913 int index;
914 for (final Vector<TargetInfo> targetsForConfig : targetVectors) {
915
916
917
918 final CompilerConfiguration config = (CompilerConfiguration) targetsForConfig.elementAt(0).getConfiguration();
919
920
921
922
923
924 int noOfCores = Runtime.getRuntime().availableProcessors();
925 log("Found " + noOfCores + " processors available");
926 if (this.maxCores > 0) {
927 noOfCores = Math.min(this.maxCores, noOfCores);
928 log("Limited processors to " + noOfCores);
929 }
930 final int noOfFiles = targetsForConfig.size();
931 if (noOfFiles < noOfCores) {
932 noOfCores = noOfFiles;
933 log("Limited used processors to " + noOfCores);
934 }
935 if (this.ordered) {
936 noOfCores = 1;
937 log("Limited processors to 1 due to ordering of source files");
938 }
939
940 final List<String>[] sourceFiles = new List[noOfCores];
941 for (int j = 0; j < sourceFiles.length; j++) {
942 sourceFiles[j] = new ArrayList<>(noOfFiles / sourceFiles.length);
943 }
944 final Enumeration<TargetInfo> targetsEnum = targetsForConfig.elements();
945 index = 0;
946 while (targetsEnum.hasMoreElements()) {
947 final TargetInfo targetInfo = targetsEnum.nextElement();
948 sourceFiles[index++].add(targetInfo.getSources()[0].toString());
949 index %= sourceFiles.length;
950 }
951
952
953 final Core[] cores = new Core[noOfCores];
954 for (int j = 0; j < cores.length; j++) {
955 cores[j] = new Core(this, j, config, this._objDir, sourceFiles[j], this.relentless, monitor);
956 log("\nStarting Core " + j + " with " + sourceFiles[j].size() + " source files...");
957 }
958
959
960 for (final Core core : cores) {
961 core.start();
962 }
963
964
965 boolean alive = false;
966 try {
967 do {
968 alive = false;
969 for (int j = 0; j < cores.length; j++) {
970 if (cores[j] != null) {
971 if (cores[j].isAlive()) {
972 alive = true;
973 } else {
974 final Exception exception = cores[j].getException();
975 if (exception != null) {
976 if (compileException == null && exception instanceof BuildException) {
977 compileException = (BuildException) exception;
978 } else {
979 log(cores[j].getName() + " " + exception + " ", Project.MSG_ERR);
980 }
981 if (!this.relentless) {
982 cores[j] = null;
983 alive = false;
984 break;
985 }
986 }
987 cores[j] = null;
988 }
989 }
990 }
991 if (alive) {
992
993 Thread.sleep(Math.min(5000, sourceFiles[0].size() * 2000));
994 }
995 } while (alive);
996 } catch (final InterruptedException e) {
997 break;
998 }
999
1000
1001 for (final Core core : cores) {
1002 if (core != null) {
1003 core.interrupt();
1004 log(core.getName() + " interrupted ");
1005 }
1006 }
1007
1008 if (!this.relentless && compileException != null) {
1009 break;
1010 }
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 }
1036 return compileException;
1037 }
1038
1039
1040
1041
1042
1043
1044
1045
1046 public int getCommandLogLevel() {
1047 return this.commandLogLevel;
1048 }
1049
1050
1051
1052
1053
1054
1055 public String getDataset() {
1056 return this.dataset;
1057 }
1058
1059
1060
1061
1062
1063
1064 public boolean getDebug() {
1065 return this.compilerDef.getDebug(null, 0);
1066 }
1067
1068
1069
1070
1071
1072
1073 public boolean getFailonerror() {
1074 return this.failOnError;
1075 }
1076
1077 protected TargetHistoryTable getLinkHistory(final TargetHistoryTable objHistory) {
1078 final File outputFileDir = new File(this._outfile.getParent());
1079
1080
1081
1082
1083 if (this._objDir.equals(outputFileDir)) {
1084 return objHistory;
1085 }
1086 return new TargetHistoryTable(this, outputFileDir);
1087 }
1088
1089 protected TargetInfo getLinkTarget(final LinkerConfiguration linkerConfig, final Vector<File> objectFiles,
1090 final Vector<File> sysObjectFiles, final Map<String, TargetInfo> compileTargets, final VersionInfo versionInfo) {
1091
1092
1093
1094
1095
1096
1097
1098 for (final TargetInfo compileTarget : compileTargets.values()) {
1099
1100
1101
1102 final int bid = linkerConfig.bid(compileTarget.getOutput().toString());
1103 if (bid > 0) {
1104 objectFiles.addElement(compileTarget.getOutput());
1105 }
1106 }
1107 final File[] objectFileArray = new File[objectFiles.size()];
1108 objectFiles.copyInto(objectFileArray);
1109 final File[] sysObjectFileArray = new File[sysObjectFiles.size()];
1110 sysObjectFiles.copyInto(sysObjectFileArray);
1111 final String baseName = this._outfile.getName();
1112 final String[] fullNames = linkerConfig.getOutputFileNames(baseName, versionInfo);
1113 final File outputFile = new File(this._outfile.getParent(), fullNames[0]);
1114 return new TargetInfo(linkerConfig, objectFileArray, sysObjectFileArray, outputFile, linkerConfig.getRebuild());
1115 }
1116
1117 public int getMaxCores() {
1118 return this.maxCores;
1119 }
1120
1121 public File getObjdir() {
1122 return this._objDir;
1123 }
1124
1125 public File getOutfile() {
1126 return this._outfile;
1127 }
1128
1129
1130
1131
1132
1133
1134 public String getOuttype() {
1135 return this.linkType.getOutputType();
1136 }
1137
1138
1139
1140
1141
1142
1143 public String getSubsystem() {
1144 return this.linkType.getSubsystem();
1145 }
1146
1147 public TargetDef getTargetPlatform() {
1148 return null;
1149 }
1150
1151
1152
1153
1154
1155
1156
1157
1158 private Map<String, TargetInfo> getTargets(final LinkerConfiguration linkerConfig, final Vector<File> objectFiles,
1159 final VersionInfo versionInfo, final File outputFile) {
1160
1161 final List<String> order = new ArrayList<>();
1162
1163 final Map<String, TargetInfo> targets = new TreeMap<>(new Comparator<String>() {
1164
1165 @Override public int compare(String f0, String f1) {
1166 if (order.isEmpty()) {
1167 return f0.compareTo(f1);
1168 }
1169
1170
1171
1172 String compf0 = FilenameUtils.getBaseName(f0);
1173 String compf1 = FilenameUtils.getBaseName(f1);
1174
1175
1176
1177 compf0 = FilenameUtils.removeExtension(compf0);
1178 compf1 = FilenameUtils.removeExtension(compf1);
1179
1180
1181 final int i0 = order.indexOf(compf0);
1182 final int i1 = order.indexOf(compf1);
1183
1184 if (i0 < 0 && i1 < 0) {
1185
1186
1187 return f0.compareTo(f1);
1188 } else {
1189
1190 CCTask.this.ordered = true;
1191
1192 if (i0 > 0 && i1 > 0) {
1193
1194 return i0 == i1 ? 0 : i0 < i1 ? -1 : +1;
1195 } else if (i1 < 0) {
1196
1197 return -1;
1198 } else {
1199
1200 return +1;
1201 }
1202 }
1203 }
1204 });
1205
1206 final TargetDef targetPlatform = getTargetPlatform();
1207
1208
1209
1210
1211
1212 order.clear();
1213 for (int i = 0; i < this._compilers.size(); i++) {
1214 final CompilerDef currentCompilerDef = this._compilers.elementAt(i);
1215 if (currentCompilerDef.isActive()) {
1216 final List<String> compilerFileOrder = currentCompilerDef.getOrder();
1217 if (compilerFileOrder != null) {
1218 order.addAll(compilerFileOrder);
1219 }
1220 }
1221 }
1222
1223
1224
1225
1226 final Vector<ProcessorConfiguration> biddingProcessors = new Vector<>(this._compilers.size());
1227 for (int i = 0; i < this._compilers.size(); i++) {
1228 final CompilerDef currentCompilerDef = this._compilers.elementAt(i);
1229 if (currentCompilerDef.isActive()) {
1230 final ProcessorConfiguration config = currentCompilerDef.createConfiguration(this, this.linkType,
1231 this.compilerDef, targetPlatform, versionInfo);
1232
1233
1234
1235 final PrecompileDef precompileDef = currentCompilerDef.getActivePrecompile(this.compilerDef);
1236 CommandLineCompilerConfiguration commandLineConfig = (CommandLineCompilerConfiguration) config;
1237 AbstractCompiler compiler = (AbstractCompiler) commandLineConfig.getCompiler();
1238 compiler.setWorkDir(currentCompilerDef.getWorkDir());
1239 ProcessorConfiguration[] localConfigs = new ProcessorConfiguration[] {
1240 config
1241 };
1242
1243
1244
1245 if (precompileDef != null) {
1246 final File prototype = precompileDef.getPrototype();
1247
1248
1249
1250 if (!prototype.exists()) {
1251 throw new BuildException("prototype (" + prototype.toString() + ") does not exist.");
1252 }
1253 if (prototype.isDirectory()) {
1254 throw new BuildException("prototype (" + prototype.toString() + ") is a directory.");
1255 }
1256 final String[] exceptFiles = precompileDef.getExceptFiles();
1257
1258
1259
1260
1261
1262 final CompilerConfiguration[] configs = ((CompilerConfiguration) config).createPrecompileConfigurations(
1263 prototype, exceptFiles);
1264 if (configs != null && configs.length == 2) {
1265
1266
1267
1268
1269 final TargetMatcher matcher = new TargetMatcher(this, this._objDir, new ProcessorConfiguration[] {
1270 configs[0]
1271 }, linkerConfig, objectFiles, targets, versionInfo);
1272
1273 matcher.visit(new File(prototype.getParent()), prototype.getName());
1274
1275
1276
1277 biddingProcessors.addElement(configs[1]);
1278 localConfigs = new ProcessorConfiguration[2];
1279 localConfigs[0] = configs[1];
1280 localConfigs[1] = config;
1281 }
1282 }
1283
1284
1285
1286
1287
1288 if (currentCompilerDef.hasFileSets()) {
1289 final TargetMatcher matcher = new TargetMatcher(this, this._objDir, localConfigs, linkerConfig, objectFiles,
1290 targets, versionInfo);
1291 currentCompilerDef.visitFiles(matcher);
1292 }
1293 biddingProcessors.addElement(config);
1294 }
1295 }
1296
1297
1298
1299 if (this._compilers.size()==0) {
1300 final ProcessorConfiguration config = this.compilerDef.createConfiguration(this, this.linkType, null,
1301 targetPlatform, versionInfo);
1302 biddingProcessors.addElement(config);
1303 final ProcessorConfiguration[] bidders = new ProcessorConfiguration[biddingProcessors.size()];
1304 biddingProcessors.copyInto(bidders);
1305
1306
1307
1308 final TargetMatcher matcher = new TargetMatcher(this, this._objDir, bidders, linkerConfig, objectFiles, targets,
1309 versionInfo);
1310 this.compilerDef.visitFiles(matcher);
1311
1312 if (outputFile != null && versionInfo != null) {
1313 final boolean isDebug = linkerConfig.isDebug();
1314 try {
1315 linkerConfig.getLinker()
1316 .addVersionFiles(versionInfo, this.linkType, outputFile, isDebug, this._objDir, matcher);
1317 } catch (final IOException ex) {
1318 throw new BuildException(ex);
1319 }
1320 }
1321 }
1322 return targets;
1323 }
1324
1325 public boolean isDecorateLinkerOptions() {
1326 return this.decorateLinkerOptions;
1327 }
1328
1329
1330
1331
1332
1333
1334
1335
1336 public void setClassname(final String classname) {
1337 this.compilerDef.setClassname(classname);
1338 this.linkerDef.setClassname(classname);
1339 }
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349 public void setCommandLogLevel(final int commandLogLevel) {
1350 this.commandLogLevel = commandLogLevel;
1351 }
1352
1353
1354
1355
1356
1357
1358
1359 public void setDataset(final String dataset) {
1360 this.dataset = dataset;
1361 }
1362
1363
1364
1365
1366 public void setDebug(final boolean debug) {
1367 this.compilerDef.setDebug(debug);
1368 this.linkerDef.setDebug(debug);
1369 }
1370
1371 public void setDecorateLinkerOptions(final boolean decorateLinkerOptions) {
1372 this.decorateLinkerOptions = decorateLinkerOptions;
1373 }
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391 public void setDependencyDepth(final int depth) {
1392 this.dependencyDepth = depth;
1393 }
1394
1395
1396
1397
1398 public void setExceptions(final boolean exceptions) {
1399 this.compilerDef.setExceptions(exceptions);
1400 }
1401
1402
1403
1404
1405
1406
1407
1408
1409 public void setFailonerror(final boolean fail) {
1410 this.failOnError = fail;
1411 }
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422 public void setIncremental(final boolean incremental) {
1423 this.linkerDef.setIncremental(incremental);
1424 }
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435 public void setLibtool(final boolean libtool) {
1436 this.compilerDef.setLibtool(libtool);
1437 this.linkerDef.setLibtool(libtool);
1438 }
1439
1440
1441
1442
1443
1444
1445
1446 @Deprecated
1447 public void setLink(final OutputTypeEnum outputType) {
1448 this.linkType.setOutputType(outputType);
1449 }
1450
1451
1452 public void setLinkCPP(final boolean linkCPP) {
1453 this.linkType.setLinkCPP(linkCPP);
1454 }
1455
1456 public void setLinkFortran(final boolean linkFortran) {
1457 this.linkType.setLinkFortran(linkFortran);
1458 }
1459
1460 public void setLinkFortranMain(final boolean linkFortranMain) {
1461 this.linkType.setLinkFortranMain(linkFortranMain);
1462 }
1463
1464
1465
1466
1467 public void setMaxCores(final int maxCores) {
1468 this.maxCores = maxCores;
1469 }
1470
1471
1472
1473
1474
1475
1476
1477 public void setMultithreaded(final boolean multi) {
1478 this.compilerDef.setMultithreaded(multi);
1479 }
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592 public void setName(final CompilerEnum name) {
1593 this.compilerDef.setName(name);
1594 final Processor compiler = this.compilerDef.getProcessor();
1595 final Linker linker = compiler.getLinker(this.linkType);
1596 this.linkerDef.setProcessor(linker);
1597 }
1598
1599
1600
1601
1602
1603 public void setNewenvironment(final boolean newenv) {
1604 this.compilerDef.setNewenvironment(newenv);
1605 for (int i = 0; i < this._compilers.size(); i++) {
1606 final CompilerDef currentCompilerDef = this._compilers.elementAt(i);
1607 currentCompilerDef.setNewenvironment(newenv);
1608 }
1609 this.linkerDef.setNewenvironment(newenv);
1610 }
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621 public void setObjdir(final File dir) {
1622 if (dir == null) {
1623 throw new NullPointerException("dir");
1624 }
1625 this._objDir = dir;
1626 }
1627
1628
1629
1630
1631
1632
1633 public void setOptimize(final OptimizationEnum optimization) {
1634 this.compilerDef.setOptimize(optimization);
1635 }
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646 public void setOutfile(final File outfile) {
1647
1648
1649
1650 if (outfile == null || outfile.toString().length() > 0) {
1651 this._outfile = outfile;
1652 }
1653 }
1654
1655
1656
1657
1658
1659 public void setOutputFileProperty(final String outputFileProperty) {
1660 this.outputFileProperty = outputFileProperty;
1661 }
1662
1663
1664
1665
1666
1667 public void setOuttype(final OutputTypeEnum outputType) {
1668 this.linkType.setOutputType(outputType);
1669 }
1670
1671
1672
1673
1674
1675
1676 @Override
1677 public void setProject(final Project project) {
1678 super.setProject(project);
1679 this.compilerDef.setProject(project);
1680 this.linkerDef.setProject(project);
1681 }
1682
1683 public void setProjectsOnly(final boolean value) {
1684 this.projectsOnly = value;
1685 }
1686
1687
1688
1689
1690
1691
1692
1693
1694 public void setRebuild(final boolean rebuildAll) {
1695 this.compilerDef.setRebuild(rebuildAll);
1696 this.linkerDef.setRebuild(rebuildAll);
1697 }
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707 public void setRelentless(final boolean relentless) {
1708 this.relentless = relentless;
1709 }
1710
1711
1712
1713
1714 public void setRtti(final boolean rtti) {
1715 this.compilerDef.setRtti(rtti);
1716 }
1717
1718
1719
1720
1721 public void setRuntime(final RuntimeType rtlType) {
1722 this.linkType.setStaticRuntime(rtlType.getIndex() == 1);
1723 }
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750 public void setSubsystem(final SubsystemEnum subsystem) {
1751 if (subsystem == null) {
1752 throw new NullPointerException("subsystem");
1753 }
1754 this.linkType.setSubsystem(subsystem);
1755 }
1756
1757
1758
1759
1760
1761 public void setWarnings(final WarningLevelEnum level) {
1762 this.compilerDef.setWarnings(level);
1763 }
1764
1765 }