<commit_before>/*******************************************************************************
 * Copyright (c) 2009, 2013 Wind River Systems, Inc. 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:
 *     <NAME> - initial API and implementation
 *     <NAME> - ARM version
 *******************************************************************************/

package ilg.gnumcueclipse.managedbuild.cross.arm;

import ilg.gnumcueclipse.core.EclipseUtils;
import ilg.gnumcueclipse.managedbuild.cross.preferences.PersistentPreferences;

import java.io.File;

import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
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.macros.IBuildMacroProvider;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;

public class EnvironmentVariableSupplier implements IConfigurationEnvironmentVariableSupplier {

	// ------------------------------------------------------------------------

	private static boolean DEBUG_PATH = true;

	// ------------------------------------------------------------------------

	public IBuildEnvironmentVariable getVariable(String variableName, IConfiguration configuration,
			IEnvironmentVariableProvider provider) {
		if (PathEnvironmentVariable.isVar(variableName)) {
			return PathEnvironmentVariable.create(configuration);
		} else {
			if (Activator.getInstance().isDebugging()) {
				System.out.println("arm.EnvironmentVariableSupplier.getVariable(" + variableName + ","
						+ configuration.getName() + ") returns null");
			}
			return null;
		}
	}

	public IBuildEnvironmentVariable[] getVariables(IConfiguration configuration,
			IEnvironmentVariableProvider provider) {
		IBuildEnvironmentVariable path = PathEnvironmentVariable.create(configuration);
		if (path != null) {
			return new IBuildEnvironmentVariable[] { path };
		} else {
			if (Activator.getInstance().isDebugging()) {
				System.out.println("arm.EnvironmentVariableSupplier.getVariables(" + configuration.getName()
						+ ") returns empty array");
			}
			return new IBuildEnvironmentVariable[0];
		}
	}

	private static class PathEnvironmentVariable implements IBuildEnvironmentVariable {

		public static String name = "PATH"; //$NON-NLS-1$

		private File path;

		private PathEnvironmentVariable(File path) {
			// System.out.println("cpath=" + path);
			this.path = path;
		}

		public static PathEnvironmentVariable create(IConfiguration configuration) {
			IToolChain toolchain = configuration.getToolChain();

			IProject project = (IProject) configuration.getManagedProject().getOwner();

			PersistentPreferences commonPersistentPreferences = ilg.gnumcueclipse.managedbuild.cross.Activator
					.getInstance().getPersistentPreferences();

			PersistentPreferences deprecatedPersistentPreferences = new PersistentPreferences(
					"ilg.gnuarmeclipse.managedbuild.cross");

			// Get the build tools path from the common store.
			String path = commonPersistentPreferences.getBuildToolsPath(project);
			if (path.isEmpty()) {
				path = deprecatedPersistentPreferences.getBuildToolsPath(project);
			}

			IOption option;
			option = toolchain.getOptionBySuperClassId(Option.OPTION_TOOLCHAIN_NAME); // $NON-NLS-1$
			String toolchainName = (String) option.getValue();

			String toolchainPath = null;

			// Get the toolchain path from this plug-in store.
			PersistentPreferences persistentPreferences = Activator.getInstance().getPersistentPreferences();
			// Get the most specific toolchain path (project, workspace,
			// Eclipse, defaults).
			toolchainPath = persistentPreferences.getToolchainPath(toolchainName, project);
			if (toolchainPath.isEmpty()) {
				// Try to get from original gnuarmeclipse store.
				toolchainPath = deprecatedPersistentPreferences.getToolchainPath(toolchainName, project);
			}

			if (path.isEmpty()) {
				path = toolchainPath;
			} else {
				if (!toolchainPath.isEmpty()) {
					// Concatenate build tools path with toolchain path.
					path += EclipseUtils.getPathSeparator();
					path += toolchainPath;
				}
			}

			if (!path.isEmpty()) {

				// if present, substitute macros
				if (path.indexOf("${") >= 0) {
					path = resolveMacros(path, configuration);
				}

				File sysroot = new File(path);
				// File bin = new File(sysroot, "bin"); //$NON-NLS-1$
				// if (bin.isDirectory()) {
				// sysroot = bin;
				// }
				if (DEBUG_PATH) {
					if (Activator.getInstance().isDebugging()) {
						System.out.println("arm.PathEnvironmentVariable.create() PATH=\"" + sysroot + "\" cfg="
								+ configuration + " prj=" + configuration.getManagedProject().getOwner().getName());
					}
				}
				return new PathEnvironmentVariable(sysroot);
			}

			if (Activator.getInstance().isDebugging()) {
				System.out.println("arm.PathEnvironmentVariable.create(" + configuration.getName() + ") returns null");
			}
			return null;
		}

