summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/MainPage.java
blob: 48c7509dc3651099ddd23f3fac7d3fe0df5278a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*******************************************************************************
 * Copyright (c) 2012 Intel Corporation.
 * 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:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.remotetools.wizards.bsp;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.yocto.sdk.remotetools.YoctoBspElement;

/**
 *
 * Setting up the parameters for creating the new Yocto BSP
 *
 * @author jzhang
 */
public class MainPage extends WizardPage {
	public static  final String  PAGE_NAME = "Main";
	private static final String KARCH_CMD = "yocto-bsp list karch";
	private static final String QARCH_CMD = "yocto-bsp list qemu property qemuarch";
	private static final String BSP_SCRIPT = "yocto-bsp";
	private static final String PROPERTIES_CMD_PREFIX = "yocto-bsp list ";
	private static final String PROPERTIES_CMD_SURFIX = " properties -o ";
	private static final String PROPERTIES_FILE = "/tmp/properties.json";

	private Button btnMetadataLoc;
	private Text textMetadataLoc;
	private Label labelMetadata;

	private Button btnBspOutputLoc;
	private Text textBspOutputLoc;
	private Label labelBspOutput;

	private Button btnBuildLoc;
	private Text textBuildLoc;
	private Label labelBuildLoc;

	private boolean buildDirChecked;
	private BuildLocationListener buildLocationListener;

	private Text textBspName;
	private Label labelBspName;

	private Combo comboKArch;
	private Label labelKArch;

	private Combo comboQArch;
	private Label labelQArch;

	private YoctoBspElement bspElem;

	public MainPage(YoctoBspElement element) {
		super(PAGE_NAME, "yocto-bsp Main page", null);

		setMessage("Enter the required fields(with *) to create new Yocto Project BSP!");
		this.bspElem = element;
	}

