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

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

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.envvar.IEnvironmentVariableManager;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.internal.core.envvar.EnvVarCollector;
import org.eclipse.cdt.managedbuilder.core.IBuildPathResolver;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IEnvVarBuildPath;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.envvar.IBuildEnvironmentVariable;
import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentBuildPathsChangeListener;
import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableProvider;
import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableSupplier;
import org.eclipse.cdt.utils.envvar.EnvVarOperationProcessor;

/**
 * This class implements the IEnvironmentVariableProvider interface and provides all
 * build environment functionality to the MBS
 *
 * @since 3.0
 */
public class EnvironmentVariableProvider implements IEnvironmentVariableProvider {
//	private static final QualifiedName fBuildPathVarProperty = new QualifiedName(ManagedBuilderCorePlugin.getUniqueIdentifier(), "buildPathVar");	//$NON-NLS-1$

//	private static final String DELIMITER_WIN32 = ";";  //$NON-NLS-1$
//	private static final String DELIMITER_UNIX = ":";  //$NON-NLS-1$

	private static EnvironmentVariableProvider fInstance = null;
	private List<IEnvironmentBuildPathsChangeListener> fListeners = null;
	private IEnvironmentVariableManager fMngr;
	private boolean fBuildPathVarCheckAllowed;

	private StoredBuildPathEnvironmentContainer fIncludeStoredBuildPathVariables;
	private StoredBuildPathEnvironmentContainer fLibraryStoredBuildPathVariables;

	/**
	 * This class is used by the EnvironmentVariableProvider to calculate the build paths
	 * in case a tool-integrator did not provide the special logic for obtaining the build
	 * paths from environment variable values
	 *
	 * @since 3.0
	 */
	static public class DefaultBuildPathResolver implements IBuildPathResolver {
		private String fDelimiter;

		public DefaultBuildPathResolver(String delimiter) {
			fDelimiter = delimiter;
		}

		@Override
		public String[] resolveBuildPaths(int pathType, String variableName, String variableValue, IConfiguration configuration) {
			if (fDelimiter == null || "".equals(fDelimiter)) //$NON-NLS-1$
				return new String[]{variableValue};

			List<String> list = EnvVarOperationProcessor.convertToList(variableValue,fDelimiter);
			return list.toArray(new String[list.size()]);
		}

	}

	protected EnvironmentVariableProvider(IEnvironmentVariableManager mngr) {
		fMngr = mngr;
	}

	public static EnvironmentVariableProvider getDefault() {
		if (fInstance == null) {
			fInstance = new EnvironmentVariableProvider(CCorePlugin.getDefault().getBuildEnvironmentManager());
			fInstance.fBuildPathVarCheckAllowed = true;
		}
		return fInstance;
	}

	@Override
	public IBuildEnvironmentVariable getVariable(String variableName, Object level, boolean includeParentLevels, boolean resolveMacros) {
		if (variableName == null || "".equals(variableName)) //$NON-NLS-1$
			return null;

		if (level instanceof IConfiguration) {
			return wrap(getVariable(variableName, (IConfiguration)level, resolveMacros));
		}
		return null;
	}

	@Override
	public IEnvironmentVariable getVariable(String variableName, IConfiguration cfg, boolean resolveMacros) {
		return getVariable(variableName, cfg, resolveMacros, true);
	}

	public IEnvironmentVariable getVariable(String variableName, IConfiguration cfg, boolean resolveMacros, boolean checkBuildPaths) {
		ICConfigurationDescription des = ManagedBuildManager.getDescriptionForConfiguration(cfg);
		if (des != null) {
			IEnvironmentVariable variable = fMngr.getVariable(variableName, des, resolveMacros);
			if (checkBuildPaths && resolveMacros && fBuildPathVarCheckAllowed)
				checkBuildPathVariable(cfg, variableName, variable);
			return variable;
		}
		return null;
	}

	@Override
	public IEnvironmentVariable[] getVariables(IConfiguration cfg, boolean resolveMacros) {
		return getVariables(cfg, resolveMacros, true);
	}

	public IEnvironmentVariable[] getVariables(IConfiguration cfg, boolean resolveMacros, boolean checkBuildPaths) {
		ICConfigurationDescription des = ManagedBuildManager.getDescriptionForConfiguration(cfg);
		if (des != null) {
			IEnvironmentVariable vars[] = fMngr.getVariables(des, resolveMacros);
			if (checkBuildPaths && resolveMacros && fBuildPathVarCheckAllowed)
				checkBuildPathVariables(cfg,vars);
			return vars;
		}
		return new IBuildEnvironmentVariable[0];
	}

