Upload size computation is now parametric

This commit is contained in:
Cristian Maglie 2012-02-06 00:51:24 +01:00
parent a141a1dc1e
commit 043c190876
4 changed files with 44 additions and 55 deletions

View File

@ -28,6 +28,7 @@ import processing.app.debug.Compiler;
import processing.app.debug.RunnerException; import processing.app.debug.RunnerException;
import processing.app.debug.Sizer; import processing.app.debug.Sizer;
import processing.app.debug.Uploader; import processing.app.debug.Uploader;
import processing.app.helpers.PreferencesMap;
import processing.app.preproc.*; import processing.app.preproc.*;
import processing.core.*; import processing.core.*;
import static processing.app.I18n._; import static processing.app.I18n._;
@ -1610,9 +1611,7 @@ public class Sketch {
* *
* @return null if compilation failed, main class name if not * @return null if compilation failed, main class name if not
*/ */
public String build(String buildPath, boolean verbose) public String build(String buildPath, boolean verbose) throws RunnerException {
throws RunnerException {
// run the preprocessor // run the preprocessor
editor.status.progressUpdate(20); editor.status.progressUpdate(20);
String primaryClassName = preprocess(buildPath); String primaryClassName = preprocess(buildPath);
@ -1621,12 +1620,11 @@ public class Sketch {
// that will bubble up to whomever called build(). // that will bubble up to whomever called build().
Compiler compiler = new Compiler(); Compiler compiler = new Compiler();
if (compiler.compile(this, buildPath, primaryClassName, verbose)) { if (compiler.compile(this, buildPath, primaryClassName, verbose)) {
size(buildPath, primaryClassName); size(compiler.getBuildPreferences());
return primaryClassName; return primaryClassName;
} }
return null; return null;
} }
protected boolean exportApplet(boolean usingProgrammer) throws Exception { protected boolean exportApplet(boolean usingProgrammer) throws Exception {
return exportApplet(tempBuildFolder.getAbsolutePath(), usingProgrammer); return exportApplet(tempBuildFolder.getAbsolutePath(), usingProgrammer);
@ -1668,31 +1666,28 @@ public class Sketch {
} }
protected void size(String buildPath, String suggestedClassName) protected void size(PreferencesMap prefs) throws RunnerException {
throws RunnerException {
long size = 0; long size = 0;
String maxsizeString = Base.getBoardPreferences().get("upload.maximum_size"); String maxsizeString = prefs.get("upload.maximum_size");
if (maxsizeString == null) return; if (maxsizeString == null)
return;
long maxsize = Integer.parseInt(maxsizeString); long maxsize = Integer.parseInt(maxsizeString);
Sizer sizer = new Sizer(buildPath, suggestedClassName); Sizer sizer = new Sizer(prefs);
try { try {
size = sizer.computeSize(); size = sizer.computeSize();
System.out.println( System.out.println(I18n
I18n.format( .format(_("Binary sketch size: {0} bytes (of a {1} byte maximum)"),
_("Binary sketch size: {0} bytes (of a {1} byte maximum)"), size, maxsize));
size, maxsize
)
);
} catch (RunnerException e) { } catch (RunnerException e) {
System.err.println(I18n.format(_("Couldn't determine program size: {0}"), e.getMessage())); System.err.println(I18n.format(_("Couldn't determine program size: {0}"),
e.getMessage()));
} }
if (size > maxsize) if (size > maxsize)
throw new RunnerException( throw new RunnerException(
_("Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.")); _("Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it."));
} }
protected String upload(String buildPath, String suggestedClassName, boolean usingProgrammer) protected String upload(String buildPath, String suggestedClassName, boolean usingProgrammer)
throws RunnerException, SerialException { throws RunnerException, SerialException {

View File

@ -689,4 +689,8 @@ public class Compiler implements MessageConsumer {
// Remove first space // Remove first space
return res.substring(1); return res.substring(1);
} }
public PreferencesMap getBuildPreferences() {
return prefs;
}
} }

View File

@ -5,6 +5,7 @@
Part of the Arduino project - http://www.arduino.cc/ Part of the Arduino project - http://www.arduino.cc/
Copyright (c) 2006 David A. Mellis Copyright (c) 2006 David A. Mellis
Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -25,42 +26,38 @@
package processing.app.debug; package processing.app.debug;
import processing.app.Base; import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*; import processing.app.helpers.PreferencesMap;
import java.util.*; import processing.app.helpers.StringReplacer;
public class Sizer implements MessageConsumer { public class Sizer implements MessageConsumer {
private String buildPath, sketchName;
private String firstLine;
private long size; private long size;
private RunnerException exception; private RunnerException exception;
private PreferencesMap prefs;
public Sizer(String buildPath, String sketchName) { private String firstLine;
this.buildPath = buildPath; private Pattern pattern;
this.sketchName = sketchName;
public Sizer(PreferencesMap _prefs) {
prefs = _prefs;
pattern = Pattern.compile(prefs.get("recipe.size.regex"));
} }
public long computeSize() throws RunnerException { public long computeSize() throws RunnerException {
String avrBasePath = Base.getAvrBasePath();
String commandSize[] = new String[] {
avrBasePath + "avr-size",
" "
};
commandSize[1] = buildPath + File.separator + sketchName + ".hex";
int r = 0; int r = 0;
try { try {
String pattern = prefs.get("recipe.size.pattern");
String cmd[] = StringReplacer.formatAndSplit(pattern, prefs, true);
exception = null; exception = null;
size = -1; size = -1;
firstLine = null; Process process = Runtime.getRuntime().exec(cmd);
Process process = Runtime.getRuntime().exec(commandSize);
MessageSiphon in = new MessageSiphon(process.getInputStream(), this); MessageSiphon in = new MessageSiphon(process.getInputStream(), this);
MessageSiphon err = new MessageSiphon(process.getErrorStream(), this); MessageSiphon err = new MessageSiphon(process.getErrorStream(), this);
boolean running = true; boolean running = true;
while(running) { while(running) {
try { try {
if (in.thread != null) if (in.thread != null)
@ -91,18 +88,8 @@ public class Sizer implements MessageConsumer {
public void message(String s) { public void message(String s) {
if (firstLine == null) if (firstLine == null)
firstLine = s; firstLine = s;
else { Matcher matcher = pattern.matcher(s.trim());
StringTokenizer st = new StringTokenizer(s, " "); if (matcher.matches())
try { size = Long.parseLong(matcher.group(1));
st.nextToken();
st.nextToken();
st.nextToken();
size = (new Integer(st.nextToken().trim())).longValue();
} catch (NoSuchElementException e) {
exception = new RunnerException(e.toString());
} catch (NumberFormatException e) {
exception = new RunnerException(e.toString());
}
}
} }
} }

View File

@ -19,8 +19,7 @@ compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,
compiler.elf2hex.flags=-O ihex -R .eeprom compiler.elf2hex.flags=-O ihex -R .eeprom
compiler.elf2hex.cmd=avr-objcopy compiler.elf2hex.cmd=avr-objcopy
compiler.ldflags= compiler.ldflags=
compiler.upload.cmd= compiler.size.cmd=avr-size
compiler.upload.flags=
# AVR compile patterns # AVR compile patterns
# -------------------- # --------------------
@ -43,6 +42,10 @@ recipe.objcopy.eep.pattern={compiler.path}{compiler.objcopy.cmd} {compiler.objco
## Create hex ## Create hex
recipe.objcopy.hex.pattern={compiler.path}{compiler.elf2hex.cmd} {compiler.elf2hex.flags} {build.path}/{build.project_name}.elf {build.path}/{build.project_name}.hex recipe.objcopy.hex.pattern={compiler.path}{compiler.elf2hex.cmd} {compiler.elf2hex.flags} {build.path}/{build.project_name}.elf {build.path}/{build.project_name}.hex
## Compute size
recipe.size.pattern={compiler.path}{compiler.size.cmd} -A {build.path}/{build.project_name}.hex
recipe.size.regex=Total\s+([0-9]+).*
# AVR Uploader/Programmers tools # AVR Uploader/Programmers tools
# ------------------- # -------------------