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
* are made available under the terms of the Eclipse Public License v1.0
* 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.ICommandLauncher;
import org.eclipse.cdt.internal.core.Cygwin;
import org.eclipse.cdt.managedbuilder.buildmodel.IBuildCommand;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
import org.eclipse.cdt.utils.PathUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
/**
@ -48,44 +45,6 @@ public class CommandBuilder implements IBuildModelBuilder {
private Process fProcess;
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 {
private OutputStream fOut;
@ -205,9 +164,7 @@ public class CommandBuilder implements IBuildModelBuilder {
}
protected ICommandLauncher createLauncher() {
// if(isWindows())
// return new CommandLauncher();
return new CommandSearchLauncher();
return new CommandLauncher();
}
public String getErrMsg() {

View file

@ -15,27 +15,31 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import org.eclipse.cdt.internal.core.Cygwin;
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.ProcessFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
/**
* @noextend This class is not intended to be subclassed by clients.
*/
public class CommandLauncher implements ICommandLauncher {
public final static int COMMAND_CANCELED = ICommandLauncher.COMMAND_CANCELED;
public final static int ILLEGAL_COMMAND = ICommandLauncher.ILLEGAL_COMMAND;
public final static int OK = ICommandLauncher.OK;
private static final String PATH_ENV = "PATH"; //$NON-NLS-1$
protected Process fProcess;
protected boolean fShowCommand;
protected String[] fCommandArgs;
private Properties fEnvironment = null;
protected String fErrorMessage = ""; //$NON-NLS-1$
@ -95,8 +99,12 @@ public class CommandLauncher implements ICommandLauncher {
*/
@Override
public Properties getEnvironment() {
if (fEnvironment == null) {
// for backward compatibility, note that this return may be not accurate
return EnvironmentReader.getEnvVars();
}
return fEnvironment;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.ICommandLauncher#getCommandLine()
@ -116,6 +124,23 @@ public class CommandLauncher implements ICommandLauncher {
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
* @since 5.1
@ -123,23 +148,14 @@ public class CommandLauncher implements ICommandLauncher {
@Deprecated
public Process execute(IPath commandPath, String[] args, String[] env, IPath changeToDirectory) {
try {
// add platform specific arguments (shell invocation)
fCommandArgs = constructCommandArray(commandPath.toOSString(), args);
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 execute(commandPath, args, env, changeToDirectory, null);
} catch (CoreException e) {
CCorePlugin.log(e);
}
return fProcess;
return null;
}
/**
* @since 5.1
* @see org.eclipse.cdt.core.ICommandLauncher#execute(IPath, String[], String[], IPath, IProgressMonitor)
@ -147,15 +163,30 @@ public class CommandLauncher implements ICommandLauncher {
@Override
public Process execute(IPath commandPath, String[] args, String[] env, IPath changeToDirectory, IProgressMonitor monitor) throws CoreException {
try {
// add platform specific arguments (shell invocation)
fCommandArgs = constructCommandArray(commandPath.toOSString(), args);
String command = commandPath.toOSString();
fCommandArgs = constructCommandArray(command, args);
fEnvironment = parseEnv(env);
File file = null;
if(changeToDirectory != null)
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);
fCommandArgs[0] = command; // to print original command on the console
fErrorMessage = ""; //$NON-NLS-1$
} catch (IOException e) {
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)
*/
@Override
@Deprecated
public int waitAndRead(OutputStream out, OutputStream err) {
if (fShowCommand) {
printCommandLine(out);

View file

@ -99,7 +99,9 @@ public interface ICommandLauncher {
/**
* 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);
/**