Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 859cce141e794e1364555d776abfa95c1d1ca71b (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
/*******************************************************************************
 * Copyright (c) 2006, 2010 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
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.managedbuilder.gnu.mingw;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
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.utils.PathUtil;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;

/**
 * @author Doug Schaefer
 *
 * @noextend This class is not intended to be subclassed by clients.
 */
public class MingwEnvironmentVariableSupplier implements IConfigurationEnvironmentVariableSupplier {
	private static String envPathValueCached = null;
	private static String envMingwHomeValueCached = null;
	private static IPath binDir = null;

	private static class MingwBuildEnvironmentVariable implements IBuildEnvironmentVariable {
		private final String name;
		private final String value;
		private final int operation;

		public MingwBuildEnvironmentVariable(String name, String value, int operation) {
			this.name = name;
			this.value = value;
			this.operation = operation;
		}

		@Override
		public String getName() {
			return name;
		}

		@Override
		public String getValue() {
			return value;
		}

		@Override
		public int getOperation() {
			return operation;
		}

		@Override
		public String getDelimiter() {
			return ";"; //$NON-NLS-1$
		}
	}

	private IBuildEnvironmentVariable path;

	/**
	 * @return location of $MINGW_HOME/bin folder on the file-system.
	 *
	 * If you use this do not cache results to ensure user preferences are accounted for.
	 * Please rely on internal caching.
	 */
	public static IPath getBinDir() {
		IEnvironmentVariable varPath = CCorePlugin.getDefault().getBuildEnvironmentManager().getVariable("PATH", null, true); //$NON-NLS-1$
		String envPathValue = varPath != null ? varPath.getValue() : null;
		IEnvironmentVariable varMingwHome = CCorePlugin.getDefault().getBuildEnvironmentManager().getVariable("MINGW_HOME", null, true); //$NON-NLS-1$
		String envMingwHomeValue = varMingwHome != null ? varMingwHome.getValue() : null;

		if (CDataUtil.objectsEqual(envPathValue, envPathValueCached) && CDataUtil.objectsEqual(envMingwHomeValue, envMingwHomeValueCached)) {
			return binDir;
		}

		envPathValueCached = envPathValue;
		envMingwHomeValueCached = envMingwHomeValue;
		binDir = null;

		// Check $MINGW_HOME
		IPath mingwBinDir = new Path(envMingwHomeValue + "\\bin"); //$NON-NLS-1$
		if (mingwBinDir.toFile().isDirectory()) {
			binDir = mingwBinDir;
			return binDir;
		}

		// Try the mingw directory in the platform install directory
		// CDT distributions like Wascana may distribute MinGW like that
		IPath installPath = new Path(Platform.getInstallLocation().getURL().getFile());
		mingwBinDir = installPath.append("mingw\\bin"); //$NON-NLS-1$
		if (mingwBinDir.toFile().isDirectory()) {
			binDir = mingwBinDir;
			return binDir;
		}

		// Look in PATH values. Look for mingw32-gcc.exe
		// TODO: Since this dir is already in the PATH, why are we adding it here?
		// This is really only to support isToolchainAvail. Must be a better way.
		// AG: Because otherwise the toolchain won't be shown in the list of "supported" toolchains in UI
		// when MinGW installed in custom location even if it is in the PATH
		IPath gccLoc = PathUtil.findProgramLocation("mingw32-gcc.exe", envPathValue); //$NON-NLS-1$
		if (gccLoc != null) {
			binDir = gccLoc.removeLastSegments(1);
			return binDir;
		}

		// Try the default MinGW install dir
		mingwBinDir = new Path("C:\\MinGW\\bin"); //$NON-NLS-1$
		if (mingwBinDir.toFile().isDirectory()) {
			binDir = mingwBinDir;
			return binDir;
		}

		return binDir;
	}

	public static IPath getMsysBinDir() {
		// Just look in the install location parent dir
		IPath installPath = new Path(Platform.getInstallLocation().getURL().getFile());
		IPath msysBinPath = installPath.append("msys\\bin"); //$NON-NLS-1$
		if (msysBinPath.toFile().isDirectory())
			return msysBinPath;

		String mingwHome = envMingwHomeValueCached;
		if (mingwHome != null) {
			msysBinPath = new Path(mingwHome + "\\msys\\1.0\\bin"); //$NON-NLS-1$
			if (msysBinPath.toFile().isDirectory())
				return msysBinPath;
		}

		// Try the new MinGW msys bin dir
		msysBinPath = new Path("C:\\MinGW\\msys\\1.0\\bin"); //$NON-NLS-1$
		if (msysBinPath.toFile().isDirectory())
			return msysBinPath;
		return null;
	}

	public MingwEnvironmentVariableSupplier() {
		IPath binPath = getBinDir();
		if (binPath != null) {
			String pathStr = binPath.toOSString();
			IPath msysBinPath = getMsysBinDir();
			if (msysBinPath != null)
				pathStr += ';' + msysBinPath.toOSString();

			path = new MingwBuildEnvironmentVariable("PATH", pathStr, IBuildEnvironmentVariable.ENVVAR_PREPEND); //$NON-NLS-1$
		}
	}

	@Override
	public IBuildEnvironmentVariable getVariable(String variableName,
			IConfiguration configuration, IEnvironmentVariableProvider provider) {
		if (path != null && variableName.equals(path.getName()))
			return path;
		else
			return null;
	}

	@Override
	public IBuildEnvironmentVariable[] getVariables(
			IConfiguration configuration, IEnvironmentVariableProvider provider) {
		return path != null
			? new IBuildEnvironmentVariable[] { path }
			: new IBuildEnvironmentVariable[0];
	}

}

Back to the top