Skip to content

Commit 518812a

Browse files
committed
Now give error if code is too big for sketch; maximum size determined by upload.maximum_size preference.
1 parent 74c0a80 commit 518812a

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

Diff for: app/Sizer.java

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Sizer - computes the size of a .hex file
5+
Part of the Arduino project - http://www.arduino.cc/
6+
7+
Copyright (c) 2006 David A. Mellis
8+
9+
This program is free software; you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation; either version 2 of the License, or
12+
(at your option) any later version.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software Foundation,
21+
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22+
23+
$Id$
24+
*/
25+
26+
package processing.app;
27+
import java.io.*;
28+
import java.util.*;
29+
30+
public class Sizer implements MessageConsumer {
31+
private String buildPath, sketchName;
32+
private String firstLine;
33+
private long size;
34+
private RunnerException exception;
35+
36+
public Sizer(String buildPath, String sketchName) {
37+
this.buildPath = buildPath;
38+
this.sketchName = sketchName;
39+
}
40+
41+
public long computeSize() throws RunnerException {
42+
String userdir = System.getProperty("user.dir") + File.separator;
43+
String commandSize[] = new String[] {
44+
((!Base.isMacOS()) ? "tools/avr/bin/avr-size" :
45+
userdir + "tools/avr/bin/avr-size"),
46+
" "
47+
};
48+
49+
commandSize[1] = buildPath + File.separator + sketchName + ".hex";
50+
51+
try {
52+
exception = null;
53+
size = -1;
54+
firstLine = null;
55+
Process process = Runtime.getRuntime().exec(commandSize);
56+
new MessageSiphon(process.getInputStream(), this);
57+
new MessageSiphon(process.getErrorStream(), this);
58+
boolean running = true;
59+
while(running) {
60+
try {
61+
process.waitFor();
62+
running = false;
63+
} catch (InterruptedException intExc) { }
64+
}
65+
} catch (Exception e) {
66+
exception = new RunnerException(e);
67+
}
68+
69+
if (exception != null)
70+
throw exception;
71+
72+
if (size == -1)
73+
throw new RunnerException(firstLine);
74+
75+
return size;
76+
}
77+
78+
public void message(String s) {
79+
if (firstLine == null)
80+
firstLine = s;
81+
else {
82+
StringTokenizer st = new StringTokenizer(s, " ");
83+
try {
84+
st.nextToken();
85+
st.nextToken();
86+
st.nextToken();
87+
size = (new Integer(st.nextToken().trim())).longValue();
88+
} catch (NoSuchElementException e) {
89+
exception = new RunnerException(e);
90+
} catch (NumberFormatException e) {
91+
exception = new RunnerException(e);
92+
}
93+
}
94+
}
95+
}

Diff for: app/Sketch.java

+19
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,24 @@ protected String build(Target target, String buildPath, String suggestedClassNam
15471547
//System.out.println("success = " + success + " ... " + primaryClassName);
15481548
return success ? primaryClassName : null;
15491549
}
1550+
1551+
protected void size(String buildPath, String suggestedClassName)
1552+
throws RunnerException {
1553+
long size = 0;
1554+
Sizer sizer = new Sizer(buildPath, suggestedClassName);
1555+
try {
1556+
size = sizer.computeSize();
1557+
System.out.println("Binary sketch size: " + size + " bytes (of a " +
1558+
Preferences.get("upload.maximum_size") + " byte maximum)");
1559+
} catch (RunnerException e) {
1560+
System.err.println("Couldn't determine program size: " + e.getMessage());
1561+
}
1562+
1563+
if (size > Preferences.getInteger("upload.maximum_size"))
1564+
throw new RunnerException(
1565+
"Sketch too big; try deleting code, removing floats, or see " +
1566+
"http://www.arduino.cc/en/Main/FAQ for more advice.");
1567+
}
15501568

15511569
protected String upload(String buildPath, String suggestedClassName)
15521570
throws RunnerException {
@@ -1628,6 +1646,7 @@ public boolean exportApplet(Target target) throws RunnerException {
16281646

16291647
// build the sketch
16301648
String foundName = build(target, appletFolder.getPath(), name);
1649+
size(appletFolder.getPath(), name);
16311650
foundName = upload(appletFolder.getPath(), name);
16321651
// (already reported) error during export, exit this function
16331652
if (foundName == null) return false;

Diff for: build/macosx/Arduino.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153

154154
/* Begin PBXBuildFile section */
155155
330B21540968180400345666 /* librxtxSerial.jnilib in CopyFiles */ = {isa = PBXBuildFile; fileRef = 330B21530968180400345666 /* librxtxSerial.jnilib */; };
156+
332D4DB609CF147F00BF81F6 /* Sizer.java in Sources */ = {isa = PBXBuildFile; fileRef = 332D4DB509CF147F00BF81F6 /* Sizer.java */; };
156157
339514EE097AEB5900193C89 /* STDCTokenTypes.txt in Resources */ = {isa = PBXBuildFile; fileRef = 33FFFE420965BD110016AC38 /* STDCTokenTypes.txt */; };
157158
339514FA097AEB8000193C89 /* license.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 33FF02B60965BD170016AC38 /* license.txt */; };
158159
339514FB097AEB8000193C89 /* readme.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 33FF02B70965BD170016AC38 /* readme.txt */; };
@@ -362,6 +363,7 @@
362363

363364
/* Begin PBXFileReference section */
364365
330B21530968180400345666 /* librxtxSerial.jnilib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.bundle"; path = librxtxSerial.jnilib; sourceTree = "<group>"; };
366+
332D4DB509CF147F00BF81F6 /* Sizer.java */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.java; path = Sizer.java; sourceTree = "<group>"; };
365367
333269E1099BB1FC007D3AE2 /* tools.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = tools.zip; sourceTree = "<group>"; };
366368
33AF620A0965D67800B514A9 /* antlr.jar */ = {isa = PBXFileReference; lastKnownFileType = archive.jar; path = antlr.jar; sourceTree = "<group>"; };
367369
33AF620B0965D67900B514A9 /* applet.html */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.html; path = applet.html; sourceTree = "<group>"; };
@@ -767,6 +769,7 @@
767769
33FFFE710965BD110016AC38 /* tools */,
768770
33FFFE730965BD110016AC38 /* UpdateCheck.java */,
769771
33FFFE740965BD110016AC38 /* Uploader.java */,
772+
332D4DB509CF147F00BF81F6 /* Sizer.java */,
770773
);
771774
name = app;
772775
path = ../../app;
@@ -1116,6 +1119,7 @@
11161119
33AF61B30965C54B00B514A9 /* WTreeParser.java in Sources */,
11171120
33AF61B40965C54B00B514A9 /* JEditTextArea.java in Sources */,
11181121
33AF61B50965C54B00B514A9 /* Base.java in Sources */,
1122+
332D4DB609CF147F00BF81F6 /* Sizer.java in Sources */,
11191123
);
11201124
runOnlyForDeploymentPostprocessing = 0;
11211125
};

Diff for: build/shared/lib/preferences.txt

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ linestatus.height = 20
271271
upload.erase=false
272272
upload.verify=false
273273
upload.programmer=stk500
274+
upload.maximum_size=7168
274275

275276
# set the parallel port defaults (used if upload.programmer=dapa)
276277
parallel.port=0x378

0 commit comments

Comments
 (0)