Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 28067794b084da9324a0bff913cb639b24e06bd3 (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
/*******************************************************************************
 * Copyright (c) 2005, 2012 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 * Miwako Tokugawa (Intel Corporation) - bug 222817 (OptionCategoryApplicability)
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.enablement;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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.IFileInfo;
import org.eclipse.cdt.managedbuilder.core.IFolderInfo;
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.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.macros.BuildMacroProvider;
import org.eclipse.cdt.managedbuilder.internal.macros.IMacroContextInfo;
import org.eclipse.cdt.managedbuilder.internal.macros.OptionContextData;
import org.eclipse.cdt.managedbuilder.macros.BuildMacroException;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver;
import org.eclipse.cdt.utils.cdtvariables.SupplierBasedCdtVariableSubstitutor;

public class CheckOptionExpression implements IBooleanExpression {
	public static final String NAME = "checkOption"; 	//$NON-NLS-1$

	public static final String OPTION_ID = "optionId"; 	//$NON-NLS-1$
	public static final String HOLDER_ID = "holderId"; 	//$NON-NLS-1$
	public static final String VALUE = "value"; 	//$NON-NLS-1$
	public static final String IS_REGEX = "isRegex";	//$NON-NLS-1$
	public static final String OTHER_OPTION_ID = "otherOptionId"; 	//$NON-NLS-1$
	public static final String OTHER_HOLDER_ID = "otherHolderId"; 	//$NON-NLS-1$

	private String fOptionId;
	private String fHolderId;
	private String fValue;
	private boolean fIsRegex;
	private String fOtherOptionId;
	private String fOtherHolderId;

	public CheckOptionExpression(IManagedConfigElement element){
		fOptionId = element.getAttribute(OPTION_ID);
		fHolderId = element.getAttribute(HOLDER_ID);
		fValue = element.getAttribute(VALUE);
		fIsRegex = OptionEnablementExpression.getBooleanValue(element.getAttribute(IS_REGEX));
		fOtherOptionId = element.getAttribute(OTHER_OPTION_ID);
		fOtherHolderId = element.getAttribute(OTHER_HOLDER_ID);
	}

	@Override
	public boolean evaluate(IResourceInfo rcInfo,
            IHoldsOptions holder,
            IOption option) {
		boolean result = false;
		IBuildObject ho[] = getHolderAndOption(fOptionId, fHolderId,
				rcInfo, holder, option);

		if(ho != null){
			if(fValue != null)
				result = evaluate((IOption)ho[1],((IHoldsOptions)ho[0]),fValue);
			else {
				IBuildObject otherHo[] = getHolderAndOption(fOtherOptionId, fOtherHolderId,
						rcInfo, holder, option);
				if(otherHo != null)
					result = evaluate((IOption)ho[1],((IHoldsOptions)ho[0]),
							(IOption)otherHo[1],((IHoldsOptions)otherHo[0]));
			}
		}
		return result;
	}

	@Override
	public boolean evaluate(IResourceInfo rcInfo,
            IHoldsOptions holder,
            IOptionCategory category) {
		boolean result = false;
		IBuildObject ho[] = getHolderAndOption(fOptionId, fHolderId,
				rcInfo, holder);

		if(ho != null){
			if(fValue != null)
				result = evaluate((IOption)ho[1],((IHoldsOptions)ho[0]),fValue);
			// otherOptionId shouldn't be set when enabling optionCategory
		}
		return result;
	}

	public boolean evaluate(IOption option, IHoldsOptions holder, String value){
		IBuildMacroProvider provider = ManagedBuildManager.getBuildMacroProvider();
		String delimiter = ManagedBuildManager.getEnvironmentVariableProvider().getDefaultDelimiter();
		String inexVal = " "; 	//$NON-NLS-1$
		try {
			String resolvedValue = provider.resolveValue(value, inexVal, delimiter,
					IBuildMacroProvider.CONTEXT_OPTION,
					new OptionContextData(option,holder));

			switch(option.getValueType()){
				case IOption.STRING:
				case IOption.TREE:
				case IOption.ENUMERATED:{
					String stringValue = option.getStringValue();
					stringValue = provider.resolveValue(stringValue, inexVal, delimiter,
							IBuildMacroProvider.CONTEXT_OPTION,
							new OptionContextData(option,holder));
					if(fIsRegex){
						Pattern pattern = Pattern.compile(resolvedValue);
						Matcher matcher = pattern.matcher(stringValue);
						return matcher.matches();
					}
					return stringValue.equals(resolvedValue);
				}
				case IOption.BOOLEAN:
					return option.getBooleanValue() == OptionEnablementExpression.getBooleanValue(resolvedValue);
				case IOption.INCLUDE_PATH:
				case IOption.STRING_LIST:
				case IOption.PREPROCESSOR_SYMBOLS:
				case IOption.LIBRARIES:
				case IOption.OBJECTS:
				case IOption.INCLUDE_FILES:
				case IOption.LIBRARY_PATHS:
				case IOption.LIBRARY_FILES:
				case IOption.MACRO_FILES:
				case IOption.UNDEF_INCLUDE_PATH:
				case IOption.UNDEF_PREPROCESSOR_SYMBOLS:
				case IOption.UNDEF_INCLUDE_FILES:
				case IOption.UNDEF_LIBRARY_PATHS:
				case IOption.UNDEF_LIBRARY_FILES:
				case IOption.UNDEF_MACRO_FILES:{
					@SuppressWarnings("unchecked")
					List<String> list = (List<String>)option.getValue();
					String listValue = provider.convertStringListToString(list.toArray(new String[list.size()]),delimiter);

					listValue = provider.resolveValue(listValue, inexVal, delimiter,
							IBuildMacroProvider.CONTEXT_OPTION,
							new OptionContextData(option,holder));

					if(fIsRegex){
						Pattern pattern = Pattern.compile(resolvedValue);
						Matcher matcher = pattern.matcher(listValue);
						return matcher.matches();
					}
					return listValue.equals(resolvedValue);
				}
				default:
					break;
			}
		} catch (BuildException e) {
		} catch (BuildMacroException e) {
		}
		return false;
	}

	public boolean evaluate(IOption option, IHoldsOptions holder,
			IOption otherOption, IHoldsOptions otherHolder){
		try {
			if(option.getValueType() != otherOption.getValueType())
				return false;

			BuildMacroProvider provider = (BuildMacroProvider)ManagedBuildManager.getBuildMacroProvider();
			String delimiter = ManagedBuildManager.getEnvironmentVariableProvider().getDefaultDelimiter();
			String inexVal = " "; 	//$NON-NLS-1$

			switch(option.getValueType()){
				case IOption.STRING:
				case IOption.TREE:
				case IOption.ENUMERATED:{
					String stringValue = option.getStringValue();
					stringValue = provider.resolveValue(stringValue, inexVal, delimiter,
							IBuildMacroProvider.CONTEXT_OPTION,
							new OptionContextData(option,holder));

					String str = otherOption.getStringValue();
					str = provider.resolveValue(str, inexVal, delimiter,
								IBuildMacroProvider.CONTEXT_OPTION,
								new OptionContextData(otherOption,otherHolder));

					return stringValue.equals(str);
				}
				case IOption.BOOLEAN:
					return option.getBooleanValue() == otherOption.getBooleanValue();
				case IOption.INCLUDE_PATH:
				case IOption.STRING_LIST:
				case IOption.PREPROCESSOR_SYMBOLS:
				case IOption.LIBRARIES:
				case IOption.OBJECTS:
				case IOption.INCLUDE_FILES:
				case IOption.LIBRARY_PATHS:
				case IOption.LIBRARY_FILES:
				case IOption.MACRO_FILES:
				case IOption.UNDEF_INCLUDE_PATH:
				case IOption.UNDEF_PREPROCESSOR_SYMBOLS:
				case IOption.UNDEF_INCLUDE_FILES:
				case IOption.UNDEF_LIBRARY_PATHS:
				case IOption.UNDEF_LIBRARY_FILES:
				case IOption.UNDEF_MACRO_FILES:{
					@SuppressWarnings("unchecked")
					List<String> list = (List<String>)option.getValue();
					String listValue[] = list.toArray(new String[list.size()]);

					@SuppressWarnings("unchecked")
					List<String> otherList = (List<String>)otherOption.getValue();
					String otherValue[] = otherList.toArray(new String[otherList.size()]);

					IMacroContextInfo info = provider.getMacroContextInfo(IBuildMacroProvider.CONTEXT_OPTION,
							new OptionContextData(option,holder));
					SupplierBasedCdtVariableSubstitutor subst = provider.getMacroSubstitutor(info,inexVal,delimiter);

					listValue = CdtVariableResolver.resolveStringListValues(listValue,subst,false);

					info = provider.getMacroContextInfo(IBuildMacroProvider.CONTEXT_OPTION,
							new OptionContextData(otherOption,otherHolder));
					subst = provider.getMacroSubstitutor(info,inexVal,delimiter);

					otherValue = CdtVariableResolver.resolveStringListValues(otherValue,subst,false);

					if(listValue.length == otherValue.length){
						for(int i = 0; i < listValue.length; i++){
							if(!listValue[i].equals(otherValue[i]))
								return false;
						}
						return true;
					}
					return false;
				}
				default:
					break;
			}
		} catch (BuildException e) {
		} catch (CdtVariableException e) {
		} catch (ClassCastException e) {
		}
		return false;
	}

	protected IBuildObject[] getHolderAndOption(String optionId,
			String holderId,
			IResourceInfo rcInfo,
	        IHoldsOptions holder,
	        IOption option
			){
		IBuildObject result[] = null;
		if(optionId == null)
			result = new IBuildObject[]{holder,option};
		else {
			IHoldsOptions hld = null;
			if(holderId == null)
				hld = holder;
			else
				hld = getHolder(holderId,rcInfo);

			if(hld != null) {
				IOption opt = getOption(optionId,hld);
				if(opt != null)
					result = new IBuildObject[]{hld,opt};
			}
		}
		return result;
	}

	/* This is called for optionCategory */
	protected IBuildObject[] getHolderAndOption(String optionId,
			String holderId,
			IResourceInfo rcInfo,
			IHoldsOptions holder
	){
		IBuildObject result[] = null;
		if(optionId != null) {
			IHoldsOptions hld = null;
			if(holderId == null)
				hld = holder;
			else
				hld = getHolder(holderId,rcInfo);

			if(hld != null) {
				IOption opt = getOption(optionId,hld);
				if(opt != null)
					result = new IBuildObject[]{hld,opt};
			}
		}
		return result;
	}

	protected IOption getOption(String optionId,
            IHoldsOptions holder){
		return holder.getOptionBySuperClassId(optionId);
	}

	protected IHoldsOptions getHolder(String id,
			IResourceInfo rcInfo){
		IHoldsOptions holder = null;
		if(rcInfo instanceof IFileInfo){
			IHoldsOptions holders[] = ((IResourceConfiguration)rcInfo).getTools();
			for(int i = 0; i < holders.length; i++){
				if(isHolder(id,holders[i])){
					holder = holders[i];
					break;
				}
			}
		} else if (rcInfo instanceof IFolderInfo){
			IToolChain tc = ((IFolderInfo)rcInfo).getToolChain();
			if(isHolder(id,tc))
				holder = tc;
			else {
				IHoldsOptions holders[] = tc.getTools();
				for(int i = 0; i < holders.length; i++){
					if(isHolder(id,holders[i])){
						holder = holders[i];
						break;
					}
				}
			}
		}

		return holder;
	}

	protected boolean isHolder(String id, IHoldsOptions holder){
		do {
			if(id.equals(holder.getId()))
				return true;
			if(holder instanceof IToolChain)
				holder = ((IToolChain)holder).getSuperClass();
			else if(holder instanceof ITool)
				holder = ((ITool)holder).getSuperClass();
			else
				holder = null;
		} while(holder != null);

		return false;
	}
}

Back to the top