Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 68349ae4b6e29ef2ad127504e1577f58d1920a82 (plain) (blame)
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package org.eclipse.cdt.arduino.core.internal.build;

import java.io.BufferedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.arduino.core.internal.Activator;
import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences;
import org.eclipse.cdt.arduino.core.internal.ArduinoTemplateGenerator;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoBoard;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoLibrary;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoManager;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoPackage;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoPlatform;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoTool;
import org.eclipse.cdt.arduino.core.internal.board.ToolDependency;
import org.eclipse.cdt.arduino.core.internal.console.ArduinoConsoleParser;
import org.eclipse.cdt.arduino.core.internal.console.ArduinoErrorParser;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IOutputEntry;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.cdt.core.model.ISourceRoot;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.osgi.service.prefs.BackingStoreException;

public class ArduinoBuildConfiguration {

	private static final String PACKAGE_NAME = "packageName"; //$NON-NLS-1$
	private static final String PLATFORM_NAME = "platformName"; //$NON-NLS-1$
	private static final String BOARD_NAME = "boardName"; //$NON-NLS-1$

	private final IBuildConfiguration config;

	private ArduinoBoard board;
	private Properties properties;

	// Cache for scanner info
	private IScannerInfo cScannerInfo;
	private IScannerInfo cppScannerInfo;

	private ArduinoBuildConfiguration(IBuildConfiguration config) {
		this.config = config;
	}

	private static Map<IBuildConfiguration, ArduinoBuildConfiguration> cache = new HashMap<>();

	public static class Factory implements IAdapterFactory {
		@SuppressWarnings("unchecked")
		@Override
		public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
			if (adapterType.equals(ArduinoBuildConfiguration.class) && adaptableObject instanceof IBuildConfiguration) {
				IBuildConfiguration config = (IBuildConfiguration) adaptableObject;
				ArduinoBuildConfiguration arduinoConfig = cache.get(config);
				if (arduinoConfig == null) {
					arduinoConfig = new ArduinoBuildConfiguration(config);
					cache.put(config, arduinoConfig);
				}
				return (T) arduinoConfig;
			}
			return null;
		}

