Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e610c9fb5829df32b9e148386fea3af5a53f34be (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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems and others.
 * 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
 *******************************************************************************/
package org.eclipse.cdt.arduino.core.internal.build;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.cdt.arduino.core.internal.Activator;
import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences;
import org.eclipse.cdt.arduino.core.internal.HierarchicalProperties;
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.remote.ArduinoRemoteConnection;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ConsoleOutputStream;
import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.IConsoleParser;
import org.eclipse.cdt.core.build.CBuildConfiguration;
import org.eclipse.cdt.core.build.IToolChain;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.core.model.ISourceRoot;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.resources.IConsole;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.remote.core.IRemoteConnectionChangeListener;
import org.eclipse.remote.core.IRemoteServicesManager;
import org.eclipse.remote.core.RemoteConnectionChangeEvent;

import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class ArduinoBuildConfiguration extends CBuildConfiguration
		implements TemplateLoader, IRemoteConnectionChangeListener {

	private static final ArduinoManager manager = Activator.getService(ArduinoManager.class);
	private static final boolean isWindows = Platform.getOS().equals(Platform.OS_WIN32);

	private final ArduinoRemoteConnection target;
	private final String launchMode;
	private ArduinoBoard defaultBoard;
	private Properties boardProperties;

	// for Makefile generation
	private Configuration templateConfig;

	/**
	 * Default configuration.
	 * 
	 * @param config
	 */
	ArduinoBuildConfiguration(IBuildConfiguration config, String name, String launchMode, ArduinoBoard defaultBoard, IToolChain toolChain) throws CoreException {
		super(config, ".default", toolChain); //$NON-NLS-1$
		this.target = null;
		this.launchMode = launchMode;
		this.defaultBoard = defaultBoard;
	}

	ArduinoBuildConfiguration(IBuildConfiguration config, String name, String launchMode, ArduinoRemoteConnection target,
			IToolChain toolChain) throws CoreException {
		super(config, name, toolChain);
		this.target = target;
		this.launchMode = launchMode;
		IRemoteServicesManager remoteManager = Activator.getService(IRemoteServicesManager.class);
		remoteManager.addRemoteConnectionChangeListener(this);
	}

	@Override
	public synchronized void connectionChanged(RemoteConnectionChangeEvent event) {
		if (event.getConnection().equals(target.getRemoteConnection())) {
			boardProperties = null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if (adapter.equals(ArduinoBuildConfiguration.class)) {
			return (T) this;
		}
		return super.getAdapter(adapter);
	}

	public String getLaunchMode() {
		return launchMode;
	}

	public ArduinoRemoteConnection getTarget() {
		return target;
	}

	public ArduinoBoard getBoard() throws CoreException {
		if (target != null) {
			return target.getBoard();
		} else {
			return defaultBoard;
		}
	}

	private synchronized Properties getBoardProperties() throws CoreException {
		if (boardProperties == null) {
			ArduinoBoard board = getBoard();
			ArduinoPlatform platform = board.getPlatform();

			// IDE generated properties
			boardProperties = new Properties();
			boardProperties.put("runtime.platform.path", platform.getInstallPath().toString()); //$NON-NLS-1$
			boardProperties.put("runtime.ide.version", "10608"); //$NON-NLS-1$ //$NON-NLS-2$
			boardProperties.put("software", "ARDUINO"); //$NON-NLS-1$ //$NON-NLS-2$
			boardProperties.put("build.arch", platform.getArchitecture().toUpperCase()); //$NON-NLS-1$
			boardProperties.put("build.path", "."); //$NON-NLS-1$ //$NON-NLS-2$
			boardProperties.put("build.core.path", //$NON-NLS-1$
					platform.getInstallPath().resolve("cores").resolve("{build.core}").toString()); //$NON-NLS-1$ //$NON-NLS-2$
			boardProperties.put("build.system.path", platform.getInstallPath().resolve("system").toString()); //$NON-NLS-1$ //$NON-NLS-2$
			boardProperties.put("build.variant.path", //$NON-NLS-1$
					platform.getInstallPath().resolve("variants").resolve("{build.variant}").toString()); //$NON-NLS-1$ //$NON-NLS-2$

			// Everyone seems to want to use arduino package tools
			ArduinoPackage arduinoPackage = manager.getPackage("arduino"); //$NON-NLS-1$
			if (arduinoPackage != null) {
				for (ArduinoTool tool : arduinoPackage.getLatestTools()) {
					boardProperties.put("runtime.tools." + tool.getName() + ".path", tool.getInstallPath().toString()); //$NON-NLS-1$ //$NON-NLS-2$
				}
				for (ArduinoTool tool : arduinoPackage.getTools()) {
					boardProperties.put("runtime.tools." + tool.getName() + '-' + tool.getVersion() + ".path", //$NON-NLS-1$ //$NON-NLS-2$
							tool.getInstallPath().toString());
				}
			}

			// Super Platform
			String core = board.getBoardProperties().getProperty("build.core"); //$NON-NLS-1$
			if (core.contains(":")) { //$NON-NLS-1$
				String[] segments = core.split(":"); //$NON-NLS-1$
				if (segments.length == 2) {
					ArduinoPlatform superPlatform = manager.getInstalledPlatform(segments[0],
							platform.getArchitecture());
					if (superPlatform != null) {
						boardProperties.putAll(superPlatform.getPlatformProperties());
					}
				}
			}

			// Platform
			boardProperties.putAll(platform.getPlatformProperties());

			// Tools
			for (ToolDependency toolDep : platform.getToolsDependencies()) {
				boardProperties.putAll(toolDep.getTool().getToolProperties());
			}

			// Board
			boardProperties.putAll(board.getBoardProperties());

			// Menus
			HierarchicalProperties menus = board.getMenus();
			if (menus != null) {
				for (Entry<String, HierarchicalProperties> menuEntry : menus.getChildren().entrySet()) {
					String key = menuEntry.getKey();
					String value = target.getMenuValue(key);
					if (value == null || value.isEmpty()) {
						Iterator<HierarchicalProperties> i = menuEntry.getValue().getChildren().values().iterator();
						if (i.hasNext()) {
							HierarchicalProperties first = i.next();
							value = first.getValue();
						}
					}
					if (value != null && !value.isEmpty()) {
						boardProperties.putAll(board.getMenuProperties(key, value));
					}
				}
			}
		}

		// always do this in case the project changes names
		boardProperties.put("build.project_name", getProject().getName()); //$NON-NLS-1$
		return boardProperties;
	}

	public Map<String, Object> getBuildModel() throws CoreException {
		IProject project = getProject();
		ArduinoBoard board = getBoard();
		ArduinoPlatform platform = board.getPlatform();

		Properties properties = new Properties();
		Map<String, Object> buildModel = new HashMap<>();
		buildModel.put("boardId", board.getId()); //$NON-NLS-1$
		properties.put("object_file", "$@"); //$NON-NLS-1$ //$NON-NLS-2$
		properties.put("source_file", "$<"); //$NON-NLS-1$ //$NON-NLS-2$

		// 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 (isSource(proxy.getName())) {
							Path sourcePath = new File(proxy.requestResource().getLocationURI()).toPath();
							sourceFiles.add(pathString(projectPath.relativize(sourcePath)));
						}
					}
					return true;
				}
			}, 0);
		}
		buildModel.put("project_srcs", sourceFiles); //$NON-NLS-1$

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

		// the recipes
		properties.putAll(getBoardProperties());
		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 : getIncludePath(platform, properties)) {
			if (includes == null) {
				includes = "-I"; //$NON-NLS-1$
			} else {
				includes += " -I"; //$NON-NLS-1$
			}
			includes += '"' + pathString(include) + '"';
		}
		
		for (ArduinoLibrary lib : manager.getLibraries(project)) {
			for (Path include : lib.getIncludePath()) {
				includes += " -I\"" + pathString(include) + '"'; //$NON-NLS-1$
			}
		}

		// Magic recipes for platform builds with platform includes
		properties.put("includes", includes); //$NON-NLS-1$
		buildModel.put("recipe_cpp_o_pattern_plat", resolveProperty("recipe.cpp.o.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_c_o_pattern_plat", resolveProperty("recipe.c.o.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_S_o_pattern_plat", resolveProperty("recipe.S.o.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$

		ArduinoPlatform corePlatform = platform;
		String core = properties.getProperty("build.core"); //$NON-NLS-1$
		if (core.contains(":")) { //$NON-NLS-1$
			String[] segments = core.split(":"); //$NON-NLS-1$
			if (segments.length == 2) {
				corePlatform = manager.getInstalledPlatform(segments[0], platform.getArchitecture());
				core = segments[1];
			}
		}
		buildModel.put("platform_path", pathString(corePlatform.getInstallPath())); //$NON-NLS-1$
		Path corePath = corePlatform.getInstallPath().resolve("cores").resolve(core); //$NON-NLS-1$
		buildModel.put("platform_core_path", pathString(corePath)); //$NON-NLS-1$
		List<String> coreSources = new ArrayList<>();
		getSources(coreSources, corePath, true);
		buildModel.put("platform_core_srcs", coreSources); //$NON-NLS-1$

		List<String> variantSources = new ArrayList<>();
		String variant = properties.getProperty("build.variant"); //$NON-NLS-1$
		if (variant != null) {
			ArduinoPlatform variantPlatform = platform;
			if (variant.contains(":")) { //$NON-NLS-1$
				String[] segments = variant.split(":"); //$NON-NLS-1$
				if (segments.length == 2) {
					variantPlatform = manager.getInstalledPlatform(segments[0], platform.getArchitecture());
					variant = segments[1];
				}
			}
			Path variantPath = variantPlatform.getInstallPath().resolve("variants").resolve(variant); //$NON-NLS-1$
			buildModel.put("platform_variant_path", pathString(variantPath)); //$NON-NLS-1$
			getSources(variantSources, variantPath, true);
		}
		buildModel.put("platform_variant_srcs", variantSources); //$NON-NLS-1$

		properties.put("archive_file", "core.a"); //$NON-NLS-1$ //$NON-NLS-2$
		properties.put("archive_file_path", "{build.path}/{archive_file}"); //$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_S_o_pattern", resolveProperty("recipe.S.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_objcopy_bin_pattern", resolveProperty("recipe.objcopy.bin.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$
		buildModel.put("recipe_size_pattern", resolveProperty("recipe.size.pattern", properties)); //$NON-NLS-1$ //$NON-NLS-2$

		return buildModel;
	}

	private static void getSources(Collection<String> sources, Path dir, boolean recurse) {
		for (File file : dir.toFile().listFiles()) {
			if (file.isDirectory()) {
				if (recurse) {
					getSources(sources, file.toPath(), recurse);
				}
			} else {
				if (ArduinoBuildConfiguration.isSource(file.getName())) {
					sources.add(ArduinoBuildConfiguration.pathString(file.toPath()));
				}
			}
		}
	}

	public IFile generateMakeFile(IProgressMonitor monitor) throws CoreException {
		IFolder buildFolder = (IFolder) getBuildContainer();
		if (!buildFolder.exists()) {
			buildFolder.create(true, true, monitor);
		}

		IFile makefile = buildFolder.getFile("Makefile"); //$NON-NLS-1$

		Map<String, Object> buildModel = getBuildModel();

		// Generate the Makefile
		try (StringWriter writer = new StringWriter()) {
			if (templateConfig == null) {
				templateConfig = new Configuration(Configuration.VERSION_2_3_22);
				templateConfig.setTemplateLoader(this);
			}

			Template template = templateConfig.getTemplate("templates/Makefile"); //$NON-NLS-1$
			template.process(buildModel, writer);
			try (ByteArrayInputStream in = new ByteArrayInputStream(
					writer.getBuffer().toString().getBytes(StandardCharsets.UTF_8))) {
				createParent(makefile, monitor);
				if (makefile.exists()) {
					makefile.setContents(in, true, true, monitor);
				} else {
					makefile.create(in, true, monitor);
				}
			}
		} catch (IOException | TemplateException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Error generating makefile", e));
		}

		return makefile;
	}

	protected static void createParent(IResource child, IProgressMonitor monitor) throws CoreException {
		if (child == null)
			return;

		IContainer container = child.getParent();
		if (container.exists()) {
			return;
		}

		IFolder parent = container.getAdapter(IFolder.class);
		createParent(parent, monitor);
		parent.create(true, true, monitor);
	}

	public static boolean isSource(String filename) {
		int i = filename.lastIndexOf('.');
		String ext = filename.substring(i + 1);
		switch (ext) {
		case "cpp": //$NON-NLS-1$
		case "c": //$NON-NLS-1$
		case "S": //$NON-NLS-1$
			return true;
		default:
			return false;
		}
	}

	private String resolvePropertyValue(String value, Properties dict) throws CoreException {
		String last;
		do {
			last = value;
			for (int i = value.indexOf('{'); i >= 0; i = value.indexOf('{', i)) {
				i++;
				if (value.charAt(i) == '{') {
					i++;
					continue;
				}

				int n = value.indexOf('}', i);
				if (n >= 0) {
					String p2 = value.substring(i, n);
					String r2 = dict.getProperty(p2);
					if (r2 != null) {
						value = value.replace('{' + p2 + '}', r2);
					} else {
						throw Activator.coreException(String.format("Undefined key %s", p2), null);
					}
				}
				i = n;
			}
		} while (!value.equals(last));

		return value.replace("}}", "}").replace("{{", "{"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}

	private String resolveProperty(String property, Properties dict) throws CoreException {
		String value = dict.getProperty(property);
		return value != null ? resolvePropertyValue(value, dict) : null;
	}

	public String getMakeCommand() {
		return isWindows ? ArduinoPreferences.getArduinoHome().resolve("make").toString() : "make"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	public String[] getBuildCommand() throws CoreException {
		return new String[] { getMakeCommand() };
	}

	public String[] getCleanCommand() throws CoreException {
		return new String[] { getMakeCommand(), "clean" }; //$NON-NLS-1$
	}

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

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

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

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

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

	public String[] getUploadCommand(String serialPort) throws CoreException {
		Properties properties = new Properties();
		properties.putAll(getBoardProperties());

		String toolName = properties.getProperty("upload.tool"); //$NON-NLS-1$
		ArduinoPlatform platform = getBoard().getPlatform();
		if (toolName.contains(":")) { //$NON-NLS-1$
			String[] segments = toolName.split(":"); //$NON-NLS-1$
			if (segments.length == 2) {
				platform = manager.getInstalledPlatform(segments[0], platform.getArchitecture());
				toolName = segments[1];
			}
		}

		ArduinoTool uploadTool = platform.getTool(toolName);
		if (uploadTool != null) {
			properties.putAll(uploadTool.getToolProperties());
		}

		properties.put("serial.port", serialPort); //$NON-NLS-1$
		// Little bit of weirdness needed for the bossac tool
		if (serialPort.startsWith("/dev/")) { //$NON-NLS-1$
			properties.put("serial.port.file", serialPort.substring(5)); //$NON-NLS-1$
		} else {
			properties.put("serial.port.file", 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 for the tool flattened
		HierarchicalProperties toolsProps = new HierarchicalProperties(platform.getPlatformProperties())
				.getChild("tools"); //$NON-NLS-1$
		if (toolsProps != null) {
			HierarchicalProperties toolProps = toolsProps.getChild(toolName);
			if (toolProps != null) {
				properties.putAll(toolProps.flatten());
			}
		}

		String command;
		if (properties.get("upload.protocol") != null) { //$NON-NLS-1$
			// TODO make this a preference
			properties.put("upload.verbose", properties.getProperty("upload.params.verbose", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			properties.put("upload.verify", properties.getProperty("upload.params.verify", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

			command = resolveProperty("upload.pattern", properties); //$NON-NLS-1$
		} else {
			// use the bootloader
			String programmer = target.getProgrammer();
			if (programmer != null) {
				HierarchicalProperties programmers = getBoard().getPlatform().getProgrammers();
				if (programmers != null) {
					HierarchicalProperties programmerProps = programmers.getChild(programmer);
					if (programmerProps != null) {
						properties.putAll(programmerProps.flatten());
					}
				}
			}

			// TODO preference
			properties.put("program.verbose", properties.getProperty("program.params.verbose", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			properties.put("program.verify", properties.getProperty("program.params.verify", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

			command = resolveProperty("program.pattern", properties); //$NON-NLS-1$
		}

		if (command == null) {
			throw Activator.coreException("Upload command not specified", null);
		}
		if (isWindows) {
			return splitCommand(command);
		} else {
			return new String[] { "sh", "-c", command }; //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	private Collection<Path> getIncludePath(ArduinoPlatform platform, Properties properties) throws CoreException {
		ArduinoPlatform corePlatform = platform;
		String core = properties.getProperty("build.core"); //$NON-NLS-1$
		if (core.contains(":")) { //$NON-NLS-1$
			String[] segments = core.split(":"); //$NON-NLS-1$
			if (segments.length == 2) {
				corePlatform = manager.getInstalledPlatform(segments[0], platform.getArchitecture());
				core = segments[1];
			}
		}

		ArduinoPlatform variantPlatform = platform;
		String variant = properties.getProperty("build.variant"); //$NON-NLS-1$
		if (variant != null) {
			if (variant.contains(":")) { //$NON-NLS-1$
				String[] segments = variant.split(":"); //$NON-NLS-1$
				if (segments.length == 2) {
					variantPlatform = manager.getInstalledPlatform(segments[0], platform.getArchitecture());
					variant = segments[1];
				}
			}
		} else {
			return Arrays.asList(corePlatform.getInstallPath().resolve("cores").resolve(core)); //$NON-NLS-1$
		}

		return Arrays.asList(corePlatform.getInstallPath().resolve("cores").resolve(core), //$NON-NLS-1$
				variantPlatform.getInstallPath().resolve("variants").resolve(variant)); //$NON-NLS-1$
	}

	// Scanner Info Cache
	private String[] cachedIncludePath;
	private String cachedInfoCommand;
	private IScannerInfo cachedScannerInfo;

	@Override
	public IScannerInfo getScannerInformation(IResource resource) {
		try {
			IContentType contentType = CCorePlugin.getContentType(resource.getProject(), resource.getName());
			String recipe;
			if (contentType != null && (contentType.getId() == CCorePlugin.CONTENT_TYPE_CSOURCE
					|| contentType.getId() == CCorePlugin.CONTENT_TYPE_CSOURCE)) {
				recipe = "recipe.c.o.pattern"; //$NON-NLS-1$
			} else {
				recipe = "recipe.cpp.o.pattern"; //$NON-NLS-1$
			}

			ArduinoPlatform platform = getBoard().getPlatform();
			Properties properties = new Properties();
			properties.putAll(getBoardProperties());

			// Overrides for scanner discovery
			properties.put("source_file", ""); //$NON-NLS-1$ //$NON-NLS-2$
			properties.put("object_file", "-"); //$NON-NLS-1$ //$NON-NLS-2$
			// the base scanner info does includes
			properties.put("includes", ""); //$NON-NLS-1$ //$NON-NLS-2$
			String commandString = resolveProperty(recipe, properties);

			List<Path> includePath = new ArrayList<>();
			includePath.addAll(getIncludePath(platform, properties));
			Collection<ArduinoLibrary> libs = manager.getLibraries(getProject());
			for (ArduinoLibrary lib : libs) {
				includePath.addAll(lib.getIncludePath());
			}
			String[] includes = null;
			try {
				includes = includePath.stream().map(path -> {
					try {
						return resolvePropertyValue(path.toString(), properties);
					} catch (CoreException e) {
						throw new RuntimeException(e);
					}
				}).collect(Collectors.toList()).toArray(new String[includePath.size()]);
			} catch (RuntimeException e) {
				if (e.getCause() != null && e.getCause() instanceof CoreException) {
					throw (CoreException) e.getCause();
				} else {
					throw e;
				}
			}

			// Use cache if we can
			if (cachedScannerInfo != null && cachedInfoCommand.equals(commandString)
					&& cachedIncludePath.length == includes.length) {
				boolean matches = true;
				for (int i = 0; i < includes.length; ++i) {
					if (!includes[i].equals(cachedIncludePath[i])) {
						matches = false;
						break;
					}
				}

				if (matches) {
					return cachedScannerInfo;
				}
			}

			ExtendedScannerInfo baseInfo = new ExtendedScannerInfo(null, includes);
			String[] command = splitCommand(commandString);
			IScannerInfo info = getToolChain().getScannerInfo(getBuildConfiguration(), Paths.get(command[0]),
					Arrays.copyOfRange(command, 1, command.length), baseInfo, resource, getBuildDirectoryURI());

			// cache the results
			cachedScannerInfo = info;
			cachedInfoCommand = commandString;
			cachedIncludePath = includes;

			return info;
		} catch (CoreException e) {
			Activator.log(e);
			return null;
		}
	}

	public static String pathString(Path path) {
		String str = path.toString();
		if (isWindows) {
			str = str.replaceAll("\\\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return str;
	}

	private String[] splitCommand(String command) {
		// TODO deal with quotes properly, for now just strip
		return command.replaceAll("\"", "").split("\\s+"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	@Override
	public IProject[] build(int kind, Map<String, String> args, IConsole console, IProgressMonitor monitor)
			throws CoreException {
		IProject project = getProject();
		try {
			project.deleteMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);

			ConsoleOutputStream consoleOut = console.getOutputStream();
			consoleOut.write(String.format("\nBuilding %s\n", project.getName()));

			generateMakeFile(monitor);

			try (ErrorParserManager epm = new ErrorParserManager(project, getBuildDirectoryURI(), this,
					getToolChain().getErrorParserIds())) {
				ProcessBuilder processBuilder = new ProcessBuilder().command(getBuildCommand())
						.directory(getBuildDirectory().toFile());
				setBuildEnvironment(processBuilder.environment());
				Process process = processBuilder.start();
				if (watchProcess(process, new IConsoleParser[] { epm }, console) == 0) {
					showSizes(console);
				}
			}

			getBuildContainer().refreshLocal(IResource.DEPTH_INFINITE, monitor);
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Build error", e));
		}

		// TODO if there are references we want to watch, return them here
		return new IProject[] { project };
	}

	@Override
	public void clean(IConsole console, IProgressMonitor monitor) throws CoreException {
		try {
			IProject project = getProject();
			project.deleteMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);

			ConsoleOutputStream consoleOut = console.getOutputStream();
			consoleOut.write(String.format("\nCleaning %s\n", project.getName()));

			ProcessBuilder processBuilder = new ProcessBuilder().command(getCleanCommand())
					.directory(getBuildDirectory().toFile());
			setBuildEnvironment(processBuilder.environment());
			Process process = processBuilder.start();

			watchProcess(process, new IConsoleParser[0], console);

			getBuildContainer().refreshLocal(IResource.DEPTH_INFINITE, monitor);
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Build error", e));
		}
	}

	private void showSizes(IConsole console) throws CoreException {
		try {
			int codeSize = -1;
			int dataSize = -1;

			String codeSizeRegex = getCodeSizeRegex();
			Pattern codeSizePattern = codeSizeRegex != null ? Pattern.compile(codeSizeRegex) : null;
			String dataSizeRegex = getDataSizeRegex();
			Pattern dataSizePattern = dataSizeRegex != null ? Pattern.compile(dataSizeRegex) : null;

			if (codeSizePattern == null && dataSizePattern == null) {
				return;
			}

			int maxCodeSize = getMaxCodeSize();
			int maxDataSize = getMaxDataSize();

			ProcessBuilder processBuilder = new ProcessBuilder().command(getSizeCommand())
					.directory(getBuildDirectory().toFile()).redirectErrorStream(true);
			setBuildEnvironment(processBuilder.environment());
			Process process = processBuilder.start();
			try (BufferedReader processOut = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
				for (String line = processOut.readLine(); line != null; line = processOut.readLine()) {
					if (codeSizePattern != null) {
						Matcher matcher = codeSizePattern.matcher(line);
						if (matcher.matches()) {
							codeSize += Integer.parseInt(matcher.group(1));
						}
					}
					if (dataSizePattern != null) {
						Matcher matcher = dataSizePattern.matcher(line);
						if (matcher.matches()) {
							dataSize += Integer.parseInt(matcher.group(1));
						}
					}
				}
			}

			ConsoleOutputStream consoleOut = console.getOutputStream();
			consoleOut.write("Program store usage: " + codeSize);
			if (maxCodeSize > 0) {
				consoleOut.write(" of maximum " + maxCodeSize);
			}
			consoleOut.write(" bytes\n");

			if (maxDataSize >= 0) {
				consoleOut.write("Initial RAM usage: " + dataSize);
				if (maxCodeSize > 0) {
					consoleOut.write(" of maximum " + maxDataSize);
				}
				consoleOut.write(" bytes\n");
			}
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Checking sizes", e));
		}
	}

	@Override
	public Object findTemplateSource(String name) throws IOException {
		return FileLocator.find(Activator.getPlugin().getBundle(), new org.eclipse.core.runtime.Path(name), null);
	}

	@Override
	public long getLastModified(Object source) {
		try {
			URL url = (URL) source;
			if (url.getProtocol().equals("file")) { //$NON-NLS-1$
				File file = new File(url.toURI());
				return file.lastModified();
			} else {
				return 0;
			}
		} catch (URISyntaxException e) {
			return 0;
		}
	}

	@Override
	public Reader getReader(Object source, String encoding) throws IOException {
		URL url = (URL) source;
		return new InputStreamReader(url.openStream(), encoding);
	}

	@Override
	public void closeTemplateSource(Object arg0) throws IOException {
		// Nothing
	}

}

Back to the top