From 09fed04601b2e6024d8eba168f8f5fb0d87dd111 Mon Sep 17 00:00:00 2001 From: Andrew Gvozdev Date: Mon, 9 Jul 2012 15:57:28 -0400 Subject: [PATCH] bug 384520: New Project Wizard ignores $PATH from preferences, won't show toolchain (issues with too aggressive caching) --- .../MingwEnvironmentVariableSupplier.java | 125 ++++++++++-------- 1 file changed, 73 insertions(+), 52 deletions(-) diff --git a/build/org.eclipse.cdt.managedbuilder.gnu.ui/src/org/eclipse/cdt/managedbuilder/gnu/mingw/MingwEnvironmentVariableSupplier.java b/build/org.eclipse.cdt.managedbuilder.gnu.ui/src/org/eclipse/cdt/managedbuilder/gnu/mingw/MingwEnvironmentVariableSupplier.java index 859cce141e7..79fb3eadc96 100644 --- a/build/org.eclipse.cdt.managedbuilder.gnu.ui/src/org/eclipse/cdt/managedbuilder/gnu/mingw/MingwEnvironmentVariableSupplier.java +++ b/build/org.eclipse.cdt.managedbuilder.gnu.ui/src/org/eclipse/cdt/managedbuilder/gnu/mingw/MingwEnvironmentVariableSupplier.java @@ -29,11 +29,15 @@ import org.eclipse.core.runtime.Platform; * @noextend This class is not intended to be subclassed by clients. */ public class MingwEnvironmentVariableSupplier implements IConfigurationEnvironmentVariableSupplier { + private static final String ENV_PATH = "PATH"; //$NON-NLS-1$ + private static String envPathValueCached = null; private static String envMingwHomeValueCached = null; private static IPath binDir = null; + private static IPath msysBinDir = null; private static class MingwBuildEnvironmentVariable implements IBuildEnvironmentVariable { + private static final String PATH_SEPARATOR = ";"; //$NON-NLS-1$ private final String name; private final String value; private final int operation; @@ -61,12 +65,10 @@ public class MingwEnvironmentVariableSupplier implements IConfigurationEnvironme @Override public String getDelimiter() { - return ";"; //$NON-NLS-1$ + return PATH_SEPARATOR; } } - private IBuildEnvironmentVariable path; - /** * @return location of $MINGW_HOME/bin folder on the file-system. * @@ -74,24 +76,47 @@ public class MingwEnvironmentVariableSupplier implements IConfigurationEnvironme * Please rely on internal caching. */ public static IPath getBinDir() { - IEnvironmentVariable varPath = CCorePlugin.getDefault().getBuildEnvironmentManager().getVariable("PATH", null, true); //$NON-NLS-1$ + locateMingw(); + return binDir; + } + + /** + * @return location of $MINGW_HOME/msys/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 getMsysBinDir() { + locateMingw(); + return msysBinDir; + } + + /** + * Locate MinGW directories. The results are judicially cached so it is reasonably cheap to call. + * The reason to call it each time is to check if user changed environment in preferences. + */ + private static void locateMingw() { + IEnvironmentVariable varPath = CCorePlugin.getDefault().getBuildEnvironmentManager().getVariable(ENV_PATH, null, true); 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; + return; } envPathValueCached = envPathValue; envMingwHomeValueCached = envMingwHomeValue; - binDir = null; + binDir = locateBinDir(); + msysBinDir = locateMsysBinDir(binDir); + } + + private static IPath locateBinDir() { // Check $MINGW_HOME - IPath mingwBinDir = new Path(envMingwHomeValue + "\\bin"); //$NON-NLS-1$ + IPath mingwBinDir = new Path(envMingwHomeValueCached + "\\bin"); //$NON-NLS-1$ if (mingwBinDir.toFile().isDirectory()) { - binDir = mingwBinDir; - return binDir; + return mingwBinDir; } // Try the mingw directory in the platform install directory @@ -99,8 +124,7 @@ public class MingwEnvironmentVariableSupplier implements IConfigurationEnvironme IPath installPath = new Path(Platform.getInstallLocation().getURL().getFile()); mingwBinDir = installPath.append("mingw\\bin"); //$NON-NLS-1$ if (mingwBinDir.toFile().isDirectory()) { - binDir = mingwBinDir; - return binDir; + return mingwBinDir; } // Look in PATH values. Look for mingw32-gcc.exe @@ -108,67 +132,64 @@ public class MingwEnvironmentVariableSupplier implements IConfigurationEnvironme // 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$ + IPath gccLoc = PathUtil.findProgramLocation("mingw32-gcc.exe", envPathValueCached); //$NON-NLS-1$ if (gccLoc != null) { - binDir = gccLoc.removeLastSegments(1); - return binDir; + return gccLoc.removeLastSegments(1); } // Try the default MinGW install dir mingwBinDir = new Path("C:\\MinGW\\bin"); //$NON-NLS-1$ if (mingwBinDir.toFile().isDirectory()) { - binDir = mingwBinDir; - return binDir; + return mingwBinDir; } - 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(); + private static IPath locateMsysBinDir(IPath binPath) { if (binPath != null) { - String pathStr = binPath.toOSString(); - IPath msysBinPath = getMsysBinDir(); - if (msysBinPath != null) - pathStr += ';' + msysBinPath.toOSString(); + // 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; + } - path = new MingwBuildEnvironmentVariable("PATH", pathStr, IBuildEnvironmentVariable.ENVVAR_PREPEND); //$NON-NLS-1$ + if (envMingwHomeValueCached != null) { + msysBinPath = new Path(envMingwHomeValueCached + "\\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; } @Override - public IBuildEnvironmentVariable getVariable(String variableName, - IConfiguration configuration, IEnvironmentVariableProvider provider) { - if (path != null && variableName.equals(path.getName())) - return path; - else - return null; + public IBuildEnvironmentVariable getVariable(String variableName, IConfiguration configuration, IEnvironmentVariableProvider provider) { + if (variableName.equals(ENV_PATH)) { + locateMingw(); + if (binDir != null) { + String pathStr = binDir.toOSString(); + if (msysBinDir != null) { + pathStr += MingwBuildEnvironmentVariable.PATH_SEPARATOR + msysBinDir.toOSString(); + } + return new MingwBuildEnvironmentVariable(ENV_PATH, pathStr, IBuildEnvironmentVariable.ENVVAR_PREPEND); + } + } + + return null; } @Override - public IBuildEnvironmentVariable[] getVariables( - IConfiguration configuration, IEnvironmentVariableProvider provider) { + public IBuildEnvironmentVariable[] getVariables(IConfiguration configuration, IEnvironmentVariableProvider provider) { + IBuildEnvironmentVariable path = getVariable(ENV_PATH, configuration, provider); return path != null ? new IBuildEnvironmentVariable[] { path } : new IBuildEnvironmentVariable[0];