1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

bug 145099: MBS Internal Builder: Switch to using the Spawner for

detecting an executable to be launched
This commit is contained in:
Andrew Gvozdev 2012-03-15 17:49:38 -04:00
parent 4c83576d7e
commit 12d44ec8be
3 changed files with 67 additions and 76 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2009 Intel Corporation and others. * Copyright (c) 2006, 2012 Intel Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -20,14 +20,11 @@ import java.util.Set;
import org.eclipse.cdt.core.CommandLauncher; import org.eclipse.cdt.core.CommandLauncher;
import org.eclipse.cdt.core.ICommandLauncher; import org.eclipse.cdt.core.ICommandLauncher;
import org.eclipse.cdt.internal.core.Cygwin;
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildCommand; import org.eclipse.cdt.managedbuilder.buildmodel.IBuildCommand;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages; import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
import org.eclipse.cdt.utils.PathUtil; import org.eclipse.cdt.utils.PathUtil;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.core.runtime.SubProgressMonitor;
/** /**
@ -48,44 +45,6 @@ public class CommandBuilder implements IBuildModelBuilder {
private Process fProcess; private Process fProcess;
private String fErrMsg; private String fErrMsg;
private class CommandSearchLauncher extends CommandLauncher {
@Override
protected String[] constructCommandArray(String command, String[] commandArgs) {
String[] args = new String[1 + commandArgs.length];
if (Platform.getOS().equals(Platform.OS_WIN32)) {
// find a location of the executable
String envPathValue = fCmd.getEnvironment().get(PATH_ENV);
IPath location = PathUtil.findProgramLocation(command, envPathValue);
if(location != null) {
try {
// Handle cygwin link
command = Cygwin.cygwinToWindowsPath(location.toString(), envPathValue);
} catch (Exception e) {
command = location.toString();
}
}
//if not found, continue with the command passed as an argument
}
args[0] = command;
System.arraycopy(commandArgs, 0, args, 1, commandArgs.length);
return args;
}
@Override
protected void printCommandLine(OutputStream os) {
if (os != null) {
String cmd = CommandBuilder.this.getCommandLine();
try {
os.write(cmd.getBytes());
os.flush();
} catch (IOException e) {
// ignore;
}
}
}
}
protected class OutputStreamWrapper extends OutputStream { protected class OutputStreamWrapper extends OutputStream {
private OutputStream fOut; private OutputStream fOut;
@ -205,12 +164,10 @@ public class CommandBuilder implements IBuildModelBuilder {
} }
protected ICommandLauncher createLauncher() { protected ICommandLauncher createLauncher() {
// if(isWindows()) return new CommandLauncher();
// return new CommandLauncher();
return new CommandSearchLauncher();
} }
public String getErrMsg(){ public String getErrMsg() {
return fErrMsg; return fErrMsg;
} }

View file

@ -15,27 +15,31 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Properties; import java.util.Properties;
import org.eclipse.cdt.internal.core.Cygwin;
import org.eclipse.cdt.internal.core.ProcessClosure; import org.eclipse.cdt.internal.core.ProcessClosure;
import org.eclipse.cdt.utils.PathUtil;
import org.eclipse.cdt.utils.spawner.EnvironmentReader; import org.eclipse.cdt.utils.spawner.EnvironmentReader;
import org.eclipse.cdt.utils.spawner.ProcessFactory; import org.eclipse.cdt.utils.spawner.ProcessFactory;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
/** /**
* @noextend This class is not intended to be subclassed by clients. * @noextend This class is not intended to be subclassed by clients.
*/ */
public class CommandLauncher implements ICommandLauncher { public class CommandLauncher implements ICommandLauncher {
public final static int COMMAND_CANCELED = ICommandLauncher.COMMAND_CANCELED; public final static int COMMAND_CANCELED = ICommandLauncher.COMMAND_CANCELED;
public final static int ILLEGAL_COMMAND = ICommandLauncher.ILLEGAL_COMMAND; public final static int ILLEGAL_COMMAND = ICommandLauncher.ILLEGAL_COMMAND;
public final static int OK = ICommandLauncher.OK; public final static int OK = ICommandLauncher.OK;
private static final String PATH_ENV = "PATH"; //$NON-NLS-1$
protected Process fProcess; protected Process fProcess;
protected boolean fShowCommand; protected boolean fShowCommand;
protected String[] fCommandArgs; protected String[] fCommandArgs;
private Properties fEnvironment = null;
protected String fErrorMessage = ""; //$NON-NLS-1$ protected String fErrorMessage = ""; //$NON-NLS-1$
@ -95,7 +99,11 @@ public class CommandLauncher implements ICommandLauncher {
*/ */
@Override @Override
public Properties getEnvironment() { public Properties getEnvironment() {
return EnvironmentReader.getEnvVars(); if (fEnvironment == null) {
// for backward compatibility, note that this return may be not accurate
return EnvironmentReader.getEnvVars();
}
return fEnvironment;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -116,6 +124,23 @@ public class CommandLauncher implements ICommandLauncher {
return args; return args;
} }
/**
* Parse array of "ENV=value" pairs to Properties.
*/
private Properties parseEnv(String[] env) {
Properties envProperties = new Properties();
for (String envStr : env) {
// Split "ENV=value" and put in Properties
int pos = envStr.indexOf('=');
if (pos < 0)
pos = envStr.length();
String key = envStr.substring(0, pos);
String value = envStr.substring(pos + 1);
envProperties.put(key, value);
}
return envProperties;
}
/** /**
* @deprecated * @deprecated
* @since 5.1 * @since 5.1
@ -123,23 +148,14 @@ public class CommandLauncher implements ICommandLauncher {
@Deprecated @Deprecated
public Process execute(IPath commandPath, String[] args, String[] env, IPath changeToDirectory) { public Process execute(IPath commandPath, String[] args, String[] env, IPath changeToDirectory) {
try { try {
// add platform specific arguments (shell invocation) return execute(commandPath, args, env, changeToDirectory, null);
fCommandArgs = constructCommandArray(commandPath.toOSString(), args); } catch (CoreException e) {
CCorePlugin.log(e);
File file = null;
if(changeToDirectory != null)
file = changeToDirectory.toFile();
fProcess = ProcessFactory.getFactory().exec(fCommandArgs, env, file);
fErrorMessage = ""; //$NON-NLS-1$
} catch (IOException e) {
setErrorMessage(e.getMessage());
fProcess = null;
} }
return fProcess; return null;
} }
/** /**
* @since 5.1 * @since 5.1
* @see org.eclipse.cdt.core.ICommandLauncher#execute(IPath, String[], String[], IPath, IProgressMonitor) * @see org.eclipse.cdt.core.ICommandLauncher#execute(IPath, String[], String[], IPath, IProgressMonitor)
@ -147,15 +163,30 @@ public class CommandLauncher implements ICommandLauncher {
@Override @Override
public Process execute(IPath commandPath, String[] args, String[] env, IPath changeToDirectory, IProgressMonitor monitor) throws CoreException { public Process execute(IPath commandPath, String[] args, String[] env, IPath changeToDirectory, IProgressMonitor monitor) throws CoreException {
try { try {
// add platform specific arguments (shell invocation) String command = commandPath.toOSString();
fCommandArgs = constructCommandArray(commandPath.toOSString(), args); fCommandArgs = constructCommandArray(command, args);
fEnvironment = parseEnv(env);
File file = null; File file = null;
if(changeToDirectory != null) if(changeToDirectory != null)
file = changeToDirectory.toFile(); file = changeToDirectory.toFile();
if (Platform.getOS().equals(Platform.OS_WIN32)) {
// Handle cygwin link
String envPathValue = (String) getEnvironment().get(PATH_ENV);
IPath location = PathUtil.findProgramLocation(command, envPathValue);
if(location != null) {
try {
fCommandArgs[0] = Cygwin.cygwinToWindowsPath(location.toString(), envPathValue);
} catch (Exception e) {
// if no cygwin nothing to worry about
}
}
}
fProcess = ProcessFactory.getFactory().exec(fCommandArgs, env, file); fProcess = ProcessFactory.getFactory().exec(fCommandArgs, env, file);
fCommandArgs[0] = command; // to print original command on the console
fErrorMessage = ""; //$NON-NLS-1$ fErrorMessage = ""; //$NON-NLS-1$
} catch (IOException e) { } catch (IOException e) {
setErrorMessage(e.getMessage()); setErrorMessage(e.getMessage());
@ -168,6 +199,7 @@ public class CommandLauncher implements ICommandLauncher {
* @see org.eclipse.cdt.core.ICommandLauncher#waitAndRead(java.io.OutputStream, java.io.OutputStream) * @see org.eclipse.cdt.core.ICommandLauncher#waitAndRead(java.io.OutputStream, java.io.OutputStream)
*/ */
@Override @Override
@Deprecated
public int waitAndRead(OutputStream out, OutputStream err) { public int waitAndRead(OutputStream out, OutputStream err) {
if (fShowCommand) { if (fShowCommand) {
printCommandLine(out); printCommandLine(out);

View file

@ -4,7 +4,7 @@
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
*******************************************************************************/ *******************************************************************************/
@ -20,7 +20,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
/** /**
* An interface for launchers of external commands. * An interface for launchers of external commands.
* *
* @since 5.1 * @since 5.1
*/ */
public interface ICommandLauncher { public interface ICommandLauncher {
@ -28,19 +28,19 @@ public interface ICommandLauncher {
public final static int COMMAND_CANCELED = 1; public final static int COMMAND_CANCELED = 1;
public final static int ILLEGAL_COMMAND = -1; public final static int ILLEGAL_COMMAND = -1;
public final static int OK = 0; public final static int OK = 0;
/** /**
* Sets the project that this launcher is associated with, or <code>null</code> if there is no such * Sets the project that this launcher is associated with, or <code>null</code> if there is no such
* project. * project.
* *
* @param project * @param project
*/ */
public void setProject(IProject project); public void setProject(IProject project);
/** /**
* Gets the project this launcher is associated with. * Gets the project this launcher is associated with.
* *
* @return IProject, or <code>null</code> if there is no such project. * @return IProject, or <code>null</code> if there is no such project.
*/ */
public IProject getProject(); public IProject getProject();
@ -53,7 +53,7 @@ public interface ICommandLauncher {
/** /**
* Returns a human readable error message corresponding to the last error encountered during command * Returns a human readable error message corresponding to the last error encountered during command
* execution. * execution.
* *
* @return A String corresponding to the error, or <code>null</code> if there has been no error. * @return A String corresponding to the error, or <code>null</code> if there has been no error.
*/ */
public String getErrorMessage(); public String getErrorMessage();
@ -61,7 +61,7 @@ public interface ICommandLauncher {
/** /**
* Sets the human readable error message corresponding to the last error encountered during command * Sets the human readable error message corresponding to the last error encountered during command
* execution. A subsequent call to getErrorMessage() will return this string. * execution. A subsequent call to getErrorMessage() will return this string.
* *
* @param error A String corresponding to the error message, or <code>null</code> if the error state is * @param error A String corresponding to the error message, or <code>null</code> if the error state is
* intended to be cleared. * intended to be cleared.
*/ */
@ -69,7 +69,7 @@ public interface ICommandLauncher {
/** /**
* Returns an array of the command line arguments that were last used to execute a command. * Returns an array of the command line arguments that were last used to execute a command.
* *
* @return an array of type String[] corresponding to the arguments. The array can be empty, but should not * @return an array of type String[] corresponding to the arguments. The array can be empty, but should not
* be null. * be null.
*/ */
@ -78,14 +78,14 @@ public interface ICommandLauncher {
/** /**
* Returns the set of environment variables in the context of which * Returns the set of environment variables in the context of which
* this launcher will execute commands. * this launcher will execute commands.
* *
* @return Properties * @return Properties
*/ */
public Properties getEnvironment(); public Properties getEnvironment();
/** /**
* Returns the constructed command line of the last command executed. * Returns the constructed command line of the last command executed.
* *
* @return String * @return String
*/ */
public String getCommandLine(); public String getCommandLine();
@ -99,7 +99,9 @@ public interface ICommandLauncher {
/** /**
* Reads output form the process to the streams. * Reads output form the process to the streams.
* @deprecated Deprecated as of CDT 8.1. Use method taking IProgressMonitor instead.
*/ */
@Deprecated
public int waitAndRead(OutputStream out, OutputStream err); public int waitAndRead(OutputStream out, OutputStream err);
/** /**