aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/bitbake/BBSession.java
blob: 87b3f53810f36b9df6aa0f55de2091562f40089a (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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*****************************************************************************
 * Copyright (c) 2009 Ken Gilmer
 * 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:
 *     Ken Gilmer - initial API and implementation
 *******************************************************************************/
package org.openembedded.bc.bitbake;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.openembedded.bc.ui.model.IModelElement;
import org.openembedded.bc.ui.model.ProjectInfo;


/**
 * BBSession encapsulates a global bitbake configuration and is the primary interface
 * for actions against a BitBake installation.
 * 
 * @author kgilmer
 *
 */
public class BBSession implements IModelElement, Map {
	public static final int TYPE_VARIABLE_ASSIGNMENT = 1;
	public static final int TYPE_UNKNOWN = 2;
	public static final int TYPE_STATEMENT = 3;
	public static final int TYPE_FLAG = 4;
	
	protected final ProjectInfo pinfo;
	protected final ShellSession shell;
	protected Map properties = null;
	protected boolean initialized = false;
	private MessageConsole sessionConsole;
	
	public BBSession(ShellSession ssession, String projectRoot) throws IOException {
		shell = ssession;
		this.pinfo = new ProjectInfo();
		pinfo.setLocation(projectRoot);
		pinfo.setInitScriptPath(ProjectInfoHelper.getInitScriptPath(projectRoot));
	}
	
	private Collection adapttoIPath(List<File> asList, IProject project) {

		List pathList = new ArrayList();

		for (Iterator i = asList.iterator(); i.hasNext();) {
			File f = (File) i.next();
			IFile ff = project.getFile(stripLeading(f.toString(), project.getLocationURI().getPath()));
			if (ff.exists()) {
				pathList.add(ff);
			}
		}

		return pathList;
	}
	
	private String appendAll(String[] elems, int st) {
		StringBuffer sb = new StringBuffer();

		for (int i = st; i < elems.length; ++i) {
			sb.append(elems[i]);
		}

		return sb.toString();
	}
	
	private int charCount(String trimmed, char c) {
		int i = 0;
		int p = 0;

		while ((p = trimmed.indexOf(c, p)) > -1) {
			i++;
			p++;
		}

		return i;
	}
	
	public void clear() {
		throw new RuntimeException("BB configuration is read-only.");
	}

	public boolean containsKey(Object arg0) {
		return properties.containsKey(arg0);
	}

	public boolean containsValue(Object arg0) {
		return properties.containsValue(arg0);
	}

	public Set entrySet() {
		return properties.entrySet();
	}

	@Override
	public boolean equals(Object arg0) {
		return properties.equals(arg0);
	}

	public ShellSession getShell() {
		return shell;
	}

	/**
	 * Recursively generate list of Recipe files from a root directory.
	 * 
	 * @param rootDir
	 * @param recipes
	 * @param fileExtension
	 * @param project
	 */
	private void findRecipes(File rootDir, List recipes, final String fileExtension, IProject project) {
		File[] children = rootDir.listFiles(new FileFilter() {

			public boolean accept(File pathname) {
				return pathname.isFile() && pathname.getName().endsWith(fileExtension);
			}

		});

		if (children != null && children.length > 0) {
			recipes.addAll(adapttoIPath(Arrays.asList(children), project));
		}

		File[] childDirs = rootDir.listFiles(new FileFilter() {

			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}

		});

		if (childDirs != null && childDirs.length > 0) {
			for (int i = 0; i < childDirs.length; ++i) {
				findRecipes(childDirs[i], recipes, fileExtension, project);
			}
		}
	}

	private Collection findRecipes(List paths, IProject project) {
		List recipes = new ArrayList();

		for (Iterator i = paths.iterator(); i.hasNext();) {
			String rawPath = (String) i.next();
			String[] elems = rawPath.split("\\*/\\*");

			if (elems.length == 2) {

				File rootDir = new File(elems[0]);

				findRecipes(rootDir, recipes, elems[1], project);
			}
		}

		return recipes;
	}

	public Object get(Object arg0) {
		return properties.get(arg0);
	}

	private List getBitBakeKeywords() {
		return Arrays.asList(BBLanguageHelper.BITBAKE_KEYWORDS);
	}

	/**
	 * @return A MessageConsole for this BB session.
	 */
	public MessageConsole getConsole() {
		if (sessionConsole == null) {
			String cName = ProjectInfoHelper.getProjectName(pinfo.getRootPath()) + " Console";
			sessionConsole = new MessageConsole(cName, null);
			ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { sessionConsole });
		}
		
		ConsolePlugin.getDefault().getConsoleManager().showConsoleView(sessionConsole);
		
		return sessionConsole;
	}

	private int getLineType(String line) {

		if (line.contains("=")) {
			return TYPE_VARIABLE_ASSIGNMENT;
		}

		for (Iterator i = getBitBakeKeywords().iterator(); i.hasNext();) {
			if (line.startsWith((String) i.next())) {
				return TYPE_STATEMENT;
			}
		}

		if (line.contains(":")) {
			return TYPE_FLAG;
		}

		return TYPE_UNKNOWN;
	}

	public Collection getRecipeFiles(IProject project) {
		if (!initialized) {
			throw new RuntimeException(this.getClass().getName() + " is not initialized.");
		}
		String bbfiles = (String) this.properties.get("BBFILES");

		List paths = parseBBFiles(bbfiles);

		return findRecipes(paths, project);
	}

	@Override
	public int hashCode() {
		return properties.hashCode();
	}

	public void initialize() throws Exception {
		if (initialized) {
			return;
		}
		
		properties = parseBBEnvironment(shell.execute("bitbake -e"));
		initialized = true;
	}

	private boolean isBlockEnd(String trimmed) {
		return charCount(trimmed, '}') > charCount(trimmed, '{');
		// return trimmed.indexOf('}') > -1 && trimmed.indexOf('{') == -1;
	}

	private boolean isBlockStart(String trimmed) {
		return charCount(trimmed, '{') > charCount(trimmed, '}');
		// return trimmed.indexOf('{') > -1 && trimmed.indexOf('}') == -1;
	}

	public boolean isEmpty() {
		return properties.isEmpty();
	}
	
	public boolean isInitialized() {
		return initialized;
	}

	public Set keySet() {
		return properties.keySet();
	}

	protected void parse(String content, Map outMap) throws Exception {
		BufferedReader reader = new BufferedReader(new StringReader(content));
		String line;
		boolean inLine = false;
		StringBuffer sb = null;
		Stack blockStack = new Stack();

		while ((line = reader.readLine()) != null) {
			String trimmed = line.trim();
			if (trimmed.length() == 0 || line.startsWith("#")) {
				// weed out the blank and comment lines
				continue;
			}
			// Now we look for block start ends, and ignore all code within
			// blocks.
			if (isBlockStart(trimmed)) {
				blockStack.push(trimmed);
			} else if (isBlockEnd(trimmed)) {
				blockStack.pop();

			}

			if (!blockStack.isEmpty()) {
				// we are in a code block, continue until we break into global
				// scope.
				continue;
			}
			if (trimmed.endsWith("\\")) {
				if (!inLine) {
					inLine = true;
					sb = new StringBuffer(trimmed.substring(0, trimmed.length() - 1));
				} else {
					sb.append(trimmed.substring(0, trimmed.length() - 1));
				}
				// Only parse the line when we have the complete contents.
				continue;
			} else if (inLine) {
				inLine = false;
				line = sb.toString();
			}

			parseLine(line, outMap);
		}
	}
	
	private void parseAdditiveAssignment(String line, String operator, Map mo) throws Exception {
		String[] elems = splitAssignment(line, "\\+=");

		if (elems.length != 2) {
			throw new Exception("Unable to parse additive variable assignment in line: " + line);
		}

		if (!mo.containsKey(elems[0])) {
			mo.put(elems[0].trim(), elems[1]);
		} else {
			String existing = (String) mo.get(elems[0]);
			if (operator.equals("+=")) {
				mo.put(elems[0], existing + elems[1]);
			} else {
				mo.put(elems[0], elems[1] + existing);
			}
		}
	}
	
	protected Map parseBBEnvironment(String bbOut) throws Exception {
		Map env = new Hashtable();

		parse(bbOut, env);

		return env;
	}
	

	private List parseBBFiles(String bbfiles) {
		return Arrays.asList(bbfiles.split(" "));
	}
	
	//Map delegate methods 

	private void parseConditionalAssignment(String line, Map mo) throws Exception {
		String[] elems = splitAssignment(line, "\\?=");

		if (elems.length != 2) {
			throw new Exception("Unable to parse conditional variable assignment in line: " + line);
		}

		if (!mo.containsKey(elems[0].trim())) {
			mo.put(elems[0].trim(), elems[1].trim());
		}
	}

	private void parseImmediateAssignment(String line, String delimiter, Map mo) throws Exception {
		String[] elems = splitAssignment(line, delimiter);

		mo.put(elems[0], substitute(elems[1], mo));
	}

	private void parseKeyValue(String line, String delimiter, Map mo) throws Exception {
		String[] elems = splitAssignment(line, delimiter);

		mo.put(elems[0], elems[1]);
	}

	private void parseLine(String line, Map mo) throws Exception {

		switch (getLineType(line)) {
		case TYPE_VARIABLE_ASSIGNMENT:
			parseVariableAssignment(line, mo);
			break;
		case TYPE_STATEMENT:
		case TYPE_FLAG:
			// for now ignore statements
			break;
		case TYPE_UNKNOWN:
			// we'll gloss over unknown lines as well;
			break;
		default:
			throw new Exception("Unable to parse line: " + line);
		}
	}

	private void parseVariableAssignment(String line, Map mo) throws Exception {
		if (line.contains("?=")) {
			parseConditionalAssignment(line, mo);
		} else if (line.contains("+=")) {
			parseAdditiveAssignment(line, "+=", mo);
		} else if (line.contains("=+")) {
			parseAdditiveAssignment(line, "=+", mo);
		} else if (line.contains(":=")) {
			parseImmediateAssignment(line, ":=", mo);
		} else {
			parseKeyValue(line, "=", mo);
		}

	}

	private List parseVars(String line) {
		List l = new ArrayList();

		int i = 0;

		while ((i = line.indexOf("${", i)) > -1) {
			int i2 = line.indexOf("}", i);

			l.add(line.subSequence(i + 2, i2));
			i++;
		}

		return l;
	}

	public Object put(Object arg0, Object arg1) {
		throw new RuntimeException("BB configuration is read-only.");
	}

	public void putAll(Map arg0) {
		throw new RuntimeException("BB configuration is read-only.");
	}

	public Object remove(Object arg0) {
		throw new RuntimeException("BB configuration is read-only.");
	}

	private String removeQuotes(String line) {
		line = line.trim();

		if (line.startsWith("\"")) {
			line = line.substring(1);
		}

		if (line.endsWith("\"")) {
			line = line.substring(0, line.length() - 1);
		}

		return line;
	}

	public int size() {
		return properties.size();
	}

	private String[] splitAssignment(String line, String seperator) throws Exception {
		String[] elems = line.split(seperator);

		if (elems.length < 2) {
			throw new Exception("Unable to parse assignment in line: " + line);
		} else if (elems.length == 2) {

			elems[0] = elems[0].trim(); // Clean up trailing or leading spaces.
			if (elems[0].startsWith("export ")) {
				elems[0] = elems[0].substring("export ".length()).trim();
			}
			elems[1] = removeQuotes(elems[1]); // Evaluate variables

			return elems;
		} else {
			String[] retVal = new String[2];

			retVal[0] = elems[0];
			if (retVal[0].startsWith("export ")) {
				retVal[0] = retVal[0].substring("export ".length()).trim();
			}
			retVal[1] = appendAll(elems, 1);

			return retVal;
		}
	}

	private String stripLeading(String target, String leading) {
		if (target.startsWith(leading)) {
			target = target.substring(leading.length());
		}

		return target;
	}

	/**
	 * Return a string with variable substitutions in place.
	 * 
	 * @param expression
	 * @return Input string with any substitutions from this file.
	 */
	public String substitute(String expression, Map mo) {

		List vars = parseVars(expression);

		for (Iterator i = vars.iterator(); i.hasNext();) {
			String varName = (String) i.next();
			String varToken = "${" + varName + "}";

			if (mo.containsKey(varName)) {
				expression = expression.replace(varToken, (String) mo.get(varName));
			} else if (System.getProperty(varName) != null) {
				expression = expression.replace(varToken, System.getProperty(varName));
			} else if (varName.toUpperCase().equals("HOME")) {
				expression = expression.replace(varToken, System.getProperty("user.home"));
			}
		}

		return expression;
	}

	public Collection values() {
		return properties.values();
	}
}