Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7e65b2f71a7cc4b6dc396da2403d48c58c698489 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
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
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
/*******************************************************************************
 * Copyright (c) 2003, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;

import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.internal.core.SafeStringInterner;
import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IConfigurationV2;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedConfigElement;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.ITarget;
import org.eclipse.cdt.managedbuilder.core.ITargetPlatform;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.IToolReference;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.internal.scannerconfig.ManagedBuildCPathEntryContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Version;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Target extends BuildObject implements ITarget {
	private static final String EMPTY_STRING = new String();
	private String artifactName;
	private String binaryParserId;
	private String cleanCommand;
	private List<IConfigurationV2> configList;
	private Map<String, IConfigurationV2> configMap;
	private String defaultExtension;
//	private Map depCalculatorsMap;
	private String errorParserIds;
	private String extension;
	private boolean isAbstract = false;
	private boolean isDirty = false;
	private boolean isTest = false;
	private String makeArguments;
	private String makeCommand;
	private IResource owner;
	private ITarget parent;
	private boolean resolved = true;
	private List<String> targetArchList;
	private List<String> targetOSList;
	private List<ITool> toolList;
	private Map<String, ITool> toolMap;
	private List<ToolReference> toolReferences;
	private ProjectType createdProjectType;
	private String scannerInfoCollectorId;

	/**
	 * This constructor is called to create a target defined by an extension point in
	 * a plugin manifest file.
	 *
	 * @param managedBuildRevision the fileVersion of Managed Build System
	 */
	public Target(IManagedConfigElement element, String managedBuildRevision) {
		// setup for resolving
		ManagedBuildManager.putConfigElement(this, element);
		resolved = false;

		// id
		setId(SafeStringInterner.safeIntern(element.getAttribute(ID)));

		// managedBuildRevision
		setManagedBuildRevision(SafeStringInterner.safeIntern(managedBuildRevision));

		// hook me up
		ManagedBuildManager.addExtensionTarget(this);

		// Get the target name
		setName(SafeStringInterner.safeIntern(element.getAttribute(NAME)));

		// Get the name of the build artifact associated with target (usually
		// in the plugin specification).
		artifactName = SafeStringInterner.safeIntern(element.getAttribute(ARTIFACT_NAME));

		// Get the ID of the binary parser
		binaryParserId = SafeStringInterner.safeIntern(element.getAttribute(BINARY_PARSER));

		// Get the semicolon separated list of IDs of the error parsers
		errorParserIds = SafeStringInterner.safeIntern(element.getAttribute(ERROR_PARSERS));

		// Get the default extension
		defaultExtension = SafeStringInterner.safeIntern(element.getAttribute(DEFAULT_EXTENSION));

		// isAbstract
		isAbstract = ("true".equals(element.getAttribute(IS_ABSTRACT))); //$NON-NLS-1$

		// Is this a test target
		isTest = ("true".equals(element.getAttribute(IS_TEST))); //$NON-NLS-1$

		// Get the clean command
		cleanCommand = SafeStringInterner.safeIntern(element.getAttribute(CLEAN_COMMAND));

		// Get the make command
		makeCommand = SafeStringInterner.safeIntern(element.getAttribute(MAKE_COMMAND));

		// Get the make arguments
		makeArguments = SafeStringInterner.safeIntern(element.getAttribute(MAKE_ARGS));

		// Get scannerInfoCollectorId
		scannerInfoCollectorId = SafeStringInterner.safeIntern(element.getAttribute(SCANNER_INFO_COLLECTOR_ID));

		// Get the comma-separated list of valid OS
		String os = element.getAttribute(OS_LIST);
		if (os != null) {
			targetOSList = new ArrayList<String>();
			String[] osTokens = os.split(","); //$NON-NLS-1$
			for (int i = 0; i < osTokens.length; ++i) {
				targetOSList.add(SafeStringInterner.safeIntern(osTokens[i].trim()));
			}
		}

		// Get the comma-separated list of valid Architectures
		String arch = element.getAttribute(ARCH_LIST);
		if (arch != null) {
			targetArchList = new ArrayList<String>();
			String[] archTokens = arch.split(","); //$NON-NLS-1$
			for (int j = 0; j < archTokens.length; ++j) {
				targetArchList.add(SafeStringInterner.safeIntern(archTokens[j].trim()));
			}
		}

		// Load any tool references we might have
		IManagedConfigElement[] toolRefs = element.getChildren(IConfigurationV2.TOOLREF_ELEMENT_NAME);
		for (int k = 0; k < toolRefs.length; ++k) {
			new ToolReference(this, toolRefs[k]);
		}
		// Then load any tools defined for the target
		IManagedConfigElement[] tools = element.getChildren(ITool.TOOL_ELEMENT_NAME);
		for (int m = 0; m < tools.length; ++m) {
			ITool newTool =  new Tool(this, tools[m], managedBuildRevision);
			// Add this tool to the target, as this is not done in the constructor
			this.addTool(newTool);
		}
		// Then load the configurations which may have tool references
		IManagedConfigElement[] configs = element.getChildren(IConfigurationV2.CONFIGURATION_ELEMENT_NAME);
		for (int n = 0; n < configs.length; ++n) {
			new ConfigurationV2(this, configs[n]);
		}
	}

	/**
	 * Set the resource that owns the target.
	 */
	protected Target(IResource owner) {
		this.owner = owner;
	}

	/**
	 * Create a copy of the target specified in the argument,
	 * that is owned by the owned by the specified resource.
	 */
	public Target(IResource owner, ITarget parent) {
		// Make the owner of the target the project resource
		this(owner);

		// Copy the parent's identity
		this.parent = parent;
		int id = ManagedBuildManager.getRandomNumber();
		setId(owner.getName() + "." + parent.getId() + "." + id);		 //$NON-NLS-1$ //$NON-NLS-2$
		setName(parent.getName());

		setManagedBuildRevision(parent.getManagedBuildRevision());

		setArtifactName(parent.getArtifactName());
		this.binaryParserId = parent.getBinaryParserId();
		this.errorParserIds = parent.getErrorParserIds();
		this.defaultExtension = parent.getArtifactExtension();
		this.isTest = parent.isTestTarget();
		this.cleanCommand = parent.getCleanCommand();
		this.scannerInfoCollectorId = ((Target)parent).scannerInfoCollectorId;

		// Hook me up
		IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(owner);
		buildInfo.addTarget(this);
	}

	/**
	 * Create target from project file.
	 */
	public Target(ManagedBuildInfo buildInfo, Element element) {
		this(buildInfo.getOwner());

		// id
		setId(element.getAttribute(ID));

		// hook me up
		buildInfo.addTarget(this);

		// name
		setName(element.getAttribute(NAME));

		// Get the name of the build artifact associated with target (should
		// contain what the user entered in the UI).
		artifactName = element.getAttribute(ARTIFACT_NAME);

		// Get the overridden extension
		if (element.hasAttribute(EXTENSION)) {
			extension = element.getAttribute(EXTENSION);
		}

		// parent
		String parentId = element.getAttribute(PARENT);
		if (parentId != null)
			parent = ManagedBuildManager.getTarget(null, parentId);

		// isAbstract
		if ("true".equals(element.getAttribute(IS_ABSTRACT))) //$NON-NLS-1$
			isAbstract = true;

		// Is this a test target
		isTest = ("true".equals(element.getAttribute(IS_TEST))); //$NON-NLS-1$

		// Get the clean command
		if (element.hasAttribute(CLEAN_COMMAND)) {
			cleanCommand = element.getAttribute(CLEAN_COMMAND);
		}

		// Get the semicolon separated list of IDs of the error parsers
		if (element.hasAttribute(ERROR_PARSERS)) {
			errorParserIds = element.getAttribute(ERROR_PARSERS);
		}

		// Get the make command and arguments
		if (element.hasAttribute(MAKE_COMMAND)) {
			makeCommand = element.getAttribute(MAKE_COMMAND);
		}
		if(element.hasAttribute(MAKE_ARGS)) {
			makeArguments = element.getAttribute(MAKE_ARGS);
		}

		Node child = element.getFirstChild();
		while (child != null) {
			if (child.getNodeName().equals(IConfigurationV2.CONFIGURATION_ELEMENT_NAME)) {
				new ConfigurationV2(this, (Element)child);
			}
			child = child.getNextSibling();
		}
	}

	public void addConfiguration(IConfigurationV2 configuration) {
		getConfigurationList().add(configuration);
		getConfigurationMap().put(configuration.getId(), configuration);
	}

	/**
	 * Adds a tool specification to the receiver. This tool is defined
	 * only for the receiver, and cannot be shared by other targets.
	 */
	public void addTool(ITool tool) {
		getToolList().add(tool);
		getToolMap().put(tool.getId(), tool);
	}

	/**
	 * Adds a tool reference to the receiver.
	 */
	public void addToolReference(ToolReference toolRef) {
		getLocalToolReferences().add(toolRef);
	}


	/**
	 * Tail-recursion method that creates a lits of tools and tool reference
	 * walking the receiver's parent hierarchy.
	 */
	private void addToolsToArray(Vector<ITool> toolArray) {
		if (parent != null) {
			((Target)parent).addToolsToArray(toolArray);
		}

		//	Add the tools from out own list
		toolArray.addAll(getToolList());

		// Add local tool references
		toolArray.addAll(getLocalToolReferences());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#createConfiguration(org.eclipse.cdt.core.build.managed.IConfigurationV2)
	 */
	@Override
	public IConfigurationV2 createConfiguration(IConfigurationV2 parent, String id) {
		isDirty = true;
		return new ConfigurationV2(this, parent, id);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#createConfiguration()
	 */
	@Override
	public IConfigurationV2 createConfiguration(String id) {
		return new ConfigurationV2(this, id);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getArtifactExtension()
	 */
	@Override
	public String getArtifactExtension() {
		// Has the user changed the extension for this target
		if (extension != null) {
			return extension;
		}
		// If not, then go through the default extension lookup
		if (defaultExtension == null) {
			// Ask my parent first
			if (parent != null) {
				return parent.getArtifactExtension();
			} else {
				return EMPTY_STRING;
			}
		} else {
			return defaultExtension;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#getArtifactName()
	 */
	@Override
	public String getArtifactName() {
		if (artifactName == null) {
			// If I have a parent, ask it
			if (parent != null) {
				return parent.getArtifactName();
			} else {
				// I'm it and this is not good!
				return EMPTY_STRING;
			}
		} else {
			return artifactName;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getBinaryParserId()
	 */
	@Override
	public String getBinaryParserId() {
		if (binaryParserId == null) {
			// If I have a parent, ask it
			if (parent != null) {
				return parent.getBinaryParserId();
			} else {
				// I'm it and this is not good!
				return EMPTY_STRING;
			}
		}
		return binaryParserId;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#getCleanCommand()
	 */
	@Override
	public String getCleanCommand() {
		// Return the command used to remove files
		if (cleanCommand == null) {
			if (parent != null) {
				return parent.getCleanCommand();
			} else {
				// User forgot to specify it. Guess based on OS.
				if (Platform.getOS().equals(Platform.OS_WIN32)) {
					return new String("del"); //$NON-NLS-1$
				} else {
					return new String("rm"); //$NON-NLS-1$
				}
			}
		} else {
			// This was spec'd in the manifest
			return cleanCommand;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#getConfiguration()
	 */
	@Override
	public IConfigurationV2 getConfiguration(String id) {
		return getConfigurationMap().get(id);
	}

	/**
	 * Safe accessor for the list of configurations.
	 *
	 * @return List containing the configurations
	 */
	private List<IConfigurationV2> getConfigurationList() {
		if (configList == null) {
			configList = new ArrayList<IConfigurationV2>();
		}
		return configList;
	}

	/**
	 * Safe accessor for the map of configuration ids to configurations
	 */
	private Map<String, IConfigurationV2> getConfigurationMap() {
		if (configMap == null) {
			configMap = new HashMap<String, IConfigurationV2>();
		}
		return configMap;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getConfigurations()
	 */
	@Override
	public IConfigurationV2[] getConfigurations() {
		return getConfigurationList().toArray(new IConfigurationV2[getConfigurationList().size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getDefaultExtension()
	 */
	@Override
	public String getDefaultExtension() {
		return defaultExtension == null ? EMPTY_STRING : defaultExtension;
	}

//	private Map getDepCalcMap() {
//		if (depCalculatorsMap == null) {
//			depCalculatorsMap = new HashMap();
//		}
//		return depCalculatorsMap;
//	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getErrorParserIds()
	 */
	@Override
	public String getErrorParserIds() {
		if (errorParserIds == null) {
			// If I have a parent, ask it
			if (parent != null) {
				return parent.getErrorParserIds();
			}
		}
		return errorParserIds;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getErrorParserList()
	 */
	@Override
	public String[] getErrorParserList() {
		String parserIDs = getErrorParserIds();
		String[] errorParsers = null;
		if (parserIDs != null) {
			// Check for an empty string
			if (parserIDs.length() == 0) {
				errorParsers = new String[0];
			} else {
				StringTokenizer tok = new StringTokenizer(parserIDs, ";"); //$NON-NLS-1$
				List<String> list = new ArrayList<String>(tok.countTokens());
				while (tok.hasMoreElements()) {
					list.add(tok.nextToken());
				}
				String[] strArr = {""};	//$NON-NLS-1$
				errorParsers = list.toArray(strArr);
			}
		} else {
			// If no error parsers are specified by the target, the default is
			// all error parsers
			errorParsers = ErrorParserManager.getErrorParserAvailableIdsInContext(ErrorParserManager.BUILD_CONTEXT);
		}
		return errorParsers;
	}

	/**
	 * A safe accesor method. It answers the tool reference list in the
	 * receiver.
	 *
	 * @return List
	 */
	protected List<ToolReference> getLocalToolReferences() {
		if (toolReferences == null) {
			toolReferences = new ArrayList<ToolReference>();
		}
		return toolReferences;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getMakeArguments()
	 */
	@Override
	public String getMakeArguments() {
		if (makeArguments == null) {
			// See if it is defined in my parent
			if (parent != null) {
				return parent.getMakeArguments();
			} else {
				// No parent and no user setting
				return new String(""); //$NON-NLS-1$
			}
		}
		return makeArguments;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#getMakeCommand()
	 */
	@Override
	public String getMakeCommand() {
		// Return the name of the make utility
		if (makeCommand == null) {
			// If I have a parent, ask it
			if (parent != null) {
				return parent.getMakeCommand();
			} else {
				// The user has forgotten to specify a command in the plugin manifest
				return new String("make"); //$NON-NLS-1$
			}
		} else {
			return makeCommand;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.IBuildObject#getName()
	 */
	@Override
	public String getName() {
		// If I am unnamed, see if I can inherit one from my parent
		if (name == null) {
			if (parent != null) {
				return parent.getName();
			} else {
				return new String(""); //$NON-NLS-1$
			}
		} else {
			return name;
		}
	}

	protected List<OptionReference> getOptionReferences(ITool tool) {
		List<OptionReference> references = new ArrayList<OptionReference>();

		// Get all the option references I add for this tool
		ToolReference toolRef = getToolReference(tool);
		if (toolRef != null) {
			references.addAll(toolRef.getOptionReferenceList());
		}

		// See if there is anything that my parents add that I don't
		if (parent != null) {
			List<OptionReference> refs = ((Target)parent).getOptionReferences(tool);
			for (OptionReference ref : refs) {
				if (!references.contains(ref)) {
					references.add(ref);
				}
			}
		}

		return references;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getOwner()
	 */
	@Override
	public IResource getOwner() {
		return owner;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getParent()
	 */
	@Override
	public ITarget getParent() {
		return parent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getTargetArchList()
	 */
	@Override
	public String[] getTargetArchList() {
		if (targetArchList == null) {
			// Ask parent for its list
			if (parent != null) {
				return parent.getTargetArchList();
			} else {
				// I have no parent and no defined list
				return new String[] {"all"}; //$NON-NLS-1$
			}
		}
		return targetArchList.toArray(new String[targetArchList.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getTargetOSList()
	 */
	@Override
	public String[] getTargetOSList() {
		if (targetOSList == null) {
			// Ask parent for its list
			if (parent != null) {
				return parent.getTargetOSList();
			} else {
				// I have no parent and no defined filter list
				return new String[] {"all"};	//$NON-NLS-1$
			}
		}
		return targetOSList.toArray(new String[targetOSList.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getTool(java.lang.String)
	 */
	@Override
	public ITool getTool(String id) {
		ITool result = null;

		// See if receiver has it in list
		result = getToolMap().get(id);

		// If not, check if parent has it
		if (result == null && parent != null) {
			result = ((Target)parent).getTool(id);
		}

		// If not defined in parents, check if defined at all
		if (result == null) {
			result = ManagedBuildManager.getExtensionTool(id);
		}

		return result;
	}

	/**
	 * A safe accessor method for the list of tools maintained by the
	 * target
	 *
	 */
	private List<ITool> getToolList() {
		if (toolList == null) {
			toolList = new ArrayList<ITool>();
		}
		return toolList;
	}

	/**
	 * A safe accessor for the tool map
	 *
	 */
	private Map<String, ITool> getToolMap() {
		if (toolMap == null) {
			toolMap = new HashMap<String, ITool>();
		}
		return toolMap;
	}

	/**
	 * Returns the reference for a given tool or <code>null</code> if one is not
	 * found.
	 */
	private ToolReference getToolReference(ITool tool) {
		// See if the receiver has a reference to the tool
		ToolReference ref = null;
		if (tool == null) return ref;
		List<ToolReference> localToolReferences = getLocalToolReferences();
		for (ToolReference temp : localToolReferences) {
			if (temp.references(tool)) {
				ref = temp;
				break;
			}
		}
		return ref;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getTools()
	 */
	@Override
	public ITool[] getTools() {
		Vector<ITool> toolArray = new Vector<ITool>();
		addToolsToArray(toolArray);
		return toolArray.toArray(new ITool[toolArray.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#hasMakeCommandOverride()
	 */
	@Override
	public boolean hasOverridenMakeCommand() {
		// We answer true if the make command or the flags are different
		return ((makeCommand != null && !makeCommand.equals(parent.getMakeCommand()))
			|| (makeArguments != null && !makeArguments.equals(parent.getMakeArguments())));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#isAbstract()
	 */
	@Override
	public boolean isAbstract() {
		return isAbstract;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#isDirty()
	 */
	@Override
	public boolean isDirty() {
		// If I need saving, just say yes
		if (isDirty) {
			return true;
		}

		// Iterate over the configurations and ask them if they need saving
		List<IConfigurationV2> configurationList = getConfigurationList();
		for (IConfigurationV2 cfgV2 : configurationList) {
			if (cfgV2.isDirty()) {
				return true;
			}
		}

		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#isTestTarget()
	 */
	@Override
	public boolean isTestTarget() {
		return isTest;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#needsRebuild()
	 */
	@Override
	public boolean needsRebuild(){
		// Iterate over the configurations and ask them if they need saving
		List<IConfigurationV2> configurationList = getConfigurationList();
		for (IConfigurationV2 cfgV2 : configurationList) {
			if (cfgV2.needsRebuild()) {
				return true;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#removeConfiguration(java.lang.String)
	 */
	@Override
	public void removeConfiguration(String id) {
		// Remove the specified configuration from the list and map
		List<IConfigurationV2> configurationList = getConfigurationList();
		for (IConfigurationV2 config : configurationList) {
			 if (config.getId().equals(id)) {
			 	configurationList.remove(config);
				getConfigurationMap().remove(id);
				isDirty = true;
			 	break;
			 }
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#resetMakeCommand()
	 */
	public void resetMakeCommand() {
		// Flag target as dirty if the reset actually changes something
		if (makeCommand != null) {
			setDirty(true);
		}
		makeCommand = null;
		makeArguments = null;
	}

	/**
	 *
	 */
	public void resolveReferences() {
		if (!resolved) {
			resolved = true;
			IManagedConfigElement element = ManagedBuildManager.getConfigElement(this);
			// parent
			String parentId = SafeStringInterner.safeIntern(element.getAttribute(PARENT));
			if (parentId != null) {
				parent = ManagedBuildManager.getTarget(null, parentId);
				// should resolve before calling methods on it
				((Target)parent).resolveReferences();
				// copy over the parents configs
				IConfigurationV2[] parentConfigs = parent.getConfigurations();
				for (IConfigurationV2 cfgV2 : parentConfigs) {
					addConfiguration(cfgV2);
				}
			}

			// call resolve references on any children
			List<ITool> toolList = getToolList();
			for (ITool current : toolList) {
				((Tool)current).resolveReferences();
			}
			List<ToolReference> localToolReferences = getLocalToolReferences();
			for (ToolReference current : localToolReferences) {
				current.resolveReferences();
			}
			List<IConfigurationV2> configurationList = getConfigurationList();
			for (IConfigurationV2 current : configurationList) {
				((ConfigurationV2)current).resolveReferences();
			}
		}
	}

	/**
	 * Persist receiver to project file.
	 */
	public void serialize(Document doc, Element element) {
		element.setAttribute(ID, getId());
		element.setAttribute(NAME, getName());
		if (parent != null)
			element.setAttribute(PARENT, parent.getId());
		element.setAttribute(IS_ABSTRACT, isAbstract ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
		element.setAttribute(ARTIFACT_NAME, getArtifactName());
		if (extension != null) {
			element.setAttribute(EXTENSION, extension);
		}
		element.setAttribute(IS_TEST, isTest ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$

		if (makeCommand != null) {
			element.setAttribute(MAKE_COMMAND, makeCommand);
		} else {
			// Make sure we use the default
		}

		if (makeArguments != null) {
			element.setAttribute(MAKE_ARGS, makeArguments);
		}
		if (errorParserIds != null) {
			element.setAttribute(ERROR_PARSERS, errorParserIds);
		}

		// Serialize the configuration settings
		List<IConfigurationV2> configurationList = getConfigurationList();
		for (IConfigurationV2 config : configurationList) {
			Element configElement = doc.createElement(IConfigurationV2.CONFIGURATION_ELEMENT_NAME);
			element.appendChild(configElement);
			((ConfigurationV2)config).serialize(doc, configElement);
		}

		// I am clean now
		isDirty = false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#setArtifactExtension(java.lang.String)
	 */
	@Override
	public void setArtifactExtension(String extension) {
		if (extension != null) {
			this.extension = extension;
			isDirty = true;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.ITarget#setArtifactName(java.lang.String)
	 */
	@Override
	public void setArtifactName(String name) {
		if (name != null) {
			artifactName = name;
			setRebuildState(true);
			isDirty = true;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#setDirty(boolean)
	 */
	@Override
	public void setDirty(boolean isDirty) {
		// Override the dirty flag here
		this.isDirty = isDirty;
		// and in the configurations
		List<IConfigurationV2> configurationList = getConfigurationList();
		for (IConfigurationV2 config : configurationList) {
			config.setDirty(isDirty);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#setErrorParserIds()
	 */
	@Override
	public void setErrorParserIds(String ids) {
		if (ids == null) return;
		String currentIds = getErrorParserIds();
		if (currentIds == null || !(currentIds.equals(ids))) {
			errorParserIds = ids;
			isDirty = true;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#setMakeArguments(java.lang.String)
	 */
	@Override
	public void setMakeArguments(String makeArgs) {
		if (makeArgs != null && !getMakeArguments().equals(makeArgs)) {
			makeArguments = makeArgs;
			setRebuildState(true);
			isDirty = true;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#setMakeCommand(java.lang.String)
	 */
	@Override
	public void setMakeCommand(String command) {
		if (command != null && !getMakeCommand().equals(command)) {
			makeCommand = command;
			setRebuildState(true);
			isDirty = true;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#setRebuildState(boolean)
	 */
	@Override
	public void setRebuildState(boolean rebuild) {
		List<IConfigurationV2> configurationList = getConfigurationList();
		for (IConfigurationV2 config : configurationList) {
			config.setRebuildState(rebuild);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#updateOwner(org.eclipse.core.resources.IResource)
	 */
	@Override
	public void updateOwner(IResource resource) {
		if (!resource.equals(owner)) {
			// Set the owner correctly
			owner = resource;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#convertToProjectType()
	 */
	@Override
	public void convertToProjectType(String managedBuildRevision) {
		// Create a ProjectType + Configuration + Toolchain + Builder + TargetPlatform
		// from the Target

		// The "parent" needs to have been converted already.
		// Do it now if necessary.
		ProjectType parentProj = null;
		if (parent != null) {
			parentProj = parent.getCreatedProjectType();
			if (parentProj == null) {
				parent.convertToProjectType(managedBuildRevision);
				parentProj = parent.getCreatedProjectType();
			}
		}
		ProjectType projectType = new ProjectType(parentProj, getId(), getName(), managedBuildRevision);
		createdProjectType = projectType;
		// Set the project type attributes
		projectType.setIsAbstract(isAbstract);
		projectType.setIsTest(isTest);
		// Add children
		// Add configurations  (Configuration -> ToolChain -> Builder -> TargetPlatform)
		List<IConfigurationV2> configurationList = getConfigurationList();
		for (IConfigurationV2 configV2 : configurationList) {
			if (configV2.getCreatedConfig() != null) continue;
			// The new config's superClass needs to be the
			// Configuration created from the ConfigurationV2 parent...
			IConfiguration configSuperClass = null;
			IConfigurationV2 parentV2 = configV2.getParent();
			if (parentV2 != null) {
				configSuperClass = parentV2.getCreatedConfig();
			}
			String id = configV2.getId();
			String name = configV2.getName();
			IConfiguration config = projectType.createConfiguration(configSuperClass, id, name);
			configV2.setCreatedConfig(config);
			// Set the configuration attributes
			config.setArtifactName(getArtifactName());
			config.setArtifactExtension(getArtifactExtension());
			config.setCleanCommand(getCleanCommand());
			config.setErrorParserIds(getErrorParserIds());
			// Create the Tool-chain
			String subId;
			String subName;
			subId = id + ".toolchain"; 	//$NON-NLS-1$
			subName = name + ".toolchain"; 	//$NON-NLS-1$
			IToolChain toolChain = config.createToolChain(null, subId, subName, true);
			// Set the tool chain attributes
			toolChain.setIsAbstract(isAbstract);
			toolChain.setOSList(getTargetOSList());
			toolChain.setArchList(getTargetArchList());
            // In target element had a scannerInfoCollector element here which
            // is now replaced with scanner config discovery profile id.
            // Using the default per project profile for managed make
			if(scannerInfoCollectorId != null && scannerInfoCollectorId.equals("org.eclipse.cdt.managedbuilder.internal.scannerconfig.DefaultGCCScannerInfoCollector")) 	//$NON-NLS-1$
				toolChain.setScannerConfigDiscoveryProfileId(ManagedBuildCPathEntryContainer.MM_PP_DISCOVERY_PROFILE_ID);
			// Create the Builder
			subId = id + ".builder"; 	//$NON-NLS-1$
			subName = name + ".builder"; 	//$NON-NLS-1$
			IBuilder builder = toolChain.createBuilder(null, subId, subName, true);
			// Set the builder attributes
			builder.setIsAbstract(isAbstract);
			builder.setCommand(getMakeCommand());
			builder.setArguments(getMakeArguments());
            IManagedConfigElement element = ManagedBuildManager.getConfigElement(this);
			if (element instanceof DefaultManagedConfigElement) {
				((Builder)builder).setBuildFileGeneratorElement(((DefaultManagedConfigElement)element).getConfigurationElement());
			}
			// Create the TargetPlatform
			subId = id + ".targetplatform"; 	//$NON-NLS-1$
			subName = name + ".targetplatform"; 	//$NON-NLS-1$
			ITargetPlatform targetPlatform = toolChain.createTargetPlatform(null, subId, subName, true);
			// Set the target platform attributes
			targetPlatform.setIsAbstract(isAbstract);
			targetPlatform.setOSList(getTargetOSList());
			targetPlatform.setArchList(getTargetArchList());
			targetPlatform.setBinaryParserList(new String[]{getBinaryParserId()});  // Older projects will always have only one binary parser set.

			// Handle ConfigurationV2 children (ToolReference)
			// The tools references fetched here are strictly local to the configuration,
			// so additional work is required to fetch the tool references from the target
			IToolReference[] configToolRefs = configV2.getToolReferences();
			// Add the "local" tool references (they are direct children of the target and
			//  its parent targets)
			Vector<IToolReference> targetToolRefs = new Vector<IToolReference>();
			addTargetToolReferences(targetToolRefs);
			IToolReference[] toolRefs;
			if (targetToolRefs.size() > 0) {
				toolRefs = new IToolReference[targetToolRefs.size() + configToolRefs.length];
				int i;
				for (i = 0; i < configToolRefs.length; ++i) {
					toolRefs[i] = configToolRefs[i];
				}
				for (IToolReference toolRef : targetToolRefs) {
					toolRefs[i++] = toolRef;
				}
			} else {
				toolRefs = configToolRefs;
			}
			for (int i = 0; i < toolRefs.length; ++i) {
				IToolReference toolRef = toolRefs[i];
				subId = id + "." + toolRef.getId(); //$NON-NLS-1$
				// The ToolReference's Tool becomes the newTool's SuperClass
				ITool newTool = toolChain.createTool(toolRef.getTool(), subId, toolRef.getName(), true);
				// Set the tool attributes
				newTool.setToolCommand(toolRef.getRawToolCommand());
				newTool.setOutputPrefix(toolRef.getRawOutputPrefix());
				newTool.setOutputFlag(toolRef.getRawOutputFlag());
				newTool.setOutputsAttribute(toolRef.getRawOutputExtensions());
				// Handle ToolReference children (OptionReference)
				List<OptionReference> optionReferenceList = toolRef.getOptionReferenceList();
				for (OptionReference optRef : optionReferenceList) {
					subId = id + "." + optRef.getId(); //$NON-NLS-1$
					IOption newOption = newTool.createOption(optRef.getOption(), subId, optRef.getName(), true);
					// Set the option attributes
					newOption.setValue(optRef.getValue());
					newOption.setValueType(optRef.getValueType());
					((Option)newOption).setWasOptRef(true);
				}
			}

            // Process the tools in the configuration, adding them to the toolchain
			// Tools for a configuration are stored in the enclosing target, so getting
			// the tools for the configuration ultimately gets them from the enclosing target
			ITool[] configTools = configV2.getTools();
			for (int i = 0; i < configTools.length; ++i) {
				ITool tool = configTools[i];
                // If tool references encountered, they have already been processed, above,
				// so ignore them now
				if (!(tool instanceof ToolReference)) {
                	// See if the toolchain already has a tool with a SuperClass that has an id
                	// equal to the tool that we are considering adding to the toolchain; if so,
					// don't add it
					// This case arises when we have added a tool to the toolchain because
					// we processed a ToolReference (above) that references this tool
					// The original tool referenced in the ToolReference becomes the SuperClass
					// of the tool that is created because of the ToolReference
					boolean found = false;
				    ITool[] tools = toolChain.getTools();
				    ITool currentTool;
				    ITool supercurrentTool;
					for (int j = 0; j < tools.length; ++j) {
						currentTool = tools[j];
						supercurrentTool = currentTool.getSuperClass();
						if (supercurrentTool != null) {
						    if (supercurrentTool.getId() == tool.getId()) {
							    found = true;
							    // If this tool was already added to the toolchain because of a
							    // ToolReference, then we disconnent this redundant
							    // tool from the target by setting the parent to null
							    ((Tool)tool).setToolParent(null);
							    break;
						    }
						}
					}

					if (!found)
						// This tool is not in the toolchain yet, so add it to the toolchain
				        ((ToolChain)toolChain).addTool((Tool)tool);

				}
			}
			// Normalize the outputextensions list by adding an empty string for each tool
			// which did not have an explicit output file extension specified
			((ToolChain)toolChain).normalizeOutputExtensions();
		}
	}

	/*
	 *  A target element may contain toolReference elements.  These get applied to all of the configurations
	 *  of the target.  The method adds the list of this target's local tool references to the passed in vector.
	 */
	public void addTargetToolReferences(Vector toolRefs) {
		toolRefs.addAll(getLocalToolReferences());
		if (parent != null) {
			Target targetParent = (Target)parent;
			targetParent.addTargetToolReferences(toolRefs);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.ITarget#getCreatedProjectType()
	 */
	@Override
	public ProjectType getCreatedProjectType() {
		return createdProjectType;
	}

	/**
	 * @return Returns the version.
	 */
	@Override
	public Version getVersion() {
		if ( version == null) {
			if ( getParent() != null) {
				return getParent().getVersion();
			}
		}
		return version;
	}

	@Override
	public void setVersion(Version version) {
		// Do nothing
	}

}

Back to the top