Now give error if code is too big for sketch; maximum size determined by upload.maximum_size preference.

This commit is contained in:
David A. Mellis 2006-03-20 23:14:10 +00:00
parent 74c0a80359
commit 518812a1e9
4 changed files with 119 additions and 0 deletions

95
app/Sizer.java Normal file
View File

@ -0,0 +1,95 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Sizer - computes the size of a .hex file
Part of the Arduino project - http://www.arduino.cc/
Copyright (c) 2006 David A. Mellis
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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id$
*/
package processing.app;
import java.io.*;
import java.util.*;
public class Sizer implements MessageConsumer {
private String buildPath, sketchName;
private String firstLine;
private long size;
private RunnerException exception;
public Sizer(String buildPath, String sketchName) {
this.buildPath = buildPath;
this.sketchName = sketchName;
}
public long computeSize() throws RunnerException {
String userdir = System.getProperty("user.dir") + File.separator;
String commandSize[] = new String[] {
((!Base.isMacOS()) ? "tools/avr/bin/avr-size" :
userdir + "tools/avr/bin/avr-size"),
" "
};
commandSize[1] = buildPath + File.separator + sketchName + ".hex";
try {
exception = null;
size = -1;
firstLine = null;
Process process = Runtime.getRuntime().exec(commandSize);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
boolean running = true;
while(running) {
try {
process.waitFor();
running = false;
} catch (InterruptedException intExc) { }
}
} catch (Exception e) {
exception = new RunnerException(e);
}
if (exception != null)
throw exception;
if (size == -1)
throw new RunnerException(firstLine);
return size;
}
public void message(String s) {
if (firstLine == null)
firstLine = s;
else {
StringTokenizer st = new StringTokenizer(s, " ");
try {
st.nextToken();
st.nextToken();
st.nextToken();
size = (new Integer(st.nextToken().trim())).longValue();
} catch (NoSuchElementException e) {
exception = new RunnerException(e);
} catch (NumberFormatException e) {
exception = new RunnerException(e);
}
}
}
}

View File

@ -1548,6 +1548,24 @@ public class Sketch {
return success ? primaryClassName : null;
}
protected void size(String buildPath, String suggestedClassName)
throws RunnerException {
long size = 0;
Sizer sizer = new Sizer(buildPath, suggestedClassName);
try {
size = sizer.computeSize();
System.out.println("Binary sketch size: " + size + " bytes (of a " +
Preferences.get("upload.maximum_size") + " byte maximum)");
} catch (RunnerException e) {
System.err.println("Couldn't determine program size: " + e.getMessage());
}
if (size > Preferences.getInteger("upload.maximum_size"))
throw new RunnerException(
"Sketch too big; try deleting code, removing floats, or see " +
"http://www.arduino.cc/en/Main/FAQ for more advice.");
}
protected String upload(String buildPath, String suggestedClassName)
throws RunnerException {
@ -1628,6 +1646,7 @@ public class Sketch {
// build the sketch
String foundName = build(target, appletFolder.getPath(), name);
size(appletFolder.getPath(), name);
foundName = upload(appletFolder.getPath(), name);
// (already reported) error during export, exit this function
if (foundName == null) return false;

View File

@ -153,6 +153,7 @@
/* Begin PBXBuildFile section */
330B21540968180400345666 /* librxtxSerial.jnilib in CopyFiles */ = {isa = PBXBuildFile; fileRef = 330B21530968180400345666 /* librxtxSerial.jnilib */; };
332D4DB609CF147F00BF81F6 /* Sizer.java in Sources */ = {isa = PBXBuildFile; fileRef = 332D4DB509CF147F00BF81F6 /* Sizer.java */; };
339514EE097AEB5900193C89 /* STDCTokenTypes.txt in Resources */ = {isa = PBXBuildFile; fileRef = 33FFFE420965BD110016AC38 /* STDCTokenTypes.txt */; };
339514FA097AEB8000193C89 /* license.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 33FF02B60965BD170016AC38 /* license.txt */; };
339514FB097AEB8000193C89 /* readme.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 33FF02B70965BD170016AC38 /* readme.txt */; };
@ -362,6 +363,7 @@
/* Begin PBXFileReference section */
330B21530968180400345666 /* librxtxSerial.jnilib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.bundle"; path = librxtxSerial.jnilib; sourceTree = "<group>"; };
332D4DB509CF147F00BF81F6 /* Sizer.java */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.java; path = Sizer.java; sourceTree = "<group>"; };
333269E1099BB1FC007D3AE2 /* tools.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = tools.zip; sourceTree = "<group>"; };
33AF620A0965D67800B514A9 /* antlr.jar */ = {isa = PBXFileReference; lastKnownFileType = archive.jar; path = antlr.jar; sourceTree = "<group>"; };
33AF620B0965D67900B514A9 /* applet.html */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.html; path = applet.html; sourceTree = "<group>"; };
@ -767,6 +769,7 @@
33FFFE710965BD110016AC38 /* tools */,
33FFFE730965BD110016AC38 /* UpdateCheck.java */,
33FFFE740965BD110016AC38 /* Uploader.java */,
332D4DB509CF147F00BF81F6 /* Sizer.java */,
);
name = app;
path = ../../app;
@ -1116,6 +1119,7 @@
33AF61B30965C54B00B514A9 /* WTreeParser.java in Sources */,
33AF61B40965C54B00B514A9 /* JEditTextArea.java in Sources */,
33AF61B50965C54B00B514A9 /* Base.java in Sources */,
332D4DB609CF147F00BF81F6 /* Sizer.java in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -271,6 +271,7 @@ linestatus.height = 20
upload.erase=false
upload.verify=false
upload.programmer=stk500
upload.maximum_size=7168
# set the parallel port defaults (used if upload.programmer=dapa)
parallel.port=0x378