Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3a907ed17c6a81ac6e382f9c35d0be1acc7318b (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
/*******************************************************************************
 * 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.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.arduino.core.internal.Activator;
import org.eclipse.cdt.build.core.CConsoleParser;
import org.eclipse.cdt.build.core.IConsoleService;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

/**
 * This class is responsible for generating the Makefile for the current build
 * config.
 */
public class ArduinoBuilder extends IncrementalProjectBuilder {

	public static final String ID = Activator.getId() + ".arduinoBuilder"; //$NON-NLS-1$

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

			IConsoleService consoleService = Activator.getService(IConsoleService.class);
			consoleService.writeOutput(String.format("Building %s\n", project.getName()));

			ArduinoBuildConfiguration config = getBuildConfig().getAdapter(ArduinoBuildConfiguration.class);
			config.generateMakeFile(monitor);

			ProcessBuilder processBuilder = new ProcessBuilder().command(config.getBuildCommand())
					.directory(config.getBuildDirectory());
			config.setEnvironment(processBuilder.environment());
			Process process = processBuilder.start();

			consoleService.monitor(process, config.getConsoleParsers().toArray(new CConsoleParser[0]),
					config.getBuildDirectory().toPath());

			if (process.exitValue() == 0) {
				showSizes(config, consoleService);
			}

			config.getBuildFolder().refreshLocal(IResource.DEPTH_INFINITE, monitor);
			consoleService.writeOutput("\n"); //$NON-NLS-1$
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Build error", e)); //$NON-NLS-1$
		}

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

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

			IConsoleService consoleService = Activator.getService(IConsoleService.class);
			consoleService.writeOutput(String.format("Cleaning %s\n", project.getName()));

			ArduinoBuildConfiguration config = getBuildConfig().getAdapter(ArduinoBuildConfiguration.class);

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

			consoleService.monitor(process, config.getConsoleParsers().toArray(new CConsoleParser[0]),
					config.getBuildDirectory().toPath());

			config.getBuildFolder().refreshLocal(IResource.DEPTH_INFINITE, monitor);
			consoleService.writeOutput("\n"); //$NON-NLS-1$
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Build error", e)); //$NON-NLS-1$
		}
	}

	private void showSizes(ArduinoBuildConfiguration config, IConsoleService console) throws CoreException {
		try {
			int codeSize = -1;
			int dataSize = -1;

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

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

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

			ProcessBuilder processBuilder = new ProcessBuilder().command(config.getSizeCommand())
					.directory(config.getBuildDirectory()).redirectErrorStream(true);
			config.setEnvironment(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));
						}
					}
				}
			}

			console.writeOutput("Program store usage: " + codeSize);
			if (maxCodeSize > 0) {
				console.writeOutput(" of maximum " + maxCodeSize);
			}
			console.writeOutput(" bytes\n");

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

}

Back to the top