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

import java.util.HashSet;
import java.util.Set;

import org.eclipse.cdt.core.envvar.IEnvironmentContributor;
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.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.internal.envvar.ExternalExtensionEnvironmentSupplier;
import org.eclipse.cdt.managedbuilder.internal.envvar.MbsEnvironmentSupplier;
import org.eclipse.cdt.utils.envvar.EnvironmentCollector;

public class BuildEnvironmentContributor implements IEnvironmentContributor {
	private BuildBuildData fBuildData;
	private IConfiguration fCfg;
	private ICConfigurationDescription fCfgDes;

	private MbsEnvironmentSupplier fMbsSupplier = new MbsEnvironmentSupplier();

	public BuildEnvironmentContributor(BuildBuildData buildData) {
		fBuildData = buildData;
		fCfg = fBuildData.getBuilder().getParent().getParent();
		fCfgDes = ManagedBuildManager.getDescriptionForConfiguration(fCfg);
	}

	@Override
	public IEnvironmentVariable getVariable(String name, IEnvironmentVariableManager provider) {

		EnvironmentCollector collector = new EnvironmentCollector();
		ExternalExtensionEnvironmentSupplier extSupplier = new ExternalExtensionEnvironmentSupplier(provider);

		boolean varFound = false;

		IEnvironmentVariable var = extSupplier.getVariable(name, fCfg.getManagedProject());
		varFound = processVariable(name, var, collector, provider, varFound);

		var = fMbsSupplier.getVariable(name, fCfg);
		varFound = processVariable(name, var, collector, provider, varFound);

		var = extSupplier.getVariable(name, fCfg);
		varFound = processVariable(name, var, collector, provider, varFound);

		return collector.getVariable(name);
	}

	@Override
	public IEnvironmentVariable[] getVariables(IEnvironmentVariableManager provider) {
		EnvironmentCollector collector = new EnvironmentCollector();
		ExternalExtensionEnvironmentSupplier extSupplier = new ExternalExtensionEnvironmentSupplier(provider);

		Set<String> set = null;

		IEnvironmentVariable vars[] = extSupplier.getVariables(fCfg.getManagedProject());
		set = processVariables(vars, collector, provider, set);

		vars = fMbsSupplier.getVariables(fCfg);
		set = processVariables(vars, collector, provider, set);

		vars = extSupplier.getVariables(fCfg);
		set = processVariables(vars, collector, provider, set);

		return collector.getVariables();
	}

	private boolean processVariable(String name, IEnvironmentVariable var, EnvironmentCollector collector,
			IEnvironmentVariableManager provider, boolean varFound) {
		if (var != null) {
			if (!varFound) {
				varFound = true;
				IEnvironmentVariable base = provider.getVariable(name, fCfgDes, false);
				if (base != null)
					collector.addVariable(base);
			}
			collector.addVariable(var);
		}

		return varFound;
	}

	private Set<String> processVariables(IEnvironmentVariable vars[], EnvironmentCollector collector,
			IEnvironmentVariableManager provider, Set<String> set) {
		boolean checkSet = true;
		if (vars != null && vars.length != 0) {
			if (set == null) {
				set = new HashSet<String>();
				checkSet = false;
			}

			for (int i = 0; i < vars.length; i++) {
				if (vars[i] == null)
					continue;

				if (set.add(vars[i].getName()) || !checkSet) {
					IEnvironmentVariable base = provider.getVariable(vars[i].getName(), fCfgDes, false);
					if (base != null) {
						collector.addVariable(base);
					}
				}

				collector.addVariable(vars[i]);
			}
			//collector.addVariables(vars);
		}

		return set;
	}

}

Back to the top