	@Override
	public void createControl(Composite parent) {
		setErrorMessage(null);
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		composite.setLayout(layout);
		gd.horizontalSpan = 2;
		composite.setLayoutData(gd);

		labelMetadata = new Label(composite, SWT.NONE);
		labelMetadata.setText("Metadata location*: ");
		Composite textContainer = new Composite(composite, SWT.NONE);
		textContainer.setLayout(new GridLayout(2, false));
		textContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		textMetadataLoc = (Text)addTextControl(textContainer, "");
		textMetadataLoc.setEnabled(true);
		textMetadataLoc.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				controlChanged(e.widget);
			}
		});
		setBtnMetadataLoc(addFileSelectButton(textContainer, textMetadataLoc));

		labelBuildLoc = new Label(composite, SWT.NONE);
		labelBuildLoc.setText("Build location: ");

		textContainer = new Composite(composite, SWT.NONE);
		textContainer.setLayout(new GridLayout(2, false));
		textContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		textBuildLoc = (Text)addTextControl(textContainer, "");
		buildLocationListener = new BuildLocationListener("");
		textBuildLoc.addFocusListener(buildLocationListener);

		setBtnBuilddirLoc(addFileSelectButton(textContainer, textBuildLoc));

		labelBspName = new Label(composite, SWT.NONE);
		labelBspName.setText("BSP Name*: ");

		textContainer = new Composite(composite, SWT.NONE);
		textContainer.setLayout(new GridLayout(2, false));
		textContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		textBspName = (Text)addTextControl(textContainer, "");
		textBspName.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				controlChanged(e.widget);
			}
		});

		labelBspOutput = new Label(composite, SWT.NONE);
		labelBspOutput.setText("BSP output location: ");

		textContainer = new Composite(composite, SWT.NONE);
		textContainer.setLayout(new GridLayout(2, false));
		textContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		textBspOutputLoc = (Text)addTextControl(textContainer, "");
		textBspOutputLoc.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				controlChanged(e.widget);
			}
		});
		setBtnBspOutLoc(addFileSelectButton(textContainer, textBspOutputLoc));

		labelKArch = new Label(composite, SWT.NONE);
		labelKArch.setText("Kernel Architecture*: ");

		textContainer = new Composite(composite, SWT.NONE);
		textContainer.setLayout(new GridLayout(2, false));
		textContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		comboKArch = new Combo(textContainer, SWT.READ_ONLY);
		comboKArch.setLayout(new GridLayout(2, false));
		comboKArch.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		comboKArch.setEnabled(false);
		comboKArch.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				controlChanged(e.widget);
			}
		});

		labelQArch = new Label(composite, SWT.NONE);
		labelQArch.setText("Qemu Architecture(* for karch as qemu): ");
		labelQArch.setEnabled(false);

		textContainer = new Composite(composite, SWT.NONE);
		textContainer.setLayout(new GridLayout(2, false));
		textContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		comboQArch = new Combo(textContainer, SWT.READ_ONLY);
		comboQArch.setLayout(new GridLayout(2, false));
		comboQArch.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		comboQArch.setEnabled(false);
		comboQArch.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				controlChanged(e.widget);
			}
		});

		setControl(composite);
		validatePage();
	}

	private Control addTextControl(final Composite parent, String value) {
		final Text text;

		text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		text.setText(value);
		text.setSize(10, 150);

		return text;
	}

	private Button addFileSelectButton(final Composite parent, final Text text) {
		Button button = new Button(parent, SWT.PUSH | SWT.LEAD);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				String dirName = new DirectoryDialog(parent.getShell()).open();
				if (dirName != null) {
					text.setText(dirName);
				}
			}
		});
		return button;
	}

	private void controlChanged(Widget widget) {
		Status status = new Status(IStatus.OK, "not_used", 0, "", null);
		setErrorMessage(null);
		String metadataLoc = textMetadataLoc.getText();

		if (widget == textMetadataLoc) {
			resetKarchCombo();
			if (metadataLoc.length() == 0) {
				status = new Status(IStatus.ERROR, "not_used", 0, "Meta data location can't be empty!", null);
			} else {
				File meta_data = new File(metadataLoc);
				if (!meta_data.exists() || !meta_data.isDirectory()) {
					status = new Status(IStatus.ERROR, "not_used", 0,
							"Invalid meta data location: Make sure it exists and is a directory!", null);
				} else {
					File bspScript = new File(metadataLoc + "/scripts/" + BSP_SCRIPT);
					if (!bspScript.exists() || !bspScript.canExecute())
						status = new Status(IStatus.ERROR, "not_used", 0,
								"Make sure yocto-bsp exists under \"" + metadataLoc + "/scripts\" and is executable!", null);
					else {
						kernelArchesHandler();
					}
				}
			}
		} else if (widget == comboKArch) {
			String selection = comboKArch.getText();
			if (!bspElem.getKarch().contentEquals(selection))
				bspElem = new YoctoBspElement();
			if (selection.matches("qemu")) {
				labelQArch.setEnabled(true);
				comboQArch.setEnabled(true);
			} else {
				labelQArch.setEnabled(false);
				comboQArch.setEnabled(false);
			}
		}

		String buildDir = textBuildLoc.getText();
		String outputDir = textBspOutputLoc.getText();
		String bspName = textBspName.getText();
		
		if (bspName.contains(" ")) {
			status = new Status(IStatus.ERROR, "not_used", 0,
					"BSP name contains space which is not allowed!", null);
		}

		if (!outputDir.isEmpty()){
			if (outputDir.matches(buildDir)) {
				status = new Status(IStatus.ERROR, "not_used", 0,
						"You've set BSP output directory the same as build directory, please leave output directory empty for this scenario!", null);
			} else {
				File outputDirectory = new File(outputDir);
				if (outputDirectory.exists()){
					status = new Status(IStatus.ERROR, "not_used", 0,
							"Your BSP output directory points to an exiting directory!", null);
				}
			}
		} else if (buildDir.startsWith(metadataLoc) && !bspName.isEmpty()) {
			String bspDirStr = metadataLoc + "/meta-" + bspName;
			File bspDir = new File(bspDirStr);
			if (bspDir.exists()) {
				status = new Status(IStatus.ERROR, "not_used", 0,
						"Your BSP with name: " + bspName + " already exist under directory: " + bspDirStr + ", please change your bsp name!", null);
			}
		}

		if (status.getSeverity() == IStatus.ERROR)
			setErrorMessage(status.getMessage());

		getWizard().getContainer().updateButtons();
		canFlipToNextPage();
	}

	private Status checkBuildDir() {

		String metadataLoc = textMetadataLoc.getText();
		String buildLoc = textBuildLoc.getText();

		if (buildLoc.isEmpty()) {
			buildLoc = metadataLoc + "/build";
			return createBuildDir(buildLoc);
		} else {
			File buildLocDir = new File(buildLoc);
			if (!buildLocDir.exists()) {
				return createBuildDir(buildLoc);
			} else if (buildLocDir.isDirectory()) {
				return createBuildDir(buildLoc);
			} else {
				return new Status(IStatus.ERROR, "not_used", 0, "Invalid build location: Make sure the build location is a directory!", null);
			}
		}
	}

	private Status createBuildDir(String buildLoc) {
		String metadataDir = textMetadataLoc.getText();

		// if we do  not change the directory to metadata location the script will be looked into the directory indicated by user.dir system property
		// system.property usually points to the location from where eclipse was started
		String createBuildDirCmd = "cd " + metadataDir + ";source " + metadataDir + "/oe-init-build-env " + buildLoc;

		try {
			ProcessBuilder builder = new ProcessBuilder(new String[] {"bash", "-c", createBuildDirCmd});
			Process proc = builder.start();
			InputStream errorStream = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(errorStream);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			String status = "";
			while ( (line = br.readLine()) != null) {
				status += line;
			}

			if (proc.waitFor() != 0)
				return new Status(IStatus.ERROR, "not_used", 0, status, null);;
			return new Status(IStatus.OK, "not_used", 0, "", null);
		} catch (Exception e) {
			return  new Status(IStatus.ERROR, "not_used", 0, e.getMessage(), null);
		}
	}

	public YoctoBspElement getBSPElement() {
		return this.bspElem;
	}


	private void resetKarchCombo() {
		comboKArch.deselectAll();
		comboQArch.deselectAll();
		comboKArch.setEnabled(false);
		labelQArch.setEnabled(false);
		comboQArch.setEnabled(false);
	}

	private void kernelArchesHandler() {
		BSPAction kArchesAction = getKArches();
		if (kArchesAction.getMessage() == null && kArchesAction.getItems().length != 0) {
			comboKArch.setItems(kArchesAction.getItems());
			comboKArch.setEnabled(true);
		} else if (kArchesAction.getMessage() != null){
			setErrorMessage(kArchesAction.getMessage());
			return;
		}
		BSPAction qArchesAction = getQArches();
		if (qArchesAction.getMessage() == null && qArchesAction.getItems().length != 0) {
			comboQArch.setItems(qArchesAction.getItems());
		} else if (qArchesAction.getMessage() != null)
			setErrorMessage(qArchesAction.getMessage());

	}

	@Override
	public boolean canFlipToNextPage(){
		String err = getErrorMessage();
		if (err != null)
			return false;
		else if (!validatePage())
			return false;
		return true;
	}


	public boolean validatePage() {
		String metadataLoc = textMetadataLoc.getText();
		String bspname = textBspName.getText();
		String karch = comboKArch.getText();
		String qarch = comboQArch.getText();
		if (metadataLoc.isEmpty() ||
				bspname.isEmpty() ||
				karch.isEmpty()) {
			return false;
		} else if (karch.matches("qemu") && qarch.isEmpty()) {
			return false;
		}

		bspElem.setBspName(bspname);
		if (!textBspOutputLoc.getText().isEmpty())
			bspElem.setBspOutLoc(textBspOutputLoc.getText());
		else
			bspElem.setBspOutLoc("");
		if (!textBuildLoc.getText().isEmpty()) {
			checkBuildDir();
			bspElem.setBuildLoc(textBuildLoc.getText());
		} else {
			bspElem.setBuildLoc(metadataLoc + "/build");
			if (!buildDirChecked) {
				checkBuildDir();
				buildDirChecked = true;
			}
		}
		bspElem.setMetadataLoc(metadataLoc);
		bspElem.setKarch(karch);
		bspElem.setQarch(qarch);


		if (!bspElem.getValidPropertiesFile()) {
			boolean validPropertiesFile = true;
			BSPAction action = createPropertiesFile();
			if (action.getMessage() != null) {
				validPropertiesFile = false;
				setErrorMessage(action.getMessage());
			}
			bspElem.setValidPropertiesFile(validPropertiesFile);
		}
		return true;
	}

	private BSPAction createPropertiesFile() {
		String createPropertiesCmd = bspElem.getMetadataLoc() + "/scripts/" +
				PROPERTIES_CMD_PREFIX + bspElem.getKarch() +
				PROPERTIES_CMD_SURFIX + PROPERTIES_FILE;
		BSPProgressDialog progressDialog = new BSPProgressDialog(getShell(),  new ErrorCollectorThread(createPropertiesCmd), "Creating properties file ");
		progressDialog.run(false);
		return progressDialog.getBspAction();
	}

	private BSPAction getKArches() {
		String getKArchCmd = textMetadataLoc.getText() + "/scripts/" + KARCH_CMD;
		BSPProgressDialog progressDialog = new BSPProgressDialog(getShell(), new KernelArchGetter(getKArchCmd), "Loading kernel architectures ");
		progressDialog.run(false);
		return progressDialog.getBspAction();
	}

	private BSPAction getQArches() {
		String getQArchCmd = textMetadataLoc.getText() + "/scripts/" + QARCH_CMD;
		BSPProgressDialog progressDialog = new BSPProgressDialog(getShell(), new QemuArchGetter(getQArchCmd), "Loading Qemu architectures ");
		progressDialog.run(false);
		return progressDialog.getBspAction();
	}

	public Button getBtnMetadataLoc() {
		return btnMetadataLoc;
	}

	public void setBtnMetadataLoc(Button btnMetadataLoc) {
		this.btnMetadataLoc = btnMetadataLoc;
	}

	public Button getBtnBspOutLoc() {
		return btnBspOutputLoc;
	}

	public void setBtnBspOutLoc(Button btnBspOutLoc) {
		this.btnBspOutputLoc = btnBspOutLoc;
	}

	public Button getBtnBuilddirLoc() {
		return btnBuildLoc;
	}

	public void setBtnBuilddirLoc(Button btnBuilddirLoc) {
		this.btnBuildLoc = btnBuilddirLoc;
	}

	class BuildLocationListener implements FocusListener{
		String value;
		boolean changed;

		BuildLocationListener(String value){
			this.value = value;
		}
		@Override
		public void focusGained(FocusEvent e) {
			value = ((Text)e.getSource()).getText();
		}

		@Override
		public void focusLost(FocusEvent e) {
			if(!((Text)e.getSource()).getText().equals(value)) {
				checkBuildDir();
				buildDirChecked = true;
			}
		}

	}
}