		@Override
		public Class<?>[] getAdapterList() {
			return new Class<?>[] { ArduinoBuildConfiguration.class };
		}
	}

	public static ArduinoBuildConfiguration getConfig(IProject project, ArduinoBoard board, IProgressMonitor monitor)
			throws CoreException {
		// return it if it exists already
		for (IBuildConfiguration config : project.getBuildConfigs()) {
			ArduinoBuildConfiguration arduinoConfig = config.getAdapter(ArduinoBuildConfiguration.class);
			if (board.equals(arduinoConfig.getBoard())) {
				return arduinoConfig;
			}
		}

		// Not found, need to create one
		Set<String> configNames = new HashSet<>();
		for (IBuildConfiguration config : project.getBuildConfigs()) {
			configNames.add(config.getName());
		}
		String newName = board.getId();
		int n = 0;
		while (configNames.contains(newName)) {
			newName = board.getId() + (++n);
		}
		configNames.add(newName);
		IProjectDescription projectDesc = project.getDescription();
		projectDesc.setBuildConfigs(configNames.toArray(new String[configNames.size()]));
		project.setDescription(projectDesc, monitor);

		// set it up for the board
		IBuildConfiguration config = project.getBuildConfig(newName);
		ArduinoBuildConfiguration arduinoConfig = config.getAdapter(ArduinoBuildConfiguration.class);
		arduinoConfig.setBoard(board);

		return arduinoConfig;
	}

	public void setActive(IProgressMonitor monitor) throws CoreException {
		IProject project = config.getProject();
		if (config.equals(project.getActiveBuildConfig())) {
			// already set
			return;
		}

		IProjectDescription projectDesc = project.getDescription();
		projectDesc.setActiveBuildConfig(config.getName());
		project.setDescription(projectDesc, monitor);
	}

	public IEclipsePreferences getSettings() {
		return (IEclipsePreferences) new ProjectScope(config.getProject()).getNode(Activator.getId()).node("config") //$NON-NLS-1$
				.node(config.getName());
	}

	public void setBoard(ArduinoBoard board) throws CoreException {
		this.board = board;

		ArduinoPlatform platform = board.getPlatform();
		ArduinoPackage pkg = platform.getPackage();

		IEclipsePreferences settings = getSettings();
		settings.put(PACKAGE_NAME, pkg.getName());
		settings.put(PLATFORM_NAME, platform.getName());
		settings.put(BOARD_NAME, board.getName());
		try {
			settings.flush();
		} catch (BackingStoreException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Saving preferences", e));
		}
	}

	public ArduinoBoard getBoard() throws CoreException {
		if (board == null) {
			IEclipsePreferences settings = getSettings();
			String packageName = settings.get(PACKAGE_NAME, ""); //$NON-NLS-1$
			String platformName = settings.get(PLATFORM_NAME, ""); //$NON-NLS-1$
			String boardName = settings.get(BOARD_NAME, ""); //$NON-NLS-1$
			board = ArduinoManager.instance.getBoard(boardName, platformName, packageName);
		}
		return board;
	}

	private Properties getProperties() throws CoreException {
		if (properties == null) {
			ArduinoBoard board = getBoard();
			ArduinoPlatform platform = board.getPlatform();
			properties = board.getBoardProperties();
			properties.putAll(board.getPlatform().getPlatformProperties());
			for (ToolDependency toolDep : platform.getToolsDependencies()) {
				properties.putAll(toolDep.getTool().getToolProperties());
			}
			properties.put("runtime.ide.version", "10607"); //$NON-NLS-1$ //$NON-NLS-2$
			properties.put("build.arch", platform.getArchitecture().toUpperCase()); //$NON-NLS-1$
			properties.put("build.path", config.getName()); //$NON-NLS-1$
		}
		// always do this in case the project changes names
		properties.put("build.project_name", config.getProject().getName()); //$NON-NLS-1$
		return properties;
	}

	public IFolder getBuildFolder() throws CoreException {
		IProject project = config.getProject();
		return project.getFolder("build"); //$NON-NLS-1$
	}

	public File getBuildDirectory() throws CoreException {
		return new File(getBuildFolder().getLocationURI());
	}

	public IFile getMakeFile() throws CoreException {
		IFolder buildFolder = getBuildFolder();
		ArduinoBoard board = getBoard();
		String makeFileName = board.getId() + ".mk"; //$NON-NLS-1$
		return buildFolder.getFile(makeFileName);
	}

	public IFile generateMakeFile(IProgressMonitor monitor) throws CoreException {
		final IProject project = config.getProject();

		IFolder buildFolder = getBuildFolder();
		if (!buildFolder.exists()) {
			buildFolder.create(true, true, monitor);
			ICProject cproject = CoreModel.getDefault().create(project);
			IOutputEntry output = CoreModel.newOutputEntry(buildFolder.getFullPath());
			IPathEntry[] oldEntries = cproject.getRawPathEntries();
			IPathEntry[] newEntries = new IPathEntry[oldEntries.length + 1];
			System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
			newEntries[oldEntries.length] = output;
			cproject.setRawPathEntries(newEntries, monitor);
		}

		ArduinoBoard board = getBoard();
		ArduinoPlatform platform = board.getPlatform();

		IFile makeFile = getMakeFile();

		// The board id
		Map<String, Object> buildModel = new HashMap<>();
		buildModel.put("boardId", board.getId()); //$NON-NLS-1$

		// The list of source files in the project
		final Path projectPath = new File(project.getLocationURI()).toPath();
		final List<String> sourceFiles = new ArrayList<>();
		for (ISourceRoot sourceRoot : CCorePlugin.getDefault().getCoreModel().create(project).getSourceRoots()) {
			sourceRoot.getResource().accept(new IResourceProxyVisitor() {
				@Override
				public boolean visit(IResourceProxy proxy) throws CoreException {
					if (proxy.getType() == IResource.FILE) {
						if (CoreModel.isValidSourceUnitName(project, proxy.getName())) {
							Path sourcePath = new File(proxy.requestResource().getLocationURI()).toPath();
							sourceFiles.add(projectPath.relativize(sourcePath).toString());
						}
					}
					return true;
				}
			}, 0);
		}
		buildModel.put("project_srcs", sourceFiles); //$NON-NLS-1$

		// The list of library sources
		List<String> librarySources = new ArrayList<>();
		for (ArduinoLibrary lib : ArduinoManager.instance.getLibraries(project)) {
			for (Path path : lib.getSources(project)) {
				librarySources.add(path.toString());
			}
		}
		buildModel.put("libraries_srcs", librarySources); //$NON-NLS-1$
		buildModel.put("libraries_path", ArduinoPreferences.getArduinoHome().resolve("libraries")); //$NON-NLS-1$ //$NON-NLS-2$

		// the recipes
		Properties properties = new Properties();
		properties.putAll(getProperties());
		buildModel.put("build_path", properties.get("build.path")); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("project_name", project.getName()); //$NON-NLS-1$

		String includes = null;
		for (Path include : platform.getIncludePath()) {
			if (includes == null) {
				includes = "-I"; //$NON-NLS-1$
			} else {
				includes += " -I"; //$NON-NLS-1$
			}
			includes += '"' + include.toString() + '"';
		}
		for (ArduinoLibrary lib : ArduinoManager.instance.getLibraries(project)) {
			for (Path include : lib.getIncludePath()) {
				includes += " -I\"" + include.toString() + '"'; //$NON-NLS-1$
			}
		}
		properties.put("includes", includes); //$NON-NLS-1$

		Path platformPath = platform.getInstallPath();
		buildModel.put("platform_path", platformPath.toString()); //$NON-NLS-1$

		Path corePath = platformPath.resolve("cores").resolve((String) properties.get("build.core")); //$NON-NLS-1$ //$NON-NLS-2$
		File[] platformFiles = corePath.toFile().listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".cpp") || name.endsWith(".c"); //$NON-NLS-1$ //$NON-NLS-2$
			}
		});

		String[] platformSource = new String[platformFiles.length];
		for (int i = 0; i < platformSource.length; ++i)

		{
			platformSource[i] = platformFiles[i].getAbsolutePath();
		}
		buildModel.put("platform_srcs", platformSource); //$NON-NLS-1$

		properties.put("object_file", "$@"); //$NON-NLS-1$ //$NON-NLS-2$
		properties.put("source_file", "$<"); //$NON-NLS-1$ //$NON-NLS-2$
		properties.put("archive_file", "core.a"); //$NON-NLS-1$ //$NON-NLS-2$
		properties.put("object_files", "$(PROJECT_OBJS) $(LIBRARIES_OBJS)"); //$NON-NLS-1$ //$NON-NLS-2$

		buildModel.put("recipe_cpp_o_pattern", resolveProperty("recipe.cpp.o.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_c_o_pattern", resolveProperty("recipe.c.o.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_ar_pattern", resolveProperty("recipe.ar.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_c_combine_pattern", resolveProperty("recipe.c.combine.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_objcopy_eep_pattern", resolveProperty("recipe.objcopy.eep.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_objcopy_hex_pattern", resolveProperty("recipe.objcopy.hex.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_size_pattern", resolveProperty("recipe.size.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$

		ArduinoTemplateGenerator templateGen = new ArduinoTemplateGenerator();
		templateGen.generateFile(buildModel, "board.mk", makeFile, monitor); //$NON-NLS-1$
		return makeFile;
	}

	private String resolveProperty(String property, Properties dict) {
		String res = dict.getProperty(property);
		if (res == null) {
			return null;
		}

		String last;
		do {
			last = res;
			for (int i = res.indexOf('{'); i >= 0; i = res.indexOf('{', i)) {
				i++;
				int n = res.indexOf('}', i);
				if (n >= 0) {
					String p2 = res.substring(i, n);
					String r2 = dict.getProperty(p2);
					if (r2 != null) {
						res = res.replace('{' + p2 + '}', r2);
					}
				}
				i = n;
			}
		} while (!res.equals(last));

		return res;
	}

	public void setEnvironment(Map<String, String> env) throws CoreException {
		// Arduino home to find platforms and libraries
		env.put("ARDUINO_HOME", ArduinoPreferences.getArduinoHome().toString()); //$NON-NLS-1$

		// Add tools to the path
		String pathKey = null;
		String path = null;
		for (Map.Entry<String, String> entry : env.entrySet()) {
			if (entry.getKey().equalsIgnoreCase("PATH")) { //$NON-NLS-1$
				pathKey = entry.getKey();
				path = entry.getValue();
				break;
			}
		}

		List<String> toolPaths = new ArrayList<>();
		ArduinoBoard board = getBoard();
		ArduinoPlatform platform = board.getPlatform();
		for (ToolDependency dep : platform.getToolsDependencies()) {
			ArduinoTool tool = dep.getTool();
			Path installPath = tool.getInstallPath();
			Path binPath = installPath.resolve("bin"); //$NON-NLS-1$
			if (binPath.toFile().exists()) {
				toolPaths.add(binPath.toString());
			} else {
				// use the install dir by default
				toolPaths.add(installPath.toString());
			}
		}
		for (String toolPath : toolPaths) {
			if (path != null) {
				path = toolPath + File.pathSeparatorChar + path;
			} else {
				path = toolPath;
			}
		}
		if (pathKey == null) {
			pathKey = "PATH"; //$NON-NLS-1$
		}
		env.put(pathKey, path);
	}

	public String[] getBuildCommand() throws CoreException {
		return new String[] { "make", "-f", getMakeFile().getName() }; //$NON-NLS-1$ //$NON-NLS-2$
	}

	public String[] getCleanCommand() throws CoreException {
		return new String[] { "make", "-f", getMakeFile().getName(), "clean" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	public String[] getSizeCommand() throws CoreException {
		// TODO this shouldn't be in the makefile
		// should be like the upload command
		return new String[] { "make", "-f", getMakeFile().getName(), "size" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	public String getCodeSizeRegex() throws CoreException {
		return (String) getBoard().getPlatform().getPlatformProperties().getProperty("recipe.size.regex"); //$NON-NLS-1$
	}

	public int getMaxCodeSize() throws CoreException {
		String sizeStr = (String) getBoard().getBoardProperties().getProperty("upload.maximum_size"); //$NON-NLS-1$
		return sizeStr != null ? Integer.parseInt(sizeStr) : -1;
	}

	public String getDataSizeRegex() throws CoreException {
		return (String) getBoard().getPlatform().getPlatformProperties().getProperty("recipe.size.regex.data"); //$NON-NLS-1$
	}

	public int getMaxDataSize() throws CoreException {
		String sizeStr = (String) getBoard().getBoardProperties().getProperty("upload.maximum_data_size"); //$NON-NLS-1$
		return sizeStr != null ? Integer.parseInt(sizeStr) : -1;
	}

	public String[] getUploadCommand(String serialPort) throws CoreException {
		String toolName = getProperties().getProperty("upload.tool"); //$NON-NLS-1$
		ArduinoTool tool = board.getPlatform().getTool(toolName);

		Properties properties = getProperties();
		properties.put("runtime.tools." + toolName + ".path", tool.getInstallPath().toString()); //$NON-NLS-1$ //$NON-NLS-2$
		properties.put("serial.port", serialPort); //$NON-NLS-1$
		// to make up for some cheating in the platform.txt file
		properties.put("path", "{tools." + toolName + ".path}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		properties.put("cmd.path", "{tools." + toolName + ".cmd.path}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		properties.put("config.path", "{tools." + toolName + ".config.path}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		properties.put("upload.verbose", "{tools." + toolName + ".upload.params.quiet}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		String command = resolveProperty("tools." + toolName + ".upload.pattern", properties); //$NON-NLS-1$ //$NON-NLS-2$
		// TODO Windows
		return new String[] { "sh", "-c", command }; //$NON-NLS-1$ //$NON-NLS-2$
	}

	public IScannerInfo getScannerInfo(IResource resource) throws CoreException {
		// what language is this resource and pick the right path;
		switch (CCorePlugin.getContentType(resource.getProject(), resource.getName()).getId()) {
		case CCorePlugin.CONTENT_TYPE_CXXSOURCE:
		case CCorePlugin.CONTENT_TYPE_CXXHEADER:
			if (cppScannerInfo == null) {
				cppScannerInfo = calculateScannerInfo("recipe.cpp.o.pattern", resource); //$NON-NLS-1$
			}
			return cppScannerInfo;
		default:
			if (cScannerInfo == null) {
				cScannerInfo = calculateScannerInfo("recipe.c.o.pattern", resource); //$NON-NLS-1$
			}
			return cScannerInfo;
		}
	}

	public void clearScannerInfoCache() {
		cppScannerInfo = null;
		cScannerInfo = null;
	}

	private IScannerInfo calculateScannerInfo(String recipe, IResource resource) throws CoreException {
		try {
			ArduinoPlatform platform = getBoard().getPlatform();
			Properties properties = new Properties();
			properties.putAll(getProperties());

			Path tmpFile = Files.createTempFile("cdt", ".cpp"); //$NON-NLS-1$ //$NON-NLS-2$
			properties.put("source_file", tmpFile.toString()); //$NON-NLS-1$
			properties.put("object_file", "-"); //$NON-NLS-1$ //$NON-NLS-2$

			String includes = "-E -P -v -dD"; //$NON-NLS-1$
			for (Path include : platform.getIncludePath()) {
				includes += " -I\"" + include.toString() + '"'; //$NON-NLS-1$
			}
			Collection<ArduinoLibrary> libs = ArduinoManager.instance.getLibraries(config.getProject());
			for (ArduinoLibrary lib : libs) {
				for (Path path : lib.getIncludePath()) {
					includes += " -I\"" + path.toString() + '"'; //$NON-NLS-1$
				}
			}
			properties.put("includes", includes); //$NON-NLS-1$

			// TODO Windows
			String[] command = new String[] { "sh", "-c", resolveProperty(recipe, properties) }; //$NON-NLS-1$ //$NON-NLS-2$
			ProcessBuilder processBuilder = new ProcessBuilder(command).directory(tmpFile.getParent().toFile())
					.redirectErrorStream(true);
			setEnvironment(processBuilder.environment());
			Process process = processBuilder.start();

			Map<String, String> symbols = new HashMap<>();
			List<String> includePath = new ArrayList<>();
			Pattern definePattern = Pattern.compile("#define (.*)\\s(.*)"); //$NON-NLS-1$
			boolean inIncludePaths = false;
			try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
				for (String line = reader.readLine(); line != null; line = reader.readLine()) {
					if (inIncludePaths) {
						if (line.equals("End of search list.")) { //$NON-NLS-1$
							inIncludePaths = false;
						} else {
							includePath.add(line.trim());
						}
					} else if (line.startsWith("#define ")) { //$NON-NLS-1$
						Matcher matcher = definePattern.matcher(line);
						if (matcher.matches()) {
							symbols.put(matcher.group(1), matcher.group(2));
						}
					} else if (line.equals("#include <...> search starts here:")) { //$NON-NLS-1$
						inIncludePaths = true;
					}
				}
			}
			Files.delete(tmpFile);
			ExtendedScannerInfo scannerInfo = new ExtendedScannerInfo(symbols,
					includePath.toArray(new String[includePath.size()]));
			return scannerInfo;
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Compiler built-ins", e));
		}
	}

	public ArduinoConsoleParser[] getBuildConsoleParsers() {
		// ../src/Test.cpp:4:1: error: 'x' was not declared in this scope

		return new ArduinoConsoleParser[] { new ArduinoErrorParser("(.*?):(\\d+):(\\d+:)? (fatal )?error: (.*)") { //$NON-NLS-1$
			@Override
			protected int getSeverity(Matcher matcher) {
				return IMarker.SEVERITY_ERROR;
			}

			@Override
			protected String getMessage(Matcher matcher) {
				return matcher.group(5);
			}

			@Override
			protected int getLineNumber(Matcher matcher) {
				return Integer.parseInt(matcher.group(2));
			}

			@Override
			protected String getFileName(Matcher matcher) {
				return matcher.group(1);
			}

			@Override
			protected int getLinkOffset(Matcher matcher) {
				return 0;
			}

			@Override
			protected int getLinkLength(Matcher matcher) {
				return matcher.group(1).length() + 1 + matcher.group(2).length() + 1 + matcher.group(3).length();
			}
		} };
	}

}

Back to the top