/
CommonBuilder.java
1398 lines (1201 loc) · 49.7 KB
/
CommonBuilder.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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2007, 2021 Intel Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Intel Corporation - Initial API and implementation
* IBM Corporation
* Dmitry Kozlov (CodeSourcery) - Build error highlighting and navigation
* Save build output (bug 294106)
* Andrew Gvozdev (Quoin Inc) - Saving build output implemented in different way (bug 306222)
* Umair Sair (Mentor Graphics) - Project dependencies are not built in the correct order (bug 546407)
* Umair Sair (Mentor Graphics) - Setting current project for markers creation (bug 545976)
* Torbjörn Svensson (STMicroelectronics) - bug #571134
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ConsoleOutputStream;
import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.ICommandLauncher;
import org.eclipse.cdt.core.ProblemMarkerInfo;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.resources.ACBuilder;
import org.eclipse.cdt.core.resources.IConsole;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.util.ListComparator;
import org.eclipse.cdt.internal.core.BuildRunnerHelper;
import org.eclipse.cdt.managedbuilder.buildmodel.BuildDescriptionManager;
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildDescription;
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildStep;
import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.internal.buildmodel.BuildDescription;
import org.eclipse.cdt.managedbuilder.internal.buildmodel.BuildStateManager;
import org.eclipse.cdt.managedbuilder.internal.buildmodel.IBuildModelBuilder;
import org.eclipse.cdt.managedbuilder.internal.buildmodel.IConfigurationBuildState;
import org.eclipse.cdt.managedbuilder.internal.buildmodel.IProjectBuildState;
import org.eclipse.cdt.managedbuilder.internal.buildmodel.StepBuilder;
import org.eclipse.cdt.managedbuilder.internal.core.jsoncdb.generator.CompilationDatabaseGenerator;
import org.eclipse.cdt.managedbuilder.macros.BuildMacroException;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator;
import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator2;
import org.eclipse.cdt.newmake.core.IMakeBuilderInfo;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IIncrementalProjectBuilder2;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceRuleFactory;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
public class CommonBuilder extends ACBuilder implements IIncrementalProjectBuilder2 {
public final static String BUILDER_ID = ManagedBuilderCorePlugin.getUniqueIdentifier() + ".genmakebuilder"; //$NON-NLS-1$
private static final String ERROR_HEADER = "GeneratedmakefileBuilder error ["; //$NON-NLS-1$
private static final String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$
private static final String TRACE_FOOTER = "]: "; //$NON-NLS-1$
private static final String TRACE_HEADER = "GeneratedmakefileBuilder trace ["; //$NON-NLS-1$
private static final String COMPILATION_DATABASE_ENABLEMENT = "generateFile"; //$NON-NLS-1$
public static boolean VERBOSE = false;
private static final int PROGRESS_MONITOR_SCALE = 100;
private static final int TICKS_STREAM_PROGRESS_MONITOR = 1 * PROGRESS_MONITOR_SCALE;
private static final int TICKS_EXECUTE_COMMAND = 1 * PROGRESS_MONITOR_SCALE;
private static final int TICKS_REFRESH_PROJECT = 1 * PROGRESS_MONITOR_SCALE;
private static final int TICKS_DELETE_OUTPUTS = 1 * PROGRESS_MONITOR_SCALE;
private static CfgBuildSet fBuildSet = new CfgBuildSet();
private boolean fBuildErrOccured;
private Set<String> builtRefConfigIds = new HashSet<>();
private Set<String> scheduledConfigIds = new HashSet<>();
public CommonBuilder() {
}
public static void outputTrace(String resourceName, String message) {
if (VERBOSE) {
System.out.println(TRACE_HEADER + resourceName + TRACE_FOOTER + message + NEWLINE);
}
}
public static void outputError(String resourceName, String message) {
if (VERBOSE) {
System.err.println(ERROR_HEADER + resourceName + TRACE_FOOTER + message + NEWLINE);
}
}
private static class CfgBuildSet {
Map<IProject, Set<String>> fMap = new HashMap<>();
public Set<String> getCfgIdSet(IProject project, boolean create) {
Set<String> set = fMap.get(project);
if (set == null && create) {
set = new HashSet<>();
fMap.put(project, set);
}
return set;
}
public void start(CommonBuilder bld) {
checkClean(bld);
}
private boolean checkClean(CommonBuilder bld) {
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (IProject wproject : projects) {
if (bld.hasBeenBuilt(wproject)) {
if (VERBOSE)
outputTrace(null,
"checking clean: the project " + wproject.getName() + " was built, no clean needed"); //$NON-NLS-1$ //$NON-NLS-2$
return false;
}
}
if (VERBOSE)
outputTrace(null, "checking clean: no projects were built.. cleanning"); //$NON-NLS-1$
fMap.clear();
return true;
}
}
private static class CfgBuildInfo {
private final IProject fProject;
private final IManagedBuildInfo fBuildInfo;
private final IConfiguration fCfg;
private final IBuilder fBuilder;
private IConsole fConsole;
CfgBuildInfo(IBuilder builder, boolean isForegound) {
this.fBuilder = builder;
this.fCfg = builder.getParent().getParent();
this.fProject = this.fCfg.getOwner().getProject();
this.fBuildInfo = ManagedBuildManager.getBuildInfo(this.fProject);
}
public IProject getProject() {
return fProject;
}
public IConsole getConsole() {
if (fConsole == null) {
fConsole = CCorePlugin.getDefault().getConsole();
fConsole.start(fProject);
}
return fConsole;
}
// public boolean isForeground(){
// return fIsForeground;
// }
public IBuilder getBuilder() {
return fBuilder;
}
public IConfiguration getConfiguration() {
return fCfg;
}
public IManagedBuildInfo getBuildInfo() {
return fBuildInfo;
}
}
public class ResourceDeltaVisitor implements IResourceDeltaVisitor {
private String buildGoalName;
private final IProject project;
private final IPath buildPaths[];
private boolean incrBuildNeeded = false;
private boolean fullBuildNeeded = false;
private final List<String> reservedNames;
public ResourceDeltaVisitor(IConfiguration cfg, IConfiguration allConfigs[]) {
this.project = cfg.getOwner().getProject();
buildPaths = new IPath[allConfigs.length];
for (int i = 0; i < buildPaths.length; i++) {
buildPaths[i] = ManagedBuildManager.getBuildFullPath(allConfigs[i], allConfigs[i].getBuilder());
}
String ext = cfg.getArtifactExtension();
//try to resolve build macros in the build artifact extension
try {
ext = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(ext, "", //$NON-NLS-1$
" ", //$NON-NLS-1$
IBuildMacroProvider.CONTEXT_CONFIGURATION, cfg);
} catch (BuildMacroException e) {
}
String name = cfg.getArtifactName();
//try to resolve build macros in the build artifact name
try {
String resolved = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(name, "", //$NON-NLS-1$
" ", //$NON-NLS-1$
IBuildMacroProvider.CONTEXT_CONFIGURATION, cfg);
if ((resolved = resolved.trim()).length() > 0)
name = resolved;
} catch (BuildMacroException e) {
}
if (ext.length() > 0) {
buildGoalName = cfg.getOutputPrefix(ext) + name + IManagedBuilderMakefileGenerator.DOT + ext;
} else {
buildGoalName = name;
}
reservedNames = Arrays.asList(new String[] { ".cdtbuild", ".cdtproject", ".project" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
private boolean isGeneratedResource(IResource resource) {
// Is this a generated directory ...
IPath path = resource.getFullPath();
for (IPath buildPath : buildPaths) {
if (buildPath != null && buildPath.isPrefixOf(path)) {
return true;
}
}
return false;
}
private boolean isProjectFile(IResource resource) {
return reservedNames.contains(resource.getName());
}
public boolean shouldBuildIncr() {
return incrBuildNeeded;
}
public boolean shouldBuildFull() {
return fullBuildNeeded;
}
@Override
public boolean visit(IResourceDelta delta) throws CoreException {
IResource resource = delta.getResource();
// If the project has changed, then a build is needed and we can stop
if (resource != null && resource.getProject() == project) {
switch (resource.getType()) {
case IResource.FILE:
String name = resource.getName();
if ((!name.equals(buildGoalName) &&
// TODO: Also need to check for secondary outputs
(resource.isDerived() || (isProjectFile(resource)) || (isGeneratedResource(resource))))) {
// The resource that changed has attributes which make it uninteresting,
// so don't do anything
} else {
// TODO: Should we do extra checks here to determine if a build is really needed,
// or do you just do exclusion checks like above?
// We could check for:
// o The build goal name
// o A secondary output
// o An input file to a tool:
// o Has an extension of a source file used by a tool
// o Has an extension of a header file used by a tool
// o Has the name of an input file specified in an InputType via:
// o An Option
// o An AdditionalInput
//
//if (resourceName.equals(buildGoalName) ||
// (buildInfo.buildsFileType(ext) || buildInfo.isHeaderFile(ext))) {
// We need to do an incremental build, at least
incrBuildNeeded = true;
if (delta.getKind() == IResourceDelta.REMOVED) {
// If a meaningful resource was removed, then force a full build
// This is required because an incremental build will trigger make to
// do nothing for a missing source, since the state after the file
// removal is uptodate, as far as make is concerned
// A full build will clean, and ultimately trigger a relink without
// the object generated from the deleted source, which is what we want
fullBuildNeeded = true;
// There is no point in checking anything else since we have
// decided to do a full build anyway
break;
}
//}
}
return false;
}
}
return true;
}
}
private static class OtherConfigVerifier implements IResourceDeltaVisitor {
IPath buildFullPaths[];
// IConfiguration buildConfigs[];
Configuration otherConfigs[];
int resourceChangeState;
private static final IPath[] ignoreList = { new Path(".cdtproject"), //$NON-NLS-1$
new Path(".cproject"), //$NON-NLS-1$
new Path(".cdtbuild"), //$NON-NLS-1$
new Path(".settings"), //$NON-NLS-1$
};
OtherConfigVerifier(IBuilder builders[], IConfiguration allCfgs[]) {
Set<IConfiguration> buildCfgSet = new HashSet<>();
for (IBuilder builder : builders) {
buildCfgSet.add(builder.getParent().getParent());
}
@SuppressWarnings("unchecked")
List<Configuration> othersList = ListComparator.getAdded(allCfgs, buildCfgSet.toArray());
if (othersList != null)
otherConfigs = othersList.toArray(new Configuration[othersList.size()]);
else
otherConfigs = new Configuration[0];
List<IPath> list = new ArrayList<>(builders.length);
// buildFullPaths = new IPath[builders.length];
for (IBuilder builder : builders) {
IPath path = ManagedBuildManager.getBuildFullPath(builder.getParent().getParent(), builder);
if (path != null)
list.add(path);
// buildFullPaths[i] = ManagedBuildManager.getBuildFullPath(builders[i].getParent().getParent(), builders[i]);
}
buildFullPaths = list.toArray(new IPath[list.size()]);
}
@Override
public boolean visit(IResourceDelta delta) throws CoreException {
IResource rc = delta.getResource();
if (rc.getType() == IResource.FILE) {
if (isResourceValuable(rc))
resourceChangeState |= delta.getKind();
return false;
}
if (!isResourceValuable(rc))
return false;
for (IPath buildFullPath : buildFullPaths) {
if (buildFullPath.isPrefixOf(rc.getFullPath()))
return false;
}
return true;
}
public void updateOtherConfigs(IResourceDelta delta) {
if (delta == null)
resourceChangeState = ~0;
else {
try {
delta.accept(this);
} catch (CoreException e) {
resourceChangeState = ~0;
}
}
setResourceChangeStateForOtherConfigs();
}
private void setResourceChangeStateForOtherConfigs() {
for (Configuration otherConfig : otherConfigs) {
otherConfig.addResourceChangeState(resourceChangeState);
}
}
private boolean isResourceValuable(IResource rc) {
IPath path = rc.getProjectRelativePath();
for (IPath ignoredPath : ignoreList) {
if (ignoredPath.equals(path))
return false;
}
return true;
}
}
protected boolean isCdtProjectCreated(IProject project) {
ICProjectDescription des = CoreModel.getDefault().getProjectDescription(project, false);
return des != null && !des.isCdtProjectCreating();
}
private class MyBoolean {
private boolean value;
public MyBoolean(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
public void setValue(boolean value) {
this.value = value;
}
}
/**
* @see IncrementalProjectBuilder#build
*/
@Override
protected IProject[] build(int kind, @SuppressWarnings("rawtypes") Map argsMap, IProgressMonitor monitor)
throws CoreException {
@SuppressWarnings("unchecked")
Map<String, String> args = argsMap;
if (DEBUG_EVENTS)
printEvent(kind, args);
fBuildSet.start(this);
builtRefConfigIds.clear();
scheduledConfigIds.clear();
IProject project = getProject();
if (!isCdtProjectCreated(project))
return project.getReferencedProjects();
if (VERBOSE)
outputTrace(project.getName(), ">>build requested, type = " + kind); //$NON-NLS-1$
IProject[] projects = null;
if (needAllConfigBuild()) {
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
IConfiguration[] cfgs = info.getManagedProject().getConfigurations();
IConfiguration defCfg = info.getDefaultConfiguration();
for (IConfiguration cfg : cfgs) {
info.setDefaultConfiguration(cfg);
IBuilder builders[] = ManagedBuilderCorePlugin.createBuilders(project, args);
projects = build(kind, project, builders, true, monitor, new MyBoolean(false));
}
info.setDefaultConfiguration(defCfg);
} else {
IBuilder builders[] = ManagedBuilderCorePlugin.createBuilders(project, args);
projects = build(kind, project, builders, true, monitor, new MyBoolean(false));
}
if (VERBOSE)
outputTrace(project.getName(), "<<done build requested, type = " + kind); //$NON-NLS-1$
return projects;
}
protected IProject[] build(int kind, IProject project, IBuilder[] builders, boolean isForeground,
IProgressMonitor monitor) throws CoreException {
return build(kind, project, builders, isForeground, monitor, new MyBoolean(false));
}
private IProject[] build(int kind, IProject project, IBuilder[] builders, boolean isForeground,
IProgressMonitor monitor, MyBoolean isBuild) throws CoreException {
if (!isCdtProjectCreated(project))
return project.getReferencedProjects();
int num = builders.length;
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
IConfiguration activeCfg = info.getDefaultConfiguration();
IProject[] refProjects = project.getReferencedProjects();
if (num != 0) {
MultiStatus status = checkBuilders(builders, activeCfg);
if (status.getSeverity() != IStatus.OK)
throw new CoreException(status);
scheduledConfigIds.add(activeCfg.getId());
IConfiguration rcfgs[] = getReferencedConfigs(builders);
monitor.beginTask("", num + rcfgs.length); //$NON-NLS-1$
if (rcfgs.length != 0) {
Set<IProject> set = buildReferencedConfigs(rcfgs, new SubProgressMonitor(monitor, 1), isBuild);// = getProjectsSet(cfgs);
if (set.size() != 0) {
set.addAll(Arrays.asList(refProjects));
refProjects = set.toArray(new IProject[set.size()]);
}
}
for (int i = 0; i < num; i++) {
if (isGenerateFileOptionEnabled()) {
CompilationDatabaseGenerator generator = new CompilationDatabaseGenerator(getProject(), activeCfg);
generator.generate();
}
//bug 219337
if (kind == INCREMENTAL_BUILD || kind == AUTO_BUILD) {
if (buildConfigResourceChanges()) { //only build projects with project resource changes
IResourceDelta delta = getDelta(project);
if (delta != null && delta.getAffectedChildren().length > 0) { //project resource has changed within Eclipse, need to build this configuration
isBuild.setValue(true);
build(kind, new CfgBuildInfo(builders[i], isForeground),
new SubProgressMonitor(monitor, 1));
} else if (isBuild.getValue()) { //one of its dependencies have rebuilt, need to rebuild this configuration
build(kind, new CfgBuildInfo(builders[i], isForeground),
new SubProgressMonitor(monitor, 1));
}
} else { //the default behaviour: 'make' is invoked on all configurations and incremental build is handled by 'make'
build(kind, new CfgBuildInfo(builders[i], isForeground), new SubProgressMonitor(monitor, 1));
}
} else { //FULL_BUILD or CLEAN
build(kind, new CfgBuildInfo(builders[i], isForeground), new SubProgressMonitor(monitor, 1));
}
}
scheduledConfigIds.remove(activeCfg.getId());
}
if (isForeground)
updateOtherConfigs(info, builders, kind);
monitor.done();
return refProjects;
}
private Set<IProject> buildReferencedConfigs(IConfiguration[] cfgs, IProgressMonitor monitor,
MyBoolean refConfigChanged) {
Set<IProject> projSet = getProjectsSet(cfgs);
cfgs = filterConfigsToBuild(cfgs);
MyBoolean nextConfigChanged = new MyBoolean(false);
if (cfgs.length != 0) {
monitor.beginTask(ManagedMakeMessages.getResourceString("CommonBuilder.22"), cfgs.length); //$NON-NLS-1$
for (IConfiguration cfg : cfgs) {
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
if (builtRefConfigIds.contains(cfg.getId())) {
subMonitor.done();
continue;
}
nextConfigChanged.setValue(false);
try {
IBuilder builder = cfg.getEditableBuilder();
// CfgBuildInfo bInfo = new CfgBuildInfo(builder, false);
if (VERBOSE)
outputTrace(cfg.getOwner().getProject().getName(),
">>>>building reference cfg " + cfg.getName()); //$NON-NLS-1$
IProject[] projs = build(INCREMENTAL_BUILD, cfg.getOwner().getProject(), new IBuilder[] { builder },
false, subMonitor, nextConfigChanged);
if (VERBOSE)
outputTrace(cfg.getOwner().getProject().getName(),
"<<<<done building reference cfg " + cfg.getName()); //$NON-NLS-1$
projSet.addAll(Arrays.asList(projs));
} catch (CoreException e) {
ManagedBuilderCorePlugin.log(e);
} finally {
builtRefConfigIds.add(cfg.getId());
subMonitor.done();
}
refConfigChanged.setValue(refConfigChanged.getValue() || nextConfigChanged.getValue());
}
} else {
monitor.done();
}
return projSet;
}
private IConfiguration[] filterConfigsToBuild(IConfiguration[] cfgs) {
List<IConfiguration> cfgList = new ArrayList<>(cfgs.length);
for (IConfiguration cfg : cfgs) {
IProject project = cfg.getOwner().getProject();
fBuildSet.getCfgIdSet(project, true).add(cfg.getId());
if (scheduledConfigIds.contains(cfg.getId())) {
ManagedBuilderCorePlugin.log(new Status(IStatus.WARNING, ManagedBuilderCorePlugin.getUniqueIdentifier(),
MessageFormat.format(ManagedMakeMessages.getString("CommonBuilder.circular_dependency"), //$NON-NLS-1$
project.getName(), cfg.getName())));
continue;
}
if (!builtRefConfigIds.contains(cfg.getId())) {
if (VERBOSE) {
outputTrace(cfg.getOwner().getProject().getName(),
"set: adding cfg " + cfg.getName() + " ( id=" + cfg.getId() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
outputTrace(cfg.getOwner().getProject().getName(),
"filtering regs: adding cfg " + cfg.getName() + " ( id=" + cfg.getId() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
cfgList.add(cfg);
} else if (VERBOSE)
outputTrace(cfg.getOwner().getProject().getName(),
"filtering regs: excluding cfg " + cfg.getName() + " ( id=" + cfg.getId() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
return cfgList.toArray(new IConfiguration[cfgList.size()]);
}
@Override
protected void startupOnInitialize() {
super.startupOnInitialize();
}
private IConfiguration[] getReferencedConfigs(IBuilder[] builders) {
Set<IConfiguration> set = new HashSet<>();
for (IBuilder builder : builders) {
IConfiguration cfg = builder.getParent().getParent();
IConfiguration refs[] = ManagedBuildManager.getReferencedConfigurations(cfg);
for (IConfiguration ref : refs) {
set.add(ref);
}
}
return set.toArray(new Configuration[set.size()]);
}
private Set<IProject> getProjectsSet(IConfiguration[] cfgs) {
if (cfgs.length == 0)
return new HashSet<>(0);
Set<IProject> set = new HashSet<>();
for (IConfiguration cfg : cfgs) {
set.add(cfg.getOwner().getProject());
}
return set;
}
protected MultiStatus checkBuilders(IBuilder builders[], IConfiguration activeCfg) {
MultiStatus status = null;
for (IBuilder builder : builders) {
boolean supportsCustomization = builder.supportsCustomizedBuild();
boolean isManagedBuildOn = builder.isManagedBuildOn();
if (isManagedBuildOn && !supportsCustomization) {
if (builder.isCustomBuilder()) {
if (status == null) {
status = new MultiStatus(ManagedBuilderCorePlugin.getUniqueIdentifier(), IStatus.ERROR, "", //$NON-NLS-1$
null);
}
status.add(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(), 0,
ManagedMakeMessages.getResourceString("CommonBuilder.1"), //$NON-NLS-1$
null));
} else if (builder.getParent().getParent() != activeCfg) {
if (status == null) {
status = new MultiStatus(ManagedBuilderCorePlugin.getUniqueIdentifier(), IStatus.ERROR, "", //$NON-NLS-1$
null);
}
status.add(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(), 0,
ManagedMakeMessages.getResourceString("CommonBuilder.2"), //$NON-NLS-1$
null));
}
}
}
if (status == null) {
status = new MultiStatus(ManagedBuilderCorePlugin.getUniqueIdentifier(), IStatus.OK, "", //$NON-NLS-1$
null);
}
return status;
}
private void updateOtherConfigs(IManagedBuildInfo info, IBuilder builders[], int buildKind) {
IConfiguration allCfgs[] = info.getManagedProject().getConfigurations();
new OtherConfigVerifier(builders, allCfgs).updateOtherConfigs(
buildKind == FULL_BUILD ? null : getDelta(info.getManagedProject().getOwner().getProject()));
}
protected class BuildStatus {
private final boolean fManagedBuildOn;
private boolean fRebuild;
private boolean fBuild = true;
private final List<String> fConsoleMessages = new ArrayList<>();
private IManagedBuilderMakefileGenerator fMakeGen;
public BuildStatus(IBuilder builder) {
fManagedBuildOn = builder.isManagedBuildOn();
}
public void setRebuild() {
fRebuild = true;
}
public boolean isRebuild() {
return fRebuild;
}
public boolean isManagedBuildOn() {
return fManagedBuildOn;
}
public boolean isBuild() {
return fBuild;
}
public void cancelBuild() {
fBuild = false;
}
public List<String> getConsoleMessagesList() {
return fConsoleMessages;
}
public IManagedBuilderMakefileGenerator getMakeGen() {
return fMakeGen;
}
public void setMakeGen(IManagedBuilderMakefileGenerator makeGen) {
fMakeGen = makeGen;
}
}
protected void build(int kind, CfgBuildInfo bInfo, IProgressMonitor monitor) throws CoreException {
if (VERBOSE)
outputTrace(bInfo.getProject().getName(), "building cfg " + bInfo.getConfiguration().getName() //$NON-NLS-1$
+ " with builder " + bInfo.getBuilder().getName()); //$NON-NLS-1$
IBuilder builder = bInfo.getBuilder();
BuildStatus status = new BuildStatus(builder);
if (!shouldBuild(kind, builder)) {
return;
}
// if (kind == IncrementalProjectBuilder.AUTO_BUILD) {
// IResourceDelta delta = getDelta(getProject());
// if (delta != null) {
// IResource res = delta.getResource();
// if (res != null) {
// bPerformBuild = res.getProject().equals(getProject());
// }
// } else {
// bPerformBuild = false;
// }
// }
if (status.isBuild()) {
IConfiguration cfg = bInfo.getConfiguration();
if (!builder.isCustomBuilder()) {
Set<String> set = fBuildSet.getCfgIdSet(bInfo.getProject(), true);
if (VERBOSE)
outputTrace(bInfo.getProject().getName(),
"set: adding cfg " + cfg.getName() + " ( id=" + cfg.getId() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
set.add(cfg.getId());
}
if (status.isManagedBuildOn()) {
status = performPrebuildGeneration(kind, bInfo, status, monitor);
}
if (status.isBuild()) {
try {
// Set the current project for markers creation
setCurrentProject(bInfo.getProject());
boolean isClean = builder.getBuildRunner().invokeBuild(kind, bInfo.getProject(),
bInfo.getConfiguration(), builder, bInfo.getConsole(), this, this, monitor);
if (isClean) {
forgetLastBuiltState();
cfg.setRebuildState(true);
} else {
if (status.isManagedBuildOn()) {
performPostbuildGeneration(kind, bInfo, status, monitor);
}
cfg.setRebuildState(false);
}
} catch (CoreException e) {
cfg.setRebuildState(true);
throw e;
}
PropertyManager.getInstance().serialize(cfg);
} else if (status.getConsoleMessagesList().size() != 0) {
emitMessage(bInfo, concatMessages(status.getConsoleMessagesList()));
}
}
checkCancel(monitor);
}
private String concatMessages(List<String> msgs) {
int size = msgs.size();
if (size == 0) {
return ""; //$NON-NLS-1$
} else if (size == 1) {
return msgs.get(0);
}
StringBuilder buf = new StringBuilder();
buf.append(msgs.get(0));
for (int i = 1; i < size; i++) {
buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$ //$NON-NLS-2$
buf.append(msgs.get(i));
}
return buf.toString();
}
/* (non-javadoc)
* Emits a message to the console indicating that there were no source files to build
* @param buildType
* @param status
* @param configName
*/
private String createNoSourceMessage(int buildType, IStatus status, CfgBuildInfo bInfo) throws CoreException {
StringBuilder buf = new StringBuilder();
String[] consoleHeader = new String[3];
String configName = bInfo.getConfiguration().getName();
String projName = bInfo.getProject().getName();
if (buildType == FULL_BUILD || buildType == INCREMENTAL_BUILD) {
consoleHeader[0] = ManagedMakeMessages.getResourceString("ManagedMakeBuider.type.incremental"); //$NON-NLS-1$
} else {
consoleHeader[0] = ""; //$NON-NLS-1$
outputError(projName, "The given build type is not supported in this context"); //$NON-NLS-1$
}
consoleHeader[1] = configName;
consoleHeader[2] = projName;
buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$ //$NON-NLS-2$
buf.append(ManagedMakeMessages.getFormattedString("ManagedMakeBuilder.message.console.header", consoleHeader)); //$NON-NLS-1$
buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$ //$NON-NLS-2$
buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$ //$NON-NLS-2$
buf.append(status.getMessage());
buf.append(System.getProperty("line.separator", "\n")); //$NON-NLS-1$//$NON-NLS-2$
return buf.toString();
}
private void emitMessage(CfgBuildInfo info, String msg) throws CoreException {
try {
IConsole console = info.getConsole();
ConsoleOutputStream consoleOutStream = console.getOutputStream();
// Report a successful clean
consoleOutStream.write(msg.getBytes());
consoleOutStream.flush();
consoleOutStream.close();
} catch (CoreException e) {
// Throw the exception back to the builder
throw e;
} catch (IOException io) { // Ignore console failures...
throw new CoreException(new Status(IStatus.ERROR, ManagedBuilderCorePlugin.getUniqueIdentifier(),
io.getLocalizedMessage(), io));
}
}
protected BuildStatus performPostbuildGeneration(int kind, CfgBuildInfo bInfo, BuildStatus buildStatus,
IProgressMonitor monitor) throws CoreException {
IBuilder builder = bInfo.getBuilder();
if (builder.isInternalBuilder())
return buildStatus;
if (buildStatus.isRebuild()) {
buildStatus.getMakeGen().regenerateDependencies(false);
} else {
buildStatus.getMakeGen().generateDependencies();
}
return buildStatus;
}
protected BuildStatus performPrebuildGeneration(int kind, CfgBuildInfo bInfo, BuildStatus buildStatus,
IProgressMonitor monitor) throws CoreException {
IBuilder builder = bInfo.getBuilder();
if (builder.isInternalBuilder())
return buildStatus;
buildStatus = performCleanning(kind, bInfo, buildStatus, monitor);
IManagedBuilderMakefileGenerator generator = builder.getBuildFileGenerator();
if (generator != null) {
initializeGenerator(generator, kind, bInfo, monitor);
buildStatus.setMakeGen(generator);
MultiStatus result = performMakefileGeneration(bInfo, generator, buildStatus, monitor);
if (result.getCode() == IStatus.WARNING || result.getCode() == IStatus.INFO) {
IStatus[] kids = result.getChildren();
for (int index = 0; index < kids.length; ++index) {
// One possibility is that there is nothing to build
IStatus status = kids[index];
// if(messages == null){
// messages = new MultiStatus(
// ManagedBuilderCorePlugin.getUniqueIdentifier(),
// IStatus.INFO,
// "",
// null);
//
// }
if (status.getCode() == IManagedBuilderMakefileGenerator.NO_SOURCE_FOLDERS) {
// performBuild = false;
buildStatus.getConsoleMessagesList().add(createNoSourceMessage(kind, status, bInfo));
buildStatus.cancelBuild();
// break;
} else {
// Stick this in the list of stuff to warn the user about
//TODO: messages.add(status);
}
}
} else if (result.getCode() == IStatus.ERROR) {
StringBuilder buf = new StringBuilder();
buf.append(ManagedMakeMessages.getString("CommonBuilder.23")).append(NEWLINE); //$NON-NLS-1$
String message = result.getMessage();
if (message != null && message.length() != 0) {
buf.append(message).append(NEWLINE);
}
buf.append(ManagedMakeMessages.getString("CommonBuilder.24")).append(NEWLINE); //$NON-NLS-1$
message = buf.toString();
buildStatus.getConsoleMessagesList().add(message);
buildStatus.cancelBuild();
}
checkCancel(monitor);
// if(result.getSeverity() != IStatus.OK)
// throw new CoreException(result);
} else {
buildStatus.cancelBuild();
}
// if(messages == null){
// messages = createMultiStatus(IStatus.OK);
// }
return buildStatus;
}
protected BuildStatus performCleanning(int kind, CfgBuildInfo bInfo, BuildStatus status, IProgressMonitor monitor)
throws CoreException {
IConfiguration cfg = bInfo.getConfiguration();
IProject curProject = bInfo.getProject();
// IBuilder builder = bInfo.getBuilder();
boolean makefileRegenerationNeeded = false;
//perform necessary cleaning and build type calculation
if (cfg.needsFullRebuild()) {
//configuration rebuild state is set to true,
//full rebuild is needed in any case
//clean first, then make a full build
outputTrace(curProject.getName(), "config rebuild state is set to true, making a full rebuild"); //$NON-NLS-1$
clean(bInfo, new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
makefileRegenerationNeeded = true;
} else {
makefileRegenerationNeeded = cfg.needsRebuild();
IBuildDescription des = null;
IResourceDelta delta = kind == FULL_BUILD ? null : getDelta(curProject);
if (delta == null)
makefileRegenerationNeeded = true;
if (cfg.needsRebuild() || delta != null) {
//use a build desacription model to calculate the resources to be cleaned
//only in case there are some changes to the project sources or build information
try {
int flags = BuildDescriptionManager.REBUILD | BuildDescriptionManager.DEPFILES
| BuildDescriptionManager.DEPS;
if (delta != null)
flags |= BuildDescriptionManager.REMOVED;
outputTrace(curProject.getName(), "using a build description.."); //$NON-NLS-1$
des = BuildDescriptionManager.createBuildDescription(cfg, getDelta(curProject), flags);
BuildDescriptionManager.cleanGeneratedRebuildResources(des);
} catch (Throwable e) {
//TODO: log error
outputError(curProject.getName(),
"error occured while build description calculation: " + e.getLocalizedMessage()); //$NON-NLS-1$
//in case an error occured, make it behave in the old stile:
if (cfg.needsRebuild()) {
//make a full clean if an info needs a rebuild
clean((Map<String, String>) null, new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
makefileRegenerationNeeded = true;
} else if (delta != null && !makefileRegenerationNeeded) {
// Create a delta visitor to detect the build type
ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(cfg,
bInfo.getBuildInfo().getManagedProject().getConfigurations());
delta.accept(visitor);