Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b5a7d2cf6126a26f547da9b4a572c3a49ca60fd (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
/*******************************************************************************
 * 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.core;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.managedbuilder.core.IBuildObject;
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.IInputType;
import org.eclipse.cdt.managedbuilder.core.IManagedConfigElement;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionApplicability;
import org.eclipse.cdt.managedbuilder.core.IOptionCategory;
import org.eclipse.cdt.managedbuilder.core.IOptionCategoryApplicability;
import org.eclipse.cdt.managedbuilder.core.IOutputType;
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.internal.enablement.AdjustmentContext;
import org.eclipse.cdt.managedbuilder.internal.enablement.OptionEnablementExpression;

public class BooleanExpressionApplicabilityCalculator implements IOptionApplicability, IOptionCategoryApplicability {
	private OptionEnablementExpression fExpressions[];

	private Map<String, Set<String>> fRefPropsMap;

	public BooleanExpressionApplicabilityCalculator(IManagedConfigElement optionElement) {
		this(optionElement.getChildren(OptionEnablementExpression.NAME));
	}

	public BooleanExpressionApplicabilityCalculator(IManagedConfigElement enablementElements[]) {
		fExpressions = new OptionEnablementExpression[enablementElements.length];

		for (int i = 0; i < enablementElements.length; i++) {
			fExpressions[i] = new OptionEnablementExpression(enablementElements[i]);
		}
	}

	@Override
	public boolean isOptionVisible(IBuildObject configuration, IHoldsOptions holder, IOption option) {
		IResourceInfo rcInfo = rcInfoFromConfiguration(configuration);
		if (rcInfo != null)
			return evaluate(rcInfo, holder, option, OptionEnablementExpression.FLAG_UI_VISIBILITY);
		return true;
	}

	public static IResourceInfo rcInfoFromConfiguration(IBuildObject configuration) {
		if (configuration instanceof IFolderInfo)
			return (IFolderInfo) configuration;
		if (configuration instanceof IFileInfo)
			return (IFileInfo) configuration;
		if (configuration instanceof IConfiguration)
			return ((IConfiguration) configuration).getRootFolderInfo();
		return null;
	}

	public boolean isInputTypeEnabled(ITool tool, IInputType type) {
		return evaluate(tool.getParentResourceInfo(), tool, null, OptionEnablementExpression.FLAG_CMD_USAGE);
	}

	public boolean isOutputTypeEnabled(ITool tool, IOutputType type) {
		return evaluate(tool.getParentResourceInfo(), tool, null, OptionEnablementExpression.FLAG_CMD_USAGE);
	}

	public boolean isToolUsedInCommandLine(IResourceInfo rcInfo, ITool tool) {
		return evaluate(rcInfo, tool, null, OptionEnablementExpression.FLAG_CMD_USAGE);
	}

	@Override
	public boolean isOptionEnabled(IBuildObject configuration, IHoldsOptions holder, IOption option) {
		IResourceInfo rcInfo = rcInfoFromConfiguration(configuration);
		if (rcInfo != null)
			return evaluate(rcInfo, holder, option, OptionEnablementExpression.FLAG_UI_ENABLEMENT);
		return true;
	}

	@Override
	public boolean isOptionUsedInCommandLine(IBuildObject configuration, IHoldsOptions holder, IOption option) {
		IResourceInfo rcInfo = rcInfoFromConfiguration(configuration);
		if (rcInfo != null)
			return evaluate(rcInfo, holder, option, OptionEnablementExpression.FLAG_CMD_USAGE);
		return true;
	}

	public boolean evaluate(IResourceInfo rcInfo, IHoldsOptions holder, IOption option, int flags) {
		for (int i = 0; i < fExpressions.length; i++) {
			if (!fExpressions[i].evaluate(rcInfo, holder, option, flags))
				return false;
		}
		return true;
	}

	/*	public boolean performAdjustment(IBuildObject configuration,
				IHoldsOptions holder, IOption option, boolean extensionAdjustment){
			boolean adjusted = false;
			for(int i = 0; i < fExpressions.length; i++){
				if(fExpressions[i].performAdjustment(configuration, holder, option, extensionAdjustment))
					adjusted = true;
			}
			return adjusted;
		}
	*/
	public boolean adjustOption(IResourceInfo rcInfo, IHoldsOptions holder, IOption option,
			boolean extensionAdjustment) {
		boolean adjusted = false;
		AdjustmentContext context = extensionAdjustment ? null : new AdjustmentContext();
		for (int i = 0; i < fExpressions.length; i++) {
			if (fExpressions[i].adjustOption(rcInfo, holder, option, context, extensionAdjustment))
				adjusted = true;
		}

		if (context != null) {
			String unadjusted[] = context.getUnadjusted();
			for (int i = 0; i < unadjusted.length; i++) {
				OptionEnablementExpression.adjustOption(rcInfo, holder, option, unadjusted[i], null,
						extensionAdjustment);
			}
		}
		return adjusted;
	}

	public boolean adjustToolChain(IFolderInfo info, IToolChain tChain, boolean extensionAdjustment) {
		boolean adjusted = false;
		AdjustmentContext context = extensionAdjustment ? null : new AdjustmentContext();
		for (int i = 0; i < fExpressions.length; i++) {
			if (fExpressions[i].adjustToolChain(info, tChain, context, extensionAdjustment))
				adjusted = true;
		}

		if (context != null) {
			String unadjusted[] = context.getUnadjusted();
			for (int i = 0; i < unadjusted.length; i++) {
				OptionEnablementExpression.adjustToolChain(info, tChain, unadjusted[i], null, extensionAdjustment);
			}
		}

		return adjusted;
	}

	public boolean adjustTool(IResourceInfo info, ITool tool, boolean extensionAdjustment) {
		boolean adjusted = false;
		AdjustmentContext context = extensionAdjustment ? null : new AdjustmentContext();
		for (int i = 0; i < fExpressions.length; i++) {
			if (fExpressions[i].adjustTool(info, tool, context, extensionAdjustment))
				adjusted = true;
		}

		if (context != null) {
			String unadjusted[] = context.getUnadjusted();
			for (int i = 0; i < unadjusted.length; i++) {
				OptionEnablementExpression.adjustTool(info, tool, unadjusted[i], null, extensionAdjustment);
			}
		}

		return adjusted;
	}

	public boolean adjustConfiguration(IConfiguration cfg, boolean extensionAdjustment) {
		boolean adjusted = false;
		AdjustmentContext context = extensionAdjustment ? null : new AdjustmentContext();
		for (int i = 0; i < fExpressions.length; i++) {
			if (fExpressions[i].adjustConfiguration(cfg, context, extensionAdjustment))
				adjusted = true;
		}

		if (context != null) {
			String unadjusted[] = context.getUnadjusted();
			for (int i = 0; i < unadjusted.length; i++) {
				OptionEnablementExpression.adjustConfiguration(cfg, unadjusted[i], null, extensionAdjustment);
			}
		}

		return adjusted;
	}

	private Map<String, Set<String>> getReferencedProperties() {
		if (fRefPropsMap == null) {
			fRefPropsMap = new HashMap<>();

			for (int i = 0; i < fExpressions.length; i++) {
				fExpressions[i].getReferencedProperties(fRefPropsMap);
			}
		}
		return fRefPropsMap;
	}

	public boolean referesProperty(String id) {
		Map<String, Set<String>> map = getReferencedProperties();

		return map.containsKey(id);
	}

	public boolean referesPropertyValue(String propertyId, String valueId) {
		Map<String, Set<String>> map = getReferencedProperties();
		Set<String> set = map.get(propertyId);
		if (set != null)
			return set.contains(valueId);
		return false;
	}

	public String[] getReferencedPropertyIds() {
		Map<String, Set<String>> map = getReferencedProperties();
		return map.keySet().toArray(new String[map.size()]);
	}

	public String[] getReferencedValueIds(String propertyId) {
		Map<String, Set<String>> map = getReferencedProperties();
		Set<String> set = map.get(propertyId);
		return set.toArray(new String[set.size()]);
	}

	@Override
	public boolean isOptionCategoryVisible(IBuildObject configuration, IHoldsOptions optHolder,
			IOptionCategory category) {
		return evaluateCategory(rcInfoFromConfiguration(configuration), optHolder, category);
	}

	private boolean evaluateCategory(IResourceInfo rcInfo, IHoldsOptions holder, IOptionCategory category) {
		for (int i = 0; i < fExpressions.length; i++) {
			if (!fExpressions[i].evaluate(rcInfo, holder, category))
				return false;
		}
		return true;
	}

}

Back to the top