Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae90cf4a416d181712df8a66811641037feaf86d (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
/*******************************************************************************
 * Copyright (c) 2005, 2018 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
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.macros;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IBuildObject;
import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IFileInfo;
import org.eclipse.cdt.managedbuilder.core.IFolderInfo;
import org.eclipse.cdt.managedbuilder.core.IHoldsOptions;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOutputType;
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
import org.eclipse.cdt.managedbuilder.core.IResourceInfo;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.internal.core.BuildSettingsUtil;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
import org.eclipse.cdt.managedbuilder.internal.core.ResourceInfo;
import org.eclipse.cdt.managedbuilder.macros.BuildMacroException;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacro;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
import org.eclipse.cdt.managedbuilder.macros.IFileContextBuildMacroValues;
import org.eclipse.cdt.managedbuilder.macros.IFileContextData;
import org.eclipse.cdt.managedbuilder.macros.IOptionContextData;
import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver;
import org.eclipse.cdt.utils.cdtvariables.IVariableSubstitutor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;

/**
 * This supplier is used to suply MBS-predefined macros
 *
 * @since 3.0
 */
public class MbsMacroSupplier extends BuildCdtVariablesSupplierBase {
	private static MbsMacroSupplier fInstance;
	public final static String DOT = "."; //$NON-NLS-1$
	public final static String EMPTY_STRING = ""; //$NON-NLS-1$

	private static final String fFileMacros[] = new String[] { "InputFileName", //$NON-NLS-1$
			"InputFileExt", //$NON-NLS-1$
			"InputFileBaseName", //$NON-NLS-1$
			"InputFileRelPath", //$NON-NLS-1$
			"InputDirRelPath", //$NON-NLS-1$
			"OutputFileName", //$NON-NLS-1$
			"OutputFileExt", //$NON-NLS-1$
			"OutputFileBaseName", //$NON-NLS-1$
			"OutputFileRelPath", //$NON-NLS-1$
			"OutputDirRelPath", //$NON-NLS-1$
	};

	private static final String fOptionMacros[] = new String[] { "IncludeDefaults", //$NON-NLS-1$
			"ParentVersion", //$NON-NLS-1$
	};

	private static final String fToolMacros[] = new String[] { "ToolVersion", //$NON-NLS-1$
	};

	private static final String fConfigurationMacros[] = new String[] {
			//		CdtVariableResolver.VAR_CONFIG_NAME,
			//		CdtVariableResolver.VAR_CONFIG_DESCRIPTION,
			"BuildArtifactFileName", //$NON-NLS-1$
			"BuildArtifactFileExt", //$NON-NLS-1$
			"BuildArtifactFileBaseName", //$NON-NLS-1$
			"BuildArtifactFilePrefix", //$NON-NLS-1$
			"TargetOsList", //$NON-NLS-1$
			"TargetArchList", //$NON-NLS-1$
			"ToolChainVersion", //$NON-NLS-1$
			"BuilderVersion", //$NON-NLS-1$
	};

	private static final String fProjectMacros[] = new String[] {
			//		CdtVariableResolver.VAR_PROJ_NAME,
			//		CdtVariableResolver.VAR_PROJ_DIR_PATH,
	};

	private static final String fWorkspaceMacros[] = new String[] {
			//		CdtVariableResolver.VAR_WORKSPACE_DIR_PATH,
			//		CdtVariableResolver.VAR_DIRECTORY_DELIMITER,
			//		CdtVariableResolver.VAR_PATH_DELIMITER,
	};

	private static final String fCDTEclipseMacros[] = new String[] {
			//		CdtVariableResolver.VAR_ECLIPSE_VERSION,
			//		CdtVariableResolver.VAR_CDT_VERSION,
			"MBSVersion", //$NON-NLS-1$
			//		CdtVariableResolver.VAR_HOST_OS_NAME,
			//		CdtVariableResolver.VAR_HOST_ARCH_NAME,
			//		CdtVariableResolver.VAR_HOST_OS_TYPE,
			//		CdtVariableResolver.VAR_HOST_ARCH_TYPE,
	};

	private class OptionData extends OptionContextData {
		private IBuildObject fOptionContainer;

		public OptionData(IOption option, IBuildObject parent, IBuildObject optionContainer) {
			super(option, parent);
			fOptionContainer = optionContainer;
		}

		public IBuildObject getOptionContainer() {
			return fOptionContainer;
		}
	}

	public class FileContextMacro extends BuildMacro {
		//		private IFileContextData fContextData;
		//		private IConfiguration fConfiguration;
		//TODO: initialize builder
		//		private IBuilder fBuilder;
		private boolean fIsExplicit = true;
		private boolean fIsInitialized;
		private String fExplicitValue;
		private boolean fIsExplicitResolved;
		private IPath fInputFileLocation;
		private IPath fOutputFileLocation;

		private FileContextMacro(String name, IFileContextData contextData) {
			fName = name;
			fType = VALUE_TEXT;

			loadValue(contextData);

			//			fContextData = contextData;
		}

		private void loadValue(IFileContextData contextData) {
			if (fIsInitialized)
				return;
			IBuilder builder = null;
			IConfiguration configuration = null;
			IOptionContextData optionContext = contextData.getOptionContextData();
			if (optionContext != null) {
				IBuildObject buildObject = optionContext.getParent();
				if (buildObject instanceof ITool) {
					buildObject = ((ITool) buildObject).getParent();
				} else if (buildObject instanceof IConfiguration) {
					buildObject = ((IConfiguration) buildObject).getToolChain();
				}
				if (buildObject instanceof IToolChain) {
					IToolChain toolChain = (IToolChain) buildObject;
					builder = toolChain.getBuilder();
					configuration = toolChain.getParent();
				} else if (buildObject instanceof IResourceInfo) {
					configuration = ((IResourceInfo) buildObject).getParent();
					if (configuration != null) {
						IToolChain toolChain = configuration.getToolChain();
						if (toolChain != null)
							builder = toolChain.getBuilder();
					}
				}
			}

			if (builder != null) {
				IFileContextBuildMacroValues values = builder.getFileContextBuildMacroValues();
				String value = values.getMacroValue(fName);
				if (value != null) {
					fStringValue = value;
					fIsExplicit = false;
				}
			}

			if (fStringValue == null) {
				fIsExplicit = true;
				fStringValue = getExplicitFileMacroValue(fName, contextData.getInputFileLocation(),
						contextData.getOutputFileLocation(), builder, configuration);
				fExplicitValue = fStringValue;
				fIsExplicitResolved = true;
			}

			fInputFileLocation = contextData.getInputFileLocation();
			fOutputFileLocation = contextData.getOutputFileLocation();
			fIsInitialized = true;
		}

		public String getExplicitMacroValue(IConfiguration configuration, IBuilder builder) {
			//			loadValue();
			if (!fIsExplicitResolved) {
				fExplicitValue = getExplicitFileMacroValue(fName, fInputFileLocation, fOutputFileLocation, builder,
						configuration);
				fIsExplicitResolved = true;
			}
			return fExplicitValue;
		}

		public boolean isExplicit() {
			//			loadValue();
			return fIsExplicit;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacro#getStringValue()
		 */
		@Override
		public String getStringValue() {
			//			loadValue();
			return fStringValue;
		}
	}

	private String getExplicitFileMacroValue(String name, IPath inputFileLocation, IPath outputFileLocation,
			IBuilder builder, IConfiguration cfg) {
		String value = null;
		if ("InputFileName".equals(name)) { //$NON-NLS-1$
			if (inputFileLocation != null && inputFileLocation.segmentCount() > 0)
				value = inputFileLocation.lastSegment();
		} else if ("InputFileExt".equals(name)) { //$NON-NLS-1$
			if (inputFileLocation != null && inputFileLocation.segmentCount() > 0)
				value = getExtension(inputFileLocation.lastSegment());
		} else if ("InputFileBaseName".equals(name)) { //$NON-NLS-1$
			if (inputFileLocation != null && inputFileLocation.segmentCount() > 0)
				value = getBaseName(inputFileLocation.lastSegment());
		} else if ("InputFileRelPath".equals(name)) { //$NON-NLS-1$
			if (inputFileLocation != null && inputFileLocation.segmentCount() > 0) {
				IPath workingDirectory = getBuilderCWD(builder, cfg);
				if (workingDirectory != null) {
					IPath filePath = ManagedBuildManager.calculateRelativePath(workingDirectory, inputFileLocation);
					if (filePath != null)
						value = filePath.toOSString();
				}
			}
		} else if ("InputDirRelPath".equals(name)) { //$NON-NLS-1$
			if (inputFileLocation != null && inputFileLocation.segmentCount() > 0) {
				IPath workingDirectory = getBuilderCWD(builder, cfg);
				if (workingDirectory != null) {
					IPath filePath = ManagedBuildManager.calculateRelativePath(workingDirectory,
							inputFileLocation.removeLastSegments(1).addTrailingSeparator());
					if (filePath != null)
						value = filePath.toOSString();
				}
			}
		} else if ("OutputFileName".equals(name)) { //$NON-NLS-1$
			if (outputFileLocation != null && outputFileLocation.segmentCount() > 0)
				value = outputFileLocation.lastSegment();
		} else if ("OutputFileExt".equals(name)) { //$NON-NLS-1$
			if (outputFileLocation != null && outputFileLocation.segmentCount() > 0)
				value = getExtension(outputFileLocation.lastSegment());
		} else if ("OutputFileBaseName".equals(name)) { //$NON-NLS-1$
			if (outputFileLocation != null && outputFileLocation.segmentCount() > 0)
				value = getBaseName(outputFileLocation.lastSegment());
		} else if ("OutputFileRelPath".equals(name)) { //$NON-NLS-1$
			if (outputFileLocation != null && outputFileLocation.segmentCount() > 0) {
				IPath workingDirectory = getBuilderCWD(builder, cfg);
				if (workingDirectory != null) {
					IPath filePath = ManagedBuildManager.calculateRelativePath(workingDirectory, outputFileLocation);
					if (filePath != null)
						value = filePath.toOSString();
				}
			}
		} else if ("OutputDirRelPath".equals(name)) { //$NON-NLS-1$
			if (outputFileLocation != null && outputFileLocation.segmentCount() > 0) {
				IPath workingDirectory = getBuilderCWD(builder, cfg);
				if (workingDirectory != null) {
					IPath filePath = ManagedBuildManager.calculateRelativePath(workingDirectory,
							outputFileLocation.removeLastSegments(1).addTrailingSeparator());
					if (filePath != null)
						value = filePath.toOSString();
				}
			}
		}

		return value;
	}

	public String[] getMacroNames(int contextType) {
		return getMacroNames(contextType, true);
	}

	private String[] getMacroNames(int contextType, boolean clone) {
		String names[] = null;
		switch (contextType) {
		case IBuildMacroProvider.CONTEXT_FILE:
			names = fFileMacros;
			break;
		case IBuildMacroProvider.CONTEXT_OPTION:
			names = fOptionMacros;
			break;
		case IBuildMacroProvider.CONTEXT_TOOL:
			names = fToolMacros;
			break;
		case IBuildMacroProvider.CONTEXT_CONFIGURATION:
			names = fConfigurationMacros;
			break;
		case IBuildMacroProvider.CONTEXT_PROJECT:
			names = fProjectMacros;
			break;
		case IBuildMacroProvider.CONTEXT_WORKSPACE:
			names = fWorkspaceMacros;
			break;
		case IBuildMacroProvider.CONTEXT_INSTALLATIONS:
			names = fCDTEclipseMacros;
			break;
		case IBuildMacroProvider.CONTEXT_ECLIPSEENV:
			break;
		}
		if (names != null)
			return clone ? (String[]) names.clone() : names;
		return null;
	}

	private MbsMacroSupplier() {

	}

	public static MbsMacroSupplier getInstance() {
		if (fInstance == null)
			fInstance = new MbsMacroSupplier();
		return fInstance;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroSupplier#getMacro(java.lang.String, int, java.lang.Object)
	 */
	@Override
	public IBuildMacro getMacro(String macroName, int contextType, Object contextData) {
		IBuildMacro macro = null;
		switch (contextType) {
		case IBuildMacroProvider.CONTEXT_FILE:
			if (contextData instanceof IFileContextData) {
				for (int i = 0; i < fFileMacros.length; i++) {
					if (macroName.equals(fFileMacros[i])) {
						macro = new FileContextMacro(macroName, (IFileContextData) contextData);
						break;
					}
				}
			}
			break;
		case IBuildMacroProvider.CONTEXT_OPTION:
			if (contextData instanceof IOptionContextData) {
				macro = getMacro(macroName, (IOptionContextData) contextData);
			}
			break;
		case IBuildMacroProvider.CONTEXT_TOOL:
			if (contextData instanceof ITool) {
				macro = getMacro(macroName, (ITool) contextData);
			}
			break;
		case IBuildMacroProvider.CONTEXT_CONFIGURATION:
			IConfiguration cfg = null;
			IBuilder builder = null;
			if (contextData instanceof IConfiguration) {
				cfg = (IConfiguration) contextData;
				builder = cfg.getBuilder();
			} else if (contextData instanceof IBuilder) {
				builder = (IBuilder) contextData;
				cfg = builder.getParent().getParent();
			}
			if (cfg != null)
				macro = getMacro(macroName, builder, cfg);

			break;
		case IBuildMacroProvider.CONTEXT_PROJECT:
			if (contextData instanceof IManagedProject) {
				macro = getMacro(macroName, (IManagedProject) contextData);
			}
			break;
		case IBuildMacroProvider.CONTEXT_WORKSPACE:
			if (contextData instanceof IWorkspace) {
				macro = getMacro(macroName, (IWorkspace) contextData);
			}
			break;
		case IBuildMacroProvider.CONTEXT_INSTALLATIONS:
			if (contextData == null) {
				macro = getMacro(macroName);
			}
			break;
		case IBuildMacroProvider.CONTEXT_ECLIPSEENV:
			break;
		}

		return macro;
	}

	public IBuildMacro getMacro(String macroName, IOptionContextData optionContext) {
		IBuildMacro macro = null;
		if ("IncludeDefaults".equals(macroName)) { //$NON-NLS-1$
			if (!canHandle(optionContext))
				optionContext = null;
			macro = new OptionMacro(macroName, optionContext);
		} else if ("ParentVersion".equals(macroName)) { //$NON-NLS-1$
			IHoldsOptions holder = OptionContextData.getHolder(optionContext);
			if (holder != null && holder.getVersion() != null)
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, holder.getVersion().toString());
		}
		return macro;
	}

	public IBuildMacro getMacro(String macroName, ITool tool) {
		IBuildMacro macro = null;
		if ("ToolVersion".equals(macroName) && tool.getVersion() != null) { //$NON-NLS-1$
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, tool.getVersion().toString());
		}
		return macro;
	}

	public IBuildMacro getMacro(String macroName, IBuilder builder, IConfiguration cfg) {
		IBuildMacro macro = null;
		if (CdtVariableResolver.VAR_CONFIG_NAME.equals(macroName)) {
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, cfg.getName());
		} else if (CdtVariableResolver.VAR_CONFIG_DESCRIPTION.equals(macroName)) {
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, cfg.getDescription());
		} else if ("BuildArtifactFileName".equals(macroName)) { //$NON-NLS-1$
			String name = cfg.getArtifactName();
			String ext = cfg.getArtifactExtension();
			if (ext != null && !EMPTY_STRING.equals(ext))
				name = name + DOT + ext;
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, name);
		} else if ("BuildArtifactFileExt".equals(macroName)) { //$NON-NLS-1$
			String ext = cfg.getArtifactExtension();
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, ext);
		} else if ("BuildArtifactFileBaseName".equals(macroName)) { //$NON-NLS-1$
			String name = cfg.getArtifactName();
			ITool targetTool = cfg.calculateTargetTool();
			if (targetTool != null) {
				IOutputType pot = targetTool.getPrimaryOutputType();
				String prefix = pot != null ? pot.getOutputPrefix() : ""; //$NON-NLS-1$

				// Resolve any macros in the outputPrefix
				// Note that we cannot use file macros because if we do a clean
				// we need to know the actual
				// name of the file to clean, and cannot use any builder
				// variables such as $@. Hence
				// we use the next best thing, i.e. configuration context.

				// figure out the configuration we're using
				IBuildObject toolParent = targetTool.getParent();
				IConfiguration config = null;
				// if the parent is a config then we're done
				if (toolParent instanceof IConfiguration)
					config = (IConfiguration) toolParent;
				else if (toolParent instanceof IToolChain) {
					// must be a toolchain
					config = ((IToolChain) toolParent).getParent();
				}

				else if (toolParent instanceof IResourceConfiguration) {
					config = ((IResourceConfiguration) toolParent).getParent();
				}

				else {
					// bad
					throw new AssertionError(ManagedMakeMessages.getResourceString("MbsMacroSupplier.1")); //$NON-NLS-1$
				}

				if (config != null) {

					try {
						prefix = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(prefix, "", //$NON-NLS-1$
								" ", //$NON-NLS-1$
								IBuildMacroProvider.CONTEXT_CONFIGURATION, config);
					}

					catch (BuildMacroException e) {
					}

				}

				if (prefix != null && !EMPTY_STRING.equals(prefix))
					name = prefix + name;
			}
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, name);
		} else if ("BuildArtifactFilePrefix".equals(macroName)) { //$NON-NLS-1$
			ITool targetTool = cfg.calculateTargetTool();
			if (targetTool != null) {
				IOutputType pot = targetTool.getPrimaryOutputType();
				String prefix = pot != null ? pot.getOutputPrefix() : ""; //$NON-NLS-1$

				// Resolve any macros in the outputPrefix
				// Note that we cannot use file macros because if we do a clean
				// we need to know the actual
				// name of the file to clean, and cannot use any builder
				// variables such as $@. Hence
				// we use the next best thing, i.e. configuration context.

				// figure out the configuration we're using
				IBuildObject toolParent = targetTool.getParent();
				IConfiguration config = null;
				// if the parent is a config then we're done
				if (toolParent instanceof IConfiguration)
					config = (IConfiguration) toolParent;
				else if (toolParent instanceof IToolChain) {
					// must be a toolchain
					config = ((IToolChain) toolParent).getParent();
				}

				else if (toolParent instanceof IResourceConfiguration) {
					config = ((IResourceConfiguration) toolParent).getParent();
				}

				else {
					// bad
					throw new AssertionError(ManagedMakeMessages.getResourceString("MbsMacroSupplier.2")); //$NON-NLS-1$
				}

				if (config != null) {

					try {
						prefix = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(prefix, "", //$NON-NLS-1$
								" ", //$NON-NLS-1$
								IBuildMacroProvider.CONTEXT_CONFIGURATION, config);
					}

					catch (BuildMacroException e) {
					}

				}

				if (prefix == null)
					prefix = EMPTY_STRING;
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, prefix);
			}
		} else if ("TargetOsList".equals(macroName)) { //$NON-NLS-1$
			IToolChain toolChain = cfg.getToolChain();
			String osList[] = toolChain.getOSList();
			if (osList == null)
				osList = new String[0];
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT_LIST, osList);
		} else if ("TargetArchList".equals(macroName)) { //$NON-NLS-1$
			IToolChain toolChain = cfg.getToolChain();
			String archList[] = toolChain.getArchList();
			if (archList == null)
				archList = new String[0];
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT_LIST, archList);

		} else if ("ToolChainVersion".equals(macroName)) { //$NON-NLS-1$
			if (cfg.getToolChain().getVersion() != null)
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, cfg.getToolChain().getVersion().toString());
		} else if ("BuilderVersion".equals(macroName)) { //$NON-NLS-1$
			Version version = cfg.getToolChain().getBuilder().getVersion();
			if (version != null)
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, version.toString());
		}
		return macro;
	}

	private String getBaseName(String name) {
		String value = null;
		int index = name.lastIndexOf('.');
		if (index == -1)
			value = name;
		else
			value = name.substring(0, index);
		return value;
	}

	private String getExtension(String name) {
		String value = null;
		int index = name.lastIndexOf('.');
		if (index != -1)
			value = name.substring(index + 1);
		return value;
	}

	public IBuildMacro getMacro(String macroName, IManagedProject mngProj) {
		IBuildMacro macro = null;
		if (CdtVariableResolver.VAR_PROJ_NAME.equals(macroName)) {
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, mngProj.getOwner().getName());
		} else if (CdtVariableResolver.VAR_PROJ_DIR_PATH.equals(macroName)) {
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_PATH_DIR,
					mngProj.getOwner().getLocation().toOSString());
		}
		return macro;
	}

	public IBuildMacro getMacro(String macroName, IWorkspace wsp) {
		IBuildMacro macro = null;
		if (CdtVariableResolver.VAR_WORKSPACE_DIR_PATH.equals(macroName)) {
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_PATH_DIR, wsp.getRoot().getLocation().toOSString());
		} else if (CdtVariableResolver.VAR_DIRECTORY_DELIMITER.equals(macroName)) {
			if (isWin32()) {
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, "\\"); //$NON-NLS-1$
			} else {
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, "/"); //$NON-NLS-1$
			}
		} else if (CdtVariableResolver.VAR_PATH_DELIMITER.equals(macroName)) {
			if (isWin32()) {
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, ";"); //$NON-NLS-1$
			} else {
				macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, ":"); //$NON-NLS-1$
			}
		}
		return macro;
	}

	private boolean isWin32() {
		String os = System.getProperty("os.name").toLowerCase(); //$NON-NLS-1$
		if (os.startsWith("windows ")) //$NON-NLS-1$
			return true;
		return false;
	}

	public IBuildMacro getMacro(String macroName) {
		IBuildMacro macro = null;
		if (CdtVariableResolver.VAR_ECLIPSE_VERSION.equals(macroName)) {
			Bundle bundle = Platform.getBundle("org.eclipse.platform"); //$NON-NLS-1$
			String version = bundle != null
					? (String) bundle.getHeaders().get(org.osgi.framework.Constants.BUNDLE_VERSION)
					: null;
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, version);
		} else if (CdtVariableResolver.VAR_CDT_VERSION.equals(macroName)) {
			String version = CCorePlugin.getDefault().getBundle().getHeaders()
					.get(org.osgi.framework.Constants.BUNDLE_VERSION);
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, version);
		} else if ("MBSVersion".equals(macroName)) { //$NON-NLS-1$
			String version = ManagedBuildManager.getBuildInfoVersion().toString();
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, version);
		} else if (CdtVariableResolver.VAR_HOST_OS_NAME.equals(macroName)) {
			String os = System.getProperty("os.name"); //$NON-NLS-1$
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, os);
		} else if (CdtVariableResolver.VAR_HOST_ARCH_NAME.equals(macroName)) {
			String arch = System.getProperty("os.arch"); //$NON-NLS-1$
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, arch);
		} else if (CdtVariableResolver.VAR_OS_TYPE.equals(macroName)) {
			String os = Platform.getOS();
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, os);
		} else if (CdtVariableResolver.VAR_ARCH_TYPE.equals(macroName)) {
			String arch = Platform.getOSArch();
			macro = new BuildMacro(macroName, IBuildMacro.VALUE_TEXT, arch);
		}

		return macro;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroSupplier#getMacros(int, java.lang.Object)
	 */
	@Override
	public IBuildMacro[] getMacros(int contextType, Object contextData) {
		String names[] = getMacroNames(contextType, false);

		if (names != null) {
			IBuildMacro macros[] = new IBuildMacro[names.length];
			int num = 0;
			for (int i = 0; i < names.length; i++) {
				IBuildMacro macro = getMacro(names[i], contextType, contextData);
				if (macro != null)
					macros[num++] = macro;
			}
			if (macros.length != num) {
				IBuildMacro tmp[] = new IBuildMacro[num];
				if (num > 0)
					System.arraycopy(macros, 0, tmp, 0, num);
				macros = tmp;
			}
			return macros;
		}
		return null;
	}

	private IPath getBuilderCWD(IBuilder builder, IConfiguration cfg) {
		return ManagedBuildManager.getBuildLocation(cfg, builder);
	}

	/* (non-Javadoc)
	 * Returns the option that matches the option ID in this tool
	 */
	public IOption getOption(ITool tool, String optionId) {
		if (optionId == null)
			return null;

		//  Look for an option with this ID, or an option with a superclass with this id
		IOption[] options = tool.getOptions();
		for (int i = 0; i < options.length; i++) {
			IOption targetOption = options[i];
			IOption option = targetOption;
			do {
				if (optionId.equals(option.getId())) {
					return targetOption;
				}
				option = option.getSuperClass();
			} while (option != null);
		}

		return null;
	}

	private class IncludeDefaultsSubstitutor implements IVariableSubstitutor {
		private IOptionContextData fOptionContextData;

		public IncludeDefaultsSubstitutor(IOptionContextData data) {
			fOptionContextData = data;
		}

		@Override
		public String resolveToString(String macroName) throws CdtVariableException {
			if (!"IncludeDefaults".equals(macroName)) //$NON-NLS-1$
				return CdtVariableResolver.createVariableReference(macroName);
			IOptionContextData parent = getParent(fOptionContextData);
			if (parent == null)
				return EMPTY_STRING;
			IncludeDefaultsSubstitutor sub = new IncludeDefaultsSubstitutor(parent);
			IOption option = parent.getOption();
			if (option == null)
				return null;

			String str = null;
			String strL[] = null;
			try {
				switch (option.getBasicValueType()) {
				case IOption.STRING:
					str = option.getStringValue();
					break;
				case IOption.STRING_LIST:
					strL = option.getBasicStringListValue();
					break;
				//				case IOption.INCLUDE_PATH :
				//					strL = option.getIncludePaths();
				//					break;
				//				case IOption.PREPROCESSOR_SYMBOLS :
				//					strL = option.getDefinedSymbols();
				//					break;
				//				case IOption.LIBRARIES :
				//					strL = option.getLibraries();
				//					break;
				//				case IOption.OBJECTS :
				//					strL = option.getUserObjects();
				//					break;
				default:
					break;
				}

				if (str != null)
					return CdtVariableResolver.resolveToString(str, sub);
				else if (strL != null) {
					strL = CdtVariableResolver.resolveStringListValues(strL, sub, true);
					return CdtVariableResolver.convertStringListToString(strL, " "); //$NON-NLS-1$
				}
			} catch (BuildException e) {

			} catch (CdtVariableException e) {

			}
			return null;
		}

		@Override
		public String[] resolveToStringList(String macroName) throws CdtVariableException {
			if (!"IncludeDefaults".equals(macroName)) //$NON-NLS-1$
				return new String[] { CdtVariableResolver.createVariableReference(macroName) };

			IOptionContextData parent = getParent(fOptionContextData);
			if (parent == null)
				return new String[] { EMPTY_STRING };
			IncludeDefaultsSubstitutor sub = new IncludeDefaultsSubstitutor(parent);
			IOption option = parent.getOption();
			if (option == null)
				return null;

			String str = null;
			String strL[] = null;
			try {
				switch (option.getBasicValueType()) {
				case IOption.STRING:
					str = option.getStringValue();
					break;
				case IOption.STRING_LIST:
					strL = option.getBasicStringListValue();
					break;
				//				case IOption.INCLUDE_PATH :
				//					strL = option.getIncludePaths();
				//					break;
				//				case IOption.PREPROCESSOR_SYMBOLS :
				//					strL = option.getDefinedSymbols();
				//					break;
				//				case IOption.LIBRARIES :
				//					strL = option.getLibraries();
				//					break;
				//				case IOption.OBJECTS :
				//					strL = option.getUserObjects();
				//					break;
				default:
					break;
				}

				if (str != null)
					return CdtVariableResolver.resolveToStringList(str, sub);
				else if (strL != null)
					return CdtVariableResolver.resolveStringListValues(strL, sub, true);
			} catch (BuildException e) {

			} catch (CdtVariableException e) {

			}
			return null;
		}
	}

	public class OptionMacro extends BuildMacro {
		//		private IOptionContextData fOptionContextData;
		//		private IOptionContextData fParentOptionContextData;
		//		private IOption fParentOption;
		private OptionMacro(String name, IOptionContextData optionContextData) {
			fName = name;
			//			fOptionContextData = optionContextData;
			IOptionContextData parentOptionContextData = getParent(optionContextData);
			load(optionContextData, parentOptionContextData);
		}

		private boolean load(IOptionContextData optionContextData, IOptionContextData parentOptionContextData) {
			fStringValue = null;
			fStringListValue = null;
			fType = 0;
			if (parentOptionContextData != null) {
				IOption option = parentOptionContextData.getOption();
				if (option != null) {
					try {
						switch (option.getValueType()) {
						case IOption.BOOLEAN:
							break;
						case IOption.STRING:
							fType = IBuildMacro.VALUE_TEXT;
							fStringValue = option.getStringValue();
							break;
						case IOption.ENUMERATED:
							break;
						case IOption.TREE:
							break;
						case IOption.STRING_LIST:
							fType = IBuildMacro.VALUE_TEXT_LIST;
							fStringListValue = option.getStringListValue();
							break;
						case IOption.INCLUDE_PATH:
							fType = IBuildMacro.VALUE_PATH_DIR_LIST;
							fStringListValue = option.getIncludePaths();
							break;
						case IOption.PREPROCESSOR_SYMBOLS:
							fType = IBuildMacro.VALUE_TEXT_LIST;
							fStringListValue = option.getDefinedSymbols();
							break;
						case IOption.LIBRARIES:
							fType = IBuildMacro.VALUE_TEXT_LIST;
							fStringListValue = option.getLibraries();
							break;
						case IOption.OBJECTS:
							fType = IBuildMacro.VALUE_PATH_FILE_LIST;
							fStringListValue = option.getUserObjects();
							break;
						case IOption.INCLUDE_FILES:
							fType = IBuildMacro.VALUE_PATH_FILE_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.LIBRARY_PATHS:
							fType = IBuildMacro.VALUE_PATH_DIR_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.LIBRARY_FILES:
							fType = IBuildMacro.VALUE_PATH_FILE_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.MACRO_FILES:
							fType = IBuildMacro.VALUE_PATH_FILE_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.UNDEF_INCLUDE_PATH:
							fType = IBuildMacro.VALUE_PATH_DIR_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.UNDEF_PREPROCESSOR_SYMBOLS:
							fType = IBuildMacro.VALUE_TEXT_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.UNDEF_INCLUDE_FILES:
							fType = IBuildMacro.VALUE_PATH_FILE_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.UNDEF_LIBRARY_PATHS:
							fType = IBuildMacro.VALUE_PATH_DIR_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.UNDEF_LIBRARY_FILES:
							fType = IBuildMacro.VALUE_PATH_FILE_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						case IOption.UNDEF_MACRO_FILES:
							fType = IBuildMacro.VALUE_PATH_FILE_LIST;
							fStringListValue = option.getBasicStringListValue();
							break;
						}
						if (fStringValue != null)
							fStringValue = CdtVariableResolver.resolveToString(fStringValue,
									new IncludeDefaultsSubstitutor(parentOptionContextData));
						else if (fStringListValue != null)
							fStringListValue = CdtVariableResolver.resolveStringListValues(fStringListValue,
									new IncludeDefaultsSubstitutor(parentOptionContextData), true);
					} catch (Exception e) {
						fType = 0;
					}
				}
			}

			boolean result = fType != 0;
			if (!result) {
				fType = VALUE_TEXT;
				fStringListValue = null;
				fStringValue = null;
			}

			return result;
		}
	}

	private OptionData getParent(IOptionContextData optionContext) {
		if (optionContext == null)
			return null;
		IOption option = optionContext.getOption();
		if (option == null)
			return null;

		IBuildObject parent = option.getParent();
		ITool tool = null;
		IToolChain tCh = null;
		IResourceInfo optionRcInfo = null;
		if (parent instanceof ITool) {
			tool = (ITool) parent;
			optionRcInfo = tool.getParentResourceInfo();
		} else if (parent instanceof IToolChain) {
			tCh = (IToolChain) parent;
			optionRcInfo = tCh.getParentFolderInfo();
		}

		if (optionRcInfo != null && optionRcInfo.isExtensionElement())
			optionRcInfo = null;

		IBuildObject parentObj = null;
		IOption parentOption = null;

		if (optionRcInfo != null) {
			//only if optionRcInfo is not null
			IBuildObject bObj = (optionContext instanceof OptionData)
					? ((OptionData) optionContext).getOptionContainer()
					: optionContext.getParent();

			IResourceInfo rcInfo = null;
			IFileInfo fileInfo = null;
			IFolderInfo folderInfo = null;
			ITool holderTool = null;
			IToolChain holderTc = null;
			if (bObj instanceof ITool) {
				holderTool = (ITool) bObj;
				rcInfo = holderTool.getParentResourceInfo();
			} else if (bObj instanceof IFileInfo) {
				fileInfo = (IFileInfo) bObj;
				rcInfo = fileInfo;
			} else if (bObj instanceof IFolderInfo) {
				folderInfo = (IFolderInfo) bObj;
				rcInfo = folderInfo;
				holderTc = folderInfo.getToolChain();
			} else if (bObj instanceof IToolChain) {
				holderTc = (IToolChain) bObj;
				folderInfo = holderTc.getParentFolderInfo();
			}

			if (rcInfo != null && rcInfo.isExtensionElement())
				rcInfo = null;

			IResourceInfo parentRcInfo = null;

			if (rcInfo != null) {
				IPath optionRcPath = optionRcInfo.getPath();
				IPath rcPath = rcInfo.getPath();
				if (optionRcPath.isPrefixOf(rcPath)) {
					parentRcInfo = ((ResourceInfo) optionRcInfo).getParentResourceInfo();
				} else {
					parentRcInfo = ((ResourceInfo) rcInfo).getParentResourceInfo();
				}
			}

			if (parentRcInfo != null) {
				if (tool != null) {
					ITool tools[] = parentRcInfo.getTools();
					ITool cur = tool;
					ITool found = null;
					do {
						String id = cur.getId();
						ITool[] tmp = BuildSettingsUtil.getToolsBySuperClassId(tools, id);
						if (tmp.length != 0) {
							found = tmp[0];
							break;
						}
						if (cur.isExtensionElement())
							break;
						cur = cur.getSuperClass();
					} while (cur != null);

					if (found != null) {
						parentOption = getParentOption(found, option);
						if (parentOption != null) {
							parentObj = found;
						}
					}
				} else if (tCh != null) {
					if (parentRcInfo instanceof IFolderInfo) {
						IToolChain parentTc = ((IFolderInfo) parentRcInfo).getToolChain();
						parentOption = getParentOption(parentTc, option);
						if (parentOption != null) {
							parentObj = parentTc;
						}
					}
				}
			}
		}

		if (parentObj == null)
			parentOption = null;
		if (parentOption == null)
			parentOption = option.getSuperClass();

		if (parentOption != null)
			return new OptionData(parentOption, optionContext.getParent(), parentObj);

		return null;
	}

	private IOption getParentOption(IHoldsOptions holder, IOption option) {
		IOption cur = option;
		IOption found = null;
		do {
			String id = cur.getId();
			found = holder.getOptionBySuperClassId(id);
			if (found != null)
				break;

			if (cur.isExtensionElement())
				break;
			cur = cur.getSuperClass();
		} while (cur != null);
		return found;
	}

	private boolean canHandle(IOptionContextData optionData) {
		IOption option = optionData.getOption();
		if (option == null)
			return false;

		boolean can = false;
		try {
			switch (option.getBasicValueType()) {
			case IOption.BOOLEAN:
				break;
			case IOption.STRING:
				can = true;
				break;
			case IOption.ENUMERATED:
				break;
			case IOption.TREE:
				break;
			case IOption.STRING_LIST:
				can = true;
				break;
			case IOption.INCLUDE_PATH:
				can = true;
				break;
			case IOption.PREPROCESSOR_SYMBOLS:
				can = true;
				break;
			case IOption.LIBRARIES:
				can = true;
				break;
			case IOption.OBJECTS:
				can = true;
				break;
			}
		} catch (BuildException e) {
			can = false;
		}
		return can;
	}

}

Back to the top