diff options
2 files changed, 54 insertions, 239 deletions
diff --git a/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java index 84e8c62..718e986 100644 --- a/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java +++ b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java @@ -13,12 +13,13 @@ package org.yocto.cmake.managedbuilder.job; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; +import java.util.List; import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.CommandLauncherManager; +import org.eclipse.cdt.core.ICommandLauncher; import org.eclipse.cdt.core.envvar.IContributedEnvironment; import org.eclipse.cdt.core.envvar.IEnvironmentVariable; import org.eclipse.cdt.core.envvar.IEnvironmentVariableManager; @@ -29,23 +30,21 @@ import org.eclipse.cdt.managedbuilder.core.BuildException; import org.eclipse.cdt.managedbuilder.core.IConfiguration; import org.eclipse.cdt.managedbuilder.core.ITool; 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.IStatus; +import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.swt.widgets.Display; import org.eclipse.ui.console.IOConsoleOutputStream; import org.yocto.cmake.managedbuilder.Activator; import org.yocto.cmake.managedbuilder.YoctoCMakeMessages; import org.yocto.cmake.managedbuilder.util.ConsoleUtility; -import org.yocto.cmake.managedbuilder.util.SystemProcess; public class ExecuteConfigureJob extends Job { - private SystemProcess configureProcess; - private LinkedList<String> configureCommand; + private Process configureProcess; private IProject project; private IConfiguration configuration; private IPath location; @@ -57,62 +56,10 @@ public class ExecuteConfigureJob extends Job { this.project = project; this.configuration = configuration; this.location = location; - createCommands(); - createProcesses(); - } - - protected void createCommands() { - configureCommand = new LinkedList<String>(); - - ITool[] configure = configuration - .getToolsBySuperClassId("org.yocto.cmake.managedbuilder.cmakeconfigure.gnu.exe"); //$NON-NLS-1$ - addCommand(configure[0]); - try { - addFlags(configure[0]); - } catch (BuildException e) { - // ignore this exception - } } - private void addCommand(ITool configure) { - String command = configuration.getToolCommand(configure); - configureCommand.add(command); - } - - private void addFlags(ITool configure) throws BuildException { - String[] flags = configure.getToolCommandFlags( - project.getLocation(), location); - for (String flag : flags) { - if (flag.contains(" ")) { //$NON-NLS-1$ - String[] separatedFlags = flag.trim().split(" "); //$NON-NLS-1$ - configureCommand.addAll(Arrays.asList(separatedFlags)); - } else { - configureCommand.add(flag); - } - } - } - - protected void createProcesses() { - configureProcess = - new SystemProcess(configureCommand, location.toFile(), getEnvVariablesAsMap(project)); - } - - private Map<String,String> getEnvVariablesAsMap (IProject project) { - Map<String, String> result = new HashMap<String, String>(); - - ICProjectDescription cpdesc = CoreModel.getDefault().getProjectDescription(project, true); - ICConfigurationDescription ccdesc = cpdesc.getActiveConfiguration(); - IEnvironmentVariableManager manager = CCorePlugin.getDefault().getBuildEnvironmentManager(); - IContributedEnvironment env = manager.getContributedEnvironment(); - - for(IEnvironmentVariable var : env.getVariables(ccdesc)) { - result.put(var.getName(), var.getValue()); - } - - return result; - } /* (non-Javadoc) * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) @@ -130,26 +77,18 @@ public class ExecuteConfigureJob extends Job { try { return buildProject(monitor, cos, ces); - } catch (IOException e) { - if(e.getMessage().startsWith("Cannot run program \"cmake\"")) { //$NON-NLS-1$ - Display.getDefault().asyncExec(new Runnable() { - @Override - public void run() { - MessageDialog.openWarning(null, - YoctoCMakeMessages.getString("ExecuteConfigureJob.cmakeWarning.dialogTitle"), //$NON-NLS-1$ - YoctoCMakeMessages.getString("ExecuteConfigureJob.cmakeWarning.dialogMessage")); //$NON-NLS-1$ - } - }); - return Status.OK_STATUS; - } else { - return new Status(Status.ERROR, - Activator.PLUGIN_ID, Status.OK, - YoctoCMakeMessages.getString("ExecuteConfigureJob.error.couldNotStart"), e); //$NON-NLS-1$ - } } catch (InterruptedException e) { return new Status(Status.WARNING, Activator.PLUGIN_ID, YoctoCMakeMessages.getString("ExecuteConfigureJob.warning.aborted")); //$NON-NLS-1$ + } catch (CoreException e) { + return new Status(Status.ERROR, + Activator.PLUGIN_ID, Status.OK, + YoctoCMakeMessages.getString("ExecuteConfigureJob.error.couldNotStart"), e); //$NON-NLS-1$ + } catch (BuildException e) { + return new Status(Status.ERROR, + Activator.PLUGIN_ID, Status.OK, + YoctoCMakeMessages.getString("ExecuteConfigureJob.error.couldNotStart"), e); //$NON-NLS-1$ } finally { try { cos.close(); @@ -160,11 +99,43 @@ public class ExecuteConfigureJob extends Job { } private IStatus buildProject(IProgressMonitor monitor, - OutputStream stdout, OutputStream stderr) throws IOException, InterruptedException { - monitor.subTask( - YoctoCMakeMessages.getString("ExecuteConfigureJob.buildingMakefile")); //$NON-NLS-1$ - configureProcess.start(stdout, stderr); - int exitValue = configureProcess.waitForResultAndStop(); + OutputStream stdout, OutputStream stderr) throws InterruptedException, CoreException, BuildException { + + ITool[] configureTools = configuration.getToolsBySuperClassId("org.yocto.cmake.managedbuilder.cmakeconfigure.gnu.exe"); //$NON-NLS-1$ + ITool configureTool = configureTools[0]; + String configureCommand = configuration.getToolCommand(configureTool); + + String[] toolFlags = configureTool.getToolCommandFlags(project.getLocation(), location); + List<String> configureCommandFlags = new ArrayList<String>(); + + for (String toolFlag : toolFlags) { + if (toolFlag.contains(" ")) { //$NON-NLS-1$ + String[] separatedFlags = toolFlag.trim().split(" "); //$NON-NLS-1$ + configureCommandFlags.addAll(Arrays.asList(separatedFlags)); + } else { + configureCommandFlags.add(toolFlag); + } + } + + List<String> result = new ArrayList<String>(); + + ICProjectDescription cpdesc = CoreModel.getDefault().getProjectDescription(project, true); + ICConfigurationDescription ccdesc = cpdesc.getActiveConfiguration(); + IEnvironmentVariableManager manager = CCorePlugin.getDefault().getBuildEnvironmentManager(); + IContributedEnvironment env = manager.getContributedEnvironment(); + + for(IEnvironmentVariable var : env.getVariables(ccdesc)) { + result.add(var.getName() + "=" + var.getValue()); + } + + + ICommandLauncher launcher = CommandLauncherManager.getInstance().getCommandLauncher(project); + launcher.setProject(project); + + monitor.subTask(YoctoCMakeMessages.getString("ExecuteConfigureJob.buildingMakefile")); //$NON-NLS-1$ + configureProcess = launcher.execute(new Path(configureCommand), configureCommandFlags.toArray(new String[]{}), result.toArray(new String[]{}), location, monitor); + int exitValue = launcher.waitAndRead(stdout, stderr, monitor); + monitor.worked(15); if (exitValue != 0) { @@ -185,6 +156,7 @@ public class ExecuteConfigureJob extends Job { */ @Override protected void canceling() { - configureProcess.interrupt(); + if (configureProcess != null) + configureProcess.destroy(); } } diff --git a/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java deleted file mode 100644 index 65c7847..0000000 --- a/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java +++ /dev/null @@ -1,157 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 BMW Car IT GmbH. - * 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: - * BMW Car IT - initial API and implementation - *******************************************************************************/ -package org.yocto.cmake.managedbuilder.util; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.LinkedList; -import java.util.Map; - -import org.eclipse.cdt.utils.PathUtil; -import org.eclipse.core.runtime.IPath; - -public class SystemProcess { - - private LinkedList<String> command = new LinkedList<String>(); - private File workingDirectory; - private ProcessBuilder builder; - private Process process; - private StreamPipe outputRedirector; - private StreamPipe errorRedirector; - - public SystemProcess(LinkedList<String> command) { - this(command, null); - } - - public SystemProcess(LinkedList<String> command, File directory) { - this(command, directory, null); - } - - public SystemProcess(LinkedList<String> command, File directory, Map<String, String> additionalEnvironmentVariables) { - super(); - this.command = command; - - // ProcessBuilder does not use the new PATH environment value for resolving - // the executable's path, so here we need to manually resolve the absolute - // path to executable. - if (additionalEnvironmentVariables.keySet().contains("PATH")) { - String executable = this.command.get(0); - IPath programLocation = PathUtil.findProgramLocation(executable, additionalEnvironmentVariables.get("PATH")); - if (programLocation != null) { - this.command.set(0, programLocation.toOSString()); - } - } - - if (directory != null) { - this.workingDirectory = directory; - } - setUpProcessBuilder(additionalEnvironmentVariables); - } - - private void setUpProcessBuilder(Map<String, String> additionalEnvironmentVariables) { - builder = new ProcessBuilder(command); - if (workingDirectory != null) { - builder.directory(workingDirectory); - } - - if(additionalEnvironmentVariables != null && !additionalEnvironmentVariables.isEmpty()) { - builder.environment().putAll(additionalEnvironmentVariables); - } - } - - public void start(OutputStream out) throws IOException { - if (builder != null) { - builder.redirectErrorStream(true); - process = builder.start(); - outputRedirector = redirectOutput(process, out); - } - } - - public void start(OutputStream out, OutputStream err) throws IOException { - if (builder != null) { - builder.redirectErrorStream(false); - process = builder.start(); - outputRedirector = redirectOutput(process, out); - errorRedirector = redirectError(process, err, out); - } - } - - public int waitForResultAndStop() throws InterruptedException { - if (process == null) { - return -1; - } - - process.waitFor(); - outputRedirector.interrupt(); - - if (!builder.redirectErrorStream()) { - errorRedirector.interrupt(); - } - - return process.exitValue(); - } - - public void interrupt() { - process.destroy(); - } - - private StreamPipe redirectError(Process process, OutputStream errOut, OutputStream out) { - InputStream err = process.getErrorStream(); - StreamPipe stderrPipe = new StreamPipe(err, errOut, out); - stderrPipe.start(); - - return stderrPipe; - } - - private StreamPipe redirectOutput(Process process, OutputStream out) { - InputStream in = process.getInputStream(); - StreamPipe stdoutPipe = new StreamPipe(in, out); - stdoutPipe.start(); - - return stdoutPipe; - } - - - private class StreamPipe extends Thread { - private InputStream in; - private OutputStream[] outputs; - boolean shutdown = false; - - public StreamPipe(InputStream in, OutputStream... outputs) { - this.in = in; - this.outputs = outputs; - } - - @Override - public void run() { - byte[] buffer = new byte[1024]; - int length = 0; - - try { - while(!shutdown && ((length = in.read(buffer)) > -1)) { - for (OutputStream out : outputs) { - out.write(buffer, 0, length); - } - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public void interrupt() { - shutdown = true; - super.interrupt(); - } - } -} |