Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 90a842c132c0ca861aae9ac354af2a646c800f2b (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
/*******************************************************************************
 * Copyright (c) 2007 QNX Software Systems 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
 *******************************************************************************/
package org.eclipse.cdt.msw.build;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.envvar.IBuildEnvironmentVariable;
import org.eclipse.cdt.managedbuilder.envvar.IConfigurationEnvironmentVariableSupplier;
import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableProvider;
import org.eclipse.cdt.managedbuilder.envvar.IProjectEnvironmentVariableSupplier;
import org.eclipse.cdt.utils.WindowsRegistry;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

/**
 * @author DSchaefer
 *
 */
public class WinEnvironmentVariableSupplier
	implements IConfigurationEnvironmentVariableSupplier, IProjectEnvironmentVariableSupplier {
	
	private static Map<String, IBuildEnvironmentVariable> envvars;
	private static String sdkDir;
	private static String vcDir;
	
	private static class WindowsBuildEnvironmentVariable implements IBuildEnvironmentVariable {
		
		private final String name;
		private final String value;
		private final int operation;
		
		public WindowsBuildEnvironmentVariable(String name, String value, int operation) {
			this.name = name;
			this.value = value;
			this.operation = operation;
		}
		
		public String getDelimiter() {
			return ";";
		}
		
		public String getName() {
			return name;
		}
		
		public String getValue() {
			return value;
		}

		public int getOperation() {
			return operation;
		}

	}

	public WinEnvironmentVariableSupplier() {
		initvars();
	}
	
	public IBuildEnvironmentVariable getVariable(String variableName,
			IManagedProject project, IEnvironmentVariableProvider provider) {
		return envvars.get(variableName);
	}
	
	public IBuildEnvironmentVariable getVariable(String variableName,
			IConfiguration configuration, IEnvironmentVariableProvider provider) {
		return envvars.get(variableName);
	}

	public IBuildEnvironmentVariable[] getVariables(IManagedProject project,
			IEnvironmentVariableProvider provider) {
		return envvars.values().toArray(new IBuildEnvironmentVariable[envvars.size()]);
	}
	
	public IBuildEnvironmentVariable[] getVariables(
			IConfiguration configuration, IEnvironmentVariableProvider provider) {
		return envvars.values().toArray(new IBuildEnvironmentVariable[envvars.size()]);
	}
	
	private static String getSoftwareKey(WindowsRegistry reg, String subkey, String name) {
		String value = reg.getLocalMachineValue("SOFTWARE\\" + subkey, name);
		// Visual Studio is a 32 bit application so on Windows 64 the keys will be in Wow6432Node 
		if (value == null) {
			value = reg.getLocalMachineValue("SOFTWARE\\Wow6432Node\\" + subkey, name);
		}
		return value;
	}
	
	// Current support is for Windows SDK 8.0 with Visual C++ 11.0
	// or Windows SDK 7.1 with Visual C++ 10.0
	// or Windows SDK 7.0 with Visual C++ 9.0 
	private static String getSDKDir() {
		WindowsRegistry reg = WindowsRegistry.getRegistry();
		String sdkDir = getSoftwareKey(reg, "Microsoft\\Microsoft SDKs\\Windows\\v8.0", "InstallationFolder");
		if (sdkDir != null)
			return sdkDir;
		sdkDir = getSoftwareKey(reg, "Microsoft\\Microsoft SDKs\\Windows\\v7.1", "InstallationFolder");
		if (sdkDir != null)
			return sdkDir;
		return getSoftwareKey(reg, "Microsoft SDKs\\Windows\\v7.0", "InstallationFolder");
	}
	
	private static String getVCDir() {
		WindowsRegistry reg = WindowsRegistry.getRegistry();
		String vcDir = getSoftwareKey(reg, "Microsoft\\VisualStudio\\SxS\\VC7", "11.0");
		if (vcDir != null)
			return vcDir;
		vcDir = getSoftwareKey(reg, "Microsoft\\VisualStudio\\SxS\\VC7", "10.0");
		if (vcDir != null)
			return vcDir;
		return getSoftwareKey(reg, "Microsoft\\VisualStudio\\SxS\\VC7", "9.0");
	}
	
	public static IPath[] getIncludePath() {
		// Include paths
		List<IPath> includePaths = new ArrayList<IPath>();
		if (sdkDir != null) {
			includePaths.add(new Path(sdkDir.concat("Include")));
			includePaths.add(new Path(sdkDir.concat("Include\\gl")));
		}
		
		if (vcDir != null) {
			includePaths.add(new Path(vcDir.concat("Include")));
		}
		return includePaths.toArray(new IPath[0]);
	}
	
	private static void addvar(IBuildEnvironmentVariable var) {
		envvars.put(var.getName(), var);
	}
	
	private static synchronized void initvars() {
		if (envvars != null)
			return;
		envvars = new HashMap<String, IBuildEnvironmentVariable>();
		
		// The SDK Location
		sdkDir = getSDKDir();
		vcDir = getVCDir();
		
		if (sdkDir == null && vcDir == null) {
			return;
		}
		
		// INCLUDE
		StringBuffer buff = new StringBuffer();
		IPath includePaths[] = getIncludePath();
		for (IPath path : includePaths) {
			buff.append(path.toOSString()).append(';');
		}
		addvar(new WindowsBuildEnvironmentVariable("INCLUDE", buff.toString(), IBuildEnvironmentVariable.ENVVAR_PREPEND));

		// LIB
		buff = new StringBuffer();
		if (vcDir != null)
			buff.append(vcDir).append("Lib;");
		if (sdkDir != null) {
			buff.append(sdkDir).append("Lib;");
			buff.append(sdkDir).append("Lib\\win8\\um\\x86;");
		}
		
		addvar(new WindowsBuildEnvironmentVariable("LIB", buff.toString(), IBuildEnvironmentVariable.ENVVAR_PREPEND));
		
		// PATH
		buff = new StringBuffer();
		if (vcDir != null) {
			buff.append(vcDir).append("..\\Common7\\IDE;");
			buff.append(vcDir).append("..\\Common7\\Tools;");
			buff.append(vcDir).append("Bin;");
			buff.append(vcDir).append("vcpackages;");
		}
		if (sdkDir != null) {
			buff.append(sdkDir).append("Bin;");
		}
		addvar(new WindowsBuildEnvironmentVariable("PATH", buff.toString(), IBuildEnvironmentVariable.ENVVAR_PREPEND));
	}

}

Back to the top