From: Atanas Gegov <atanas.ge...@bmw-carit.de>

This job will execute the configure step for the
CMake projects. It uses the command defined by the
CMake tool ("cmake") and gets the provided options
from the user to assemble the command line.
---
 .../managedbuilder/YoctoCMakeMessages.properties   |   18 +++
 .../managedbuilder/job/ExecuteConfigureJob.java    |  124 ++++++++++++++++++++
 .../cmake/managedbuilder/util/SystemProcess.java   |  118 +++++++++++++++++++
 3 files changed, 260 insertions(+)
 create mode 100644 
plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/YoctoCMakeMessages.properties
 create mode 100644 
plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java
 create mode 100644 
plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java

diff --git 
a/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/YoctoCMakeMessages.properties
 
b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/YoctoCMakeMessages.properties
new file mode 100644
index 0000000..ac5dad8
--- /dev/null
+++ 
b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/YoctoCMakeMessages.properties
@@ -0,0 +1,18 @@
+#/*******************************************************************************
+# * 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 implementation
+# 
*******************************************************************************/
+
+# Job
+ExecuteConfigureJob.runConfigure=Running configure
+ExecuteConfigureJob.consoleName=Configure using CMake [{0}]
+ExecuteConfigureJob.buildingMakefile=Building Makefile
+ExecuteConfigureJob.warning.aborted=Build of project has been aborted
+ExecuteConfigureJob.error.couldNotStart=Build of project could not be started
+ExecuteConfigureJob.error.buildFailed=Build of project failed
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
new file mode 100644
index 0000000..ebed4bb
--- /dev/null
+++ 
b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java
@@ -0,0 +1,124 @@
+/*******************************************************************************
+ * 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.job;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.LinkedList;
+
+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.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+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;
+import org.yocto.sdk.ide.utils.YoctoSDKUtils;
+
+
+public class ExecuteConfigureJob extends Job {
+       private SystemProcess configureProcess;
+       private LinkedList<String> configureCommand;
+       private IProject project;
+       private IConfiguration configuration;
+       private IPath location;
+
+
+       public ExecuteConfigureJob(String name,
+                       IProject project, IConfiguration configuration, IPath 
location) {
+               super(name);
+               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(), YoctoSDKUtils.getEnvVariablesAsMap(project));
+       }
+
+       /* (non-Javadoc)
+        * @see 
org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
+        */
+       @Override
+       protected IStatus run(IProgressMonitor monitor) {
+       }
+
+       private IStatus buildProject(IProgressMonitor monitor,
+                       IOConsoleOutputStream cos) throws IOException, 
InterruptedException {
+               monitor.subTask(
+                               
YoctoCMakeMessages.getString("ExecuteConfigureJob.buildingMakefile")); 
//$NON-NLS-1$
+               configureProcess.start(cos);
+               int exitValue = configureProcess.waitForResultAndStop();
+               monitor.worked(15);
+
+               if (exitValue != 0) {
+                       return new Status(Status.ERROR, Activator.PLUGIN_ID,
+                                       
YoctoCMakeMessages.getString("ExecuteConfigureJob.error.buildFailed") + " " + 
exitValue); //$NON-NLS-1$ //$NON-NLS-2$
+               }
+
+               return Status.OK_STATUS;
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.core.runtime.jobs.Job#canceling()
+        */
+       /**
+        * Cancels the job and interrupts the system process.
+        * {@inheritDoc}
+        */
+       @Override
+       protected void canceling() {
+               configureProcess.interrupt();
+       }
+}
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
new file mode 100644
index 0000000..9ca6e60
--- /dev/null
+++ 
b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java
@@ -0,0 +1,118 @@
+/*******************************************************************************
+ * 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;
+
+public class SystemProcess {
+
+       private LinkedList<String> command = new LinkedList<String>();
+       private File workingDirectory;
+       private ProcessBuilder builder;
+       private Process process;
+       private StreamPipe outputRedirector;
+
+       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;
+               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);
+               }
+               builder.redirectErrorStream(true);
+
+               if(additionalEnvironmentVariables != null && 
!additionalEnvironmentVariables.isEmpty()) {
+                       
builder.environment().putAll(additionalEnvironmentVariables);
+               }
+       }
+
+       public void start(OutputStream out) throws IOException {
+               if (builder != null) {
+                       process = builder.start();
+                       outputRedirector = redirectOutput(process, out);
+               }
+       }
+
+       public int waitForResultAndStop() throws InterruptedException {
+               if (process == null) {
+                       return -1;
+               }
+
+               process.waitFor();
+               outputRedirector.interrupt();
+
+               return process.exitValue();
+       }
+
+       public void interrupt() {
+               process.destroy();
+       }
+
+       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 out;
+               boolean shutdown = false;
+
+               public StreamPipe(InputStream in, OutputStream out) {
+                       this.in = in;
+                       this.out = out;
+               }
+
+               @Override
+               public void run() {
+                       byte[] buffer = new byte[1024];
+                       int length = 0;
+
+                       try {
+                               while(!shutdown && ((length = in.read(buffer)) 
> -1)) {
+                                       out.write(buffer, 0, length);
+                               }
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+               }
+
+               @Override
+               public void interrupt() {
+                       shutdown = true;
+                       super.interrupt();
+               }
+       }
+}
-- 
1.7.9.5

_______________________________________________
yocto mailing list
yocto@yoctoproject.org
https://lists.yoctoproject.org/listinfo/yocto

Reply via email to