Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9d900f51edec45a1995cfc10133419ba825e1912 (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
/*******************************************************************************
 * Copyright (c) 2003, 2016 IBM 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:
 *     IBM - Initial API and implementation
 *     Miwako Tokugawa (Intel Corporation) - bug 222817 (OptionCategoryApplicability)
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.settings.model.ICStorageElement;
import org.eclipse.cdt.internal.core.SafeStringInterner;
import org.eclipse.cdt.managedbuilder.core.IBuildObject;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IHoldsOptions;
import org.eclipse.cdt.managedbuilder.core.IManagedConfigElement;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.IOptionCategoryApplicability;
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.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.internal.enablement.OptionEnablementExpression;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Path;
import org.osgi.framework.Version;

/**
 *
 */
public class OptionCategory extends BuildObject implements IOptionCategory {

	private static final String EMPTY_STRING = ""; //$NON-NLS-1$
	private static final IOptionCategory[] emtpyCategories = new IOptionCategory[0];

	//  Parent and children
	private IHoldsOptions holder;
	private List<OptionCategory> children; // Note: These are logical Option Category children, not "model" children
	//  Managed Build model attributes
	private IOptionCategory owner; // The logical Option Category parent
	private String ownerId;
	private URL iconPathURL;

	private IConfigurationElement applicabilityCalculatorElement = null;
	private IOptionCategoryApplicability applicabilityCalculator = null;
	private BooleanExpressionApplicabilityCalculator booleanExpressionCalculator = null;

	public static final String APPLICABILITY_CALCULATOR = "applicabilityCalculator"; //$NON-NLS-1$

	//  Miscellaneous
	private boolean isExtensionOptionCategory = false;
	private boolean isDirty = false;
	private boolean resolved = true;

	/*
	 *  C O N S T R U C T O R S
	 */

	public OptionCategory(IOptionCategory owner) {
		this.owner = owner;
	}

	/**
	 * This constructor is called to create an option category defined by an extension point in
	 * a plugin manifest file, or returned by a dynamic element provider
	 *
	 * @param parent  The IHoldsOptions parent of this category, or <code>null</code> if
	 *                defined at the top level
	 * @param element The category definition from the manifest file or a dynamic element
	 *                provider
	 */
	public OptionCategory(IHoldsOptions parent, IManagedConfigElement element) {
		this.holder = parent;
		isExtensionOptionCategory = true;

		// setup for resolving
		resolved = false;

		loadFromManifest(element);

		// Hook me up to the Managed Build Manager
		ManagedBuildManager.addExtensionOptionCategory(this);

		// Add the category to the parent
		parent.addOptionCategory(this);
	}

	/**
	 * Create an <codeOptionCategory</code> based on the specification stored in the
	 * project file (.cdtbuild).
	 *
	 * @param parent The <code>IHoldsOptions</code> object the OptionCategory will be added to.
	 * @param element The XML element that contains the OptionCategory settings.
	 */
	public OptionCategory(IHoldsOptions parent, ICStorageElement element) {
		this.holder = parent;
		isExtensionOptionCategory = false;

		// Initialize from the XML attributes
		loadFromProject(element);

		// Add the category to the parent
		parent.addOptionCategory(this);
	}

	/*
	 *  E L E M E N T   A T T R I B U T E   R E A D E R S   A N D   W R I T E R S
	 */

	public void loadFromManifest(IManagedConfigElement element) {
		ManagedBuildManager.putConfigElement(this, element);

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

		// name
		setName(SafeStringInterner.safeIntern(element.getAttribute(IOptionCategory.NAME)));

		// owner
		ownerId = SafeStringInterner.safeIntern(element.getAttribute(IOptionCategory.OWNER));

		// icon
		if (element.getAttribute(IOptionCategory.ICON) != null && element instanceof DefaultManagedConfigElement) {
			String icon = element.getAttribute(IOptionCategory.ICON);
			iconPathURL = ManagedBuildManager.getURLInBuildDefinitions((DefaultManagedConfigElement) element,
					new Path(icon));
		}

		//get enablements
		IManagedConfigElement enablements[] = element.getChildren(OptionEnablementExpression.NAME);
		if (enablements.length > 0)
			booleanExpressionCalculator = new BooleanExpressionApplicabilityCalculator(enablements);

		// get the applicability calculator, if any
		String applicabilityCalculatorStr = element.getAttribute(APPLICABILITY_CALCULATOR);
		if (applicabilityCalculatorStr != null && element instanceof DefaultManagedConfigElement) {
			applicabilityCalculatorElement = ((DefaultManagedConfigElement) element).getConfigurationElement();
		} else {
			applicabilityCalculator = booleanExpressionCalculator;
		}
	}

	/* (non-Javadoc)
	 * Initialize the OptionCategory information from the XML element
	 * specified in the argument
	 *
	 * @param element An XML element containing the OptionCategory information
	 */
	protected void loadFromProject(ICStorageElement element) {

		// id (unique, do not intern)
		setId(element.getAttribute(IBuildObject.ID));

		// name
		if (element.getAttribute(IBuildObject.NAME) != null) {
			setName(SafeStringInterner.safeIntern(element.getAttribute(IBuildObject.NAME)));
		}

		// owner
		if (element.getAttribute(IOptionCategory.OWNER) != null) {
			ownerId = SafeStringInterner.safeIntern(element.getAttribute(IOptionCategory.OWNER));
		}
		if (ownerId != null) {
			owner = holder.getOptionCategory(ownerId);
		} else {
			owner = getNullOptionCategory();
		}

		// icon - was saved as URL in string form
		if (element.getAttribute(IOptionCategory.ICON) != null) {
			String iconPath = element.getAttribute(IOptionCategory.ICON);
			try {
				iconPathURL = new URL(iconPath);
			} catch (MalformedURLException e) {
				// Print a warning
				ManagedBuildManager.outputIconError(iconPath);
				iconPathURL = null;
			}
		}

		// Hook me in
		if (owner == null)
			((HoldsOptions) holder).addChildCategory(this);
		else if (owner instanceof Tool)
			((Tool) owner).addChildCategory(this);
		else
			((OptionCategory) owner).addChildCategory(this);
	}

	private IOptionCategory getNullOptionCategory() {
		// Handle difference between Tool and others by using
		// the fact that Tool implements IOptionCategory. If so,
		// the holder is in fact a parent category to this category.
		if (holder instanceof IOptionCategory) {
			return (IOptionCategory) holder;
		}
		return null;
	}

	/**
	 * Persist the OptionCategory to the project file.
	 */
	public void serialize(ICStorageElement element) {
		element.setAttribute(IBuildObject.ID, id);

		if (name != null) {
			element.setAttribute(IBuildObject.NAME, name);
		}

		if (owner != null)
			element.setAttribute(IOptionCategory.OWNER, owner.getId());

		if (iconPathURL != null) {
			// Save as URL in string form
			element.setAttribute(IOptionCategory.ICON, iconPathURL.toString());
		}

		// I am clean now
		isDirty = false;
	}

	/*
	 *  P A R E N T   A N D   C H I L D   H A N D L I N G
	 */

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getChildCategories()
	 */
	@Override
	public IOptionCategory[] getChildCategories() {
		if (children != null)
			return children.toArray(new IOptionCategory[children.size()]);
		else
			return emtpyCategories;
	}

	public void addChildCategory(OptionCategory category) {
		if (children == null)
			children = new ArrayList<>();
		children.add(category);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getOptions()
	 */
	@Override
	public Object[][] getOptions(IConfiguration configuration, IHoldsOptions optionHolder) {
		IHoldsOptions[] optionHolders = new IHoldsOptions[1];
		optionHolders[0] = optionHolder;
		return getOptions(optionHolders, FILTER_PROJECT);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getOptions()
	 */
	@Override
	public Object[][] getOptions(IConfiguration configuration) {
		IHoldsOptions[] optionHolders = null;
		if (configuration != null) {
			IHoldsOptions optionHolder = getOptionHolder();
			if (optionHolder instanceof ITool) {
				optionHolders = configuration.getFilteredTools();
			} else if (optionHolder instanceof IToolChain) {
				// Get the toolchain of this configuration, which is
				// the holder equivalent for this option
				optionHolders = new IHoldsOptions[1];
				optionHolders[0] = configuration.getToolChain();
			}
			// TODO: if further option holders were to be added in future,
			// this function needs to be extended
		}
		return getOptions(optionHolders, FILTER_PROJECT);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getOptions()
	 */
	@Override
	public Object[][] getOptions(IResourceInfo resinfo, IHoldsOptions optionHolder) {
		IHoldsOptions[] optionHolders = new IHoldsOptions[1];
		optionHolders[0] = optionHolder;
		boolean isRoot = false;
		if (resinfo instanceof ResourceInfo)
			isRoot = ((ResourceInfo) resinfo).isRoot();
		else if (resinfo instanceof MultiResourceInfo)
			isRoot = ((MultiResourceInfo) resinfo).isRoot();
		return getOptions(optionHolders, isRoot ? FILTER_PROJECT : FILTER_FILE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getOptions()
	 */
	@Override
	public Object[][] getOptions(IResourceConfiguration resConfig) {
		IHoldsOptions[] optionHolders = null;
		if (resConfig != null) {
			IHoldsOptions optionHolder = getOptionHolder();
			if (optionHolder instanceof ITool) {
				optionHolders = resConfig.getTools();
			} else if (optionHolder instanceof IToolChain) {
				// Resource configurations do not support categories that are children
				// of toolchains. The reason for this is that options in such categories
				// are intended to be global. Thus return nothing.
				// TODO: Remove this restriction in future?
				optionHolders = new IHoldsOptions[1];
				optionHolders[0] = null;
			}
			// TODO: if further option holders were to be added in future,
			// this function needs to be extended
		}
		return getOptions(optionHolders, FILTER_FILE);
	}

	private IHoldsOptions getOptionHoldersSuperClass(IHoldsOptions optionHolder) {
		if (optionHolder instanceof ITool)
			return ((ITool) optionHolder).getSuperClass();
		else if (optionHolder instanceof IToolChain)
			return ((IToolChain) optionHolder).getSuperClass();
		return null;
	}

	private Object[][] getOptions(IHoldsOptions[] optionHolders, int filterValue) {
		IHoldsOptions catHolder = getOptionHolder();
		IHoldsOptions optionHolder = null;

		if (optionHolders != null) {
			// Find the child of the configuration/resource configuration that represents the same tool.
			// It could the tool itself, or a "sub-class" of the tool.
			for (int i = 0; i < optionHolders.length; ++i) {
				IHoldsOptions current = optionHolders[i];
				do {
					if (catHolder == current) {
						optionHolder = optionHolders[i];
						break;
					}
				} while ((current = getOptionHoldersSuperClass(current)) != null);
				if (optionHolder != null)
					break;
			}
		}
		if (optionHolder == null) {
			optionHolder = catHolder;
		}

		// Get all of the tool's options and see which ones are part of
		// this category.
		IOption[] allOptions = optionHolder.getOptions();
		Object[][] myOptions = new Object[allOptions.length][2];
		int index = 0;
		for (int i = 0; i < allOptions.length; ++i) {
			IOption option = allOptions[i];
			if (option.getCategory().equals(this)) {

				// Check whether this option can be displayed for a specific resource type.
				if ((option.getResourceFilter() == FILTER_ALL) || (option.getResourceFilter() == filterValue)) {
					myOptions[index] = new Object[2];
					myOptions[index][0] = optionHolder;
					myOptions[index][1] = option;
					index++;
				}
			}
		}

		return myOptions;
	}
	/*
	 *  M O D E L   A T T R I B U T E   A C C E S S O R S
	 */

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getOwner()
	 */
	@Override
	public IOptionCategory getOwner() {
		return owner;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getOptionHolder()
	 */
	@Override
	public IHoldsOptions getOptionHolder() {
		// This will stop at the parent's top category
		if (owner != null)
			return owner.getOptionHolder();
		return holder;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getTool()
	 */
	@Override
	public ITool getTool() {
		// This will stop at the tool's top category
		IHoldsOptions parent = owner.getOptionHolder();
		if (parent instanceof ITool) {
			return (ITool) parent;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.build.managed.IOptionCategory#getIconPath()
	 */
	@Override
	public URL getIconPath() {
		return iconPathURL;
	}

	/*
	 *  O B J E C T   S T A T E   M A I N T E N A N C E
	 */

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.IOptionCategory#isExtensionElement()
	 */
	public boolean isExtensionElement() {
		return isExtensionOptionCategory;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.IOptionCategory#isDirty()
	 */
	@Override
	public boolean isDirty() {
		// This shouldn't be called for an extension OptionCategory
		if (isExtensionOptionCategory)
			return false;
		return isDirty;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.managedbuilder.core.IIOptionCategory#setDirty(boolean)
	 */
	@Override
	public void setDirty(boolean isDirty) {
		this.isDirty = isDirty;
	}

	public void resolveReferences() {
		boolean error = false;
		if (!resolved) {
			resolved = true;
			if (ownerId != null) {
				owner = holder.getOptionCategory(ownerId);
				if (owner == null) {
					if (holder instanceof IOptionCategory) {
						// Report error, only if the parent is a tool and thus also
						// an option category.
						ManagedBuildManager.outputResolveError("owner", //$NON-NLS-1$
								ownerId, "optionCategory", //$NON-NLS-1$
								getId());
						error = true;
					} else if (false == holder.getId().equals(ownerId)) {
						// Report error, if the holder ID does not match the owner's ID.
						ManagedBuildManager.outputResolveError("owner", //$NON-NLS-1$
								ownerId, "optionCategory", //$NON-NLS-1$
								getId());
						error = true;
					}
				}
			}
			if (owner == null) {
				owner = getNullOptionCategory();
			}

			// Hook me in
			if (owner == null && error == false)
				((HoldsOptions) holder).addChildCategory(this);
			else if (owner instanceof Tool)
				((Tool) owner).addChildCategory(this);
			else
				((OptionCategory) owner).addChildCategory(this);
		}
	}

	/**
	 * @return Returns the managedBuildRevision.
	 */
	@Override
	public String getManagedBuildRevision() {
		if (managedBuildRevision == null) {
			if (getOptionHolder() != null) {
				return getOptionHolder().getManagedBuildRevision();
			}
		}
		return managedBuildRevision;
	}

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

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

	/**
	 * Creates a name that uniquely identifies a category. The match name is
	 * a concatenation of the tool and categories, e.g. Tool->Cat1->Cat2
	 * maps onto the string "Tool|Cat1|Cat2|"
	 *
	 * @param catOrTool category or tool for which to build the match name
	 * @return match name
	 */
	static public String makeMatchName(IBuildObject catOrTool) {
		String catName = EMPTY_STRING;

		// Build the match name.
		do {
			catName = catOrTool.getName() + "|" + catName; //$NON-NLS-1$
			if (catOrTool instanceof ITool)
				break;
			else if (catOrTool instanceof IOptionCategory) {
				catOrTool = ((IOptionCategory) catOrTool).getOwner();
			} else
				break;
		} while (catOrTool != null);

		return catName;
	}

	/**
	 * Finds an option category from an array of categories by comparing against
	 * a match name. The match name is a concatenation of the tool and categories,
	 * e.g. Tool->Cat1->Cat2 maps onto the string "Tool|Cat1|Cat2|"
	 *
	 * @param matchName an identifier to search
	 * @param cats as returned by getChildCategories(), i.e. non-flattened
	 * @return category or tool, if found and null otherwise
	 */
	static public Object findOptionCategoryByMatchName(String matchName, IOptionCategory[] cats) {
		Object primary = null;

		for (int j = 0; j < cats.length; j++) {
			IBuildObject catOrTool = cats[j];
			// Build the match name
			String catName = makeMatchName(catOrTool);
			// Check whether the name matches
			if (catName.equals(matchName)) {
				primary = cats[j];
				break;
			} else if (matchName.startsWith(catName)) {
				// If there is a common root then check for any further children
				primary = findOptionCategoryByMatchName(matchName, cats[j].getChildCategories());
				if (primary != null)
					break;
			}
		}
		return primary;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.cdt.managedbuilder.core.IOptionCategory#getApplicabilityCalculator()
	 */
	@Override
	public IOptionCategoryApplicability getApplicabilityCalculator() {
		if (applicabilityCalculator == null) {
			if (applicabilityCalculatorElement != null) {
				try {
					if (applicabilityCalculatorElement.getAttribute(APPLICABILITY_CALCULATOR) != null)
						applicabilityCalculator = (IOptionCategoryApplicability) applicabilityCalculatorElement
								.createExecutableExtension(APPLICABILITY_CALCULATOR);
				} catch (CoreException e) {
					ManagedBuilderCorePlugin.log(e);
				}
			}
		}
		return applicabilityCalculator;
	}
}

Back to the top