Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 200176190e6639457037f4a8898df50a789fbb40 (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
/*******************************************************************************
 * Copyright (c) 2005, 2011 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
 * Miwako Tokugawa (Intel Corporation) - bug 222817 (OptionCategoryApplicability)
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.macros;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.core.cdtvariables.ICdtVariable;
import org.eclipse.cdt.core.cdtvariables.ICdtVariableManager;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
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.IResourceConfiguration;
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.Tool;
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.IBuildMacroSupplier;
import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver;
import org.eclipse.cdt.utils.cdtvariables.ICdtVariableSupplier;
import org.eclipse.cdt.utils.cdtvariables.IVariableSubstitutor;
import org.eclipse.cdt.utils.cdtvariables.SupplierBasedCdtVariableManager;
import org.eclipse.cdt.utils.cdtvariables.SupplierBasedCdtVariableSubstitutor;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.core.variables.IStringVariable;

/**
 * The default IBuildMacroProvider implementation
 * @since 3.0
 */
public class BuildMacroProvider implements IBuildMacroProvider, IMacroContextInfoProvider {
	private static final String PATTERN_MACRO_NAME = "="; //$NON-NLS-1$

	static private BuildMacroProvider fDefault;

	public static MbsMacroSupplier fMbsMacroSupplier = MbsMacroSupplier.getInstance();

	protected BuildMacroProvider() {

	}

	public static BuildMacroProvider getDefault() {
		if (fDefault == null)
			fDefault = new BuildMacroProvider();
		return fDefault;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#getMacro(java.lang.String, int, java.lang.Object, boolean)
	 */
	@Override
	public IBuildMacro getMacro(String macroName, int contextType, Object contextData, boolean includeParentContexts) {
		ICdtVariable var = getVariable(macroName, contextType, contextData, includeParentContexts);
		if (var != null)
			return wrap(var);
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#getMacros(int, java.lang.Object, boolean)
	 */
	@Override
	public IBuildMacro[] getMacros(int contextType, Object contextData, boolean includeParentContexts) {
		ICdtVariable[] vars = getVariables(contextType, contextData, includeParentContexts);
		if (vars != null)
			return wrap(vars);
		return null;
	}

	public static CoreMacrosSupplier createCoreSupplier(IConfiguration cfg) {
		ICConfigurationDescription cfgDes = ManagedBuildManager.getDescriptionForConfiguration(cfg);
		if (cfgDes != null) {
			return new CoreMacrosSupplier(cfgDes);
		}
		return null;
	}

	@Override
	public ICdtVariable getVariable(String macroName, int contextType, Object contextData,
			boolean includeParentContexts) {
		return SupplierBasedCdtVariableManager.getVariable(macroName, getMacroContextInfo(contextType, contextData),
				includeParentContexts);
	}

	@Override
	public ICdtVariable[] getVariables(int contextType, Object contextData, boolean includeParentContexts) {
		return SupplierBasedCdtVariableManager.getVariables(getMacroContextInfo(contextType, contextData),
				includeParentContexts);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#getSuppliers(int, java.lang.Object)
	 */
	@Override
	public IBuildMacroSupplier[] getSuppliers(int contextType, Object contextData) {
		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);
		if (info != null) {
			ICdtVariableSupplier suppliers[] = info.getSuppliers();
			if (suppliers != null)
				return filterMacroSuppliers(suppliers);
		}
		return null;
	}

	private static IBuildMacroSupplier[] filterMacroSuppliers(ICdtVariableSupplier suppliers[]) {
		List<ICdtVariableSupplier> list = new ArrayList<ICdtVariableSupplier>(suppliers.length);
		for (int i = 0; i < suppliers.length; i++) {
			if (suppliers[i] instanceof IBuildMacroSupplier)
				list.add(suppliers[i]);
		}
		return list.toArray(new IBuildMacroSupplier[list.size()]);
	}

	@Override
	public IMacroContextInfo getMacroContextInfo(int contextType, Object contextData) {
		DefaultMacroContextInfo info = new DefaultMacroContextInfo(contextType, contextData);
		if (info.getSuppliers() != null)
			return info;
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#convertStringListToString(java.lang.String[], java.lang.String)
	 */
	@Override
	public String convertStringListToString(String[] value, String listDelimiter) {
		return CdtVariableResolver.convertStringListToString(value, listDelimiter);
	}

	private static class VariableWrapper implements IBuildMacro {
		private ICdtVariable fVariable;

		public VariableWrapper(ICdtVariable var) {
			if (var == null)
				throw new NullPointerException();

			fVariable = var;
		}

		//		public ICdtVariable getVariable(){
		//			return fVariable;
		//		}

		@Override
		public int getMacroValueType() {
			return fVariable.getValueType();
		}

		@Override
		public String[] getStringListValue() throws BuildMacroException {
			try {
				return fVariable.getStringListValue();
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}

		@Override
		public String getStringValue() throws BuildMacroException {
			try {
				return fVariable.getStringValue();
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}

		@Override
		public String getName() {
			return fVariable.getName();
		}

		@Override
		public int getValueType() {
			return fVariable.getValueType();
		}
	}

	public static IBuildMacro wrap(ICdtVariable var) {
		if (var == null)
			return null;
		if (var instanceof IBuildMacro)
			return (IBuildMacro) var;
		return new VariableWrapper(var);
	}

	public static IBuildMacro[] wrap(ICdtVariable vars[]) {
		if (vars instanceof IBuildMacro[])
			return (IBuildMacro[]) vars;

		IBuildMacro macros[] = new IBuildMacro[vars.length];
		for (int i = 0; i < macros.length; i++) {
			macros[i] = wrap(vars[i]);
		}
		return macros;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#resolveValue(java.lang.String, java.lang.String, java.lang.String, int, java.lang.Object)
	 */
	@Override
	public String resolveValue(String value, String nonexistentMacrosValue, String listDelimiter, int contextType,
			Object contextData) throws BuildMacroException {
		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);

		if (info != null) {
			try {
				return CdtVariableResolver.resolveToString(value,
						getMacroSubstitutor(info, nonexistentMacrosValue, listDelimiter));
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}
		return value;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#resolveStringListValue(java.lang.String, java.lang.String, int, java.lang.Object)
	 */
	@Override
	public String[] resolveStringListValue(String value, String nonexistentMacrosValue, String listDelimiter,
			int contextType, Object contextData) throws BuildMacroException {

		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);
		if (info != null) {
			try {
				return CdtVariableResolver.resolveToStringList(value,
						getMacroSubstitutor(info, nonexistentMacrosValue, listDelimiter));
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#resolveValueToMakefileFormat(java.lang.String, java.lang.String, java.lang.String, int, java.lang.Object)
	 */
	@Override
	public String resolveValueToMakefileFormat(String value, String nonexistentMacrosValue, String listDelimiter,
			int contextType, Object contextData) throws BuildMacroException {

		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);
		if (info != null) {
			try {
				return CdtVariableResolver.resolveToString(value,
						getBuildfileMacroSubstitutor(info, nonexistentMacrosValue, listDelimiter));
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#resolveStringListValueToMakefileFormat(java.lang.String, java.lang.String, int, java.lang.Object)
	 */
	@Override
	public String[] resolveStringListValueToMakefileFormat(String value, String nonexistentMacrosValue,
			String listDelimiter, int contextType, Object contextData) throws BuildMacroException {

		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);
		if (info != null) {
			try {
				return CdtVariableResolver.resolveToStringList(value,
						getBuildfileMacroSubstitutor(info, nonexistentMacrosValue, listDelimiter));
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#isStringListValue(java.lang.String)
	 */
	@Override
	public boolean isStringListValue(String value, int contextType, Object contextData) throws BuildMacroException {
		try {
			CdtVariableResolver.resolveToStringList(value,
					getMacroSubstitutor(getMacroContextInfo(contextType, contextData), " ", null)); //$NON-NLS-1$
		} catch (CdtVariableException e) {
			return false;
		}
		return true;

	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#checkIntegrity(int, java.lang.Object)
	 */
	@Override
	public void checkIntegrity(int contextType, Object contextData) throws BuildMacroException {

		final ICdtVariableManager mngr = CCorePlugin.getDefault().getCdtVariableManager();
		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);
		IVariableSubstitutor subst = new SupplierBasedCdtVariableSubstitutor(info, null, "") { //$NON-NLS-1$
			@Override
			protected ResolvedMacro resolveMacro(ICdtVariable macro) throws CdtVariableException {
				IStringVariable var = mngr.toEclipseVariable(macro, null);
				if (var instanceof IDynamicVariable)
					return new ResolvedMacro(macro.getName(), ""); //$NON-NLS-1$
				return super.resolveMacro(macro);
			}
		};
		if (info != null) {
			try {
				CdtVariableResolver.checkIntegrity(info, subst);
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}
	}

	public SupplierBasedCdtVariableSubstitutor getMacroSubstitutor(IMacroContextInfo info, String inexistentMacroValue,
			String listDelimiter) {
		return new SupplierBasedCdtVariableSubstitutor(info, inexistentMacroValue, listDelimiter);
	}

	public IVariableSubstitutor getBuildfileMacroSubstitutor(IMacroContextInfo info, String inexistentMacroValue,
			String listDelimiter) {
		return new BuildfileMacroSubstitutor(info, inexistentMacroValue, listDelimiter);
	}

	/**
	 * @return whether the environment macros are to be expanded in the buildfile
	 */
	public boolean areMacrosExpandedInBuildfile(IConfiguration cfg) {
		IBuilder builder = cfg.getBuilder();
		boolean expanded = !builder.keepEnvironmentVariablesInBuildfile();
		if (expanded || canKeepMacrosInBuildfile(cfg))
			return expanded;
		return true;
	}

	/**
	 * sets whether the environment macros are to be expanded in the buildfile or not
	 * If a builder does not support treating environment variables as its own ones this method
	 * has no effect
	 * Returns the result of this set operation. That is whether the environment macros are to be expanded in the buildfile
	 *
	 * @return whether the environment macros are to be expanded in the buildfile
	 */
	public boolean expandMacrosInBuildfile(IConfiguration cfg, boolean expand) {
		if (expand || canKeepMacrosInBuildfile(cfg)) {
			IBuilder builder = cfg.getEditableBuilder();
			builder.setKeepEnvironmentVariablesInBuildfile(!expand);
			return expand;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#resolveStringListValues(java.lang.String[], java.lang.String, java.lang.String, int, java.lang.Object)
	 */
	@Override
	public String[] resolveStringListValues(String[] value, String nonexistentMacrosValue, String listDelimiter,
			int contextType, Object contextData) throws BuildMacroException {
		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);
		if (info != null) {
			try {
				return CdtVariableResolver.resolveStringListValues(value,
						getMacroSubstitutor(info, nonexistentMacrosValue, listDelimiter), true);
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider#resolveStringListValuesToMakefileFormat(java.lang.String[], java.lang.String, java.lang.String, int, java.lang.Object)
	 */
	@Override
	public String[] resolveStringListValuesToMakefileFormat(String[] value, String nonexistentMacrosValue,
			String listDelimiter, int contextType, Object contextData) throws BuildMacroException {
		IMacroContextInfo info = getMacroContextInfo(contextType, contextData);
		if (info != null) {
			try {
				return CdtVariableResolver.resolveStringListValues(value,
						getBuildfileMacroSubstitutor(info, nonexistentMacrosValue, listDelimiter), true);
			} catch (CdtVariableException e) {
				throw new BuildMacroException(e);
			}
		}
		return null;
	}

	/**
	 * @return whether the builder used for the given configuration is capable
	 * of handling macros in the buildfile
	 */
	public static boolean canKeepMacrosInBuildfile(IConfiguration cfg) {
		if (cfg != null) {
			IToolChain toolChain = cfg.getToolChain();
			if (toolChain != null)
				return canKeepMacrosInBuildfile(toolChain.getBuilder());
		}
		return false;
	}

	/**
	 * @return whether the given builder is capable
	 * of handling macros in the buildfile
	 */
	public static boolean canKeepMacrosInBuildfile(IBuilder builder) {
		if (builder != null) {
			String pattern = builder.getBuilderVariablePattern();
			if (pattern != null && pattern.indexOf(PATTERN_MACRO_NAME) != -1)
				return true;
		}
		return false;
	}

	/**
	 * creates a macro reference in the buildfile format for the given builder.
	 * If the builder can not treat macros, returns null
	 */
	public static String createBuildfileMacroReference(String name, IBuilder builder) {
		String ref = null;
		if (builder != null) {
			String pattern = builder.getBuilderVariablePattern();
			if (pattern != null && pattern.indexOf(PATTERN_MACRO_NAME) != -1)
				ref = pattern.replaceAll(PATTERN_MACRO_NAME, name);
		}
		return ref;
	}

	/**
	 * creates a macro reference in the buildfile format for the builder used for
	 * the given configuration.
	 * If the builder can not treat macros, returns null
	 */
	public static String createBuildfileMacroReference(String name, IConfiguration cfg) {
		String ref = null;
		if (cfg != null) {
			IToolChain toolChain = cfg.getToolChain();
			if (toolChain != null)
				ref = createBuildfileMacroReference(name, toolChain.getBuilder());
		}
		return ref;
	}

	/**
	 * @return the array of the explicit file macros, referenced in the tool's options
	 * (Explicit file macros are the file-specific macros, whose values are not provided
	 * by the tool-integrator. As a result these macros contain explicit values, but not the values
	 * specified in the format of the builder automatic variables and text functions)
	 */
	public static IBuildMacro[] getReferencedExplitFileMacros(ITool tool) {
		if (tool instanceof Tool) {
			Tool t = (Tool) tool;
			ExplicitFileMacroCollector collector = new ExplicitFileMacroCollector(null);
			t.getToolCommandFlags(null, null, collector, getDefault());
			return collector.getExplicisFileMacros();
		}
		return new IBuildMacro[0];
	}

	static IConfiguration getConfiguration(ITool tool) {
		IBuildObject bo = tool.getParent();
		if (bo instanceof IResourceConfiguration)
			return ((IResourceConfiguration) bo).getParent();
		else if (bo instanceof IToolChain)
			return ((IToolChain) bo).getParent();
		return null;
	}

	/**
	 * @return the array of the explicit file macros, referenced in the given string
	 * (Explicit file macros are the file-specific macros, whose values are not provided
	 * by the tool-integrator. As a result these macros contain explicit values, but not the values
	 * specified in the format of the builder automatic variables and text functions)
	 */
	public static IBuildMacro[] getReferencedExplitFileMacros(String expression, int contextType, Object contextData) {
		ExplicitFileMacroCollector collector = new ExplicitFileMacroCollector(
				getDefault().getMacroContextInfo(contextType, contextData));
		try {
			CdtVariableResolver.resolveToString(expression, collector);
		} catch (CdtVariableException e) {
		}
		return collector.getExplicisFileMacros();
	}

}

Back to the top