		private static String resolveMacros(String str, IConfiguration configuration) {

			String result = str;
			try {
				result = ManagedBuildManager.getBuildMacroProvider().resolveValue(str, "", " ", //$NON-NLS-1$ //$NON-NLS-2$
						IBuildMacroProvider.CONTEXT_CONFIGURATION, configuration);
			} catch (CdtVariableException e) {
				Activator.log("arm.PathEnvironmentVariable.resolveMacros " + e.getMessage());
			}

			Activator.log("arm.PathEnvironmentVariable.resolveMacros(\"" + str + "\", \"" + configuration.getName()
					+ "\") = \"" + "\"");
			return result;

		}

		public static boolean isVar(String name) {
			// Windows has case insensitive env var names
			return Platform.getOS().equals(Platform.OS_WIN32) ? name.equalsIgnoreCase(PathEnvironmentVariable.name)
					: name.equals(PathEnvironmentVariable.name);
		}

		public String getDelimiter() {
			return Platform.getOS().equals(Platform.OS_WIN32) ? ";" : ":"; //$NON-NLS-1$ //$NON-NLS-2$
		}

		public String getName() {
			return name;
		}

		public int getOperation() {
			return IBuildEnvironmentVariable.ENVVAR_PREPEND;<commit_msg>[#315] cross.arm: resolveMacros() debug message<commit_after>/*******************************************************************************
 * Copyright (c) 2009, 2013 Wind River Systems, Inc. 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:
 *     <NAME> - initial API and implementation
 *     <NAME> - ARM version
 *******************************************************************************/

package ilg.gnumcueclipse.managedbuild.cross.arm;

import ilg.gnumcueclipse.core.EclipseUtils;
import ilg.gnumcueclipse.managedbuild.cross.preferences.PersistentPreferences;

import java.io.File;

import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
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.macros.IBuildMacroProvider;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;

public class EnvironmentVariableSupplier implements IConfigurationEnvironmentVariableSupplier {

	// ------------------------------------------------------------------------

	private static boolean DEBUG_PATH = true;

	// ------------------------------------------------------------------------

	public IBuildEnvironmentVariable getVariable(String variableName, IConfiguration configuration,
			IEnvironmentVariableProvider provider) {
		if (PathEnvironmentVariable.isVar(variableName)) {
			return PathEnvironmentVariable.create(configuration);
		} else {
			if (Activator.getInstance().isDebugging()) {
				System.out.println("arm.EnvironmentVariableSupplier.getVariable(" + variableName + ","
						+ configuration.getName() + ") returns null");
			}
			return null;
		}
	}

	public IBuildEnvironmentVariable[] getVariables(IConfiguration configuration,
			IEnvironmentVariableProvider provider) {
		IBuildEnvironmentVariable path = PathEnvironmentVariable.create(configuration);
		if (path != null) {
			return new IBuildEnvironmentVariable[] { path };
		} else {
			if (Activator.getInstance().isDebugging()) {
				System.out.println("arm.EnvironmentVariableSupplier.getVariables(" + configuration.getName()
						+ ") returns empty array");
			}
			return new IBuildEnvironmentVariable[0];
		}
	}

	private static class PathEnvironmentVariable implements IBuildEnvironmentVariable {

		public static String name = "PATH"; //$NON-NLS-1$

		private File path;

		private PathEnvironmentVariable(File path) {
			// System.out.println("cpath=" + path);
			this.path = path;
		}

		public static PathEnvironmentVariable create(IConfiguration configuration) {
			IToolChain toolchain = configuration.getToolChain();

			IProject project = (IProject) configuration.getManagedProject().getOwner();

			PersistentPreferences commonPersistentPreferences = ilg.gnumcueclipse.managedbuild.cross.Activator
					.getInstance().getPersistentPreferences();

			PersistentPreferences deprecatedPersistentPreferences = new PersistentPreferences(
					"ilg.gnuarmeclipse.managedbuild.cross");

			// Get the build tools path from the common store.
			String path = commonPersistentPreferences.getBuildToolsPath(project);
			if (path.isEmpty()) {
				path = deprecatedPersistentPreferences.getBuildToolsPath(project);
			}

			IOption option;
			option = toolchain.getOptionBySuperClassId(Option.OPTION_TOOLCHAIN_NAME); // $NON-NLS-1$
			String toolchainName = (String) option.getValue();

			String toolchainPath = null;

			// Get the toolchain path from this plug-in store.
			PersistentPreferences persistentPreferences = Activator.getInstance().getPersistentPreferences();
			// Get the most specific toolchain path (project, workspace,
			// Eclipse, defaults).
			toolchainPath = persistentPreferences.getToolchainPath(toolchainName, project);
			if (toolchainPath.isEmpty()) {
				// Try to get from original gnuarmeclipse store.
				toolchainPath = deprecatedPersistentPreferences.getToolchainPath(toolchainName, project);
			}

			if (path.isEmpty()) {
				path = toolchainPath;
			} else {
				if (!toolchainPath.isEmpty()) {
					// Concatenate build tools path with toolchain path.
					path += EclipseUtils.getPathSeparator();
					path += toolchainPath;
				}
			}

			if (!path.isEmpty()) {

				// if present, substitute macros
				if (path.indexOf("${") >= 0) {
					path = resolveMacros(path, configuration);
				}

				File sysroot = new File(path);
				// File bin = new File(sysroot, "bin"); //$NON-NLS-1$
				// if (bin.isDirectory()) {
				// sysroot = bin;
				// }
				if (DEBUG_PATH) {
					if (Activator.getInstance().isDebugging()) {
						System.out.println("arm.PathEnvironmentVariable.create() PATH=\"" + sysroot + "\" cfg="
								+ configuration + " prj=" + configuration.getManagedProject().getOwner().getName());
					}
				}
				return new PathEnvironmentVariable(sysroot);
			}

			if (Activator.getInstance().isDebugging()) {
				System.out.println("arm.PathEnvironmentVariable.create(" + configuration.getName() + ") returns null");
			}
			return null;
		}

		private static String resolveMacros(String str, IConfiguration configuration) {

			String result = str;
			try {
				result = ManagedBuildManager.getBuildMacroProvider().resolveValue(str, "", " ", //$NON-NLS-1$ //$NON-NLS-2$
						IBuildMacroProvider.CONTEXT_CONFIGURATION, configuration);
			} catch (CdtVariableException e) {
				Activator.log("arm.PathEnvironmentVariable.resolveMacros " + e.getMessage());
			}

			if (Activator.getInstance().isDebugging()) {
				Activator.log("arm.PathEnvironmentVariable.resolveMacros(\"" + str + "\", \"" + configuration.getName()
					+ "\") = \"" + "\"");
			}
			return result;

		}

		public static boolean isVar(String name) {
			// Windows has case insensitive env var names
			return Platform.getOS().equals(Platform.OS_WIN32) ? name.equalsIgnoreCase(PathEnvironmentVariable.name)
					: name.equals(PathEnvironmentVariable.name);
		}

		public String getDelimiter() {
			return Platform.getOS().equals(Platform.OS_WIN32) ? ";" : ":"; //$NON-NLS-1$ //$NON-NLS-2$
		}

		public String getName() {
			return name;
		}

		public int getOperation() {
			return IBuildEnvironmentVariable.ENVVAR_PREPEND;
{'fraction_non_alphanumeric': 0.15493062807698046, 'fraction_numerical': 0.003953453677457855, 'mean_word_length': 3.9637171417993335, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822623', 'n_tokens_mistral': 4190, 'n_tokens_neox': 3554, 'n_words': 749}