	public static IBuildEnvironmentVariable wrap(IEnvironmentVariable var) {
		if (var == null)
			return null;
		if (var instanceof IBuildEnvironmentVariable)
			return (IBuildEnvironmentVariable)var;
		return new BuildEnvVar(var);
	}

	public static IBuildEnvironmentVariable[] wrap(IEnvironmentVariable vars[]) {
		if (vars == null)
			return null;
		if (vars instanceof IBuildEnvironmentVariable[])
			return (IBuildEnvironmentVariable[])vars;

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

/*	protected ICConfigurationDescription getDescription(IConfiguration cfg) {
		IProject project = cfg.getOwner().getProject();
		ICProjectDescription des = CoreModel.getDefault().getProjectDescription(project, false);
		if (des != null) {
			return des.getConfigurationById(cfg.getId());
		}
		return null;
	}
*/
	@Override
	public IBuildEnvironmentVariable[] getVariables(Object level, boolean includeParentLevels, boolean resolveMacros) {
		if (level instanceof IConfiguration) {
			return wrap(getVariables((IConfiguration)level, resolveMacros));
		}
		return new IBuildEnvironmentVariable[0];
	}

	@Override
	public String getDefaultDelimiter() {
		return fMngr.getDefaultDelimiter();
	}

	@Override
	public boolean isVariableCaseSensitive() {
		return fMngr.isVariableCaseSensitive();
	}

	@Override
	public IEnvironmentVariableSupplier[] getSuppliers(Object level) {
		return null;
	}

	@Override
	public String[] getBuildPaths(IConfiguration configuration, int buildPathType) {
		ITool tools[] = configuration.getFilteredTools();
		List<String> list = new ArrayList<String>();

		for (ITool tool : tools) {
			IEnvVarBuildPath pathDescriptors[] = tool.getEnvVarBuildPaths();

			if (pathDescriptors == null || pathDescriptors.length == 0)
				continue;

			for (IEnvVarBuildPath curPathDes : pathDescriptors) {
				if (curPathDes.getType() != buildPathType)
					continue;

				String vars[] = curPathDes.getVariableNames();
				if (vars == null || vars.length == 0)
					continue;

				IBuildPathResolver pathResolver = curPathDes.getBuildPathResolver();
				if (pathResolver == null) {
					String delimiter = curPathDes.getPathDelimiter();
					if (delimiter == null)
						delimiter = getDefaultDelimiter();
					pathResolver = new DefaultBuildPathResolver(delimiter);
				}

				for (String varName : vars) {
					IEnvironmentVariable var = getVariable(varName,configuration,true, false);
					if (var == null)
						continue;

					String varValue = var.getValue();
					String paths[] = pathResolver.resolveBuildPaths(buildPathType,varName,varValue,configuration);
					if (paths != null && paths.length != 0)
						list.addAll(Arrays.asList(paths));
				}
			}
		}

		return list.toArray(new String[list.size()]);
	}

	/**
	 * @return a list of registered listeners
	 */
	private List<IEnvironmentBuildPathsChangeListener> getListeners() {
		if (fListeners == null)
			fListeners = new ArrayList<IEnvironmentBuildPathsChangeListener>();
		return fListeners;
	}

	/**
	 * notifies registered listeners
	 */
	private void notifyListeners(IConfiguration configuration, int buildPathType) {
		List<IEnvironmentBuildPathsChangeListener> listeners = getListeners();
		for (IEnvironmentBuildPathsChangeListener listener : listeners) {
			listener.buildPathsChanged(configuration,buildPathType);
		}
	}

	@Override
	public synchronized void subscribe(IEnvironmentBuildPathsChangeListener listener) {
		if (listener == null)
			return;

		List<IEnvironmentBuildPathsChangeListener> listeners = getListeners();

		if (!listeners.contains(listener))
			listeners.add(listener);
	}

	@Override
	public synchronized void unsubscribe(IEnvironmentBuildPathsChangeListener listener) {
		if (listener == null)
			return;

		List<IEnvironmentBuildPathsChangeListener> listeners = getListeners();

		listeners.remove(listener);
	}

	/**
	 * performs a check of the build path variables for the given configuration
	 * If the build variables are changed, the notification is sent
	 */
	public void checkBuildPathVariables(IConfiguration configuration) {
		checkBuildPathVariables(configuration, getVariables(configuration ,true, false));
	}

	/**
	 * performs a check of the build path variables of the specified type
	 * for the given configuration
	 * If the build variables are changed, the notification is sent
	 */
	public void checkBuildPathVariables(IConfiguration configuration,int buildPathType) {
		EnvVarCollector cr = new EnvVarCollector();
		cr.add(getVariables(configuration, true, false));
		checkBuildPathVariables(configuration,buildPathType,cr);
	}

	/**
	 * performs a check of the build path variables
	 * for the given configuration given the set of the variables
	 * defined for this configuration
	 * If the build variables are changed, the notification is sent
	 */
	protected void checkBuildPathVariables(IConfiguration configuration, IEnvironmentVariable vars[]) {
		EnvVarCollector cr = new EnvVarCollector();
		cr.add(vars);
		checkBuildPathVariables(configuration,IEnvVarBuildPath.BUILDPATH_INCLUDE,cr);
		checkBuildPathVariables(configuration,IEnvVarBuildPath.BUILDPATH_LIBRARY,cr);
	}

	/**
	 * performs a check of whether the given variable is the build path variable
	 * and if true checks whether it is changed.
	 * In the case of it is changed all other build path variables are checked
	 * and notification is sent.
	 * If it is not changed, other build path variables are not checked
	 * In the case of the given variable is not the build path one, this method does nothing
	 */
	protected void checkBuildPathVariable(IConfiguration configuration, String varName, IEnvironmentVariable var) {
		checkBuildPathVariable(configuration, IEnvVarBuildPath.BUILDPATH_INCLUDE, varName, var);
		checkBuildPathVariable(configuration, IEnvVarBuildPath.BUILDPATH_LIBRARY, varName, var);
	}

	/**
	 * performs a check of whether the given variable is the build path variable
	 * of the specified type and if true checks whether it is changed.
	 * In the case of it is changed all other build path variables of that type are checked
	 * and notification is sent.
	 * If it is not changed, other build path variables are not checked
	 * In the case of the given variable is not the build path one, this method does nothing
	 */
	protected void checkBuildPathVariable(IConfiguration configuration, int buildPathType, String varName, IEnvironmentVariable var) {
		StoredBuildPathEnvironmentContainer buildPathVars = getStoredBuildPathVariables(buildPathType);
		if (buildPathVars == null)
			return;
		if (buildPathVars.isVariableChanged(varName,var,configuration)) {
			EnvVarCollector cr = new EnvVarCollector();
			cr.add(getVariables(configuration, true, false));
			buildPathVars.synchronize(cr,configuration);
			notifyListeners(configuration, buildPathType);
		}
	}

	/**
	 * performs a check of the build path variables of the specified type
	 * for the given configuration given the set of the variables
	 * defined for this configuration.
	 * If the build variables are changed, the notification is sent
	 */
	protected void checkBuildPathVariables(IConfiguration configuration, int buildPathType, EnvVarCollector varSet) {
		StoredBuildPathEnvironmentContainer buildPathVars = getStoredBuildPathVariables(buildPathType);
		if (buildPathVars == null)
			return;
		if (buildPathVars.checkBuildPathChange(varSet,configuration)) {
			notifyListeners(configuration, buildPathType);
		}
	}

	/**
	 * returns the container of the build variables of the specified type
	 */
	protected StoredBuildPathEnvironmentContainer getStoredBuildPathVariables(int buildPathType) {
		return buildPathType == IEnvVarBuildPath.BUILDPATH_LIBRARY ?
				getStoredLibraryBuildPathVariables() :
				getStoredIncludeBuildPathVariables();
	}

	/**
	 * returns the container of the Include path variables
	 */
	protected StoredBuildPathEnvironmentContainer getStoredIncludeBuildPathVariables() {
		if (fIncludeStoredBuildPathVariables == null)
			fIncludeStoredBuildPathVariables = new StoredBuildPathEnvironmentContainer(IEnvVarBuildPath.BUILDPATH_INCLUDE);
		return fIncludeStoredBuildPathVariables;
	}

	/**
	 * returns the container of the Library path variables
	 */
	protected StoredBuildPathEnvironmentContainer getStoredLibraryBuildPathVariables() {
		if (fLibraryStoredBuildPathVariables == null)
			fLibraryStoredBuildPathVariables = new StoredBuildPathEnvironmentContainer(IEnvVarBuildPath.BUILDPATH_LIBRARY);
		return fLibraryStoredBuildPathVariables;
	}
}

Back to the top