summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java')
-rw-r--r--plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java186
1 files changed, 186 insertions, 0 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
new file mode 100644
index 0000000..354d930
--- /dev/null
+++ b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/job/ExecuteConfigureJob.java
@@ -0,0 +1,186 @@
1/*******************************************************************************
2 * Copyright (c) 2013 BMW Car IT GmbH.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * BMW Car IT - initial API and implementation
10 *******************************************************************************/
11package org.yocto.cmake.managedbuilder.job;
12
13import java.io.IOException;
14import java.util.Arrays;
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.Map;
18
19import org.eclipse.cdt.core.CCorePlugin;
20import org.eclipse.cdt.core.envvar.IContributedEnvironment;
21import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
22import org.eclipse.cdt.core.envvar.IEnvironmentVariableManager;
23import org.eclipse.cdt.core.model.CoreModel;
24import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
25import org.eclipse.cdt.core.settings.model.ICProjectDescription;
26import org.eclipse.cdt.managedbuilder.core.BuildException;
27import org.eclipse.cdt.managedbuilder.core.IConfiguration;
28import org.eclipse.cdt.managedbuilder.core.ITool;
29import org.eclipse.core.resources.IProject;
30import org.eclipse.core.runtime.IPath;
31import org.eclipse.core.runtime.IProgressMonitor;
32import org.eclipse.core.runtime.IStatus;
33import org.eclipse.core.runtime.Status;
34import org.eclipse.core.runtime.jobs.Job;
35import org.eclipse.jface.dialogs.MessageDialog;
36import org.eclipse.swt.widgets.Display;
37import org.eclipse.ui.console.IOConsoleOutputStream;
38import org.yocto.cmake.managedbuilder.Activator;
39import org.yocto.cmake.managedbuilder.YoctoCMakeMessages;
40import org.yocto.cmake.managedbuilder.util.ConsoleUtility;
41import org.yocto.cmake.managedbuilder.util.SystemProcess;
42
43
44public class ExecuteConfigureJob extends Job {
45 private SystemProcess configureProcess;
46 private LinkedList<String> configureCommand;
47 private IProject project;
48 private IConfiguration configuration;
49 private IPath location;
50
51
52 public ExecuteConfigureJob(String name,
53 IProject project, IConfiguration configuration, IPath location) {
54 super(name);
55 this.project = project;
56 this.configuration = configuration;
57 this.location = location;
58 createCommands();
59 createProcesses();
60 }
61
62 protected void createCommands() {
63 configureCommand = new LinkedList<String>();
64
65 ITool[] configure = configuration
66 .getToolsBySuperClassId("org.yocto.cmake.managedbuilder.cmakeconfigure.gnu.exe"); //$NON-NLS-1$
67
68 addCommand(configure[0]);
69
70 try {
71 addFlags(configure[0]);
72 } catch (BuildException e) {
73 // ignore this exception
74 }
75 }
76
77 private void addCommand(ITool configure) {
78 String command = configuration.getToolCommand(configure);
79 configureCommand.add(command);
80 }
81
82 private void addFlags(ITool configure) throws BuildException {
83 String[] flags = configure.getToolCommandFlags(
84 project.getLocation(), location);
85 for (String flag : flags) {
86 if (flag.contains(" ")) { //$NON-NLS-1$
87 String[] separatedFlags = flag.trim().split(" "); //$NON-NLS-1$
88 configureCommand.addAll(Arrays.asList(separatedFlags));
89 } else {
90 configureCommand.add(flag);
91 }
92 }
93 }
94
95 protected void createProcesses() {
96 configureProcess =
97 new SystemProcess(configureCommand, location.toFile(), getEnvVariablesAsMap(project));
98 }
99
100 private Map<String,String> getEnvVariablesAsMap (IProject project) {
101 Map<String, String> result = new HashMap<String, String>();
102
103 ICProjectDescription cpdesc = CoreModel.getDefault().getProjectDescription(project, true);
104 ICConfigurationDescription ccdesc = cpdesc.getActiveConfiguration();
105 IEnvironmentVariableManager manager = CCorePlugin.getDefault().getBuildEnvironmentManager();
106 IContributedEnvironment env = manager.getContributedEnvironment();
107
108 for(IEnvironmentVariable var : env.getVariables(ccdesc)) {
109 result.put(var.getName(), var.getValue());
110 }
111
112 return result;
113 }
114
115 /* (non-Javadoc)
116 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
117 */
118 @Override
119 protected IStatus run(IProgressMonitor monitor) {
120 monitor.beginTask(
121 YoctoCMakeMessages.getString("ExecuteConfigureJob.runConfigure"), 20); //$NON-NLS-1$
122
123 IOConsoleOutputStream cos =
124 ConsoleUtility.getConsoleOutput(YoctoCMakeMessages.getFormattedString("ExecuteConfigureJob.consoleName", //$NON-NLS-1$
125 project.getName()));
126 monitor.worked(1);
127
128 try {
129 return buildProject(monitor, cos);
130 } catch (IOException e) {
131 if(e.getMessage().startsWith("Cannot run program \"cmake\"")) { //$NON-NLS-1$
132 Display.getDefault().asyncExec(new Runnable() {
133 @Override
134 public void run() {
135 MessageDialog.openWarning(null,
136 YoctoCMakeMessages.getString("ExecuteConfigureJob.cmakeWarning.dialogTitle"), //$NON-NLS-1$
137 YoctoCMakeMessages.getString("ExecuteConfigureJob.cmakeWarning.dialogMessage")); //$NON-NLS-1$
138 }
139 });
140 return Status.OK_STATUS;
141 } else {
142 return new Status(Status.ERROR,
143 Activator.PLUGIN_ID, Status.OK,
144 YoctoCMakeMessages.getString("ExecuteConfigureJob.error.couldNotStart"), e); //$NON-NLS-1$
145 }
146 } catch (InterruptedException e) {
147 return new Status(Status.WARNING,
148 Activator.PLUGIN_ID,
149 YoctoCMakeMessages.getString("ExecuteConfigureJob.warning.aborted")); //$NON-NLS-1$
150 } finally {
151 try {
152 cos.close();
153 } catch (IOException e) {
154 cos = null;
155 }
156 }
157 }
158
159 private IStatus buildProject(IProgressMonitor monitor,
160 IOConsoleOutputStream cos) throws IOException, InterruptedException {
161 monitor.subTask(
162 YoctoCMakeMessages.getString("ExecuteConfigureJob.buildingMakefile")); //$NON-NLS-1$
163 configureProcess.start(cos);
164 int exitValue = configureProcess.waitForResultAndStop();
165 monitor.worked(15);
166
167 if (exitValue != 0) {
168 return new Status(Status.ERROR, Activator.PLUGIN_ID,
169 YoctoCMakeMessages.getString("ExecuteConfigureJob.error.buildFailed") + " " + exitValue); //$NON-NLS-1$ //$NON-NLS-2$
170 }
171
172 return Status.OK_STATUS;
173 }
174
175 /* (non-Javadoc)
176 * @see org.eclipse.core.runtime.jobs.Job#canceling()
177 */
178 /**
179 * Cancels the job and interrupts the system process.
180 * {@inheritDoc}
181 */
182 @Override
183 protected void canceling() {
184 configureProcess.interrupt();
185 }
186}